Skip to content

Commit 994c80f

Browse files
authored
Add error handling in request method
Handle exceptions during request transmission and close connection on failure.
1 parent 425f24e commit 994c80f

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

Lib/http/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,13 @@ def endheaders(self, message_body=None, *, encode_chunked=False):
13391339
def request(self, method, url, body=None, headers={}, *,
13401340
encode_chunked=False):
13411341
"""Send a complete request to the server."""
1342+
try:
13421343
self._send_request(method, url, body, headers, encode_chunked)
1344+
except:
1345+
# If the transmission fails (e.g. timeout), close the connection
1346+
# to reset the state machine to _CS_IDLE.
1347+
self.close()
1348+
raise
13431349

13441350
def _send_request(self, method, url, body, headers, encode_chunked):
13451351
# Honor explicitly requested Host: and Accept-Encoding: headers.

0 commit comments

Comments
 (0)