Skip to content

Symmetric ABI implementation (includes design documents) - #1799

Draft
cpetig wants to merge 1177 commits into
bytecodealliance:mainfrom
cpetig:symmetric
Draft

cpetig wants to merge 1177 commits into
bytecodealliance:mainfrom
cpetig:symmetric

Conversation

@cpetig

@cpetig cpetig commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

AI helped me to separate the symmetric ABI support from the rest of my long standing fork. While the commits list is extremely long due to long term maintenance between branches, the functionality inside should be reasonably focussed (I will try to sharpen it in the coming weeks).

This implements WebAssembly/component-model#386 and replaces the much to broad #1098 .

cpetig and others added 29 commits November 28, 2026 01:20
…tream fixes

import_future/export_future bridge async functions to the executor with
threads, background threads are joined when the library unloads. Futures
without payload work, set_reader_until can stop reading (closing the stream)
and data_available reads until the merged data and end events are consumed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Async imports return std::future (results lifted after the event), async
exports call the user's std::future function and write the results once it
is ready. The mode is an error without --symmetric and uses wit::span and
wit::bit_cast so the output compiles as C++17.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Files can be restricted to --symmetric (symmetric-only) and compiled with
another C++ standard ([lang] std). Runners with a main instead of run are
started through main. Expected failures of the C++17 combinations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Either side can arrive first and offer a buffer, the side arriving second
copies min(capacity, size) elements and completes both operations. Adds
offer-write/write-result/write-detached, cancel-read/cancel-write, an element
size in buffers, and a mutex protected state machine (readers and writers can
be on different threads). The previous reader-lends protocol
(start-reading/start-writing/finish-writing) keeps working.

The Rust, C and C++ clients are regenerated/adapted in the following commits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sure

* futures and streams use start-writing when a reader is waiting and offer
  their (lowered) values otherwise; unconsumed elements are lifted back
* FutureWrite::cancel, FutureRead::cancel, StreamWrite::cancel,
  StreamRead::cancel, StreamWriter::cancel replace the todo!s; dropping an
  operation takes its offer back before the memory is freed
* a FutureWriter dropped without writing resolves the future with the
  default value (write-detached)
* backpressure_inc/backpressure_dec delay the start of exported async tasks
* sync lowered async exports return a null event (the test runner reads it)
* cancel-import compiles now, it still fails at run time (no task
  cancellation), so it moves to the run-time expected failures

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Minimal changes: buffers get the element size (new constructor argument),
the version in the linked symbol names changes. The protocol they use
(reader offers, writer takes the offer) is unchanged. Writers of other
languages may now offer data first, which the C and C++ readers pick up
only when they look at the stream again (follow-up).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both orders of arrival, partial consumption, cancellation, EOF, detached
writes, and a two thread stress test through the raw stream interface.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…me 0.4.0)

Reads offer the caller's buffer and check read-result at once, writes use
offer-write (strings are duplicated for the reader, the copies of
untransferred elements are released). Partial transfers keep a pending write
pending, cancel reports the transferred count. Removes the pair table,
remainders and the executor callbacks of pending writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…detached

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A read completes with the first elements it gets. A write completes only
when all its elements were consumed: the runtime now keeps the offset of
a partly consumed write offer, the remainder stays on offer for later
reads (which complete synchronously) and the write stays pending, with
accumulated progress. When the reader goes away (or the write is
cancelled) the write result reports the elements consumed so far; the
writer decides between COMPLETED and DROPPED when it observes the result
(is-read-closed). Specification, generated bindings and protocol tests
updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The runtime keeps a partly consumed write pending, so the writer only
has to wait for the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The runtime remembers the offset of a partly consumed write and keeps the
write pending; the status is evaluated when the write is observed
(DROPPED with the total count if the reader is gone by then).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Remove the entries resolved by the runtime change (simple-stream-payload
runner.c with test.c/test.rs/test.cpp, runner.cpp with test.c) and the
blanket expected failure of the Rust runner of simple-stream-payload; the
Rust writer with the single element buffer of the C++17 reader remains
an expected failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The import of the C++17 client is completed by a helper thread which
runs the executor, so whether the std::future is ready right after the
call is a race.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The caller of an async function can now cancel the task behind the
subscription it got (canonical `subtask.cancel`):

- `event-generator.link-cancel(cancel)`: the callee links a generator to
  the completion event of its task, it is activated on cancellation.
- `event-generator.activate-cancelled()`: the task ended because of the
  cancellation, its results are not written.
- `cancel-task(task) -> bool`: returns false right away if the task has
  completed, otherwise activates the linked cancel generator (if any) and
  runs the executor until the callee completed. True if it ended with
  activate-cancelled. Afterwards the callee no longer accesses arguments or
  results; a callee without link-cancel simply runs to completion.

As cancel-task (like block-on) is called from executor callbacks, the
nested executor loop now processes all registered callbacks, not just
the ones registered meanwhile: the callbacks live in a list which is not
locked while a callback runs (the ones registered during a pass are
still added by the next iteration of the loop, as before).

