From 866cb0eb7dbda89cdde294fb45c679cfbed03aee Mon Sep 17 00:00:00 2001 From: Narendra S Date: Sat, 29 Nov 2025 00:54:51 +0530 Subject: [PATCH 1/2] 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. --- Lib/http/client.py | 2 +- .../Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst diff --git a/Lib/http/client.py b/Lib/http/client.py index 4b9a61cfc1159f..15b5327332d202 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -1449,7 +1449,7 @@ def getresponse(self): return response except: - response.close() + self.close() raise try: diff --git a/Misc/NEWS.d/next/Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst b/Misc/NEWS.d/next/Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst new file mode 100644 index 00000000000000..4242918274ffdc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst @@ -0,0 +1,4 @@ +Fix `http.client.HTTPConnection` to properly reset its internal state when +reading the response raises an exception such as `TimeoutError`, ensuring +the connection can be reused or raises appropriate errors on subsequent +requests. From 97958043ad60ba30e049cb86029d010adb532008 Mon Sep 17 00:00:00 2001 From: Narendra S Date: Sat, 29 Nov 2025 01:44:56 +0530 Subject: [PATCH 2/2] gh-141938: fix NEWS.d to include appropriate directives --- .../Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst b/Misc/NEWS.d/next/Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst index 4242918274ffdc..755e322ccc5553 100644 --- a/Misc/NEWS.d/next/Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst +++ b/Misc/NEWS.d/next/Library/2025-11-29-00-52-37.gh-issue-141938.ZADQRl.rst @@ -1,4 +1,4 @@ -Fix `http.client.HTTPConnection` to properly reset its internal state when -reading the response raises an exception such as `TimeoutError`, ensuring +Fix :class:`http.client.HTTPConnection` to properly reset its internal state when +reading the response raises an exception such as :exc:`TimeoutError`, ensuring the connection can be reused or raises appropriate errors on subsequent requests.