Fix: propagate stream read error on retried Response.content (#4965)#7447
Open
trippusultan wants to merge 1 commit into
Open
Fix: propagate stream read error on retried Response.content (#4965)#7447trippusultan wants to merge 1 commit into
trippusultan wants to merge 1 commit into
Conversation
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.
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.
Fixes #4965
Bug: When
Response.iter_content()raises an exception during the first access toresponse.content, the exception is silently lost. The state (self._contentstaysFalse,self._content_consumedstaysFalse) means a second access to.contentretriesiter_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 intry/except. On exception:self._contentso any subsequent.contentaccess raises it consistently.self._content_consumed = Trueto prevent re-reading the stream.self.rawso the connection isn’t leaked.Test-added: The error-path is now stable — a second
.contentcall raises the same original exception, not a secondary one.