Skip to content

Conversation

@alexcrichton
Copy link
Member

This commit fixes a preexisting issue in the async support in the rust
bindings where inter-component-model-task wakeups/communication did not
work. Previously wit-bindgen would end up either panicking or wedging
itself if this situation arose, effectively not handling it at all.

Inter-task wakeups are supported in the component model with unit
streams and futures (e.g. future and stream with no type parameter).
For these types the component model allows the same instance to both
read/write from the stream or future. The support here is implemented
with stream as a task might receive many wake up notifications over
its lifetime.

Implementing this is a bit tricky, so the highlights of the
implementation are:

  • This relies on new "magic function imports" recognized by
    wit-component specifically related to unit-streams and unit-futures.
    This is a very new feature of wit-component and thus hasn't had time
    to propagate throughout the ecosystem yet. Due to the niche nature of
    needing cross-task wakeups this commit disables cross-task wakeups by
    default and adds support for it behind a Cargo feature. The thinking
    is that if anyone runs into this there's now a clear panic/error
    message indicating that the feature can be enabled. Otherwise the hope
    is that no one runs into this (as it's relatively niche) and when the
    time is right we can enable this by default.

  • All tasks started by wit-bindgen will need this new inter-task-wakeup
    stream for the reason that we don't actually know what will wake a
    task up when it blocks. To avoid adding a stream allocation on the
    fast path, the allocation of the inter-task stream is deferred until
    a task blocks for the first time.

  • Right now a read of the inter-task stream starts when a task blocks.
    The read is cancelled when the task wakes up. A future optimization
    would be to keep the read-in-progress and only restart it if
    necessary. For now that's deferred, however.

Closes #1431

@alexcrichton
Copy link
Member Author

alexcrichton commented Nov 26, 2025

This is temporarily built on #1439 and relies on unpublished versions of wasm-tools so CI will be broken.

This commit fixes a preexisting issue in the async support in the rust
bindings where inter-component-model-task wakeups/communication did not
work. Previously `wit-bindgen` would end up either panicking or wedging
itself if this situation arose, effectively not handling it at all.

Inter-task wakeups are supported in the component model with unit
streams and futures (e.g. `future` and `stream` with no type parameter).
For these types the component model allows the same instance to both
read/write from the stream or future. The support here is implemented
with `stream` as a task might receive many wake up notifications over
its lifetime.

Implementing this is a bit tricky, so the highlights of the
implementation are:

* This relies on new "magic function imports" recognized by
  `wit-component` specifically related to unit-streams and unit-futures.
  This is a very new feature of `wit-component` and thus hasn't had time
  to propagate throughout the ecosystem yet. Due to the niche nature of
  needing cross-task wakeups this commit disables cross-task wakeups by
  default and adds support for it behind a Cargo feature. The thinking
  is that if anyone runs into this there's now a clear panic/error
  message indicating that the feature can be enabled. Otherwise the hope
  is that no one runs into this (as it's relatively niche) and when the
  time is right we can enable this by default.

* All tasks started by wit-bindgen will need this new inter-task-wakeup
  stream for the reason that we don't actually know what will wake a
  task up when it blocks. To avoid adding a stream allocation on the
  fast path, the allocation of the inter-task stream is deferred until
  a task blocks for the first time.

* Right now a read of the inter-task stream starts when a task blocks.
  The read is cancelled when the task wakes up. A future optimization
  would be to keep the read-in-progress and only restart it if
  necessary. For now that's deferred, however.

Closes bytecodealliance#1431
@alexcrichton alexcrichton marked this pull request as ready for review December 1, 2025 19:24
@alexcrichton alexcrichton requested a review from dicej December 1, 2025 19:24
@alexcrichton alexcrichton added this pull request to the merge queue Dec 1, 2025
Merged via the queue into bytecodealliance:main with commit 8dd4b89 Dec 1, 2025
22 checks passed
@alexcrichton alexcrichton deleted the cross-task-wakeup branch December 1, 2025 21:53
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.

rust: make FutureWaker component-model-task-aware

2 participants