fix(export): remove the file a failed export left behind - #336
Open
harsh-thakkar7 wants to merge 1 commit into
Open
harsh-thakkar7 wants to merge 1 commit into
harsh-thakkar7 wants to merge 1 commit into
Conversation
_make_output_path creates the output file up front, so by the time the exporter runs the file already exists. export_docx and export_pdf then only converted an exporter exception into a 500, leaving the artifact behind: two consecutive failures in one temp directory produced tmp3er68z13.docx and tmpt26yg_8i.pdf, both 0 bytes, neither removed by anything. Clean up the generated file on failure. Deliberately asymmetric: a caller who passed output_path may be holding a partial file they want to look at, so only paths we generated are removed. An exporter that fails partway through writing to a caller-supplied path still leaves that partial file, which is the lesser evil next to deleting a file the caller asked to have. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This branch has not been deployed
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.
Summary
A failed DOCX or PDF export leaves its output file behind in the temporary
directory. Nothing ever removes it.
Cause
_make_output_pathcreates the output file before the exporter runs, becausetempfile.mkstempboth allocates the name and creates the file:So by the time
export_docx/export_pdfcall the exporter the file is alreadyon disk. Their
exceptclauses then only converted the exception into a 500:Two consecutive failures in one temp directory on
main:Neither was removed. A partial write leaves a partial file rather than an empty
one, so the litter is not always zero-length either.
Fix
_discard_failed_exportremoves the file on failure — but only when the path wasgenerated for this request:
That asymmetry is deliberate. A caller who passed
output_pathmay be holding apartial file they want to inspect, and silently deleting a file someone asked to
have written is worse than leaving it. So an exporter that fails partway through
writing to a caller-supplied path still leaves that partial file; only the files
we created ourselves are cleaned up.
export_htmlneeds no change — it renders before allocating the path, so afailure cannot orphan anything.
Verification
pytest: 270 passed → 274 passed on the full backend suite, no new failures.ruff check .clean;ruff format --check .reports no offenders.not requested_pathguard (i.e. deleting unconditionally) iscaught by
test_failed_export_keeps_a_caller_supplied_path;test_successful_pdf_export_still_keeps_its_file.The tests drive the routes through
TestClientwith the exporter replaced by onethat writes a partial file and then raises, so they cover the partial-output case
rather than just the empty-file case.