https://github.com/python/cpython/commit/66fabd8c83c2b0e7228c37c9c0c93b90fcfc64b1
commit: 66fabd8c83c2b0e7228c37c9c0c93b90fcfc64b1
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-22T18:35:55Z
summary:

[3.14] gh-160206: Close connection explicitly in test_ssl (GH-160222) (#160274)

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 9d42aee5736a442af1c33524cd41ddc534cc6979)

Co-authored-by: Victor Stinner <[email protected]>

files:
M Lib/test/test_ssl.py

diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index d1486a46526160..7b8700ed75a7c3 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -5561,7 +5561,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):
@@ -5767,28 +5771,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]

Reply via email to