All bindings depending on the version are regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Caller: the future of an async import (`await_result`) calls
`cancel-task` when it is dropped while the call is in progress, so the
return area and the arguments stay valid until the callee is done with
them (it was a use after free before).

Callee: a task of an async export links a cancel generator to its
completion event when it goes pending for the first time and is
registered on it. On cancellation the future is dropped (and with it the
arguments) and the completion is activated with `activate-cancelled`.
When the task ends normally its cancel generator is activated as well,
so that the registration goes away and the state is freed. Callbacks of
a task which run while it is being polled or dropped (by the nested
executor loop of `cancel-task` or `block_on`) are deferred to the one
holding the lock instead of dead locking.

Tasks queued by backpressure can be cancelled as well: the future is
dropped without being polled. For that, the generated code of symmetric
async exports lifts the arguments before the task is created, so the
future owns them from the start (except for borrows of imported
resources, which stay inside of the task).

`TaskCancelOnDrop` stays a no-op, documented why.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
C: `subtask_cancel` of a running call maps to `cancel-task`
(RETURNED_CANCELLED or RETURNED), tasks of async exports link a cancel
generator, deliver EVENT_CANCEL to the callback and `task_cancel` ends
them with `activate-cancelled`.

C++20: destroying the `wit::Task` of an async import while the call is
in progress cancels it (`ImportAwaiter`). The coroutine of an async
export is destroyed when its caller cancels it.

The C++17 API (`--async-api future`) has no cancellation, as documented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`cancel-import`, `rust-cancel-wake`, `nested-future-stream` and
`stream-write-cancel` pass with the symmetric ABI now (Rust and C++
components); `cancel-import` still fails with C components (no
backpressure in C, no "starting" state for the C runner), documented
at the expected failure.

Protocol tests of cancel-task in leak-test: with and without
link-cancel, after completion, of async export tasks, of tasks queued by
backpressure (before and after they started) and from within a task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`callback-registration.cancel` is implemented (it was a todo!()) and now
returns `option<callback-data>`, `is-pending` is new:

- some(data): the callback was not running, the registration is removed
  from wherever it waits (the task list, the list of new tasks, or the
  hands of a tick, which then drops it) together with its eventfd and
  its entry for register-unique; the caller owns the data again. If the
  callback runs on another thread, cancel waits for it to return and
  gives back the data if it returned pending.
- none: the callback returned ready (also when waited for on another
  thread), completed inside of register, or runs on the calling thread
  (cancel from inside the callback or a nested loop it runs): it is not
  called again after it returned, whatever it returns.

Each registration has a shared state (queued, running on thread X,
running+cancelled, done) which the tick and cancel use; the TASKS lock is
still never held while a callback runs. Cancelling wakes a thread blocked
in the executor, so run() can return when nothing is left. Also don't put
the invalid fd of a timeout into the select set when block_on waits for a
timeout.

All bindings depending on the version are regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A task keeps the handles of its registrations (pruned with is-pending at
every poll) and cancels them when it ends, normally or by cancellation,
instead of letting them wait for their events (before, a finished task
activated its wake and cancel generators so that the callbacks freed the
state, registrations on other events kept the state until they fired and
made run() wait for them). The state is freed by `settle` once no
registration refers to it: a cancelled registration releases its
instance there, one which ran accounted for itself on entry (or was
deferred). The registrations are cancelled with the state unlocked, the
callback registering the events of a poll keeps an instance meanwhile.

A task waiting for backpressure unregisters its cancel_queued_task
callback when it starts, instead of having the task activate the cancel
generator at its end.

New `unregister` helper, `CallbackRegistration` is exported. The leak
tests cover unregistering before and after the event fired, from inside
of the callback, from another thread while it runs and while another
thread waits, the interaction with register-unique, eventfds, and tasks
which end or are cancelled while waiting for an event that never fires
(fd_leak no longer needs to activate its shutdown event to end run()).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
C: a task keeps its wake up registrations (replaced at every wait instead
of piling up) and the one for its cancellation, task_free cancels them,
so the `task_live` lookup (and the task list behind it) and the leaked
trigger generator are gone. waitable-set.wait drops its wake up
registrations after the wait.

C++ (wit.h, WIT_SYMMETRIC): an EventAwaiter destroyed while waiting
unregisters its callback and frees the state (no `dead` flag), an
ImportAwaiter does so before cancelling the call. ExportTask unregisters
its cancel callback in cleanup instead of activating the cancel event for
a separately allocated link object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- cpp store(): convert the value explicitly again (-Wconstant-conversion
  for variant discriminants > 127), pointers are converted implicitly
- rust HandleLift of a borrowed exported resource: use the full type path
  and zero extend the i32 handle in canonical mode
- cpp: clang_format/first()/AsyncMethod(id) as upstream, clippy fixes
- remove the leftover c.rs.orig/c.rs.rej

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

2 participants