Skip to content

fix: retain stream read error on subsequent response.content accesses (#4965)#7527

Closed
armorbreak001 wants to merge 1 commit into
psf:mainfrom
armorbreak001:fix/retain-content-error-4965
Closed

fix: retain stream read error on subsequent response.content accesses (#4965)#7527
armorbreak001 wants to merge 1 commit into
psf:mainfrom
armorbreak001:fix/retain-content-error-4965

Conversation

@armorbreak001

Copy link
Copy Markdown

Summary

Fixes #4965 (supersedes #7415)

When response.content raises an exception during stream reading (e.g., ConnectionError, ChunkedEncodingError), subsequent accesses would silently return an empty bytestring or raise a generic RuntimeError instead of re-raising the original error.

This is especially problematic in debuggers where properties may be accessed multiple times — the original error is silently lost, making debugging very difficult.

Root Cause

In the content property:

  1. First access enters if self._content is False, calls iter_content()
  2. If iter_content() raises (e.g., connection reset mid-stream), the exception propagates
  3. But _content_consumed was already set to True inside iter_content()
  4. On second access: _content is False is still True, but now hits the _content_consumed guard → raises generic RuntimeError
  5. Or if the error happened after partial read, _content may be set to incomplete data

Changes

  • Add _content_error attribute to cache the first read exception
  • Check _content_error before the _content is False block (addresses reviewer feedback on Fix: retain stream read error on subsequent response.content access #7415)
  • Wrap content reading in try/except to capture and store the exception
  • Re-raise the same exception object on subsequent accesses (preserves traceback)

Testing

Two new regression tests in test_lowlevel.py:

  1. test_response_content_retains_error: Full read failure → second access raises same ConnectionError
  2. test_response_content_retains_error_after_partial_read: Partial read then failure → second access raises same ChunkedEncodingError

Both tests verify that the exception type and identity are preserved across multiple accesses.

Difference from #7415

  • Initializes _content_error in __init__ (not dynamically in property)
  • Checks _content_error before the if _content is False guard (prevents unnecessary re-read attempt)
  • Covers both full-failure and partial-read-failure scenarios with tests

…psf#4965)

When accessing response.content raises an exception during stream reading
(e.g., ConnectionError, ChunkedEncodingError), subsequent accesses would
return empty bytes (b"") or raise a generic RuntimeError instead of
re-raising the original error.

This made debugging especially difficult in debuggers where properties
may be accessed multiple times, as the original error was silently lost.

Changes:
- Add _content_error attribute to cache the first read exception
- Check _content_error early in content property (before re-read attempt)
- Re-raise the same exception object on subsequent accesses
- Add regression tests for full-error and partial-read failure cases

Fixes psf#4965
@sigmavirus24 sigmavirus24 added spam mass-automation-pr This PR was created from an account spamming large projects with "fixes". labels Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mass-automation-pr This PR was created from an account spamming large projects with "fixes". spam

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessing response.content twice removes forgets read error

2 participants