Verified Solution

[rust-lang/rust] `std::thread::spawn` unconditionally disabled on WASI breaks `wasm32-wasip1-threads` with emnapi's working pthread implementation

Sponsored Content
### ROOT CAUSE The issue arises because the Rust standard library's `std::thread::spawn` is unconditionally disabled for all WASI targets via a `cfg!(target_os = "wasi")` guard. This guard prevents the use of pthread-based threading, even for targets like `wasm32-wasip1-threads` that use emnapi's working pthread implementation. The blanket disabling is too broad and breaks projects that rely on WASI's threading capabilities. ### CODE FIX To fix this, we should conditionally disable threading for WASI targets that do not support pthreads. Specifically, we need to check if the target is `wasm32-wasip1-threads` or if it uses a WASI libc that properly supports pthreads. However, since we cannot directly detect the pthread implementation, we can adjust the condition to exclude the `wasm32-wasip1-threads` target. Here's the fix: ```rust impl Thread { pub unsafe fn new(stack: usize, init: Box) -> io::Result { // Check if the target is a WASI target that does not support pthreads (i.e., standard WASI libc) // We want to disable only for targets that are known to have the issue, such as the default WASI targets. // The `wasm32-wasip1-threads` target should be allowed to use pthreads. if cfg!(target_os = "wasi") && !cfg!(target_arch = "wasm32") { return Err(io::Error::new(io::ErrorKind::Other, "not supported on this WASI target")); } // Original code to call pthread_create (if not WASI or if target is wasm32-wasip1-threads) // ... (rest of the code) } } ``` **Explanation:** 1. The condition now checks if the target is a WASI target (`target_os = "wasi"`) and not a `wasm32` architecture. 2. This ensures that the `wasm32-wasip1-threads` target (which uses emnapi) is not affected by the disabling. 3. For other WASI targets (e.g., `wasm32-wasi`), the code still returns an error, as they may still use the standard WASI libc that does not support pthreads. This fix maintains the disabling for non-`wasm32` WASI targets while allowing `wasm32-wasip1-threads` to use pthreads. Note that this might require further adjustments if other WASI targets (not `wasm32`) need to support pthreads in the future.
Deploy on DigitalOcean ($200 Credit)

Related Fixes

[gitlab-org/gitlab] Auto-merge (MWCP) does not respect "Skipped pipelines are considered successful" project setting
[gitlab-org/gitlab] Creation flow for custom types
[rust-lang/rust] Any prrogram force-depends on sigaction (make std's signal handler optional)