Skip to content

fix: clean up temporary pdf files after response#57

Open
AlwinXue wants to merge 1 commit intoOSIPI:mainfrom
AlwinXue:alwin/fix-issue-17
Open

fix: clean up temporary pdf files after response#57
AlwinXue wants to merge 1 commit intoOSIPI:mainfrom
AlwinXue:alwin/fix-issue-17

Conversation

@AlwinXue
Copy link
Copy Markdown

@AlwinXue AlwinXue commented Mar 31, 2026

Hi mentors! 👋

This PR addresses the temporary PDF file leak reported in Issue #17.

The Problem:
The /api/report/report-pdf endpoint creates a temporary PDF using NamedTemporaryFile(delete=False) and returns it through FileResponse, but nothing cleans that file up afterward. Over time, repeated PDF downloads can leave orphaned files on disk.

The Solution:
Response Cleanup: Added a BackgroundTask to the FileResponse so the generated PDF is deleted after the response is sent.
Regression Test: Added a targeted backend test that patches the temporary file and PDF writer, then verifies the response succeeds and the temporary file is removed afterward.

Validation:
Ran python -m pytest tests/test_report.py -q -k "test_report_pdf_endpoint_removes_temp_file" (with DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib) and the new cleanup test passed.
Note: the broader backend test file still contains unrelated failures from other open issues, so validation here is intentionally scoped to Issue #17.

Fixes #17

@AlwinXue AlwinXue force-pushed the alwin/fix-issue-17 branch from 76634ca to 8d4ad3a Compare March 31, 2026 01:47
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.

Temporary PDF file created with delete=False is never cleaned up

1 participant