Skip to content

Commit 43558fe

Browse files
vstinnermiss-islington
authored andcommitted
gh-160206: Close connection explicitly in test_ssl (GH-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. (cherry picked from commit 9d42aee) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 1bcc0d1 commit 43558fe

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
@@ -5561,7 +5561,11 @@ def __exit__(self, *args):
55615561
self.listener.close()
55625562
except OSError:
55635563
pass
5564-
self.join()
5564+
timeout = support.SHORT_TIMEOUT
5565+
self.join(timeout=timeout)
5566+
if self.is_alive():
5567+
raise AssertionError("failed to join the server thread "
5568+
f"in {timeout} seconds")
55655569
self.wrap_error = None # avoid dangling references
55665570

55675571
def start(self):
@@ -5767,28 +5771,30 @@ def call_after_accept(conn_to_client):
57675771
call_after_accept=call_after_accept,
57685772
name="non_tls_http_RST_responder",
57695773
timeout=timeout)
5770-
self.enterContext(server) # starts it & unittest.TestCase stops it.
5771-
# Redundant; call_after_accept sets SO_LINGER on the accepted conn.
5772-
set_socket_so_linger_on_with_zero_timeout(server.listener)
5773-
5774-
connection = SynchronizedHTTPSConnection(
5775-
server.listener.getsockname()[0],
5776-
port=server.port,
5777-
context=ssl.create_default_context(),
5778-
timeout=timeout,
5779-
)
57805774

5781-
# There are lots of reasons this raises as desired, long before this
5782-
# test was added. Sending the request requires a successful TLS wrapped
5783-
# socket; that fails if the connection is broken. It may seem pointless
5784-
# to test this. It serves as an illustration of something that we never
5785-
# want to happen... properly not happening.
5786-
with warnings_helper.check_no_resource_warning(self), \
5787-
self.assertRaises(OSError):
5788-
connection.request("HEAD", "/test", headers={"Host": "localhost"})
5789-
response = connection.getresponse()
5775+
with server:
5776+
# Redundant; call_after_accept sets SO_LINGER on the accepted conn.
5777+
set_socket_so_linger_on_with_zero_timeout(server.listener)
5778+
5779+
connection = SynchronizedHTTPSConnection(
5780+
server.listener.getsockname()[0],
5781+
port=server.port,
5782+
context=ssl.create_default_context(),
5783+
timeout=timeout,
5784+
)
57905785

5791-
server.join()
5786+
# There are lots of reasons this raises as desired, long before this
5787+
# test was added. Sending the request requires a successful TLS wrapped
5788+
# socket; that fails if the connection is broken. It may seem pointless
5789+
# to test this. It serves as an illustration of something that we never
5790+
# want to happen... properly not happening.
5791+
with warnings_helper.check_no_resource_warning(self), \
5792+
self.assertRaises(OSError):
5793+
try:
5794+
connection.request("HEAD", "/test", headers={"Host": "localhost"})
5795+
response = connection.getresponse()
5796+
finally:
5797+
connection.close()
57925798

57935799

57945800
class TestEnumerations(unittest.TestCase):

0 commit comments

Comments
 (0)