fix: Clean up temporary PDF file after FileResponse using BackgroundTask#18
Open
Devguru-codes wants to merge 1 commit intoOSIPI:mainfrom
Open
fix: Clean up temporary PDF file after FileResponse using BackgroundTask#18Devguru-codes wants to merge 1 commit intoOSIPI:mainfrom
Devguru-codes wants to merge 1 commit intoOSIPI:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
download_pdfendpoint creates a temp PDF withdelete=Falsebut never cleans it up. Each request leaks a temp file. This fix attaches aBackgroundTaskto theFileResponsethat callsos.unlink(tmp_path)after the response is fully sent.Fix
from starlette.background import BackgroundTaskimportbackground=BackgroundTask(os.unlink, tmp_path)to theFileResponseFile Changed
apps/backend/app/routers/reports.pyAPI Endpoint Validation (
POST /api/report/report-pdf)Request
{ "report_data": { "asl_parameters": [["Magnetic Field Strength", "3T"], ["Manufacturer", "Siemens"]], "basic_report": "Test report content", "extended_report": "Extended test report", "missing_parameters": ["EchoTime"] } }Response (after fix)
API Endpoint Test Results
POST /api/report/report-pdfreturns 200content-typeisapplication/pdfContent-Dispositionincludesreport.pdfAPI Summary: PASSED=5 FAILED=0
Before/After Comparison
mainbranch (before fix)delete=FalseFileResponseusesBackgroundTaskSummary: PASSED=0 FAILED=3
Temporary_PDFbranch (after fix)delete=Falsepaired withBackgroundTaskcleanupos.unlinkvia BackgroundTaskFileResponseusesBackgroundTaskSummary: PASSED=3 FAILED=0
Resolves #17