Skip to content

Commit 52f35d8

Browse files
gh-143443: Try to fix test_ssl_in_multiple_threads
Monkey-patch not thread-safe subTest. Ensure that waiting for all threads does not hang indefinitely.
1 parent b866a1c commit 52f35d8

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

Lib/test/test_ssl.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from test.support import warnings_helper
1414
from test.support import asyncore
1515
import array
16+
import contextlib
1617
import re
1718
import socket
1819
import select
@@ -2998,44 +2999,38 @@ def test_echo(self):
29982999
@unittest.skipUnless(support.Py_GIL_DISABLED, "test is only useful if the GIL is disabled")
29993000
def test_ssl_in_multiple_threads(self):
30003001
# See GH-124984: OpenSSL is not thread safe.
3002+
self.enterContext(
3003+
support.swap_item(globals(), 'USE_SAME_TEST_CONTEXT', True))
3004+
self.enterContext(
3005+
support.swap_attr(self, 'subTest',
3006+
lambda *args, **kwargs: contextlib.nullcontext()))
3007+
warnings_filters = sys.flags.context_aware_warnings
3008+
funcs = (
3009+
self.test_echo,
3010+
self.test_alpn_protocols,
3011+
self.test_getpeercert,
3012+
self.test_crl_check,
3013+
functools.partial(
3014+
self.test_check_hostname_idn,
3015+
warnings_filters=warnings_filters,
3016+
),
3017+
self.test_wrong_cert_tls12,
3018+
self.test_wrong_cert_tls13,
3019+
)
3020+
# Be careful with the number of threads here.
3021+
# Too many can result in failing tests.
30013022
threads = []
3023+
for num in range(5):
3024+
for func in funcs:
3025+
threads.append(Thread(target=func))
30023026

3003-
warnings_filters = sys.flags.context_aware_warnings
3004-
global USE_SAME_TEST_CONTEXT
3005-
USE_SAME_TEST_CONTEXT = True
3006-
try:
3007-
for func in (
3008-
self.test_echo,
3009-
self.test_alpn_protocols,
3010-
self.test_getpeercert,
3011-
self.test_crl_check,
3012-
functools.partial(
3013-
self.test_check_hostname_idn,
3014-
warnings_filters=warnings_filters,
3015-
),
3016-
self.test_wrong_cert_tls12,
3017-
self.test_wrong_cert_tls13,
3018-
):
3019-
# Be careful with the number of threads here.
3020-
# Too many can result in failing tests.
3021-
for num in range(5):
3022-
with self.subTest(func=func, num=num):
3023-
threads.append(Thread(target=func))
3024-
3025-
with threading_helper.catch_threading_exception() as cm:
3026-
for thread in threads:
3027-
with self.subTest(thread=thread):
3028-
thread.start()
3029-
3030-
for thread in threads:
3031-
with self.subTest(thread=thread):
3032-
thread.join()
3033-
if cm.exc_value is not None:
3034-
# Some threads can skip their test
3035-
if not isinstance(cm.exc_value, unittest.SkipTest):
3036-
raise cm.exc_value
3037-
finally:
3038-
USE_SAME_TEST_CONTEXT = False
3027+
with threading_helper.catch_threading_exception() as cm:
3028+
with threading_helper.start_threads(threads):
3029+
pass
3030+
if cm.exc_value is not None:
3031+
# Some threads can skip their test
3032+
if not isinstance(cm.exc_value, unittest.SkipTest):
3033+
raise cm.exc_value
30393034

30403035
def test_getpeercert(self):
30413036
if support.verbose:

0 commit comments

Comments
 (0)