From 1421ad36372a3ea22c5c9cf32a03f9a2f9bae439 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 4 Jun 2025 14:00:25 +0100 Subject: [PATCH 1/2] gh-126483: disable warnings filters mutation in concurrent test (GH-132694) The `test_ssl_in_multiple_threads` test failed because `test_check_hostname_idn()` modified the global warnings filters via `warnings_helper.check_no_resource_warning()`. Only check for warnings when the context aware warnings feature is enabled, which makes the warnings filter context-local and thread-safe. (cherry picked from commit 40c8be0008ecadb5d0dc9a017434b1133a3a6e06) Co-authored-by: Thomas Grainger --- Lib/test/test_ssl.py | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index dfd5d2a0b2c79e..3476e7d6af9791 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -31,6 +31,7 @@ import platform import sysconfig import functools +from contextlib import nullcontext try: import ctypes except ImportError: @@ -2871,6 +2872,7 @@ def test_ssl_in_multiple_threads(self): # See GH-124984: OpenSSL is not thread safe. threads = [] + warnings_filters = sys.flags.context_aware_warnings global USE_SAME_TEST_CONTEXT USE_SAME_TEST_CONTEXT = True try: @@ -2879,7 +2881,10 @@ def test_ssl_in_multiple_threads(self): self.test_alpn_protocols, self.test_getpeercert, self.test_crl_check, - self.test_check_hostname_idn, + functools.partial( + self.test_check_hostname_idn, + warnings_filters=warnings_filters, + ), self.test_wrong_cert_tls12, self.test_wrong_cert_tls13, ): @@ -3125,7 +3130,7 @@ def test_dual_rsa_ecc(self): cipher = s.cipher()[0].split('-') self.assertTrue(cipher[:2], ('ECDHE', 'ECDSA')) - def test_check_hostname_idn(self): + def test_check_hostname_idn(self, warnings_filters=True): if support.verbose: sys.stdout.write("\n") @@ -3180,16 +3185,30 @@ def test_check_hostname_idn(self): server_hostname="python.example.org") as s: with self.assertRaises(ssl.CertificateError): s.connect((HOST, server.port)) - with ThreadedEchoServer(context=server_context, chatty=True) as server: - with warnings_helper.check_no_resource_warning(self): - with self.assertRaises(UnicodeError): - context.wrap_socket(socket.socket(), - server_hostname='.pythontest.net') - with ThreadedEchoServer(context=server_context, chatty=True) as server: - with warnings_helper.check_no_resource_warning(self): - with self.assertRaises(UnicodeDecodeError): - context.wrap_socket(socket.socket(), - server_hostname=b'k\xf6nig.idn.pythontest.net') + with ( + ThreadedEchoServer(context=server_context, chatty=True) as server, + ( + warnings_helper.check_no_resource_warning(self) + if warnings_filters + else nullcontext() + ), + self.assertRaises(UnicodeError), + ): + context.wrap_socket(socket.socket(), server_hostname='.pythontest.net') + + with ( + ThreadedEchoServer(context=server_context, chatty=True) as server, + ( + warnings_helper.check_no_resource_warning(self) + if warnings_filters + else nullcontext() + ), + self.assertRaises(UnicodeDecodeError), + ): + context.wrap_socket( + socket.socket(), + server_hostname=b'k\xf6nig.idn.pythontest.net', + ) def test_wrong_cert_tls12(self): """Connecting when the server rejects the client's certificate From 7abdb515235c99d0bc142c277e9807e8d8d8a433 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Wed, 4 Jun 2025 13:06:18 +0000 Subject: [PATCH 2/2] Fix for 3.13 --- Lib/test/test_ssl.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 3476e7d6af9791..83cf7e08dfa398 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -2872,7 +2872,6 @@ def test_ssl_in_multiple_threads(self): # See GH-124984: OpenSSL is not thread safe. threads = [] - warnings_filters = sys.flags.context_aware_warnings global USE_SAME_TEST_CONTEXT USE_SAME_TEST_CONTEXT = True try: @@ -2883,7 +2882,7 @@ def test_ssl_in_multiple_threads(self): self.test_crl_check, functools.partial( self.test_check_hostname_idn, - warnings_filters=warnings_filters, + warnings_filters=False, # gh-126483 ), self.test_wrong_cert_tls12, self.test_wrong_cert_tls13,