Skip to content

Commit 8097d96

Browse files
authored
gh-130902 Add optional socket shutdown to HTTPConnection.close
Similar to gh-130862 but for http library
1 parent 9c69150 commit 8097d96

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

Lib/http/client.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,19 +1012,37 @@ def connect(self):
10121012
if self._tunnel_host:
10131013
self._tunnel()
10141014

1015-
def close(self):
1016-
"""Close the connection to the HTTP server."""
1015+
def close(self, shutdown=False):
10171016
self.__state = _CS_IDLE
10181017
try:
10191018
sock = self.sock
10201019
if sock:
10211020
self.sock = None
1022-
sock.close() # close it manually... there may be other refs
1021+
if shutdown:
1022+
try:
1023+
sock.shutdown(socket.SHUT_RDWR)
1024+
except OSError as e:
1025+
print(f"Socket shutdown error: {e}", file=sys.stderr)
1026+
except Exception as e:
1027+
print(f"Unexpected error during socket shutdown: {e}", file=sys.stderr)
1028+
try:
1029+
sock.close()
1030+
except OSError as e:
1031+
print(f"Socket close error: {e}", file=sys.stderr)
1032+
except Exception as e:
1033+
print(f"Unexpected error during socket close: {e}", file=sys.stderr)
10231034
finally:
10241035
response = self.__response
10251036
if response:
10261037
self.__response = None
1027-
response.close()
1038+
try:
1039+
response.close()
1040+
except OSError as e:
1041+
print(f"Response close error: {e}", file=sys.stderr)
1042+
except Exception as e:
1043+
print(f"Unexpected error during response close: {e}", file=sys.stderr)
1044+
1045+
10281046

10291047
def send(self, data):
10301048
"""Send 'data' to the server.

0 commit comments

Comments
 (0)