https://github.com/python/cpython/commit/9d42aee5736a442af1c33524cd41ddc534cc6979
commit: 9d42aee5736a442af1c33524cd41ddc534cc6979
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-22T12:43:27-05:00
summary:
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.
files:
M Lib/test/test_ssl.py
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index d4213482cd1215..27d5e56368bfbc 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -5927,7 +5927,11 @@ def __exit__(self, *args):
self.listener.close()
except OSError:
pass
- self.join()
+ timeout = support.SHORT_TIMEOUT
+ self.join(timeout=timeout)
+ if self.is_alive():
+ raise AssertionError("failed to join the server thread "
+ f"in {timeout} seconds")
self.wrap_error = None # avoid dangling references
def start(self):
@@ -6133,28 +6137,30 @@ def call_after_accept(conn_to_client):
call_after_accept=call_after_accept,
name="non_tls_http_RST_responder",
timeout=timeout)
- self.enterContext(server) # starts it & unittest.TestCase stops it.
- # Redundant; call_after_accept sets SO_LINGER on the accepted conn.
- set_socket_so_linger_on_with_zero_timeout(server.listener)
-
- connection = SynchronizedHTTPSConnection(
- server.listener.getsockname()[0],
- port=server.port,
- context=ssl.create_default_context(),
- timeout=timeout,
- )
- # There are lots of reasons this raises as desired, long before this
- # test was added. Sending the request requires a successful TLS wrapped
- # socket; that fails if the connection is broken. It may seem pointless
- # to test this. It serves as an illustration of something that we never
- # want to happen... properly not happening.
- with warnings_helper.check_no_resource_warning(self), \
- self.assertRaises(OSError):
- connection.request("HEAD", "/test", headers={"Host": "localhost"})
- response = connection.getresponse()
+ with server:
+ # Redundant; call_after_accept sets SO_LINGER on the accepted conn.
+ set_socket_so_linger_on_with_zero_timeout(server.listener)
+
+ connection = SynchronizedHTTPSConnection(
+ server.listener.getsockname()[0],
+ port=server.port,
+ context=ssl.create_default_context(),
+ timeout=timeout,
+ )
- server.join()
+ # There are lots of reasons this raises as desired, long before
this
+ # test was added. Sending the request requires a successful TLS
wrapped
+ # socket; that fails if the connection is broken. It may seem
pointless
+ # to test this. It serves as an illustration of something that we
never
+ # want to happen... properly not happening.
+ with warnings_helper.check_no_resource_warning(self), \
+ self.assertRaises(OSError):
+ try:
+ connection.request("HEAD", "/test", headers={"Host":
"localhost"})
+ response = connection.getresponse()
+ finally:
+ connection.close()
class TestEnumerations(unittest.TestCase):
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]