From ce020681af9dda88e9fcbf16fbbc8e666bd1511c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:15:40 -0700 Subject: [PATCH 1/6] chore(quest): claim quest/m1/ffi-export/session-tokio.md Quest-Claim: 58e0b7aafe5e26e5 From 0c4dd223fb32a712853fb1e538272dfc978018aa Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:24:00 -0700 Subject: [PATCH 2/6] test(ffi): serve and read a moq-tokio session from C, bound to port 0 Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 11 +- ffi/moq/moq.c | 183 ++++++++++++++++++++++++++- quest/m1/ffi-export/README.md | 1 - quest/m1/ffi-export/session-quic.md | 6 +- quest/m1/ffi-export/session-tokio.md | 35 ----- 5 files changed, 191 insertions(+), 45 deletions(-) delete mode 100644 quest/m1/ffi-export/session-tokio.md diff --git a/ffi/README.md b/ffi/README.md index 95d96f840..24ebde6b3 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -131,7 +131,8 @@ refused, and the census counts why. it finishes or is freed. - **Wakers** move into Rust, which calls `drop` once it lets go of the last clone, so a state machine polled with a waker, such as moq's `Driver`s through kio's `Waiter`, runs - from the caller's event loop. + from the caller's event loop. Rust may call `wake` and `drop` from any thread, as a + runtime's workers do. - **Callbacks** are a struct of a table and its data. Rust calls `free` once it drops the implementation, and may call the others from any thread. A callback takes scalars and buffers lent for the call, and returns a scalar. @@ -292,6 +293,8 @@ moq's crates export through `target/moq-ffi`, an empty library on each that `jus writes in place of moq-ffi's remainder. `moq/moq.c` publishes and subscribes to a moq-net track in one process, polling each read, scopes an origin to moq-pattern's patterns and hands it to a client to publish, polls its driver until the origin's last owner drops, reads an OpusHead into hang's catalog through moq-mux, -publishes a binary track the catalog lists, and reads the catalog lent to C through -`catalog::Producer::mutate`. `moq/moq.cpp` does the same through the C++ header, at -C++17 without exceptions or RTTI, as OBS builds it, and `moq/moq.kt` through the Kotlin. +sets and reads a field of the catalog lent to C through `catalog::Producer::mutate`, and +serves a broadcast from a moq-tokio server bound to port 0 to a client that reads it over +QUIC, both on moq-tokio's fallback runtime, whose threads wake C's. `moq/moq.cpp` does the +same through the C++ header, short of the catalog's field and the session, at C++17 +without exceptions or RTTI, as OBS builds it, and `moq/moq.kt` through the Kotlin. diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 8824e86d5..36d7db088 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -4,11 +4,18 @@ * each names the others' types, the catalog is lent to C for a call, and a session's * config is filled in field by field. * - * It needs no runtime: the waker counts wakes, and each poll that must wait is polled - * again after the write that wakes it. */ + * The local reads need no runtime: the waker counts wakes, and each poll that must wait + * is polled again after the write that wakes it. A session over QUIC runs on moq-tokio's + * fallback runtime, whose threads wake this one. */ +#define _POSIX_C_SOURCE 200809L + +#include +#include #include +#include #include +#include #include "moq.h" @@ -59,6 +66,62 @@ static void wake(void *data) { wakes++; } +/* A session's futures complete on moq-tokio's runtime threads, so its waker signals this + * thread, which waits for a wake after a poll finds a future pending. */ +static pthread_mutex_t woken_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t woken_cond = PTHREAD_COND_INITIALIZER; +static unsigned long woken = 0; + +static void wake_session(void *data) { + (void)data; + pthread_mutex_lock(&woken_lock); + woken++; + pthread_cond_broadcast(&woken_cond); + pthread_mutex_unlock(&woken_lock); +} + +static const moq_waker session_waker = {.wake = wake_session}; + +static unsigned long wakes_so_far(void) { + pthread_mutex_lock(&woken_lock); + unsigned long n = woken; + pthread_mutex_unlock(&woken_lock); + return n; +} + +/* Waits for a wake after `seen`, failing the run rather than hanging when none comes. */ +static void wait_wake(unsigned long seen) { + struct timespec deadline; + clock_gettime(CLOCK_REALTIME, &deadline); + deadline.tv_sec += 30; + pthread_mutex_lock(&woken_lock); + while (woken == seen) { + if (pthread_cond_timedwait(&woken_cond, &woken_lock, &deadline) == ETIMEDOUT) { + fprintf(stderr, "no wake in 30 seconds\n"); + abort(); + } + } + pthread_mutex_unlock(&woken_lock); +} + +/* The server's accept loop, which must keep running while the client waits on it. */ +static moq_tokio_server_serve_publish_future *serving = NULL; + +/* Polls `poll` until it is ready, and the server's accept loop alongside it. */ +#define AWAIT(poll) \ + for (;;) { \ + unsigned long seen = wakes_so_far(); \ + moq_tokio_error *serve_err = NULL; \ + if (moq_tokio_server_serve_publish_poll(serving, session_waker, &serve_err) == MOQ_POLL_READY) { \ + fprintf(stderr, "%s:%d: the server stopped\n", __FILE__, __LINE__); \ + abort(); \ + } \ + if ((poll) == MOQ_POLL_READY) { \ + break; \ + } \ + wait_wake(seen); \ + } + int main(void) { moq_waker waker = {.wake = wake}; moq_error *err = NULL; @@ -403,6 +466,122 @@ int main(void) { CHECK(alpns_all.len > 0); moq_vec_string_free(alpns_all); + /* A session from C: a server binds QUIC on port 0, reads back the port it got, and + * serves an origin with one call, and a client dials it with one call and reads a frame + * back. Neither has a runtime of C's, so moq-tokio's fallback runs both. */ + moq_origin_producer *served = moq_origin_spawn(); + moq_origin_publication *live = NULL; + CHECK(moq_origin_producer_create_broadcast(served, str("live"), &live, &err)); + moq_origin_route *route = moq_origin_route_default(); + CHECK(moq_origin_publication_announce(live, route, &err)); + moq_origin_route_free(route); + moq_broadcast_producer *live_broadcast = moq_origin_publication_broadcast(live); + moq_track_producer *live_track = NULL; + CHECK(moq_broadcast_producer_create_track(live_broadcast, str("video"), NULL, &live_track, &err)); + moq_group_producer *live_group = NULL; + CHECK(moq_track_producer_append_group(live_track, &live_group, &err)); + CHECK(moq_group_producer_write_frame(live_group, NULL, str("hello"), &err)); + CHECK(moq_group_producer_finish(live_group, &err)); + moq_group_producer_free(live_group); + + moq_server_config *server_config = moq_server_config_default(); + moq_listen_config *listen_config = moq_server_config_listen(server_config); + moq_listen_bind any = {.tag = MOQ_LISTEN_BIND_ADDR, .addr = {._0 = {.port = 0, .v6 = true}}}; + moq_listen_config_set_bind(listen_config, (moq_option_listen_bind){.some = true, .value = any}); + moq_tls_listen *tls_listen = moq_tls_listen_default(); + moq_buf names[] = {str("localhost")}; + moq_tls_listen_set_generate(tls_listen, (moq_vec_string){.ptr = names, .len = 1}); + moq_listen_config_set_tls(listen_config, tls_listen); + moq_tls_listen_free(tls_listen); + moq_server_config_set_listen(server_config, listen_config); + moq_listen_config_free(listen_config); + moq_tokio_server *server = NULL; + CHECK(moq_server_config_init(server_config, &server, &tokio_err)); + moq_server_config_free(server_config); + moq_socket_addr local; + CHECK(moq_tokio_server_local_addr(server, &local, &tokio_err)); + CHECK(local.v6 && local.port != 0); + moq_origin_consumer *published = moq_origin_producer_consume(served); + serving = moq_tokio_server_serve_publish(server, published); + moq_tokio_server_free(server); + moq_origin_consumer_free(published); + + char address[32]; + snprintf(address, sizeof address, "moqt://localhost:%u", (unsigned)local.port); + moq_connect_config *dialing = moq_connect_config_default(); + CHECK(moq_url_parse(str(address), &url, &parse_err)); + moq_connect_config_set_url(dialing, url); + moq_url_free(url); + tls = moq_connect_config_tls(dialing); + moq_tls_connect_set_insecure(tls, (moq_option_bool){.some = true, .value = true}); + moq_connect_config_set_tls(dialing, tls); + moq_tls_connect_free(tls); + moq_client_config *session_client = moq_client_config_default(); + moq_client_config_set_connect(session_client, dialing); + moq_connect_config_free(dialing); + moq_tokio_client *dialer = NULL; + CHECK(moq_client_config_init(session_client, &dialer, &tokio_err)); + moq_client_config_free(session_client); + moq_tokio_client *dialer_copy = moq_tokio_client_clone(dialer); + moq_tokio_client *once = moq_tokio_client_with_reconnect(dialer_copy, false); + moq_tokio_client_free(dialer_copy); + moq_origin_producer *received = moq_origin_spawn(); + moq_origin_consumer *remote = moq_origin_producer_consume(received); + moq_connection *connection = moq_tokio_client_consume(once, received); + moq_tokio_client_free(once); + moq_origin_producer_free(received); + CHECK(connection != NULL); + + moq_connection_established_future *establishing = moq_connection_established(connection); + moq_connection_free(connection); + moq_connection *established = NULL; + AWAIT(moq_connection_established_poll(establishing, session_waker, &established, &tokio_err)); + moq_connection_established_future_free(establishing); + CHECK(established != NULL); + + moq_origin_consumer_request_broadcast_future *requesting = moq_origin_consumer_request_broadcast(remote, str("live"), NULL); + moq_broadcast_consumer *remote_broadcast = NULL; + AWAIT(moq_origin_consumer_request_broadcast_poll(requesting, session_waker, &remote_broadcast, &err)); + moq_origin_consumer_request_broadcast_future_free(requesting); + CHECK(remote_broadcast != NULL); + moq_track_consumer *remote_track = NULL; + CHECK(moq_broadcast_consumer_track(remote_broadcast, str("video"), &remote_track, &err)); + subscribing = moq_track_consumer_subscribe(remote_track, NULL); + AWAIT(moq_track_consumer_subscribe_poll(subscribing, session_waker, &subscriber, &err)); + moq_track_consumer_subscribe_future_free(subscribing); + CHECK(subscriber != NULL); + receiving = moq_track_subscriber_recv_group(subscriber); + group = NULL; + AWAIT(moq_track_subscriber_recv_group_poll(receiving, session_waker, &group, &err)); + moq_track_subscriber_recv_group_future_free(receiving); + CHECK(group != NULL); + reading = moq_group_consumer_read_frame(group); + frame = NULL; + AWAIT(moq_group_consumer_read_frame_poll(reading, session_waker, &frame, &err)); + moq_group_consumer_read_frame_future_free(reading); + CHECK(frame != NULL && is(moq_frame_frame_payload(frame), "hello")); + moq_frame_frame_free(frame); + moq_group_consumer_free(group); + moq_track_subscriber_free(subscriber); + moq_track_consumer_free(remote_track); + moq_broadcast_consumer_free(remote_broadcast); + + moq_connection_close_future *closing = moq_connection_close(established); + moq_connection_free(established); + AWAIT(moq_connection_close_poll(closing, session_waker, &tokio_err)); + moq_connection_close_future_free(closing); + moq_tokio_client_close_future *client_closing = moq_tokio_client_close(dialer); + moq_tokio_client_free(dialer); + AWAIT(moq_tokio_client_close_poll(client_closing, session_waker)); + moq_tokio_client_close_future_free(client_closing); + moq_tokio_server_serve_publish_future_free(serving); + moq_origin_consumer_free(remote); + moq_track_producer_free(live_track); + moq_broadcast_producer_free(live_broadcast); + moq_origin_publication_free(live); + moq_origin_producer_free(served); + CHECK(tokio_err == NULL); + if (failures == 0) { printf("ok\n"); } diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index 620d9fd33..f7854c39b 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -50,7 +50,6 @@ Every generator on the C ABI: - [Payloads cross as bytes](/quest/m1/ffi-export/payload-bytes.md) - a payload that is not UTF-8 crosses unchanged, where today it aborts from C, C++, and Kotlin - [Iterator and JSON arguments the exporter can't map yet](/quest/m1/ffi-export/instance-keys.md) - `IntoIterator` as a `Vec`, and `serde_json::Value` - [moq-clock's C names once it moves into kio](/quest/m1/ffi-export/crates.md) - `moq_now` and `moq_origin` become the clock's, after moq moves it into `kio::clock` -- [Sessions on moq-tokio](/quest/m1/ffi-export/session-tokio.md) - bindings connect and accept with one call, the library running moq-tokio inside - [Sessions on sans-IO QUIC](/quest/m1/ffi-export/session-quic.md) - bindings move UDP datagrams and drive the session's poll themselves - [Glue that builds without warnings](/quest/m1/ffi-export/warnings.md) - the exporter's Rust glue builds with `-D warnings` - [The recursion depth lint on moq's export](/quest/m1/ffi-export/recursion-lint.md) - `just ffi moq` builds clean before the lint becomes an error diff --git a/quest/m1/ffi-export/session-quic.md b/quest/m1/ffi-export/session-quic.md index e22aa339c..e8b9ecdcc 100644 --- a/quest/m1/ffi-export/session-quic.md +++ b/quest/m1/ffi-export/session-quic.md @@ -9,9 +9,9 @@ poll from its own event loop, as driving decided for the origin's ## Plan -Decided with the maintainer on 2026-10-27: the full-control path, after -[sessions on moq-tokio](/quest/m1/ffi-export/session-tokio.md). It suits consumers with -their own event loop or sockets. +Decided with the maintainer on 2026-10-27: the full-control path, after sessions on +moq-tokio, which `moq/moq.c` proves with moq-tokio's fallback runtime. It suits consumers +with their own event loop or sockets. - Exported from moq-quic's and moq-net's `pub` API; where moq-quic's session does not yet fit moq-net's transport trait, that adapter is moq's change, not the exporter's. diff --git a/quest/m1/ffi-export/session-tokio.md b/quest/m1/ffi-export/session-tokio.md deleted file mode 100644 index 753cb4b22..000000000 --- a/quest/m1/ffi-export/session-tokio.md +++ /dev/null @@ -1,35 +0,0 @@ -# [M] Sessions on moq-tokio - -## Goal - -A binding connects and accepts moq sessions with one call, as moq-ffi does today: the -exported library runs moq-tokio's sockets and runtime inside, and the binding never drives -a transport. Proven from C in `moq/moq.c` against a local relay or server, including a -server that binds port 0 and reads its address back. - -## Plan - -Decided with the maintainer on 2026-10-27, from -[#150](https://github.com/kixelated/rust2/pull/150): `Client::connect` and `Server::accept` -need a transport C can't supply. Bindings get both: this easy path first, since most -consumers want it, then [the sans-IO path](/quest/m1/ffi-export/session-quic.md). - -- The runtime inside is moq-tokio's own, exported from its `pub` API like everything else. - Where its API needs a wrapper to cross the C ABI, that wrapper is new API in moq-tokio. - -The one call already exports: `Client::publish` and `consume` dial `connect.url`, and -`Server::serve_publish`, `serve_consume`, and `serve_both` accept until the listener stops. -Found from C on 2026-10-27 at the pinned moq: `connect::Config::init` returns moq-tokio's -`NoRuntime`, since its QUIC endpoint needs the current tokio runtime, and -`Client::connect` would `tokio::spawn`, a panic that aborts. The futures C polls -(`Server::bind`, the `serve_*`, `Listener::accept`, `Request::ok`) make timers and spawn in -their bodies, so each poll needs the runtime entered, not only the call. - -Decided with the maintainer on 2026-10-28: moq-tokio starts a fallback runtime on first -use unless the caller supplies one. Each entry point and future that needs tokio runs in -the caller's `Handle::try_current()`, else in that process-wide runtime. C gets no new -names, a Rust caller inside tokio sees no change, and one outside gets a runtime instead -of `NoRuntime`. It landed in [moq-dev/moq#7123](https://github.com/moq-dev/moq/pull/7123); -rust2's moq pin includes it. - -Public API: the C names moq's bindings get. From 2fbd68c2702ab3275d08a9c463d9486a64d6dcaf Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:31:15 -0700 Subject: [PATCH 3/6] test(ffi): run the QUIC session round trip from C++ and Kotlin too Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 5 +- ffi/moq/moq.cpp | 153 +++++++++++++++++++++++++++++++++++++++++++++++- ffi/moq/moq.kt | 59 ++++++++++++++++++- 3 files changed, 211 insertions(+), 6 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 24ebde6b3..be70eec08 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -296,5 +296,6 @@ hands it to a client to publish, polls its driver until the origin's last owner sets and reads a field of the catalog lent to C through `catalog::Producer::mutate`, and serves a broadcast from a moq-tokio server bound to port 0 to a client that reads it over QUIC, both on moq-tokio's fallback runtime, whose threads wake C's. `moq/moq.cpp` does the -same through the C++ header, short of the catalog's field and the session, at C++17 -without exceptions or RTTI, as OBS builds it, and `moq/moq.kt` through the Kotlin. +same through the C++ header, short of the catalog's field, at C++17 without exceptions or +RTTI, as OBS builds it, serving on an executor's thread and blocking on each read, and +`moq/moq.kt` through the Kotlin, serving in a coroutine and suspending on each read. diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index f72ca8df6..86d759d5a 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -3,11 +3,21 @@ // moq-pattern, moq-clock, moq-tokio, hang, and moq-mux exported into the same library, // the catalog lent to C++ for a call, and a session's config filled in field by field. // -// It needs no runtime: the waker counts wakes, and each poll that must wait is polled -// again after the write that wakes it. +// The local reads need no runtime: the waker counts wakes, and each poll that must wait +// is polled again after the write that wakes it. A session over QUIC runs on moq-tokio's +// fallback runtime, whose threads wake C++'s. +#include +#include +#include #include +#include #include +#include +#include +#include +#include +#include #include "moq.hpp" @@ -35,6 +45,78 @@ bool is(const moq::buf &buf, const char *s) { int wakes = 0; +// Runs the jobs an executor is given on a thread of its own, until destroyed. +class worker { + std::mutex mutex_; + std::condition_variable cv_; + std::deque> jobs_; + bool stopping_ = false; + std::thread thread_; + + void run() { + for (;;) { + std::function job; + { + std::unique_lock lock(mutex_); + cv_.wait(lock, [&]() { return stopping_ || !jobs_.empty(); }); + if (jobs_.empty()) { + return; + } + job = std::move(jobs_.front()); + jobs_.pop_front(); + } + job(); + } + } + +public: + worker() : thread_([this]() { run(); }) {} + ~worker() { + { + std::lock_guard lock(mutex_); + stopping_ = true; + } + cv_.notify_one(); + thread_.join(); + } + + auto executor() { + return [this](std::function job) { + { + std::lock_guard lock(mutex_); + jobs_.push_back(std::move(job)); + } + cv_.notify_one(); + }; + } +}; + +// Fails the run rather than hanging when what it guards stalls past `limit`. +class watchdog { + std::mutex mutex_; + std::condition_variable cv_; + bool done_ = false; + std::thread thread_; + +public: + explicit watchdog(std::chrono::seconds limit) + : thread_([this, limit]() { + std::unique_lock lock(mutex_); + if (!cv_.wait_for(lock, limit, [&]() { return done_; })) { + std::fprintf(stderr, "stalled for %lld seconds\n", static_cast(limit.count())); + std::abort(); + } + }) {} + ~watchdog() { + { + std::lock_guard lock(mutex_); + done_ = true; + } + cv_.notify_one(); + thread_.join(); + } +}; + } // namespace int main() { @@ -234,6 +316,73 @@ int main() { CHECK(moq::connect_error_is_auth(moq::connect_error::unauthorized)); CHECK(is(moq::connect_error_to_string(moq::connect_error::unauthorized), "unauthorized")); + // A session, as moq.c runs it: a server bound to port 0 reads back the port it got and + // serves with one call, on an executor of its own, and a client dials it with one call + // and reads a frame back, blocking on each read while moq-tokio's fallback runtime wakes + // it from its threads. + { + watchdog limit(std::chrono::seconds(30)); + moq::origin_producer served = moq::origin_spawn(); + auto live = served.create_broadcast("live"); + CHECK(live && live->announce(moq::origin_route::default_())); + moq::broadcast_producer live_broadcast = live->broadcast(); + auto live_track = live_broadcast.create_track("video", std::nullopt); + CHECK(live_track); + auto live_group = live_track->append_group(); + CHECK(live_group && live_group->write_frame(nullptr, "hello")); + CHECK(std::move(*live_group).finish()); + + moq::server_config server_config = moq::server_config::default_(); + moq::listen_config listen = server_config.listen(); + moq::socket_addr any{}; + any.v6 = true; + listen.set_bind(moq::listen_bind(moq::listen_bind_addr{any})); + moq::tls_listen tls = listen.tls(); + tls.set_generate({"localhost"}); + listen.set_tls(std::move(tls)); + server_config.set_listen(std::move(listen)); + auto server = std::move(server_config).init(); + CHECK(server); + auto local = server->local_addr(); + CHECK(local && local->v6 && local->port != 0); + worker serving_thread; + std::atomic stopped{false}; + moq::task serving = std::move(*server).serve_publish(served.consume()).then(serving_thread.executor(), + [&](auto &&) { stopped = true; }); + + moq::connect_config connect = moq::connect_config::default_(); + connect.set_url(moq::url::parse("moqt://localhost:" + std::to_string(local->port)).value()); + moq::tls_connect insecure = connect.tls(); + insecure.set_insecure(true); + connect.set_tls(std::move(insecure)); + moq::client_config client_config = moq::client_config::default_(); + client_config.set_connect(std::move(connect)); + auto client = std::move(client_config).init(); + CHECK(client); + moq::origin_producer received = moq::origin_spawn(); + moq::origin_consumer remote = received.consume(); + auto connection = moq::tokio_client(*client).with_reconnect(false).consume(std::move(received)); + CHECK(connection); + auto established = std::move(*connection).established().get(); + CHECK(established); + + auto remote_broadcast = remote.request_broadcast("live", std::nullopt).get(); + CHECK(remote_broadcast); + auto remote_track = remote_broadcast->track("video"); + CHECK(remote_track); + auto remote_subscriber = remote_track->subscribe(std::nullopt).get(); + CHECK(remote_subscriber); + auto remote_group = remote_subscriber->recv_group().get(); + CHECK(remote_group && remote_group.value()); + auto remote_frame = remote_group.value()->read_frame().get(); + CHECK(remote_frame && remote_frame.value() && is(remote_frame.value()->payload(), "hello")); + + CHECK(std::move(*established).close().get()); + std::move(*client).close().get(); + serving = moq::task(); + CHECK(!stopped); + } + if (failures == 0) { std::printf("ok\n"); } diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index 1481bc709..9a048beb2 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -3,15 +3,20 @@ // moq-pattern, moq-clock, moq-tokio, hang, and moq-mux exported into the same library, // and the catalog lent to Kotlin for a call. // -// It needs no runtime: each read that must wait resumes on a dispatcher the script -// drains, after the write that wakes it. +// The local reads need no runtime: each read that must wait resumes on a dispatcher the +// script drains, after the write that wakes it. A session over QUIC runs on moq-tokio's +// fallback runtime, whose threads resume Kotlin's. +import java.net.InetAddress +import java.net.InetSocketAddress import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.Executor +import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout import moq.* var failures = 0 @@ -249,6 +254,56 @@ fun main() { expect(tls.root.isEmpty() && tls.fingerprint.isEmpty(), "a default TLS config lists none") } + // A session, as moq.c runs it: a server bound to port 0 reads back the port it got and + // serves with one call, in a coroutine of its own, and a client dials it with one call + // and reads a frame back, each suspend fun resuming once moq-tokio's fallback runtime + // wakes it from its threads. A stall fails with the timeout's exception. + runBlocking { + withTimeout(30.seconds) { + val served = originSpawn() + val live = served.createBroadcast("live") + live.announce(OriginRoute.default()) + val liveTrack = live.broadcast().createTrack("video", null) + val liveGroup = liveTrack.appendGroup() + liveGroup.writeFrame(null, "hello") + liveGroup.finish() + + val serverConfig = ServerConfig.default() + val listen = serverConfig.listen + listen.setBind(ListenBind.Addr(InetSocketAddress(InetAddress.getByName("::"), 0))) + val tls = listen.tls + tls.setGenerate(listOf("localhost")) + listen.setTls(tls) + serverConfig.setListen(listen) + val server = serverConfig.init() + val local = server.localAddr() + expect(local.port != 0, "a server bound to port 0 reads back the port it got") + val serving = launch { server.servePublish(served.consume()) } + + val connect = ConnectConfig.default() + connect.setUrl(Url.parse("moqt://localhost:${local.port}")) + val insecure = connect.tls + insecure.setInsecure(true) + connect.setTls(insecure) + val clientConfig = ClientConfig.default() + clientConfig.setConnect(connect) + val client = clientConfig.init() + val received = originSpawn() + val remote = received.consume() + val connection = client.clone().withReconnect(false).consume(received) + expect(connection != null, "a client with a URL dials it") + val established = connection!!.established() + + val subscriber = remote.requestBroadcast("live", null).track("video").subscribe(null) + val frame = subscriber.recvGroup()?.readFrame() + expect(frame?.payload?.decodeToString() == "hello", "a session reads the frame the server serves") + + established.close_() + client.close_() + serving.cancel() + } + } + if (failures == 0) println("ok") kotlin.system.exitProcess(if (failures == 0) 0 else 1) } From 5e2eb96e970dc68ca9a9c43960b39e69cd2fbe62 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:51:22 -0700 Subject: [PATCH 4/6] test(ffi): keep the C++ worker's queue alive for a late post Co-Authored-By: Claude Opus 5.5 --- ffi/moq/moq.cpp | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index 86d759d5a..e784f5ad9 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -45,48 +46,57 @@ bool is(const moq::buf &buf, const char *s) { int wakes = 0; -// Runs the jobs an executor is given on a thread of its own, until destroyed. +// Runs the jobs an executor is given on a thread of its own, until destroyed. A future +// may still post to its executor after its task is destroyed, so the queue is shared with +// every copy of the executor and outlives the worker, dropping what comes after it stops. class worker { - std::mutex mutex_; - std::condition_variable cv_; - std::deque> jobs_; - bool stopping_ = false; + struct queue { + std::mutex mutex; + std::condition_variable cv; + std::deque> jobs; + bool stopping = false; + }; + + std::shared_ptr queue_ = std::make_shared(); std::thread thread_; - void run() { + static void run(const std::shared_ptr &q) { for (;;) { std::function job; { - std::unique_lock lock(mutex_); - cv_.wait(lock, [&]() { return stopping_ || !jobs_.empty(); }); - if (jobs_.empty()) { + std::unique_lock lock(q->mutex); + q->cv.wait(lock, [&]() { return q->stopping || !q->jobs.empty(); }); + if (q->jobs.empty()) { return; } - job = std::move(jobs_.front()); - jobs_.pop_front(); + job = std::move(q->jobs.front()); + q->jobs.pop_front(); } job(); } } public: - worker() : thread_([this]() { run(); }) {} + worker() : thread_([q = queue_]() { run(q); }) {} ~worker() { { - std::lock_guard lock(mutex_); - stopping_ = true; + std::lock_guard lock(queue_->mutex); + queue_->stopping = true; } - cv_.notify_one(); + queue_->cv.notify_one(); thread_.join(); } auto executor() { - return [this](std::function job) { + return [q = queue_](std::function job) { { - std::lock_guard lock(mutex_); - jobs_.push_back(std::move(job)); + std::lock_guard lock(q->mutex); + if (q->stopping) { + return; + } + q->jobs.push_back(std::move(job)); } - cv_.notify_one(); + q->cv.notify_one(); }; } }; From 8f50579942f2f182c9e04d3bbe4f7cb10f4c63a1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:41:05 -0700 Subject: [PATCH 5/6] fix(ffi): destroying a C++ task waits out a post under way Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 2 +- ffi/moq/moq.cpp | 48 +++++++++++++++----------------------- ffi/runtime/rt.hpp | 24 +++++++++++++++---- ffi/tests/api.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 34 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index be70eec08..08963b3b9 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -202,7 +202,7 @@ exceptions and RTTI on or off: it throws nothing and reads no type information. loses nothing. `poll(waker)` polls once; `std::move(f).get()` blocks the thread; `std::move(f).then(exec, callback)` polls on an executor and calls back there, returning a `task` that cancels when destroyed, after which the callback has finished or never - runs; and from C++20, `co_await std::move(f).via(exec)` resumes the coroutine on the + runs and the executor is never called again; and from C++20, `co_await std::move(f).via(exec)` resumes the coroutine on the executor. An executor is any callable taking a `void()` job to run once, later, on any thread: never inline, since a wake may come from inside Rust. Each wake that lands while the future polls polls it again, and a future has one poller at a time, since awaiting diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index e784f5ad9..86d759d5a 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -46,57 +45,48 @@ bool is(const moq::buf &buf, const char *s) { int wakes = 0; -// Runs the jobs an executor is given on a thread of its own, until destroyed. A future -// may still post to its executor after its task is destroyed, so the queue is shared with -// every copy of the executor and outlives the worker, dropping what comes after it stops. +// Runs the jobs an executor is given on a thread of its own, until destroyed. class worker { - struct queue { - std::mutex mutex; - std::condition_variable cv; - std::deque> jobs; - bool stopping = false; - }; - - std::shared_ptr queue_ = std::make_shared(); + std::mutex mutex_; + std::condition_variable cv_; + std::deque> jobs_; + bool stopping_ = false; std::thread thread_; - static void run(const std::shared_ptr &q) { + void run() { for (;;) { std::function job; { - std::unique_lock lock(q->mutex); - q->cv.wait(lock, [&]() { return q->stopping || !q->jobs.empty(); }); - if (q->jobs.empty()) { + std::unique_lock lock(mutex_); + cv_.wait(lock, [&]() { return stopping_ || !jobs_.empty(); }); + if (jobs_.empty()) { return; } - job = std::move(q->jobs.front()); - q->jobs.pop_front(); + job = std::move(jobs_.front()); + jobs_.pop_front(); } job(); } } public: - worker() : thread_([q = queue_]() { run(q); }) {} + worker() : thread_([this]() { run(); }) {} ~worker() { { - std::lock_guard lock(queue_->mutex); - queue_->stopping = true; + std::lock_guard lock(mutex_); + stopping_ = true; } - queue_->cv.notify_one(); + cv_.notify_one(); thread_.join(); } auto executor() { - return [q = queue_](std::function job) { + return [this](std::function job) { { - std::lock_guard lock(q->mutex); - if (q->stopping) { - return; - } - q->jobs.push_back(std::move(job)); + std::lock_guard lock(mutex_); + jobs_.push_back(std::move(job)); } - q->cv.notify_one(); + cv_.notify_one(); }; } }; diff --git a/ffi/runtime/rt.hpp b/ffi/runtime/rt.hpp index fd8060a54..b27df692a 100644 --- a/ffi/runtime/rt.hpp +++ b/ffi/runtime/rt.hpp @@ -399,7 +399,8 @@ void spawn(task &slot, typename Op::raw *raw, Exec exec, F f); } // namespace detail // A future running on an executor until it calls back with the output. Destroying it -// cancels the future: once that returns, the callback has finished or never runs. +// cancels the future: once that returns, the callback has finished or never runs, and the +// executor is never called again, so it may go too. class task { detail::task_node *node_ = nullptr; @@ -465,13 +466,15 @@ class future { // Poll on `exec` until ready, then call `f` there with the output. `exec` is any // callable that takes a `void()` callable and runs it once, later, on any thread: never - // within the call, which may come from inside Rust. + // within the call, which may come from inside Rust. Once the returned task is + // destroyed, `exec` is never called again. template task then(Exec exec, F f) &&; // From C++20, `co_await` it, polled and resumed on `exec`, as `then` takes it. - // Destroying the suspended coroutine cancels the future. Declared at every standard, - // so the class is one definition across translation units built at different ones. + // Destroying the suspended coroutine cancels the future, and once that returns, `exec` + // is never called again. Declared at every standard, so the class is one definition + // across translation units built at different ones. template auto via(Exec exec) &&; }; @@ -579,6 +582,9 @@ class driver final : public task_node { enum : int { idle, queued, running, done }; ::std::mutex mutex_; + // Held across each call to the executor, so `cancel` returns only once none is under + // way, and none starts after. + ::std::mutex posting_; // Null once the future finished or was cancelled. typename Op::raw *raw_; Exec exec_; @@ -592,6 +598,11 @@ class driver final : public task_node { bool given_ = false; void post() { + ::std::lock_guard<::std::mutex> lock(posting_); + // Cancelled since the wake that queued it, so the executor may be gone. + if (state_.load(::std::memory_order_acquire) == done) { + return; + } ref(); exec_([self = ptr(this)]() { self->run(); }); } @@ -660,6 +671,11 @@ class driver final : public task_node { state_.store(done, ::std::memory_order_release); Op::free(::std::exchange(raw_, nullptr)); } + // Wait out a post under way, which may have passed its check before `done`: after + // this, the executor is never called again. + { + ::std::lock_guard<::std::mutex> lock(posting_); + } unref(); } }; diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 7f04c6159..a1d6a6aaf 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -4,6 +4,8 @@ // With an argument, it instead checks the futures' semantics, or breaks a contract of the // ABI, which must abort. +#include +#include #include #include #include @@ -628,6 +630,62 @@ int futures() { check(called, "a task cancelled in its callback finishes"); } + // Destroyed while a wake is posting it, a task returns only once that post is done, so + // its executor can go with it. This executor blocks inside the wake's post until released: + // a destroy that returns before then has left the executor in use. Seeing that the + // destroy waits needs a bound, so it is given 100 ms, more than a destroy that does not + // wait ever takes. + { + std::mutex mutex; + std::condition_variable cv; + std::deque> jobs; + int posts = 0; + bool entered = false; + bool released = false; + bool destroyed = false; + auto exec = [&](std::function job) { + std::unique_lock lock(mutex); + jobs.push_back(std::move(job)); + // The first post is the task's own first poll; the second, the send's wake. + if (++posts == 2) { + entered = true; + cv.notify_all(); + cv.wait(lock, [&]() { return released; }); + } + }; + api::task task = channel.recv().then(exec, [](api::buf) {}); + std::function first; + { + std::lock_guard lock(mutex); + first = std::move(jobs.front()); + jobs.pop_front(); + } + first(); + std::thread sender([&]() { channel.send("raced"); }); + { + std::unique_lock lock(mutex); + cv.wait(lock, [&]() { return entered; }); + } + std::thread destroyer([&]() { + task = api::task(); + std::lock_guard lock(mutex); + destroyed = true; + cv.notify_all(); + }); + bool early = false; + { + std::unique_lock lock(mutex); + early = cv.wait_for(lock, std::chrono::milliseconds(100), [&]() { return destroyed; }); + released = true; + } + cv.notify_all(); + sender.join(); + destroyer.join(); + check(!early, "destroying a task waits for a post under way"); + jobs.clear(); + check(channel.recv().get().str() == "raced", "a task destroyed mid-post loses nothing"); + } + #if defined(__cpp_impl_coroutine) && __cpp_impl_coroutine >= 201902L && __has_include() // Awaited in a coroutine, resumed on the executor. { From c8b18268e04b259c7bf7a904b7dee3c128747548 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:45:57 -0700 Subject: [PATCH 6/6] test(ffi): write the Kotlin session's payload as bytes, as #197 exports it Co-Authored-By: Claude Opus 5.5 --- ffi/moq/moq.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index f1c05cfd9..6e06d09f6 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -266,7 +266,7 @@ fun main() { live.announce(OriginRoute.default()) val liveTrack = live.broadcast().createTrack("video", null) val liveGroup = liveTrack.appendGroup() - liveGroup.writeFrame(null, "hello") + liveGroup.writeFrame(null, "hello".encodeToByteArray()) liveGroup.finish() val serverConfig = ServerConfig.default()