Skip to content

Commit 866cb0e

Browse files
gh-141938: Fix HTTPConnection being stuck in a dirty state when response.begin() raises exceptions
When response.begin() raises an exception other than ConnectionError (such as TimeoutError), the outer exception handler calls response.close() but not self.close(), leaving the HTTPConnection's internal state stuck at _CS_REQ_SENT instead of being reset to _CS_IDLE. This causes subsequent requests on the same connection to raise CannotSendRequest even though the underlying socket has been closed. The fix changes the outer exception handler to call self.close() instead of just response.close(). This properly resets the connection state and closes the socket, consistent with the inner ConnectionError handler.
1 parent 5ec03cf commit 866cb0e

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

Lib/http/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,7 @@ def getresponse(self):
14491449

14501450
return response
14511451
except:
1452-
response.close()
1452+
self.close()
14531453
raise
14541454

14551455
try:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix `http.client.HTTPConnection` to properly reset its internal state when
2+
reading the response raises an exception such as `TimeoutError`, ensuring
3+
the connection can be reused or raises appropriate errors on subsequent
4+
requests.

0 commit comments

Comments
 (0)