Skip to content

Fix: propagate stream read error on retried Response.content (#4965)#7447

Open
trippusultan wants to merge 1 commit into
psf:mainfrom
trippusultan:main
Open

Fix: propagate stream read error on retried Response.content (#4965)#7447
trippusultan wants to merge 1 commit into
psf:mainfrom
trippusultan:main

Conversation

@trippusultan
Copy link
Copy Markdown

Fixes #4965

Bug: When Response.iter_content() raises an exception during the first access to response.content, the exception is silently lost. The state (self._content stays False, self._content_consumed stays False) means a second access to .content retries iter_content() on an already-exhausted stream instead of re-raising the relevant error. In a debugger, this means the real error is hidden by a secondary "content already consumed" or empty-bytes result.

Fix: Wrap the iter_content() call in try/except. On exception:

  1. Store the original error as self._content so any subsequent .content access raises it consistently.
  2. Set self._content_consumed = True to prevent re-reading the stream.
  3. Close self.raw so the connection isn’t leaked.
  4. Re-raise the original exception immediately.

Test-added: The error-path is now stable — a second .content call raises the same original exception, not a secondary one.

When iter_content() raised during the first .content access the exception
was silently lost (superceded by the second read attempting to re-consume
the already-exhausted raw stream), so subsequent .content calls could not
re-produce or raise the relevant error for the caller.

Fix: wrap the iter_content() call in try/except, store the original
exception as _content, mark _content_consumed=True, close the raw if not
already released, and re-raise. Second .content access now raises the
original read error consistently.
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.

Accessing response.content twice removes forgets read error

1 participant