33import random
44import string
55from logging import LoggerAdapter
6+ import time
67
78import aiofiles
89import aiofiles .tempfile
@@ -188,9 +189,9 @@ async def upload_image(self, token, flag):
188189 raise MumbleException (f"Failed to upload image (status={ resp .status_code } )" )
189190 return resp .json ()
190191 finally :
191- await loop .run_in_executor (None , safe_unlink , flagfile_path )
192+ await loop .run_in_executor (None , safe_unlink , flagfile_path , self . logger )
192193 if await loop .run_in_executor (None , os .path .exists , steghide_img_path ):
193- await loop .run_in_executor (None , safe_unlink , steghide_img_path )
194+ await loop .run_in_executor (None , safe_unlink , steghide_img_path , self . logger )
194195
195196 async def get_image (self , filename , token ):
196197 self .logger .info (f"[get_image_p] /images/{ filename } token={ token } ..." )
@@ -235,8 +236,8 @@ async def extract_flag_from_image(self, image_path):
235236 return extracted_flag .strip ()
236237
237238 finally :
238- await safe_unlink ( flagfile_path )
239- await safe_unlink ( image_path )
239+ await loop . run_in_executor ( None , safe_unlink , flagfile_path , self . logger )
240+ await loop . run_in_executor ( None , safe_unlink , image_path , self . logger )
240241
241242
242243#########################################
@@ -246,15 +247,17 @@ async def extract_flag_from_image(self, image_path):
246247def rand_str (n ):
247248 return "" .join (random .choices (ALPHABET , k = n ))
248249
249-
250- async def safe_unlink (path ):
250+ def safe_unlink (path , logger ):
251251 for _ in range (3 ):
252252 try :
253253 if os .path .exists (path ):
254254 os .unlink (path )
255255 return
256- except Exception :
257- await asyncio .sleep (0.1 )
256+ except Exception as e :
257+ logger .warning (f"safe_unlink: Failed to delete { path } : { e } " )
258+ time .sleep (0.1 )
259+ if os .path .exists (path ):
260+ logger .error (f"safe_unlink: Giving up, { path } could not be deleted after 3 tries." )
258261
259262def username_normalform (username ):
260263 letters = sorted ([c for c in username if c .isalpha ()])
0 commit comments