Skip to content

align multithreading and trap behavior with CM spec - #14146

Merged
dicej merged 10 commits into
bytecodealliance:mainfrom
dicej:spec-threading-and-sync-blocking-updates
Aug 31, 2026
Merged

align multithreading and trap behavior with CM spec#14146
dicej merged 10 commits into
bytecodealliance:mainfrom
dicej:spec-threading-and-sync-blocking-updates

Conversation

@dicej

@dicej dicej commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

This updates Wasmtime's Component Model async and cooperative multithreading support to match the current specification, including:

  • Refined rules for trapping when a sync-typed function blocks. We now enforce this "lazily" rather than "eagerly", mwaning a sync-typed function is allowed to call an async-typed function or blocking intrinsic, and if it doesn't actually block, we won't trap. And if the call does block, we will look for any eligible threads to run and run them until no such threads remain, only trapping if and when we still need to block and have no more threads to run.

  • Ensure that the predicate for determining which threads can be run when a sync-typed function is executing in an instance matches the spec.

  • Remove the previous "may block" bookkeeping at the task and root instance level, replacing it with (sub-)instance level tracking of whether any sync-typed function is running in that instance.

  • Run the event loop during start function calls since they are now allowed to call async-typed functions, create and resume threads, etc.

Note that this includes test/component-model submodule updates which haven't yet been merged to the main branch of the upstream repo, but should be merged soon. See WebAssembly/component-model#696

Fixes #14117

@dicej
dicej requested a review from alexcrichton August 17, 2026 15:48
@dicej
dicej force-pushed the spec-threading-and-sync-blocking-updates branch 3 times, most recently from 8506000 to 5d88b4c Compare August 17, 2026 17:22

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd like to review more of concurrent.rs but here's some initial thoughts. It's at the point where whenever I expand context on github it just sends me randomly elsewhere in this diff and I keep losing my spot in the otherwise big diff in concurrent.rs. I'm hoping my changes in dicej#7 which reduce the number of files changed helps with that...

Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/instance.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/instance.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/instance.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/instance.rs
Comment thread crates/wasmtime/src/runtime/instance.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Aug 18, 2026
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
@alexcrichton

Copy link
Copy Markdown
Member

I think this may also still have lingering management of may_block as it looks like there's some here too

@dicej
dicej force-pushed the spec-threading-and-sync-blocking-updates branch 3 times, most recently from 6a0209e to 25af96d Compare August 28, 2026 14:09
@dicej
dicej force-pushed the spec-threading-and-sync-blocking-updates branch 2 times, most recently from 29ac4ee to b045d7e Compare August 28, 2026 19:32
@dicej
dicej requested a review from alexcrichton August 28, 2026 19:32
@dicej

dicej commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

@alexcrichton would you mind taking one more look at this? Aligning it with WebAssembly/component-model#705 required an overhaul and uncovered a few subtle divergences with the spec which I had to address.

dicej and others added 6 commits August 28, 2026 13:37
This updates Wasmtime's Component Model async and cooperative multithreading
support to match the current specification, including:

- Refined rules for trapping when a sync-typed function blocks.  We now enforce
  this "lazily" rather than "eagerly", mwaning a sync-typed function is allowed
  to call an async-typed function or blocking intrinsic, and if it doesn't
  actually block, we won't trap.  And if the call _does_ block, we will look for
  any eligible threads to run and run them until no such threads remain, only
  trapping if and when we still need to block and have no more threads to run.

- Allow reentrance in all cases except when the instance has trapped.

- Remove the previous "may block" bookkeeping at the task and root instance
  level, replacing it with (sub-)instance level tracking of whether any
  sync-typed function is running in that instance.

- Run the event loop during start function calls since they are now allowed to
  call async-typed functions, create and resume threads, etc.  I've also added
  some code to assert that the event loop is running when it is required.

- Add `ConcurrentState::switch_item` for use when we need to run a specific work
  item at the next turn of the event loop, regardless of what's already in the
  `high_priority` queue.  This is necessary because the spec is particular about
  which thread to switch to e.g. when calling a function or promoting a thread,
  and it won't allow us to run any other threads first.

Note that this includes `test/component-model` submodule updates which haven't
yet been merged to the main branch of the upstream repo, but should be merged
soon.  See WebAssembly/component-model#705

Fixes bytecodealliance#14117

Co-authored-by: Alex Crichton <alex@alexcrichton.com>
Accept an `unsafe` block which I believe is correct and still otherwise
safe at the invocation site. The main hidden constraint now is that we
can't transfer fibers to other non-store-bound-locations but that's
effectively already true so shouldn't be too onerous to uphold.
- Ensure that subtask status updates are delivered promptly and
  deterministically according to the spec by using
  `ConcurrentState::switch_item` instead of `ConcurrentState::high_priority`

- Fix reentrance scenarious involving `subtask.cancel` where the subtask tries
  to add itself to a waitable set before or after being canceled

- Refine rules for switching-or-trapping on thread exit when the current
  instance has a sync-typed call in progress

- Misc. bug fixes
Notably, this makes Wasmtime more aggressive about poisoning the store if an
error happens when e.g. lifting a result (e.g. due to a misaligned pointer), and
the tests have been updated accordingly.
@dicej
dicej force-pushed the spec-threading-and-sync-blocking-updates branch from b045d7e to f511826 Compare August 28, 2026 19:42
@dicej
dicej marked this pull request as ready for review August 28, 2026 19:42
@dicej
dicej requested review from a team as code owners August 28, 2026 19:42
Comment thread crates/wasmtime/src/runtime/component/instance.rs
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs
@dicej
dicej added this pull request to the merge queue Aug 31, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 31, 2026
@alexcrichton
alexcrichton added this pull request to the merge queue Aug 31, 2026
@dicej

dicej commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

@alexcrichton I think the miri compiler error that kicked it out of the merge queue is a real thing, not a flake; I've reproduced it locally and am trying to address it.

@alexcrichton
alexcrichton removed this pull request from the merge queue due to a manual request Aug 31, 2026
@alexcrichton

Copy link
Copy Markdown
Member

oh huh, when I looked at the failures everything was actually still running so I assumed it was just some github actions flake, but maybe the logs were just delayed? Anyway I'll leave it in your hands in that case

@dicej

dicej commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Adding #![recursion_limit = "256"] to src/lib.rs addresses the issue. Any concerns with me adding that to this PR?

@alexcrichton

Copy link
Copy Markdown
Member

I'd personally prefer to avoid increasing that if we can as I suspect it's going to be a game of whack-a-mole, would it be possible to put a trait object somewhere in the middle perhaps?

@dicej
dicej enabled auto-merge August 31, 2026 16:42
@dicej
dicej added this pull request to the merge queue Aug 31, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 31, 2026
@dicej
dicej enabled auto-merge August 31, 2026 18:01
@dicej
dicej added this pull request to the merge queue Aug 31, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 31, 2026
@dicej
dicej force-pushed the spec-threading-and-sync-blocking-updates branch from 39b6c8e to 2359e22 Compare August 31, 2026 20:06
@dicej
dicej enabled auto-merge August 31, 2026 20:07
@dicej
dicej added this pull request to the merge queue Aug 31, 2026
Merged via the queue into bytecodealliance:main with commit 92f238b Aug 31, 2026
54 checks passed
@dicej
dicej deleted the spec-threading-and-sync-blocking-updates branch August 31, 2026 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[async] non-async-typed function behavior diverges from spec in two cases

2 participants