Skip to content

Add async support via AsyncExecutor runtime - #7

Closed
alan-george-lk wants to merge 19 commits into
livekit/devfrom
livekit/dev-async-support
Closed

alan-george-lk wants to merge 19 commits into
livekit/devfrom
livekit/dev-async-support

Conversation

@alan-george-lk

@alan-george-lk alan-george-lk commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is a fairly hefty set of changes to enable async support. As C++ has no native async runtime, one had to be created.

AsyncExecutor is a class/singleton environment that is created lazily on the first async call, or whose lifecycle can be explicitly controlled via API. Presently, this is implemented with only a single worker thread, but was sufficient in all testing that I threw at it. A small thread pool (say ~4 threads by default) was considered, and can easily be implemented later, but did not seem required so far even under the heaviest loads.

I vetted this through:

  • New unit/integration tests within the bindgen for async generated code and executor runtime edges (lifecycle, thread safety, etc.)
  • Replacing data tracks within the C++ SDK with this implementation, while changing no unit/integration tests (solid coverage there), and everything passed
  • Lengthy agent sessions where regression testing was constantly evaluated after each major change

Use AsyncExecutor throughout the generated bindings. Keep shutdown idempotent, free rejected Rust handles once, and deliver callback and value-transfer failures through the future lifecycle.

Generate async record and enum methods, cover executor and FFI behavior with focused tests, and replace benchmark output with functional assertions. Document the runtime and its UniFFI contract.
Link the Windows synchronization library for the Rust fixtures and include the string declarations used by async bindings and tests.
Retain the shared executor for process lifetime and keep explicit shutdown available. Verify exit from a continuation and exit with pending Rust futures in separate test processes.
Serialize restart with shutdown and keep operation IDs monotonic. Prevent retired callbacks from starting in a subsequent run, and verify restarted workers and Rust callback round trips.
Keep a copy of the weak cancellation action so another thread can cancel a blocking get or a future transferred into then. Test the handle after completion and destruction of the owning future.
Closing admission can reject a queued C++ callback before shutdown reaches the Rust call that awaits it. Send cancellation before the next poll and preserve completion for futures that already signaled READY.

Exercise the ordering with a queued callback and a paused shutdown cancellation in the executor fixture.
Explain how UniFFI lifts a callback cancellation and how a Rust trait method can convert it into its declared error. Clarify where applications may invoke the completion callbacks.
Hold the worker until admission closes and pause shutdown before per-call cancellation. Verify that the outer call receives AsyncCancelledError.
Document callback rejection after shutdown and link to the complete UniFFI error conversion macro.
Complete ready futures inline and keep pending work on the executor. Share the driver across exports, cover Rust-backed traits and callbacks from Tokio, and reuse the upstream futures fixture.
GCC emits the included binding's vtable initializers. Use the futures binding already linked by this test.
Queue new first polls on the worker while shutdown waits for inline polls. Cover callback startup during the wait and cancellation after the poll returns.
@alan-george-lk alan-george-lk changed the title Add async support via executor runtime Add async support via AsyncExecutor runtime Nov 23, 2026
// block.
class AsyncExecutor {
public:
AsyncExecutor() : worker_([this] { run(); }) {}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer note: only one worked thread was needed in testing (data tracks, integration/stress testing on C++ SDK). Can increase this to a fixed-size pool if we run into issues with this as other features get migrated over

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