Skip to content

Commit 40c8be0

Browse files
authored
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.
1 parent bc00ce9 commit 40c8be0

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

Lib/test/test_ssl.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import platform
3232
import sysconfig
3333
import functools
34+
from contextlib import nullcontext
3435
try:
3536
import ctypes
3637
except ImportError:
@@ -2843,6 +2844,7 @@ def test_ssl_in_multiple_threads(self):
28432844
# See GH-124984: OpenSSL is not thread safe.
28442845
threads = []
28452846

2847+
warnings_filters = sys.flags.context_aware_warnings
28462848
global USE_SAME_TEST_CONTEXT
28472849
USE_SAME_TEST_CONTEXT = True
28482850
try:
@@ -2851,7 +2853,10 @@ def test_ssl_in_multiple_threads(self):
28512853
self.test_alpn_protocols,
28522854
self.test_getpeercert,
28532855
self.test_crl_check,
2854-
self.test_check_hostname_idn,
2856+
functools.partial(
2857+
self.test_check_hostname_idn,
2858+
warnings_filters=warnings_filters,
2859+
),
28552860
self.test_wrong_cert_tls12,
28562861
self.test_wrong_cert_tls13,
28572862
):
@@ -3097,7 +3102,7 @@ def test_dual_rsa_ecc(self):
30973102
cipher = s.cipher()[0].split('-')
30983103
self.assertTrue(cipher[:2], ('ECDHE', 'ECDSA'))
30993104

3100-
def test_check_hostname_idn(self):
3105+
def test_check_hostname_idn(self, warnings_filters=True):
31013106
if support.verbose:
31023107
sys.stdout.write("\n")
31033108

@@ -3152,16 +3157,30 @@ def test_check_hostname_idn(self):
31523157
server_hostname="python.example.org") as s:
31533158
with self.assertRaises(ssl.CertificateError):
31543159
s.connect((HOST, server.port))
3155-
with ThreadedEchoServer(context=server_context, chatty=True) as server:
3156-
with warnings_helper.check_no_resource_warning(self):
3157-
with self.assertRaises(UnicodeError):
3158-
context.wrap_socket(socket.socket(),
3159-
server_hostname='.pythontest.net')
3160-
with ThreadedEchoServer(context=server_context, chatty=True) as server:
3161-
with warnings_helper.check_no_resource_warning(self):
3162-
with self.assertRaises(UnicodeDecodeError):
3163-
context.wrap_socket(socket.socket(),
3164-
server_hostname=b'k\xf6nig.idn.pythontest.net')
3160+
with (
3161+
ThreadedEchoServer(context=server_context, chatty=True) as server,
3162+
(
3163+
warnings_helper.check_no_resource_warning(self)
3164+
if warnings_filters
3165+
else nullcontext()
3166+
),
3167+
self.assertRaises(UnicodeError),
3168+
):
3169+
context.wrap_socket(socket.socket(), server_hostname='.pythontest.net')
3170+
3171+
with (
3172+
ThreadedEchoServer(context=server_context, chatty=True) as server,
3173+
(
3174+
warnings_helper.check_no_resource_warning(self)
3175+
if warnings_filters
3176+
else nullcontext()
3177+
),
3178+
self.assertRaises(UnicodeDecodeError),
3179+
):
3180+
context.wrap_socket(
3181+
socket.socket(),
3182+
server_hostname=b'k\xf6nig.idn.pythontest.net',
3183+
)
31653184

31663185
def test_wrong_cert_tls12(self):
31673186
"""Connecting when the server rejects the client's certificate

0 commit comments

Comments
 (0)