fix(workflows): clean up download temp file on interrupt or typer.Exit - #4134
Open
chelsealong wants to merge 1 commit into
Open
fix(workflows): clean up download temp file on interrupt or typer.Exit#4134chelsealong wants to merge 1 commit into
chelsealong wants to merge 1 commit into
Conversation
`specify workflow add --from <url>` creates a delete=False temp file before streaming the response body into it. The except clauses around that read only handled typer.Exit (re-raise, no cleanup) and Exception (cleanup + re-raise). KeyboardInterrupt is a BaseException, so Ctrl+C during the size-limited read skipped both and left the file behind in the system temp directory. Adds a shared cleanup helper and a BaseException handler so any exit path after the temp file is created -- error, typer.Exit, or interrupt -- unlinks it, matching the existing best-effort cleanup on other download errors. Assisted-by: Claude Sonnet 5 (autonomous)
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.
Description
Fixes #4132.
specify workflow add <url>streams the download into atempfile.NamedTemporaryFile(..., delete=False). The file is created ondisk immediately (before any bytes are written), so any exit after that
point is supposed to clean it up.
Two exit paths skipped that cleanup:
except typer.Exit: raisere-raised without unlinkingtmp_path.KeyboardInterruptis aBaseException, not anException, sopressing Ctrl+C during the size-limited read wasn't caught by the
existing
except Exceptionhandler at all — the temp file leakedsilently.
The later
finally: tmp_path.unlink(...)only runs once installation hasstarted, i.e. after a successful download — it never covers these two
paths.
Fix
Factored the existing unlink-with-warn logic into a small
_cleanup_download_tmp_path()helper, and added aexcept BaseExceptionhandler alongside the existing
except typer.Exitandexcept Exceptionhandlers so any exit after the temp file is created — error, clean exit,
or interrupt — unlinks it.
Testing
Added
test_add_from_url_interrupt_during_read_leaves_no_temp_fileintests/test_workflows.py, which simulates aKeyboardInterruptraisedmid-read and asserts no temp file is left behind.
Confirmed the new test fails without the fix (
git checkout HEAD~1 -- src/specify_cli/workflows/_commands.py, rerun, restore):With the fix:
Full workflow suite:
Full test suite:
The 10 failures are pre-existing and unrelated to this change (template
composition / python-parity / terminal-width-dependent Rich formatting
tests in
test_check_prerequisites_python_parity.py,test_create_new_feature_python_parity.py,test_presets.py,test_resolve_template_python_parity.py,test_setup_plan_python_parity.py,test_setup_tasks_python_parity.py). Verified they fail identically onthis same environment with the fix reverted (unmodified tree), so they
are environment-specific and not caused by this change.
Lint:
Agent config consistency:
AI Disclosure
This PR was written by an autonomous AI coding agent (Claude, Anthropic)
acting on behalf of the repository owner. The agent read the issue,
identified the missing
BaseException/typer.Exitcleanup paths,implemented the minimal fix, added a regression test, verified the test
fails without the fix and passes with it, and ran the project's lint and
test suites as described above.