Skip to content

Commit 127ef3e

Browse files
Rollup merge of rust-lang#151016 - fix_wasi_threading, r=alexcrichton
fix: WASI threading regression by disabling pthread usage PR rust-lang#147572 changed WASI to use the Unix threading implementation, but WASI does not support threading. When the Unix code tries to call pthread_create, it fails with EAGAIN, causing libraries like rayon to panic when trying to initialize their global thread pool. The old wasip1/wasip2 implementations: - wasip1: Threading conditionally available with atomics (experimental) - wasip2: Threading unconditionally unsupported This fix restores that behavior by disabling pthread-based threading for all WASI targets: 1. Guard the pthread-based Thread implementation with #[cfg(not(target_os = "wasi"))] 2. Provide an unsupported stub (Thread(!)) for WASI 3. Return Err(io::Error::UNSUPPORTED_PLATFORM) when Thread::new is called Fixes the regression where rayon-based code (e.g., lopdf in PDF handling) panicked on WASI after nightly-2025-12-10. Before fix: pthread_create returns EAGAIN (error code 6) ThreadPoolBuildError { kind: IOError(Os { code: 6, kind: WouldBlock, message: "Resource temporarily unavailable" }) } After fix: Thread::new returns Err(io::Error::UNSUPPORTED_PLATFORM) Libraries can gracefully handle the lack of threading support r? @alexcrichton
2 parents 4cd42e8 + c1bcae0 commit 127ef3e

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

library/std/src/sys/thread/unix.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ impl Thread {
4444
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
4545
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
4646
pub unsafe fn new(stack: usize, init: Box<ThreadInit>) -> io::Result<Thread> {
47+
// FIXME: remove this block once wasi-sdk is updated with the fix from
48+
// https://github.com/WebAssembly/wasi-libc/pull/716
49+
// WASI does not support threading via pthreads. While wasi-libc provides
50+
// pthread stubs, pthread_create returns EAGAIN, which causes confusing
51+
// errors. We return UNSUPPORTED_PLATFORM directly instead.
52+
if cfg!(target_os = "wasi") {
53+
return Err(io::Error::UNSUPPORTED_PLATFORM);
54+
}
55+
4756
let data = init;
4857
let mut attr: mem::MaybeUninit<libc::pthread_attr_t> = mem::MaybeUninit::uninit();
4958
assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);

0 commit comments

Comments
 (0)