diff --git a/quest/m1/driver-sync-depth.md b/quest/m1/driver-sync-depth.md deleted file mode 100644 index 6abeeb3921..0000000000 --- a/quest/m1/driver-sync-depth.md +++ /dev/null @@ -1,47 +0,0 @@ -# [S] Prove the session driver Sync, and moq-tokio's spawn Send, within the default recursion limit - -## Goal - -`moq_net::Driver: Send + Sync` and moq-tokio's spawned session future prove `Send` at -Rust's default recursion limit, so moq-tokio and any crate that needs `Driver: Sync`, such -as the moq-ffi facade rust2's exporter builds, compile with -`-D recursion_depth_exceeding_limit`. The lint warns today and becomes a hard error in a -later nightly ([rust-lang/rust#159228](https://github.com/rust-lang/rust/issues/159228)). -`just rs send-depth` checks both, so the overflow cannot return. Raising -`recursion_limit` is not a fix. - -## Plan - -Decided 2026-11-14 (maintainer, in -[kixel-ai/rust2#809](https://github.com/kixel-ai/rust2/pull/809)): ✅ wrap the lite -publisher's `kio::Tasks` so it proves `Send + Sync` once, as -[#8657](https://github.com/moq-dev/moq/pull/8657) did for the subscriber, and gate moq-tokio -and the facade under the lint (rejected: record the wait only; allow the lint for moq's -crates, which only defers the failure until a nightly makes it a hard error). - -Measured 2026-11-18 at `71c6209f` with rust2's `just ffi moq-cpp`, which denies the lint, on -the flake's pinned nightly: - -- moq-tokio overflows proving `Send` for the `Instrumented<...>` spawned at - `rs/moq-tokio/src/client.rs:703`, through `panic::isolate`, `kio::clock::drive`, and - `moq_net::time::run`. -- The facade overflows proving `moq_net::Driver: Sync`, through `driver::State`, - `Protocol`, `lite::session::Driver`, `lite::publisher::Publisher`, and its - `kio::Tasks` (`rs/moq-net/src/lite/publisher.rs:264`), into `ControlState` and - `RequestServe`. - -Both chains run through the session driver, so the publisher's proof likely fixes both; -check moq-tokio after it. `sh/rs/send-depth.sh` checks only moq-net's lib and its `send` -test, which proves downstream futures `Send` but never `Driver: Sync`: add that assertion -to the `send` test and check moq-tokio's lib in the same script. The script's nightly is -the flake's pin, so only a pin bump changes the compiler it checks. - -Recommended: prove `Send + Sync` at the narrowest type that cuts the chain, with an -`unsafe impl` only where a safe newtype cannot, and a `// SAFETY:` that names what the -wrapped state holds. Measure the gate's added time. - -This unblocks rust2's exporter moq pin bump -([kixel-ai/rust2's quest/m1/ffi-refusals/moq-pin.md](https://github.com/kixel-ai/rust2/blob/main/quest/m1/ffi-refusals/moq-pin.md)), -which waits on `just ffi moq-cpp` passing at moq's `main`. - -Public API: none. Wire: none. diff --git a/rs/moq-net/src/driver.rs b/rs/moq-net/src/driver.rs index 7fc72dfaf0..1d07069d06 100644 --- a/rs/moq-net/src/driver.rs +++ b/rs/moq-net/src/driver.rs @@ -16,10 +16,14 @@ use crate::time::{Deadlines, Instant}; /// It holds no session handle: dropping the last [`crate::Session`] requests /// closure on the next poll. Dropping the driver cancels the session, and /// [`crate::Session::closed`] resolves with [`Error::Cancel`]. It is `Send` on -/// native, like the [`Boxable`](crate::transport::poll::Boxable) transport it drives. +/// native, like the [`Boxable`](crate::transport::poll::Boxable) transport it drives, and never `Sync`. /// A panic during polling is published as a transport failure before unwinding to the caller. #[must_use = "the session makes no progress unless its driver is polled"] pub struct Driver { + // Polled only through `&mut`, so sharing gains nothing, and moq-transport's task set holds + // non-`Sync` children anyway. The definite no keeps a `Sync` check, such as an exporter's, + // from overflowing the default recursion limit on the session model's depth. + not_sync: std::marker::PhantomData>, state: State, clock: Deadlines, } @@ -68,7 +72,11 @@ pub(crate) struct State { impl Driver { pub(crate) fn new(clock: Deadlines, state: State) -> Self { - Self { state, clock } + Self { + not_sync: std::marker::PhantomData, + state, + clock, + } } /// Process ready work at `now`, registering for external activity. @@ -167,8 +175,19 @@ impl std::fmt::Debug for Driver { } } -// The relay and the bindings spawn drivers on multi-threaded runtimes. +// The relay and the bindings spawn drivers on multi-threaded runtimes. The fields prove `Send` +// here, once, so a future holding the driver doesn't walk the whole session model again. +// The exhaustive pattern fails to compile when a field is added without being checked. const _: () = { - const fn assert_send() {} - assert_send::(); + fn assert_send(_: &T) {} + #[allow(dead_code, reason = "only type-checked, never called")] + fn fields(driver: &Driver) { + let Driver { not_sync, state, clock } = driver; + assert_send(not_sync); + assert_send(state); + assert_send(clock); + } }; + +// SAFETY: the assertion above proves every field `Send`. +unsafe impl Send for Driver {} diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index e4973db61e..619aa41303 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -24,7 +24,7 @@ use crate::{ priority::{Priority, PriorityHandle, PriorityQueue}, track::Order, transport::poll::{GroupOf, SendGroup as _, SendOptions}, - util::Idle, + util::{Idle, SendCell}, }; use super::Version; @@ -261,7 +261,9 @@ pub(super) struct Publisher { // A dedicated accept handle: the poll interface takes `&mut self`, and the // shared context stays behind the Arc for the per-stream children. accept: Transport, - children: kio::Tasks, + // Each control child reaches the whole track model: the cell proves `Send + Sync` once + // here, so the session driver's proof stays within the default recursion limit. + children: SendCell>, /// Aborts the session on a publication outside our grant (dialing side only). enforce: Option, } @@ -303,7 +305,7 @@ impl Publisher { }), runtime: config.runtime, accept, - children: kio::Tasks::new(), + children: SendCell::default(), } } } diff --git a/rs/moq-net/src/util/cell.rs b/rs/moq-net/src/util/cell.rs index a53710a953..13d1a49d98 100644 --- a/rs/moq-net/src/util/cell.rs +++ b/rs/moq-net/src/util/cell.rs @@ -48,3 +48,10 @@ impl std::ops::Deref for SendCell { &self.0 } } + +// A `&mut` can swap in only another `T`, whose bounds construction already proved. +impl std::ops::DerefMut for SendCell { + fn deref_mut(&mut self) -> &mut T { + &mut self.0 + } +} diff --git a/rs/moq-net/tests/send.rs b/rs/moq-net/tests/send.rs index f0fa6daae6..999e64051b 100644 --- a/rs/moq-net/tests/send.rs +++ b/rs/moq-net/tests/send.rs @@ -41,3 +41,40 @@ fn group_read_is_send() { require_send(read_frames(consumer)); }; } + +// Exporters such as moq-ffi's ask whether `Driver` is `Sync` to pick a wrapper, so asking must +// settle within the limit. Method resolution evaluates the `T: Sync` bound on the first +// candidate and falls back to the second when it fails. +struct Probe(std::marker::PhantomData); + +trait Synced { + fn synced(&self) -> bool { + true + } +} + +impl Synced for Probe {} + +trait Unsynced { + fn synced(&self) -> bool { + false + } +} + +impl Unsynced for &Probe {} + +#[test] +#[allow( + clippy::needless_borrow, + reason = "the borrow picks which candidate method resolution tries first" +)] +fn driver_is_not_sync() { + assert!(!(&Probe::(std::marker::PhantomData)).synced()); + // The control proves the probe can answer yes. + assert!((&Probe::(std::marker::PhantomData)).synced()); +} + +#[test] +fn driver_is_send() { + let _: fn(moq_net::Driver) = require_send; +} diff --git a/sh/rs/select.sh b/sh/rs/select.sh index 7b34f7564c..b06d1bce6f 100755 --- a/sh/rs/select.sh +++ b/sh/rs/select.sh @@ -174,8 +174,9 @@ case "$action" in fi # moq-net without `async`, which every other build unifies back in. if wants moq-net; then just rs no-async; fi - # The session driver's Send proof, which a deeper session state breaks. - if [[ "$action" == lint ]] && wants moq-net; then just rs send-depth; fi + # The session driver's Send proof, which a deeper session state breaks, and + # moq-tokio's spawn of it. + if [[ "$action" == lint ]] && wants '(moq-net|moq-tokio)'; then just rs send-depth; fi # Every C caller still matches libmoq's generated header. if [[ "$action" == lint ]] && wants libmoq; then just rs c-tests --syntax; fi # moq-cli's media tasks are behind `play`; only check-test runs tests. diff --git a/sh/rs/select.test.py b/sh/rs/select.test.py index a519b938c2..f10caa18dc 100644 --- a/sh/rs/select.test.py +++ b/sh/rs/select.test.py @@ -137,6 +137,8 @@ def test_gate_proves_send_depth_for_net(self): # Only the lint phase runs it, so the gate compiles it once. self.assertNotIn("rs send-depth", self.selected("rs/moq-net/src/lib.rs", action="test")) self.assertNotIn("rs send-depth", self.selected("rs/moq-auth/src/lib.rs")) + # moq-tokio spawns the driver, so its own changes are proved too. + self.assertIn("rs send-depth", self.selected("rs/moq-tokio/src/client.rs")) def test_gate_compiles_c_callers_against_libmoq(self): # libmoq, a crate its header reaches, and the C callers' own inputs. diff --git a/sh/rs/send-depth.sh b/sh/rs/send-depth.sh index 2b88b2fd55..f2ff8c8816 100644 --- a/sh/rs/send-depth.sh +++ b/sh/rs/send-depth.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash -# Prove the session driver and native downstream futures Send at the default +# Prove the session driver Send, its Sync check settled, moq-tokio's spawned +# session future Send, and native downstream futures Send at the default # recursion limit on the flake's pinned nightly, so only a pin bump changes the # compiler it checks. `async` is off by default, and without it the lib has no # driver and the `send` test compiles to nothing. @@ -35,3 +36,5 @@ export RUSTFLAGS="${RUSTFLAGS:+$RUSTFLAGS }-D recursion_depth_exceeding_limit" rustc --version cargo --version cargo check --locked -p moq-net --features async --lib --test send +# moq-tokio spawns the session future, which must prove Send at the same limit. +cargo check --locked -p moq-tokio --lib