Skip to content

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

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
rust-wasi-web:wasi-available-parallelism-fix
Apr 13, 2026
Merged

Fix thread::available_parallelism on WASI targets with threads#153604
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
rust-wasi-web:wasi-available-parallelism-fix

Conversation

@surban
Copy link
Copy Markdown
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 WASI to the standard UNIX cfg_select branch.

Depends on #155057 (Update libc to v0.2.184).

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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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.

@surban surban force-pushed the wasi-available-parallelism-fix branch 2 times, most recently from a41f6b5 to 00f08b0 Compare March 18, 2026 12:19
@surban
Copy link
Copy Markdown
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
Copy Markdown

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
)]
@alexcrichton
Copy link
Copy Markdown
Member

Should I keep this or always (single-threaded and multi-threaded wasm) delegate to wasi-libc?

I'd recommend always delegating to wasi-libc, and to avoid blocking on a libc release I think it'd be reasonable to inline the definition of the _SC_* symbol here with a wasi-specific cfg and a comment saying it can be removed when libc is updated.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 22, 2026
@surban surban mentioned this pull request Apr 9, 2026
@surban surban force-pushed the wasi-available-parallelism-fix branch from 00f08b0 to bc12037 Compare April 9, 2026 17:15
@rustbot

This comment has been minimized.

@surban
Copy link
Copy Markdown
Contributor Author

surban commented Apr 9, 2026

@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 9, 2026
@rust-log-analyzer

This comment has been minimized.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 11, 2026
… r=Mark-Simulacrum

Update libc to v0.2.184

This includes the WASI _SC_* sysconf constants needed for `thread::available_parallelism` on WASI targets (rust-lang#153604).
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 11, 2026
… r=Mark-Simulacrum

Update libc to v0.2.184

This includes the WASI _SC_* sysconf constants needed for `thread::available_parallelism` on WASI targets (rust-lang#153604).
rust-timer added a commit that referenced this pull request Apr 12, 2026
Rollup merge of #155057 - rust-wasi-web:update-libc-0.2.184, r=Mark-Simulacrum

Update libc to v0.2.184

This includes the WASI _SC_* sysconf constants needed for `thread::available_parallelism` on WASI targets (#153604).
@surban
Copy link
Copy Markdown
Contributor Author

surban commented Apr 12, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Apr 12, 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, causing it to fall through to the unsupported
catch-all. This silently regressed the support originally added in
f0b7008.

Fix this by adding WASI to the sysconf-based cfg_select arm alongside
other platforms that use libc::sysconf(_SC_NPROCESSORS_ONLN). This
delegates to wasi-libc, which currently always returns 1 but opens up
the possibility for wasi-libc to report actual processor counts in the
future.

This requires libc to export _SC_NPROCESSORS_ONLN for WASI targets,
which has been added in libc 0.2.184.
@surban surban force-pushed the wasi-available-parallelism-fix branch from bc12037 to b5ddfd5 Compare April 12, 2026 07:53
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 12, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@surban
Copy link
Copy Markdown
Contributor Author

surban commented Apr 12, 2026

I have included the necessary constants in the updated libc. Hence this now follows the standard UNIX path.

@alexcrichton
Copy link
Copy Markdown
Member

@bors: r+

Thanks!

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 13, 2026

Unknown command ":". Run @bors help to see available commands.

@alexcrichton
Copy link
Copy Markdown
Member

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 13, 2026

📌 Commit b5ddfd5 has been approved by alexcrichton

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 13, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 13, 2026
…lelism-fix, r=alexcrichton

Fix thread::available_parallelism on WASI targets with threads

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 to the standard UNIX cfg_select branch.

Depends on rust-lang#155057 (Update libc to v0.2.184).
rust-bors bot pushed a commit that referenced this pull request Apr 13, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #155162 (relnotes for 1.95)
 - #140763 (Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for `bf16(xN)` and `i1xN`)
 - #153604 (Fix thread::available_parallelism on WASI targets with threads)
 - #154193 (Implement EII for statics)
 - #154389 (Add more robust handling of nested query cycles)
 - #154435 (resolve: Some import resolution cleanups)
 - #155236 (Normalize individual predicate of `InstantiatedPredicates` inside `predicates_for_generics`)
 - #155243 (cg_ssa: transmute between scalable vectors)
 - #153941 (tests/debuginfo/basic-stepping.rs: Explain why all lines are not steppable)
 - #154587 (Add --verbose-run-make-subprocess-output flag to suppress verbose run-make output for passing tests)
 - #154624 (Make `DerefPure` dyn-incompatible)
 - #154929 (Add `const Default` impls for `LazyCell` and `LazyLock`)
 - #154944 (Small refactor of `arena_cache` query values)
 - #155055 (UI automation)
 - #155062 (Move tests from `tests/ui/issues/` to appropriate directories)
 - #155131 (Stabilize feature `uint_bit_width`)
 - #155147 (Stabilize feature `int_lowest_highest_one`)
 - #155174 (Improve emission of `UnknownDiagnosticAttribute` lint)
 - #155194 (Fix manpage version replacement and use verbose version)
@rust-bors rust-bors bot merged commit 148ef32 into rust-lang:main Apr 13, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 13, 2026
rust-timer added a commit that referenced this pull request Apr 13, 2026
Rollup merge of #153604 - rust-wasi-web:wasi-available-parallelism-fix, r=alexcrichton

Fix thread::available_parallelism on WASI targets with threads

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 to the standard UNIX cfg_select branch.

Depends on #155057 (Update libc to v0.2.184).
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 13, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - rust-lang/rust#155162 (relnotes for 1.95)
 - rust-lang/rust#140763 (Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for `bf16(xN)` and `i1xN`)
 - rust-lang/rust#153604 (Fix thread::available_parallelism on WASI targets with threads)
 - rust-lang/rust#154193 (Implement EII for statics)
 - rust-lang/rust#154389 (Add more robust handling of nested query cycles)
 - rust-lang/rust#154435 (resolve: Some import resolution cleanups)
 - rust-lang/rust#155236 (Normalize individual predicate of `InstantiatedPredicates` inside `predicates_for_generics`)
 - rust-lang/rust#155243 (cg_ssa: transmute between scalable vectors)
 - rust-lang/rust#153941 (tests/debuginfo/basic-stepping.rs: Explain why all lines are not steppable)
 - rust-lang/rust#154587 (Add --verbose-run-make-subprocess-output flag to suppress verbose run-make output for passing tests)
 - rust-lang/rust#154624 (Make `DerefPure` dyn-incompatible)
 - rust-lang/rust#154929 (Add `const Default` impls for `LazyCell` and `LazyLock`)
 - rust-lang/rust#154944 (Small refactor of `arena_cache` query values)
 - rust-lang/rust#155055 (UI automation)
 - rust-lang/rust#155062 (Move tests from `tests/ui/issues/` to appropriate directories)
 - rust-lang/rust#155131 (Stabilize feature `uint_bit_width`)
 - rust-lang/rust#155147 (Stabilize feature `int_lowest_highest_one`)
 - rust-lang/rust#155174 (Improve emission of `UnknownDiagnosticAttribute` lint)
 - rust-lang/rust#155194 (Fix manpage version replacement and use verbose version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants