Skip to content

Commit 9d42aee

Browse files
authored
gh-160206: Close connection explicitly in test_ssl (#160222)
test_https_client_non_tls_response_ignored() now uses a context manager for the server and always closes the connection to avoid ResourceWarning. Also add a timeout to SingleConnectionTestServerThread.__exit__() to not hang forever if something goes wrong.
1 parent d0212f6 commit 9d42aee

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

‎Lib/test/test_ssl.py‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5927,7 +5927,11 @@ def __exit__(self, *args):
59275927
self.listener.close()
59285928
except OSError:
59295929
pass
5930-
self.join()
5930+
timeout = support.SHORT_TIMEOUT
5931+
self.join(timeout=timeout)
5932+
if self.is_alive():
5933+
raise AssertionError("failed to join the server thread "
5934+
f"in {timeout} seconds")
59315935
self.wrap_error = None # avoid dangling references
59325936

59335937
def start(self):
@@ -6133,28 +6137,30 @@ def call_after_accept(conn_to_client):
61336137
call_after_accept=call_after_accept,
61346138
name="non_tls_http_RST_responder",
61356139
timeout=timeout)
6136-
self.enterContext(server) # starts it & unittest.TestCase stops it.
6137-
# Redundant; call_after_accept sets SO_LINGER on the accepted conn.
6138-
set_socket_so_linger_on_with_zero_timeout(server.listener)
6139-
6140-
connection = SynchronizedHTTPSConnection(
6141-
server.listener.getsockname()[0],
6142-
port=server.port,
6143-
context=ssl.create_default_context(),
6144-
timeout=timeout,
6145-
)
61466140

6147-
# There are lots of reasons this raises as desired, long before this
6148-
# test was added. Sending the request requires a successful TLS wrapped
6149-
# socket; that fails if the connection is broken. It may seem pointless
6150-
# to test this. It serves as an illustration of something that we never
6151-
# want to happen... properly not happening.
6152-
with warnings_helper.check_no_resource_warning(self), \
6153-
self.assertRaises(OSError):
6154-
connection.request("HEAD", "/test", headers={"Host": "localhost"})
6155-
response = connection.getresponse()
6141+
with server:
6142+
# Redundant; call_after_accept sets SO_LINGER on the accepted conn.
6143+
set_socket_so_linger_on_with_zero_timeout(server.listener)
6144+
6145+
connection = SynchronizedHTTPSConnection(
6146+
server.listener.getsockname()[0],
6147+
port=server.port,
6148+
context=ssl.create_default_context(),
6149+
timeout=timeout,
6150+
)
61566151

6157-
server.join()
6152+
# There are lots of reasons this raises as desired, long before this
6153+
# test was added. Sending the request requires a successful TLS wrapped
6154+
# socket; that fails if the connection is broken. It may seem pointless
6155+
# to test this. It serves as an illustration of something that we never
6156+
# want to happen... properly not happening.
6157+
with warnings_helper.check_no_resource_warning(self), \
6158+
self.assertRaises(OSError):
6159+
try:
6160+
connection.request("HEAD", "/test", headers={"Host": "localhost"})
6161+
response = connection.getresponse()
6162+
finally:
6163+
connection.close()
61586164

61596165

61606166
class TestEnumerations(unittest.TestCase):

0 commit comments

Comments
 (0)