Skip to content

fix: replace atexit with BackgroundTask for temp zip cleanup#624

Open
hobostay wants to merge 1 commit into
OpenBMB:mainfrom
hobostay:fix/temp-file-leak-session-download
Open

fix: replace atexit with BackgroundTask for temp zip cleanup#624
hobostay wants to merge 1 commit into
OpenBMB:mainfrom
hobostay:fix/temp-file-leak-session-download

Conversation

@hobostay
Copy link
Copy Markdown

Summary

  • Replace atexit.register(cleanup_zip) with Starlette's BackgroundTask for cleaning up temporary zip files after session downloads
  • atexit handlers only run at process shutdown, so every download accumulates a temp zip file on disk until the server restarts

Bug Details

In server/routes/sessions.py, after creating a temporary zip archive for download, the cleanup is registered via atexit:

def cleanup_zip():
    if zip_path.exists():
        zip_path.unlink()

atexit.register(cleanup_zip)

Problem: atexit handlers only run when the Python process exits. In a long-running server, this means:

  1. Each download creates a new temp zip file
  2. None of them are cleaned up until the server restarts
  3. Under high usage, temp files accumulate and consume disk space
  4. Each atexit.register call also adds to a growing list of callbacks

Fix: Use Starlette's BackgroundTask which runs after the HTTP response is fully sent.

Test plan

  • Download a session and verify the zip file is created and served correctly
  • Verify the temp zip file is deleted after the download completes

🤖 Generated with Claude Code

Using atexit to clean up temporary zip files is unreliable because
atexit handlers only run when the process exits, not after each
download. This means temp files accumulate on disk, one per download,
until the server restarts.

Replace with Starlette's BackgroundTask which runs cleanup after
the response is fully sent, ensuring temp files are deleted promptly.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant