Repository navigation
Close owned download files on errors - #408
Draft
lexingtonhibiki wants to merge 1 commit into
Draft
lexingtonhibiki wants to merge 1 commit into
lexingtonhibiki wants to merge 1 commit into
Conversation
Always release destinations opened by stream_response_to_file when\nreading or writing fails, while keeping caller-owned streams open.
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.
Problem
stream_response_to_filecloses a destination it opens only after all response chunks have been written. Ifiter_contentraises (for exampleChunkedEncodingError) or writing raisesIOError, cleanup is skipped. Retaining the exception traceback also retains the local file handle, so cleanup may be delayed until the traceback is released. This is a resource-lifecycle correction, not a security-vulnerability claim.Change
Put the existing write loop in
try/finally, closing only the destination opened by this function. Caller-owned streams remain open, as documented. The response ownership, partial-file contents, filename handling, and existing-file checks are unchanged. This does not overlap #403's exclusive-creation fix.Normal successful close preserves the original read/write exception. If close itself raises, ordinary
finallysemantics still apply: its exception can replace the primary exception, with exception context retained. No new suppression or retry policy is introduced.Validation
Six regressions are added to the existing
tests/test_downloadutils.py: owned real temporary file versus caller-owned stream, each with success, read failure, and write failure. The tests retain the exception while checking handle closure and preserve successful bytes/return values and original exception identity when close succeeds.git diff --checkpasses.The initial focused cases above were extracted from the final test module for offline execution. A subsequent complete download-module check on the same commit used the committed Betamax cassette in playback-only mode, with network/process guards (0 guard events):
tests/test_downloadutils.py: 20 passed after filtering only the knowndatetime.utcnow()deprecation warnings inbetamax.adapter,betamax.cassette.cassette, andbetamax.cassette.interaction.This validates the download module, not the full project suite. Documentation builds, older Python versions, Windows execution, and hosted test CI have not been validated for this commit. Prepared and independently reviewed with AI assistance; no human-review claim is made.