Skip to content

Close owned download files on errors - #408

Draft
lexingtonhibiki wants to merge 1 commit into
requests:mainfrom
lexingtonhibiki:fix/close-owned-download-on-error
Draft

lexingtonhibiki wants to merge 1 commit into
requests:mainfrom
lexingtonhibiki:fix/close-owned-download-on-error

Conversation

@lexingtonhibiki

@lexingtonhibiki lexingtonhibiki commented Oct 23, 2026 •

Copy link
Copy Markdown

Problem

stream_response_to_file closes a destination it opens only after all response chunks have been written. If iter_content raises (for example ChunkedEncodingError) or writing raises IOError, 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 finally semantics 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.

  • Current main with these cases: 2 failed, 4 passed (owned read/write error paths).
  • Candidate: 6 passed, with strict DeprecationWarning handling and network/process guard; guard events 0.
  • Independent AI review reran the six cases and checked six additional boundaries.
  • git diff --check passes.

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):

  • Entire tests/test_downloadutils.py: 20 passed after filtering only the known datetime.utcnow() deprecation warnings in betamax.adapter, betamax.cassette.cassette, and betamax.cassette.interaction.
  • Original parent production code with the same complete test module and narrow filters: 18 passed, 2 failed (the owned read/write cleanup regressions).
  • Strict DeprecationWarning-as-error: the candidate has 11 passed / 9 failed; original code has 9 passed / 11 failed. Both have the same nine pre-existing Betamax cassette warning failures; the original additionally fails the two cleanup regressions.

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.

Always release destinations opened by stream_response_to_file when\nreading or writing fails, while keeping caller-owned streams open.
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.

2 participants