Skip to content

Commit f05bab5

Browse files
committed
* fixed cleanup
1 parent e10ffed commit f05bab5

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

checker/src/checker.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import random
44
import string
55
from logging import LoggerAdapter
6+
import time
67

78
import aiofiles
89
import 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):
246247
def 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

259262
def username_normalform(username):
260263
letters = sorted([c for c in username if c.isalpha()])

service/backend/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,6 @@ async def cleanup_old_records():
591591
user upload folders asynchronously.
592592
"""
593593
while True:
594-
await asyncio.sleep(240)
595594
db_gen = get_async_db()
596595
db = await anext(db_gen)
597596
try:
@@ -677,7 +676,7 @@ async def cleanup_old_records():
677676
finally:
678677
await db_gen.aclose()
679678

680-
679+
await asyncio.sleep(240)
681680

682681
@app.post("/images/upload", status_code=status.HTTP_201_CREATED)
683682
async def upload_dungeon_image(file: UploadFile = File(...),

0 commit comments

Comments
 (0)