Skip to content

Conversation

@dicej
Copy link
Contributor

@dicej dicej commented Nov 18, 2025

This implements a spec change (PR pending) such that tasks created for calls to synchronous exports may not call potentially-blocking imports or return wait or poll callback codes prior to returning a value. Specifically, the following are prohibited in that scenario:

  • returning callback-code.{wait,poll}
  • sync calling an async import
  • sync calling subtask.cancel
  • sync calling {stream,future}.{read,write}
  • sync calling {stream,future}.cancel-{read,write}
  • calling waitable-set.{wait,poll}
  • calling thread.suspend

This breaks a number of tests, which will be addressed in follow-up commits:

  • The {tcp,udp}-socket.bind implementation in wasmtime-wasi is implemented using Linker::func_wrap_concurrent and thus assumed to be async, whereas the WIT interface says they're sync, leading to a type mismatch error at runtime. Alex and I have discussed this and have a general plan to address it. EDIT: addressed by Relax required host capabilities in wasip3 tcp/udp bindings #12085

  • A number of tests in the tests/component-model submodule that points to the spec repo are failing. Those will presumably be fixed as part of the upcoming spec PR (although some could be due to bugs in this implementation, in which case I'll fix them).

  • A number of tests in tests/misc_testsuite are failing. I'll address those in a follow-up commit.

@dicej dicej requested a review from alexcrichton November 18, 2025 18:30
@dicej dicej force-pushed the trap-blocking-in-sync-tasks branch from c6c26c4 to 3547452 Compare November 18, 2025 18:33
@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Nov 18, 2025
@alexcrichton
Copy link
Member

Talked about this with @dicej today, current blockers are:

  • Waiting on upstream PR to the spec to have an official point-of-reference
  • Current upstream tests are failing, and upstream spec PR likely to update.
  • Tests for "full matrix of trapping behavior" still needed

@dicej
Copy link
Contributor Author

dicej commented Nov 24, 2025

Regarding the {tcp,udp}-socket.bind test failures, I've opened WebAssembly/wasi-sockets#144.

Eventually, we'll want a proper way for host functions to "cheat" and block the whole guest without monopolizing the Store, as well, but no need to block this PR on that.

@alexcrichton
Copy link
Member

New thought: I think we're going to be required to implement "component-model-sync function can do async things on the host" in Wasmtime. Ignoring p3 for a moment, that's exactly how all async works in WASIp2. All WASIp2 functions are "sync" functions which leverage host superpowers to suspend the guest, and we can't break WASIp2, so I don't think that "just solve tcp/udp for wasip3" is a viable solution after all.

@dicej
Copy link
Contributor Author

dicej commented Nov 24, 2025

All WASIp2 functions are "sync" functions which leverage host superpowers to suspend the guest, and we can't break WASIp2

Agreed, but I don't see how that's relevant to this PR. Sync-typed exports are still permitted to call sync-typed imports, and wasmtime-wasi's p2 implementation uses Linker::func_wrap_async, which defines a sync-typed host function, so nothing in this PR or the proposed spec changes will break wasmtime-wasi's p2 implementation.

Certainly we'll need to confront p2 head-on once we remove or replace Linker::func_wrap_async, but we're not doing that here.

@alexcrichton
Copy link
Member

Er right, yes, wires crossed again. That means though that #12085 is possible which "fixes" tcp/udp.

This implements a spec change (PR pending) such that tasks created for calls to
synchronous exports may not call potentially-blocking imports or return `wait`
or `poll` callback codes prior to returning a value.  Specifically, the
following are prohibited in that scenario:

- returning callback-code.{wait,poll}
- sync calling an async import
- sync calling subtask.cancel
- sync calling {stream,future}.{read,write}
- sync calling {stream,future}.cancel-{read,write}
- calling waitable-set.{wait,poll}
- calling thread.suspend

This breaks a number of tests, which will be addressed in follow-up commits:

- The `{tcp,udp}-socket.bind` implementation in `wasmtime-wasi` is implemented
  using `Linker::func_wrap_concurrent` and thus assumed to be async, whereas the
  WIT interface says they're sync, leading to a type mismatch error at runtime.
  Alex and I have discussed this and have a general plan to address it.

- A number of tests in the tests/component-model submodule that points to the
  spec repo are failing.  Those will presumably be fixed as part of the upcoming
  spec PR (although some could be due to bugs in this implementation, in which
  case I'll fix them).

- A number of tests in tests/misc_testsuite are failing.  I'll address those in
  a follow-up commit.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
`check_blocking` needs access to the current task, but that's not set for
post-return functions since those should not be calling _any_ imports at all, so
first check for that.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
This amounts to adding `async` to any exported component functions that might
need to block.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Per the proposed spec changes, `thread.yield` should return control to the guest
immediately without allowing any other thread to run.  Similarly, when an
async-lifted export or callback returns `CALLBACK_CODE_YIELD`, we should call
the callback again immediately without allowing another thread to run.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
@dicej dicej force-pushed the trap-blocking-in-sync-tasks branch from 3d14cbf to c3fb452 Compare November 24, 2025 23:04
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
@dicej
Copy link
Contributor Author

dicej commented Nov 25, 2025

Some of the WAST tests are hanging and/or busy looping. I'll investigate this morning.

dicej added a commit to WebAssembly/component-model that referenced this pull request Nov 25, 2025
This anticipates the forthcoming changes to WIT/CM async and helps us get CI
green for bytecodealliance/wasmtime#12043

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
Note that this temporarily updates the `tests/component-model` submodule to the
branch for WebAssembly/component-model#577 until that PR
is merged.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
@dicej dicej marked this pull request as ready for review November 25, 2025 17:06
@dicej dicej requested a review from a team as a code owner November 25, 2025 17:06
@dicej
Copy link
Contributor Author

dicej commented Nov 25, 2025

CI is green now; I'm temporarily pointing the tests/component-model submodule at WebAssembly/component-model#577 until that PR is merged.

@dicej
Copy link
Contributor Author

dicej commented Nov 25, 2025

I believe we're just waiting on a spec PR (hopefully accompanied by WAST tests covering the changes) at this point.

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.

2 participants