From 6612541ea7b2aa6ea48649ceb2aad0e50c2a509f Mon Sep 17 00:00:00 2001 From: Abdul Taufeeq Date: Fri, 15 May 2026 07:40:55 +0000 Subject: [PATCH] Fix: propagate stream read error on retry of Response.content (#4965) 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. --- src/requests/models.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/requests/models.py b/src/requests/models.py index 59b5615960..b65383ac0d 100644 --- a/src/requests/models.py +++ b/src/requests/models.py @@ -1039,7 +1039,17 @@ def content(self) -> bytes: if self.status_code == 0 or self.raw is None: self._content = None else: - self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b"" + try: + self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b"" + except Exception as err: + # Issue #4965: propagate stream read error to subsequent .content + # accesses instead of silently discarding the state so a second + # call re-reads the now-empty raw stream. + self._content = err + self._content_consumed = True + if self.raw is not None: + self.raw.close() + raise self._content_consumed = True # don't need to release the connection; that's been handled by urllib3