Skip to content

CABI: remove the may_enter flag/trap - #705

Open
lukewagner wants to merge 3 commits into
mainfrom
simplify
Open

CABI: remove the may_enter flag/trap#705
lukewagner wants to merge 3 commits into
mainfrom
simplify

Conversation

@lukewagner

Copy link
Copy Markdown
Member

This PR removes the may_enter flag+trap that shows up on the canon lift, resource.drop (destructor) and subtask.cancel (eager-cancellation) paths as well as from some of the cooperative thread scheduling conditions. It also adds a bunch of WAST tests for reentrance cases that used to trap but are now valid. This PR also includes updated versions of the new tests sitting in #696, updating them accordingly. (Both #696 and #697 are obviated by this PR.)

As background: already #650 removed the previous attempt to define+check "asynchronous recursion" (by necessity: it's not well-defined when the host is involved, unfortunately), leaving only a weaker check that prevents "synchronous recursion". This trap has been around since before 0.2 was released and in theory was supposed to simplify the lives of guest toolchains and bindings generators, but now that we've built a bunch, it doesn't actually appear to be load-bearing (once you already have to support all the other kinds of reentrance). In the meantime, there are valid use cases for reentrance (even before callbacks) coming soon, so it seems best to just remove the trap if it's costing (both perf and expressivity) without buying us anything concrete.

What still remains is the "run to completion" semantics (as it's now called in Explainer.md#component-invariants in this PR) of core wasm running as part of a 0.3.0 async function which serializes individual turns of the callback event loop using automatic backpressure; this is load-bearing in our current async bindgen.

dicej added a commit to dicej/wasmtime that referenced this pull request Aug 26, 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.

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

1 participant