From 934bad87074c7ad8c8fe04a5ba14e7e3c1c37e93 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Fri, 1 Apr 2022 23:09:31 +0200 Subject: [PATCH 1/2] bpo-40280: Detect if WASM platform supports threading --- Lib/test/libregrtest/runtest.py | 5 ++++- Lib/test/support/threading_helper.py | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py index 83c5f249841ccc..62cf1a3f1175ee 100644 --- a/Lib/test/libregrtest/runtest.py +++ b/Lib/test/libregrtest/runtest.py @@ -11,6 +11,7 @@ from test import support from test.support import os_helper +from test.support import threading_helper from test.libregrtest.cmdline import Namespace from test.libregrtest.save_env import saved_test_environment from test.libregrtest.utils import clear_caches, format_duration, print_warning @@ -179,7 +180,9 @@ def _runtest(ns: Namespace, test_name: str) -> TestResult: output_on_failure = ns.verbose3 - use_timeout = (ns.timeout is not None) + use_timeout = ( + ns.timeout is not None and threading_helper.can_start_thread + ) if use_timeout: faulthandler.dump_traceback_later(ns.timeout, exit=True) diff --git a/Lib/test/support/threading_helper.py b/Lib/test/support/threading_helper.py index 92a64e8354acbc..7e6b5166365768 100644 --- a/Lib/test/support/threading_helper.py +++ b/Lib/test/support/threading_helper.py @@ -207,3 +207,29 @@ def __exit__(self, *exc_info): del self.exc_value del self.exc_traceback del self.thread + + +def _can_start_thread() -> bool: + """Detect if Python can start new threads + + Some WebAssembly platforms do not provide a working pthread + implementation. Thread support is stubbed and any attempt + to create a new thread fails. + + - wasm32-wasi does not have threading + - wasm32-emscripten can be compiled with or without pthread + support. (-s USE_PTHREADS / __EMSCRIPTEN_PTHREADS__). + """ + if sys.platform == "emscripten": + try: + _thread.start_new_thread(lambda: None, ()) + except RuntimeError: + return False + return True + elif sys.platform == "wasi": + return False + else: + # assume all other platforms have working thread support. + return True + +can_start_thread = _can_start_thread() From 413398a84b9965a3481e619701a152498b164fd9 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Sat, 2 Apr 2022 09:45:30 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Brett Cannon --- Lib/test/support/threading_helper.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/test/support/threading_helper.py b/Lib/test/support/threading_helper.py index 7e6b5166365768..2ae4577f41f7be 100644 --- a/Lib/test/support/threading_helper.py +++ b/Lib/test/support/threading_helper.py @@ -210,22 +210,23 @@ def __exit__(self, *exc_info): def _can_start_thread() -> bool: - """Detect if Python can start new threads + """Detect if Python can start new threads. Some WebAssembly platforms do not provide a working pthread implementation. Thread support is stubbed and any attempt to create a new thread fails. - - wasm32-wasi does not have threading + - wasm32-wasi does not have threading. - wasm32-emscripten can be compiled with or without pthread - support. (-s USE_PTHREADS / __EMSCRIPTEN_PTHREADS__). + support (-s USE_PTHREADS / __EMSCRIPTEN_PTHREADS__). """ if sys.platform == "emscripten": try: _thread.start_new_thread(lambda: None, ()) except RuntimeError: return False - return True + else: + return True elif sys.platform == "wasi": return False else: