Skip to content

Fix CABI definition of coop thread promotability and add tests - #696

Open
lukewagner wants to merge 2 commits into
mainfrom
fix-exclusive-bug
Open

Fix CABI definition of coop thread promotability and add tests#696
lukewagner wants to merge 2 commits into
mainfrom
fix-exclusive-bug

Conversation

@lukewagner

Copy link
Copy Markdown
Member

Currently, there is some divergence between Wasmtime and the spec for which coop threads can be switched-to during a synchronous call (when one thread blocks but others can be run). Digging into it, I realized that the spec is buggy (at ensuring the stated Component Invariant #3), so this PR fixes the spec to match what Wasmtime is already correctly doing. However, testing revealed other cases where I expect it's Wasmtime that we want to change to match the (fixed) spec.

(The WASTs added here assume #687 and its WAST tests pass (regarding thread.*-then-promote) and also probably dovetail with the WASTs merged in #691 discussed in wasmtime/#14117, so cc @alexcrichton and @dicej)

@lukewagner
lukewagner force-pushed the fix-exclusive-bug branch 2 times, most recently from 7559f7f to aa835e1 Compare August 16, 2026 05:12
Now that the spec requires runtimes to enforce trapping lazily for subtask and
intrinsic calls that block sync-typed tasks, several existing tests which
previously relied on eager enforcement need to be updated.
dicej added a commit to dicej/wasmtime that referenced this pull request Aug 17, 2026
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 bytecodealliance#14117

Co-authored-by: Alex Crichton <alex@alexcrichton.com>
Comment on lines +313 to +317
;; TODO: also test sync calls to `{stream,future}.cancel-{read,write}`. As of
;; this writing, such calls can only ever block if the host has the other end,
;; meaning we can't currently test that case in pure WAST; we'd need custom host
;; code. In the future, additional intrinsics (e.g. `stream.read-complete`) may
;; enable us to test those cases in pure WAST.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For this Wasmtime has a psuedo-host-world of sorts which is intended to be simple-to-define here -- https://github.com/bytecodealliance/wasmtime/blob/bc2f967927f9f0e839095a752bf554c4a09fdd13/crates/wast/src/spectest.rs#L91. I think it'd be reasonable to have a world spectest { ... } or similar with some canonical WIT that has comments for how the functions are expected to be implemented, and that could exercise some host-related functionality too without too much of an increase in complexity on the host side.

dicej added a commit to dicej/wasmtime that referenced this pull request Aug 17, 2026
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 bytecodealliance#14117

Co-authored-by: Alex Crichton <alex@alexcrichton.com>
dicej added a commit to dicej/wasmtime that referenced this pull request Aug 17, 2026
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 bytecodealliance#14117

Co-authored-by: Alex Crichton <alex@alexcrichton.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants