Skip to content

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

Merged
zxrys merged 1 commit into
OpenBMB:mainfrom
hobostay:fix/temp-file-leak-session-download
May 26, 2026
Merged

fix: replace atexit with BackgroundTask for temp zip cleanup#624
zxrys merged 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>
@zxrys
Copy link
Copy Markdown
Collaborator

zxrys commented May 26, 2026

This bug did exist; thank you for fixing it.

@zxrys zxrys merged commit 4d85134 into OpenBMB:main May 26, 2026
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.

2 participants