Skip to content

Commit 59be9cd

Browse files
authored
bpo-40280: Detect if WASM platform supports threading (GH-32243)
Automerge-Triggered-By: GH:tiran
1 parent 7000cd7 commit 59be9cd

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,30 @@ 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+
else:
229+
return True
230+
elif sys.platform == "wasi":
231+
return False
232+
else:
233+
# assume all other platforms have working thread support.
234+
return True
235+
236+
can_start_thread = _can_start_thread()

0 commit comments

Comments
 (0)