Skip to content

Add C++20 coroutine (co20) backend with Linux verification - #166

Open
dallison wants to merge 9 commits into
mainfrom
co20
Open

dallison wants to merge 9 commits into
mainfrom
co20

Conversation

@dallison

@dallison dallison commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Adds the C++20 stackless coroutine (co20) backend (--config=cpp20 / -DSUBSPACE_CORO_BACKEND=co20) to the async layer, client, server, tunnel, FFI and plugins. See docs/cpp20-coroutines.md.
  • Adds a Linux verification/debug handoff guide (docs/co20-linux-debug-handoff.md) with the results of the Linux run.
  • Coroutine frames come from a per-thread recycling pool (common/async/frame_pool.h, installed for every co20::ValueTask by task.h), so the publish and read paths allocate nothing once warm. Previously they cost about 7 and 5 heap allocations per message. //client:alloc_test now runs under co20. The pool is off under ASan/TSan; -DSUBSPACE_ASYNC_FRAME_POOL=0 disables it.
  • Follow-up commits fix three problems found while verifying on Linux and in CI:
    • --config=cpp20 --config=linux_memfd did not compile: ClientChannel::OpenBuffer called the async GetRegisteredClientBuffer without awaiting. OpenBuffer/AttachShmBuffers are now async::Task, and the memfd-only AndroidBufferRegistrationTest is updated.
    • The CMake co20 build failed: c_client, shadow and rpc tests (which construct Server with co::CoroutineScheduler) are now gated off under co20 as in Bazel, and subspace_common links subspace_async (for every backend, since context.h references its link-check symbol) so common_test/split_buffer_test link.

Test plan

Linux, Ubuntu 22.04, GCC 11:

  • bazelisk test --config=cpp20 //...
  • bazelisk test //...
  • --config=cpp20 --config=asan on common/client/server/rpc targets, plus 5x stress of bridge/tunnel tests
  • --config=cpp20 --config=tsan (with setarch -R)
  • --config=cpp20 --config=linux_memfd (full suite) and default backend with linux_memfd
  • --config=cpp20 --config=mtls, --config=cpp20 --config=slot_size_64
  • CMake co20 build + ctest (21/21)
  • Clang (not installed on the Linux host)

Known failures that also occur on the default backend (not co20 regressions):

  • ASan: //client/python:client_test (runtime not first in library list), //coro_rpc/{client,test} (leaks in old coro_rpc)
  • TSan: PluginTest in //client:client_test (nop_plugin logging race through shared toolbelt::Logger buffer in Server::Stop)
  • memfd: //rust_client:client_test cross-language tests (Rust client has no Linux memfd support)

Adds SUBSPACE_CORO_BACKEND_CO20, selected with --config=cpp20 in Bazel
and automatically by CMake when CMAKE_CXX_STANDARD >= 20.  The build
always defines the backend macro, and a link-time symbol check rejects
objects built for different backends.

common/async gains task.h: async::Task<T>, ASYNC_AWAIT and ASYNC_RETURN
let a function that waits be written once for every backend, and
SyncWait runs a task from non-coroutine code.  Waits, sockets,
ByteStream and AsyncRuntime are implemented on co20::Scheduler.  A
co20 Context holds a co20 coroutine (which suspends) or an AsyncWaiter
(which blocks the thread, yielding a stackful co::Coroutine), and falls
back to poll().

The client's blocking API now takes const toolbelt::AsyncWaiter*
instead of const co::Coroutine*.

The co library is patched (patches/co_cpp20_wait_any_and_reap.patch)
with a multi-fd Wait, reaping of finished coroutines, a fix for the
poll fallback resuming a coroutine twice, non-aggregate ValueTask
promises for GCC, destruction of all nested frames when a suspended
coroutine is destroyed, and Run() honouring Stop() from a coroutine in
epoll mode.

The client and server are not yet converted to co20.
co 3.5.2 stores a ValueTask result in a std::optional and keeps an
exception in exception_ptr, rather than storing a bare value and
swallowing exceptions.

SyncWait now rethrows a stored exception, as the other backends do,
and unwraps the optional result.

Under co20, Wait returns -1 on timeout as well as for a closed fd.  Only
a closed fd is an error, so a timed wait that expires is reported as
DeadlineExceeded again.
The co20 fixes that were in patches/co_cpp20_wait_any_and_reap.patch
are now a branch in ~/co (co 3.5.2, co20-subspace-support, commit
fe00d44).  Pin coroutines to that commit through a file:// remote in
MODULE.bazel and in the CMake FetchContent, and delete the patch and its
PATCH_COMMAND.

The file:// pin only resolves on this machine until the branch is
pushed to dallison/co.
The co20 changes were merged upstream as dallison/co#82 (db9ecf4), so
the pin moves from the local ~/co branch to the GitHub remote.
- Convert ClientImpl server round-trips, buffer attachment/unmapping,
  split-buffer registration/lookup, channel reload callbacks, and
  publisher/subscriber slot search paths to return async::Task<T> using
  ASYNC_AWAIT / ASYNC_RETURN, keeping the synchronous public Client,
  Publisher, and Subscriber APIs unchanged via ASYNC_BLOCK (SyncWait).
- Add <SyncName>Async variants (including CloseAsync) on Client,
  Publisher, and Subscriber for coroutines running on a co20::Scheduler,
  rejecting thread-safe clients with FailedPrecondition.
- Convert Server, ClientHandler, TunnelManager, FFI, Python server
  wrapper, and plugin entry points to async::Task and RuntimeEngine,
  using SERVER_CLIENT_CALL / SERVER_CLIENT_CLOSE for in-process clients
  running on the server scheduler thread.
- Route ASYNC_AWAIT through FastAwait so ValueTasks that complete
  without parking on the scheduler unwind in place instead of relying on
  compiler symmetric-transfer TCO (avoiding C stack overflow under GCC
  -O0 and AddressSanitizer), and guard hot-path reload/update checks.
- Prevent ShouldReconnect from blocking in connect() when a co20
  coroutine's interrupt fd is already signaled during server shutdown,
  and yield instead of sleeping in ListenerCoroutine's shutdown drain.
- Gate co-only and zero-allocation tests under //:coro_backend_co20, add
  the cpp20 CI workflow job, and document the co20 backend in
  docs/cpp20-coroutines.md and CHANGELOG.md.
- Make ClientChannel::OpenBuffer and AttachShmBuffers async::Task so the
  memfd path can await GetRegisteredClientBuffer under co20.
- Update the memfd-only AndroidBufferRegistrationTest for Task callbacks.
- Gate c_client/shadow/rpc test targets off under the co20 CMake backend
  and link subspace_async into subspace_common so common tests link.
- Record Linux verification results in the co20 handoff doc.
common_test and split_buffer_test failed to link on every backend because
context.h references a backend link-check symbol defined in subspace_async.
Every call to an async::Task function allocated a coroutine frame from the
global heap: about 7 allocations per publish and 5 per read. Give every
co20::ValueTask a promise type (via a std::coroutine_traits specialisation)
whose operator new/delete use a per-thread, size-class frame pool, so a path
that has run once on a thread allocates nothing.

- Add common/async/frame_pool.{h,cc}. The pool is off under ASan/TSan so
  they still see frame lifetimes; SUBSPACE_ASYNC_FRAME_POOL=0/1 overrides.
- Include task.h from co20_rpc's shared_ptr_pipe.h so every definition of its
  ValueTask coroutine uses the same promise type.
- Run //client:alloc_test under co20 now that it holds, and add frame pool
  tests to async_test.
- Update the co20 docs.
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