Skip to content

Fix thread::available_parallelism on WASI targets with threads#153604

Open
surban wants to merge 1 commit intorust-lang:mainfrom
rust-wasi-web:wasi-available-parallelism-fix
Open

Fix thread::available_parallelism on WASI targets with threads#153604
surban wants to merge 1 commit intorust-lang:mainfrom
rust-wasi-web:wasi-available-parallelism-fix

Conversation

@surban
Copy link
Contributor

@surban surban commented Mar 9, 2026

The refactoring in ba46286 ("std: Use more unix.rs code on WASI targets") moved WASI from its own thread module into the shared unix.rs module. However, it did not carry over the available_parallelism() implementation for WASI with threads, causing it to fall through to the unsupported catch-all. This silently regressed the support originally added in f0b7008.

Fix this by adding a dedicated cfg_select arm for WASI with atomics.
Plain wasip1 without threads continues to return unsupported, matching the previous behavior.

Copilot AI review requested due to automatic review settings March 9, 2026 10:31
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 9, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 9, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restores thread::available_parallelism() support for WASI targets compiled with the atomics target feature. A prior refactoring (ba462864f11) unified WASI into the shared unix.rs thread module but failed to migrate the WASI+atomics implementation of available_parallelism(), causing it to silently fall through to the unsupported catch-all.

Changes:

  • Adds a dedicated cfg_select arm for all(target_os = "wasi", target_feature = "atomics") in available_parallelism() that calls libc::sysconf with a hardcoded constant _SC_NPROCESSORS_ONLN = 84 (since the libc crate does not export this constant for WASI targets).
  • Plain wasip1 without threads continues to fall through to the _ arm and return an unsupported error, matching pre-regression behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

View changes since this review

@surban surban force-pushed the wasi-available-parallelism-fix branch from 6ca9426 to 0fcb2b0 Compare March 9, 2026 10:37
@surban surban requested a review from Copilot March 9, 2026 11:05

This comment was marked as spam.

@joboet
Copy link
Member

joboet commented Mar 10, 2026

Given that wasi-libc stubs sysconf(_SC_NPROCESSORS_ONLN) and unconditionally returns 1, I'm not sure whether this is actually an improvement. IMHO, available_parallelism should always fail if the answer cannot be determined, programs can always use .unwrap_or(0) if they desire.

CC @alexcrichton

@alexcrichton
Copy link
Member

Personally I think that this is a reasonable implementation, although I think it'd be best to lump this in with the implementation for _SC_NPROCESSORS_ONLN above with other platforms. That'll probably require a wasi-specific define of the constant, but @surban would you be up for sending a PR to the libc crate to add this constant there too?

For the correctness of this function for single-threaded wasm 1 is the correct answer, and otherwise the only other target would be for wasm32-wasip1-threads in which case I would consider it a bug in libc to stub it to return one, but it's still most accurate for Rust to just delegate to libc.

The refactoring in ba46286 ("std: Use more unix.rs code on WASI
targets") moved WASI from its own thread module into the shared unix.rs
module. However, it did not carry over the available_parallelism()
implementation for WASI with threads, causing it to fall through to the
unsupported catch-all. This silently regressed the support originally
added in f0b7008.

Fix this by:

- Adding WASI with atomics (i.e. wasm32-wasip1-threads) to the
  sysconf-based cfg_select arm alongside other platforms that use
  libc::sysconf(_SC_NPROCESSORS_ONLN).

- For single-threaded WASI (without atomics), returning Ok(1) since
  there is always exactly one thread of execution.

This requires libc to export _SC_NPROCESSORS_ONLN for WASI targets,
which is tracked in a companion PR to the libc crate.
@surban surban force-pushed the wasi-available-parallelism-fix branch from a41f6b5 to 00f08b0 Compare March 18, 2026 12:19
@surban
Copy link
Contributor Author

surban commented Mar 18, 2026

Personally I think that this is a reasonable implementation, although I think it'd be best to lump this in with the implementation for _SC_NPROCESSORS_ONLN above with other platforms. That'll probably require a wasi-specific define of the constant, but @surban would you be up for sending a PR to the libc crate to add this constant there too?

Yes, I have opened PR rust-lang/libc#5023 for all _SC_ constants in wasi-libc.
Checks should pass once the PR is merged and Rust updates its libc version.

For the correctness of this function for single-threaded wasm 1 is the correct answer, and otherwise the only other target would be for wasm32-wasip1-threads in which case I would consider it a bug in libc to stub it to return one, but it's still most accurate for Rust to just delegate to libc.

I have now changed it to always return Ok(1) for single-threaded wasm. Should I keep this or always (single-threaded and multi-threaded wasm) delegate to wasi-libc?

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

View changes since this review

Comment on lines +318 to +321
all(target_os = "wasi", not(target_feature = "atomics")) => {
// Single-threaded WASM always has exactly one thread of execution.
Ok(NonZero::new(1).unwrap())
}
Comment on lines 81 to 90
#[cfg_attr(
any(
target_os = "redox",
target_os = "l4re",
target_env = "sgx",
target_os = "solid_asp3",
target_os = "teeos",
target_os = "wasi"
),
should_panic
)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants