Skip to content

Commit 934bad8

Browse files
committed
bpo-40280: Detect if WASM platform supports threading
1 parent 0f68c20 commit 934bad8

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Lib/test/libregrtest/runtest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from test import support
1313
from test.support import os_helper
14+
from test.support import threading_helper
1415
from test.libregrtest.cmdline import Namespace
1516
from test.libregrtest.save_env import saved_test_environment
1617
from test.libregrtest.utils import clear_caches, format_duration, print_warning
@@ -179,7 +180,9 @@ def _runtest(ns: Namespace, test_name: str) -> TestResult:
179180

180181
output_on_failure = ns.verbose3
181182

182-
use_timeout = (ns.timeout is not None)
183+
use_timeout = (
184+
ns.timeout is not None and threading_helper.can_start_thread
185+
)
183186
if use_timeout:
184187
faulthandler.dump_traceback_later(ns.timeout, exit=True)
185188

Lib/test/support/threading_helper.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,29 @@ def __exit__(self, *exc_info):
207207
del self.exc_value
208208
del self.exc_traceback
209209
del self.thread
210+
211+
212+
def _can_start_thread() -> bool:
213+
"""Detect if Python can start new threads
214+
215+
Some WebAssembly platforms do not provide a working pthread
216+
implementation. Thread support is stubbed and any attempt
217+
to create a new thread fails.
218+
219+
- wasm32-wasi does not have threading
220+
- wasm32-emscripten can be compiled with or without pthread
221+
support. (-s USE_PTHREADS / __EMSCRIPTEN_PTHREADS__).
222+
"""
223+
if sys.platform == "emscripten":
224+
try:
225+
_thread.start_new_thread(lambda: None, ())
226+
except RuntimeError:
227+
return False
228+
return True
229+
elif sys.platform == "wasi":
230+
return False
231+
else:
232+
# assume all other platforms have working thread support.
233+
return True
234+
235+
can_start_thread = _can_start_thread()

0 commit comments

Comments
 (0)