Skip to content

Commit 769d7d6

Browse files
committed
Fix WASI threading regression by disabling pthread usage
Recent changes made WASI targets 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
1 parent b115ea2 commit 769d7d6

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

library/std/src/sys/pal/wasi/stack_overflow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#[cfg_attr(target_os = "wasi", allow(dead_code))]
12
pub struct Handler;
23

34
impl Handler {
5+
#[cfg_attr(target_os = "wasi", allow(dead_code))]
46
pub unsafe fn new() -> Handler {
57
Handler
68
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
target_os = "wasi",
99
)))]
1010
use crate::ffi::CStr;
11+
#[cfg(target_os = "wasi")]
12+
use crate::io;
13+
#[cfg(not(target_os = "wasi"))]
1114
use crate::mem::{self, DropGuard, ManuallyDrop};
1215
use crate::num::NonZero;
1316
#[cfg(all(target_os = "linux", target_env = "gnu"))]
1417
use crate::sys::weak::dlsym;
1518
#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto",))]
1619
use crate::sys::weak::weak;
20+
#[cfg(not(target_os = "wasi"))]
1721
use crate::sys::{os, stack_overflow};
1822
use crate::thread::ThreadInit;
1923
use crate::time::Duration;
24+
#[cfg(not(target_os = "wasi"))]
2025
use crate::{cmp, io, ptr};
2126
#[cfg(not(any(
2227
target_os = "l4re",
@@ -32,6 +37,7 @@ pub const DEFAULT_MIN_STACK_SIZE: usize = 256 * 1024;
3237
#[cfg(any(target_os = "espidf", target_os = "nuttx"))]
3338
pub const DEFAULT_MIN_STACK_SIZE: usize = 0; // 0 indicates that the stack size configured in the ESP-IDF/NuttX menuconfig system should be used
3439

40+
#[cfg(not(target_os = "wasi"))]
3541
pub struct Thread {
3642
id: libc::pthread_t,
3743
}
@@ -41,6 +47,7 @@ pub struct Thread {
4147
unsafe impl Send for Thread {}
4248
unsafe impl Sync for Thread {}
4349

50+
#[cfg(not(target_os = "wasi"))]
4451
impl Thread {
4552
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
4653
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
@@ -138,6 +145,7 @@ impl Thread {
138145
}
139146
}
140147

148+
#[cfg(not(target_os = "wasi"))]
141149
impl Drop for Thread {
142150
fn drop(&mut self) {
143151
let ret = unsafe { libc::pthread_detach(self.id) };
@@ -885,7 +893,8 @@ unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
885893
// No point in looking up __pthread_get_minstack() on non-glibc platforms.
886894
#[cfg(all(
887895
not(all(target_os = "linux", target_env = "gnu")),
888-
not(any(target_os = "netbsd", target_os = "nuttx"))
896+
not(any(target_os = "netbsd", target_os = "nuttx")),
897+
not(target_os = "wasi"),
889898
))]
890899
unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
891900
libc::PTHREAD_STACK_MIN
@@ -904,3 +913,18 @@ unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
904913
stack as usize
905914
})
906915
}
916+
917+
// WASI does not support threading.
918+
#[cfg(target_os = "wasi")]
919+
pub struct Thread(!);
920+
921+
#[cfg(target_os = "wasi")]
922+
impl Thread {
923+
pub unsafe fn new(_stack: usize, _init: Box<ThreadInit>) -> io::Result<Thread> {
924+
Err(io::Error::UNSUPPORTED_PLATFORM)
925+
}
926+
927+
pub fn join(self) {
928+
self.0
929+
}
930+
}

0 commit comments

Comments
 (0)