mirror of
https://github.com/OpenBMB/ChatDev.git
synced 2026-08-01 19:06:06 +00:00
Merge pull request #624 from hobostay/fix/temp-file-leak-session-download
fix: replace atexit with BackgroundTask for temp zip cleanup
This commit is contained in:
commit
4d85134f60
@ -1,4 +1,3 @@
|
|||||||
import atexit
|
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
@ -6,6 +5,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
|
from starlette.background import BackgroundTask
|
||||||
|
|
||||||
from server.settings import WARE_HOUSE_DIR
|
from server.settings import WARE_HOUSE_DIR
|
||||||
from utils.exceptions import ResourceNotFoundError, ValidationError
|
from utils.exceptions import ResourceNotFoundError, ValidationError
|
||||||
@ -64,13 +64,12 @@ async def download_session(session_id: str):
|
|||||||
if zip_path.exists():
|
if zip_path.exists():
|
||||||
zip_path.unlink()
|
zip_path.unlink()
|
||||||
|
|
||||||
atexit.register(cleanup_zip)
|
|
||||||
|
|
||||||
return FileResponse(
|
return FileResponse(
|
||||||
path=zip_path,
|
path=zip_path,
|
||||||
filename=f"{dir_name}.zip",
|
filename=f"{dir_name}.zip",
|
||||||
media_type="application/zip",
|
media_type="application/zip",
|
||||||
headers={"Content-Disposition": f"attachment; filename={dir_name}.zip"},
|
headers={"Content-Disposition": f"attachment; filename={dir_name}.zip"},
|
||||||
|
background=BackgroundTask(cleanup_zip),
|
||||||
)
|
)
|
||||||
except ValidationError as exc:
|
except ValidationError as exc:
|
||||||
raise HTTPException(status_code=400, detail=str(exc))
|
raise HTTPException(status_code=400, detail=str(exc))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user