Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions quest/m1/driver-sync-depth.md

This file was deleted.

29 changes: 24 additions & 5 deletions rs/moq-net/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::cell::Cell<()>>,
state: State,
clock: Deadlines,
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<T: Send>() {}
assert_send::<Driver>();
fn assert_send<T: 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 {}
8 changes: 5 additions & 3 deletions rs/moq-net/src/lite/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Control>,
// 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<kio::Tasks<Control>>,
/// Aborts the session on a publication outside our grant (dialing side only).
enforce: Option<Enforce>,
}
Expand Down Expand Up @@ -303,7 +305,7 @@ impl Publisher {
}),
runtime: config.runtime,
accept,
children: kio::Tasks::new(),
children: SendCell::default(),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions rs/moq-net/src/util/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ impl<T> std::ops::Deref for SendCell<T> {
&self.0
}
}

// A `&mut` can swap in only another `T`, whose bounds construction already proved.
impl<T> std::ops::DerefMut for SendCell<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
37 changes: 37 additions & 0 deletions rs/moq-net/tests/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(std::marker::PhantomData<T>);

trait Synced {
fn synced(&self) -> bool {
true
}
}

impl<T: Sync> Synced for Probe<T> {}

trait Unsynced {
fn synced(&self) -> bool {
false
}
}

impl<T> Unsynced for &Probe<T> {}

#[test]
#[allow(
clippy::needless_borrow,
reason = "the borrow picks which candidate method resolution tries first"
)]
fn driver_is_not_sync() {
assert!(!(&Probe::<moq_net::Driver>(std::marker::PhantomData)).synced());
// The control proves the probe can answer yes.
assert!((&Probe::<moq_net::Session>(std::marker::PhantomData)).synced());
}

#[test]
fn driver_is_send() {
let _: fn(moq_net::Driver) = require_send;
}
5 changes: 3 additions & 2 deletions sh/rs/select.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions sh/rs/select.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion sh/rs/send-depth.sh
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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