From 4b635f3c292b4a9c95a5887ecc7c7e038fd5b062 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 09:33:33 -0700 Subject: [PATCH 1/4] quest: claim quest/m0/wildcard/datagram-start Co-Authored-By: Claude Opus 5.5 From 70c0979a12acc1fffd6aa25eaa219377c8435dbc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 10:53:58 -0700 Subject: [PATCH 2/4] fix(lite): keep datagrams behind SUBSCRIBE_START A dropped (oversized) datagram no longer resolves the start. In Rust a datagram that goes first resolves it like a group, under the source's start, and nothing is served while the first item waits on the source. In JS the start's floor is pinned when the start is claimed, so a group popped before the START write runs cannot go out below it. A lite-07 Rust subscriber delivers a datagram only after its own subscription's SUBSCRIBE_OK, not just an admitted track provenance. Closes quest/m0/wildcard/datagram-start. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 55 +++++++ js/net/src/lite/publisher.ts | 36 +++-- quest/m0/wildcard/README.md | 1 - quest/m0/wildcard/datagram-start.md | 53 ------- rs/moq-net/src/lite/publisher.rs | 213 ++++++++++++++++++++------ rs/moq-net/src/lite/subscriber.rs | 27 +++- rs/moq-net/src/lite/test_transport.rs | 42 ++++- 7 files changed, 305 insertions(+), 122 deletions(-) delete mode 100644 quest/m0/wildcard/datagram-start.md diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 2466166cd29..f5aa5891f23 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -585,6 +585,9 @@ async function servedSubscription( group.close(); track.writeGroup(group); }, + datagram(sequence: number, bytes = 1) { + track.insertDatagram(sequence, Timestamp.fromMillis(sequence), new Uint8Array(bytes)); + }, // The next group stream the publisher opened, drained, or undefined once it has gone // idle. A group the publisher dropped then fails an assertion instead of hanging the // test on a stream that will never arrive. @@ -666,6 +669,58 @@ test("lite draft-05: a straggler below the announced start group is not served", } }); +// A datagram too large for the transport is never sent, so it must not resolve the start: +// that would drop an older group below it for nothing. +test("lite draft-05: a dropped datagram does not resolve the start", async () => { + const debug = spyOn(console, "debug"); + const sub = await servedSubscription(); + try { + sub.datagram(10, 2000); + while (!debug.mock.calls.some(([msg]) => String(msg).startsWith("dropping oversize datagram"))) await flush(); + + sub.serve(5); + expect(await sub.servedSequence()).toBe(5); + const resp = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_05); + if (!("start" in resp)) throw new Error("expected SUBSCRIBE_START"); + expect(resp.start.group).toBe(5); + } finally { + debug.mockRestore(); + await sub.close(); + } +}); + +// The datagram and group loops race to claim SUBSCRIBE_START, and the START write runs a few +// turns after the claim. Whichever loop wins, no group goes out below the start it announced, +// even one popped before that write ran. Sweeping the group's arrival across those turns +// covers the window without depending on how many microtasks each loop takes. +test("lite draft-05: no group goes out below a datagram's start", async () => { + const debug = spyOn(console, "debug"); + try { + for (let turns = 0; turns < 16; turns++) { + debug.mockClear(); + const sub = await servedSubscription(); + try { + // Both loops are parked, so the datagram and the groups race from here. + while (!debug.mock.calls.some(([msg]) => String(msg).startsWith("publish ok"))) await flush(); + await flush(); + + sub.datagram(10); + for (let i = 0; i < turns; i++) await Promise.resolve(); + sub.serve(5); + sub.serve(11); + + const resp = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_05); + if (!("start" in resp)) throw new Error("expected SUBSCRIBE_START"); + expect(await sub.servedSequence()).toBeGreaterThanOrEqual(resp.start.group); + } finally { + await sub.close(); + } + } + } finally { + debug.mockRestore(); + } +}); + // A group pop is the linearization point. Once the publisher reaches the SUBSCRIBE_START write, // the group and its range have already been decided, so an update queued during the write applies // to the next pop rather than reaching backward into this one. diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 94fc5a46a20..3df0ad7cf4a 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -267,18 +267,24 @@ class SubscriptionControls { */ class SubscribeResponses { #controls: SubscriptionControls; - #start: (sequence: number) => Promise; + #start: (sequence: number) => () => Promise; #writes: Promise = Promise.resolve(true); #started?: Promise; - constructor(controls: SubscriptionControls, start: (sequence: number) => Promise) { + /** `start` claims the start synchronously and returns the SUBSCRIBE_START write. */ + constructor(controls: SubscriptionControls, start: (sequence: number) => () => Promise) { this.#controls = controls; this.#start = start; } - /** Sends SUBSCRIBE_START at `sequence`, once; false when peer departure superseded it. */ + /** + * Sends SUBSCRIBE_START at `sequence`, once; false when peer departure superseded it. + * + * The first call claims the start in the same turn, so whatever either serving loop pops + * next already sees it, even while the write waits its turn. + */ start(sequence: number): Promise { - this.#started ??= this.#write(() => this.#start(sequence)); + this.#started ??= this.#write(this.#start(sequence)); return this.#started; } @@ -662,18 +668,21 @@ export class Publisher { endGroup: msg.endGroup, endFrame: msg.endFrame, }; - const responses = new SubscribeResponses(controls, async (sequence) => { + const responses = new SubscribeResponses(controls, (sequence) => { // SUBSCRIBE_START promises nothing below this sequence will be delivered. - // Arrival-order serving could later surface a straggler below the first - // group, so pin the floor to what was announced. + // Arrival-order serving could later surface a straggler below it, so pin the + // floor to what is announced now, not when the write runs: a group popped in + // between would otherwise go out below it. hooks.replaceGroups(track, { start: { included: sequence }, end: bounds.endGroup === undefined ? undefined : { included: bounds.endGroup }, }); - // Read once content flowed: an upstream's SUBSCRIBE_START named the origin - // before any of its content did. - const start = new SubscribeStart(sequence, wireOf(front).origin()); - await encodeSubscribeResponse(stream.writer, { start }, this.version); + return async () => { + // Read once content flowed: an upstream's SUBSCRIBE_START named the origin + // before any of its content did. + const start = new SubscribeStart(sequence, wireOf(front).origin()); + await encodeSubscribeResponse(stream.writer, { start }, this.version); + }; }); // Serve datagrams concurrently with groups whenever the transport carries them @@ -1011,18 +1020,19 @@ export class Publisher { for (;;) { const datagram = await track.recvDatagram(); if (!datagram) return; // Track finished; #runTrack tears the subscription down. - if (!(await responses.start(datagram.sequence))) return; // Convert the timestamp to the track's advertised timescale, matching #serveGroup. const ts = Math.round(datagram.timestamp.as(timescale)); const body = new DatagramMessage(sub, datagram.sequence, ts, datagram.payload).encode(); - // No group fallback: drop anything that doesn't fit a single datagram. + // No group fallback: drop anything that doesn't fit a single datagram. It is never + // sent, so it must not resolve the start either. if (body.byteLength > maxSize) { console.debug(`dropping oversize datagram: sub=${sub} size=${body.byteLength} max=${maxSize}`); continue; } + if (!(await responses.start(datagram.sequence))) return; await writer.ready; await writer.write(body); } diff --git a/quest/m0/wildcard/README.md b/quest/m0/wildcard/README.md index a6fbb088993..32eb706a601 100644 --- a/quest/m0/wildcard/README.md +++ b/quest/m0/wildcard/README.md @@ -191,7 +191,6 @@ distinguish recording generations reads the catalog's archive entry ## Required -- [Datagrams behind SUBSCRIBE_START](/quest/m0/wildcard/datagram-start.md) - no datagram crosses a subscription's start in either direction, in Rust or JS - [JS origin granularity](/quest/m0/wildcard/js-origin.md) - `@moq/net` tracks a reply's origin at the same granularity as Rust - [Pool resolution benchmark](/quest/m0/wildcard/pool-bench.md) - route resolution is benchmarked over pool size and requested paths diff --git a/quest/m0/wildcard/datagram-start.md b/quest/m0/wildcard/datagram-start.md deleted file mode 100644 index 54e9be23d76..00000000000 --- a/quest/m0/wildcard/datagram-start.md +++ /dev/null @@ -1,53 +0,0 @@ -# [M] Datagrams behind SUBSCRIBE_START - -## Goal - -On lite-07, no datagram crosses a subscription's SUBSCRIBE_START in either -direction, in Rust or JS: a publisher commits the start only for something it -actually sends, never sends a group below the start it announced, and a -subscriber delivers a datagram only after that subscription's own -SUBSCRIBE_START has named an admitted origin. - -## Plan - -[#4279](https://github.com/moq-dev/moq/pull/4279) made SUBSCRIBE_START -(SUBSCRIBE_OK) carry the serving origin and go out ahead of the first group -served by stream or datagram. Its last Codex round left three P1s unanswered, -and the PR auto-merged before CI. The maintainer ruled in the 09-28 -merged-PR audit that all three block the line: - -- **An oversized first datagram commits the start** - ([r4117485530](https://github.com/moq-dev/moq/pull/4279#discussion_r4117485530)). - Rust's `Recv::Datagram` arm in `rs/moq-net/src/lite/publisher.rs` calls - `send_start` before `serve_datagram` drops a body over `max_datagram_size`, and - JS `#runDatagrams` in `js/net/src/lite/publisher.ts` awaits - `responses.start` before its size check. A dropped datagram at sequence 10 - can so announce a start of 10 and discard a valid group at 5. Decide - whether a datagram can be sent before resolving the start from it. - Since main's held first group (`TrackRun::first`), a group waits for the - source's start (`track.poll_start`) while datagrams keep flowing with no - START, and a datagram that goes first still resolves the start from its - own sequence without consulting the source. Both paths should resolve the - start the same way. -- **JS start-sequence race** - ([r4117485532](https://github.com/moq-dev/moq/pull/4279#discussion_r4117485532)). - `SubscribeResponses.start` reserves `#started` for whichever loop arrives - first, but the write runs later through the `#writes` chain, so the group - loop can pop a lower sequence in between and send group 5 after a START - for 10. Choose the start and apply its floor atomically, or revalidate a - group popped while the start was pending. -- **FETCH_OK admits a datagram before its own SUBSCRIBE_START** - ([r4117485533](https://github.com/moq-dev/moq/pull/4279#discussion_r4117485533)). - `route_datagram` in `rs/moq-net/src/lite/subscriber.rs` gates only on the - shared track `Provenance` being admitted. A FETCH on the same track can - admit it through FETCH_OK while this subscription has not started, so a - racing datagram is delivered, and a later START naming another origin is - caught only after content was exposed. Require this subscription's own - start as well. Check whether the JS subscriber has the same gap. - -Each fix gets a regression test that fails without it, in the language it -touches. - -## Related - -- [Wildcard](/quest/m0/wildcard/README.md) - the line this blocks diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 5ff64d9b331..8aa080df133 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2292,35 +2292,39 @@ struct Opens { } impl Subscription { - /// Send one datagram best-effort over a QUIC datagram (lite-05 §6.4). + /// Encode one datagram for a QUIC datagram (lite-05 §6.4), or `None` when it is dropped. /// - /// The datagram is dropped (there is no group fallback) if the encoded body doesn't fit the - /// transport's datagram limit or the send fails (congestion / no capacity right now). - fn serve_datagram(&mut self, datagram: crate::Datagram) { + /// There is no group fallback: a body that doesn't fit the transport's datagram limit + /// is never sent, so it resolves nothing either. + fn encode_datagram(&self, datagram: crate::Datagram) -> Option { + let sequence = datagram.sequence; let body = lite::Datagram { subscribe: self.id, - sequence: datagram.sequence, + sequence, // Already at the track timescale (normalized by the model producer). timestamp: datagram.timestamp.value(), payload: datagram.payload, }; // has_datagrams is checked before this runs, so encoding never hits the version guard. - let Ok(body) = body.encode_bytes(self.version) else { - return; - }; + let body = body.encode_bytes(self.version).ok()?; let max = self.session.max_datagram_size(); if body.len() > max { tracing::debug!( - sequence = datagram.sequence, + sequence, size = body.len(), max, "dropping datagram larger than the transport limit" ); - return; + return None; } + Some(body) + } - let _ = self.session.send_datagram(&body); + /// Send an encoded datagram best-effort: a failed send (congestion, no capacity right + /// now) drops it. + fn send_datagram(&mut self, body: &[u8]) { + let _ = self.session.send_datagram(body); } /// Read the latest SUBSCRIBE_UPDATE track priority, marking it seen. @@ -2354,6 +2358,28 @@ enum TrackEnd { Finished, } +/// The first thing a subscription serves, which resolves its SUBSCRIBE_START. +// Held once per subscription, in the slot that held the bare group before, so the big +// variant costs nothing boxing would save. +#[allow(clippy::large_enum_variant)] +enum First { + Group(group::Consumer), + /// An encoded datagram that fits the transport. + Datagram { + sequence: u64, + body: bytes::Bytes, + }, +} + +impl First { + fn sequence(&self) -> u64 { + match self { + Self::Group(group) => group.sequence, + Self::Datagram { sequence, .. } => *sequence, + } + } +} + /// A subscription's run loop: one subscriber cursor serving groups, datagrams, /// and SUBSCRIBE_UPDATE messages, with an in-flight group machine per group. struct TrackRun { @@ -2366,12 +2392,15 @@ struct TrackRun { start_frame: Option<(u64, u64)>, end_frame: Option<(u64, u64)>, // Lite05+ resolves the range on the Subscribe Stream itself: SUBSCRIBE_START - // once the first group is known, SUBSCRIBE_END as soon as the track declares its + // once the first group or datagram is known, SUBSCRIBE_END as soon as the track declares its // exclusive final sequence (which may be ahead of the live edge). emit_range: bool, start_sent: bool, - // The first servable group, held until the source resolves where its feed starts. - first: Option, + // The first servable group or datagram, held until the source resolves where its feed + // starts. + first: Option, + // A datagram that resolved the start, sent once its SUBSCRIBE_START is flushed. + after_start: Option, // Groups skipped for a missing head before the start resolved, which it must not name. skipped: BTreeSet, end_sent: bool, @@ -2411,6 +2440,7 @@ impl TrackRun { emit_range, start_sent: false, first: None, + after_start: None, skipped: BTreeSet::new(), end_sent: false, count_streams, @@ -2429,6 +2459,9 @@ impl TrackRun { loop { // Deliver the buffered range messages before selecting more work. ready!(stream.writer.poll_flush(&mut cx))?; + if let Some(body) = self.after_start.take() { + self.ctx.send_datagram(&body); + } // Drive the in-flight group machines; completions just retire. let _ = self.children.poll(waiter); @@ -2462,22 +2495,16 @@ impl TrackRun { continue; } - // The first group waits for the source to resolve where its feed starts; datagrams - // keep flowing meanwhile. - if let Some(group) = self.first.take() { + // The first group or datagram waits for the source to resolve where its feed + // starts. Nothing else is served meanwhile: it would go out ahead of the start. + if let Some(first) = self.first.take() { match self.track.poll_start(waiter) { Poll::Ready(source) => { - self.start(group, source, stream)?; + self.start(first, source, stream)?; continue; } Poll::Pending => { - self.first = Some(group); - if self.datagrams - && let Poll::Ready(Some(datagram)) = self.track.poll_recv_datagram(waiter)? - { - self.ctx.serve_datagram(datagram); - continue; - } + self.first = Some(first); return Poll::Pending; } } @@ -2502,15 +2529,19 @@ impl TrackRun { continue; } match self.emit_range && !self.start_sent { - true => self.first = Some(group), + true => self.first = Some(First::Group(group)), false => self.serve(group), } } Recv::Datagram(datagram) => { - if self.emit_range && !self.start_sent { - self.send_start(stream, datagram.sequence)?; + let sequence = datagram.sequence; + // A dropped datagram is never sent, so it cannot resolve the start. + if let Some(body) = self.ctx.encode_datagram(datagram) { + match self.emit_range && !self.start_sent { + true => self.first = Some(First::Datagram { sequence, body }), + false => self.ctx.send_datagram(&body), + } } - self.ctx.serve_datagram(datagram); } Recv::Boundary(group) => { // The track declared its exclusive final sequence. Forward it now, @@ -2547,27 +2578,26 @@ impl TrackRun { } impl TrackRun { - /// Send SUBSCRIBE_START for the first servable group, then serve it. + /// Send SUBSCRIBE_START for the first servable group or datagram, then serve it. /// /// A relay caches groups in upstream arrival order, and a newer group's stream can - /// beat an older one, so the first group here need not be the oldest the source + /// beat an older one, so the first thing here need not be the oldest the source /// serves. `source` is where the source's feed starts, raised to this cursor's floor, - /// so the resolved start is the lower of the two: resolving from the later group would - /// drop the older one for good. - fn start( - &mut self, - group: group::Consumer, - source: Option, - stream: &mut Stream, - ) -> Result<(), Error> { - let mut start = source.map_or(group.sequence, |source| source.min(group.sequence)); - // A skipped group is never served, so it cannot be where the feed starts. This stops - // at the held group at the latest, since it was not skipped. - while self.skipped.contains(&start) { + /// so the resolved start is the lower of the two: resolving from the later sequence + /// would drop the older group for good. + fn start(&mut self, first: First, source: Option, stream: &mut Stream) -> Result<(), Error> { + let sequence = first.sequence(); + let mut start = source.map_or(sequence, |source| source.min(sequence)); + // A skipped group is never served, so it cannot be where the feed starts. The held + // one is served, so the start stops there at the latest. + while start < sequence && self.skipped.contains(&start) { start += 1; } self.send_start(stream, start)?; - self.serve(group); + match first { + First::Group(group) => self.serve(group), + First::Datagram { body, .. } => self.after_start = Some(body), + } Ok(()) } @@ -3465,7 +3495,7 @@ mod serve_group_test { .with_max_age(Duration::from_secs(30)); let subscriber = track.subscribe(subscription); - let mut session = ScriptedSession::new(Vec::new()); + let mut session = ScriptedSession::new(Vec::new()).with_datagrams(MAX_DATAGRAM); let (send, recv) = futures::future::poll_fn(|cx| { ::poll_open_bi(&mut session, cx) }) @@ -3524,6 +3554,99 @@ mod serve_group_test { let start = start.encode_bytes(Version::Lite07).unwrap(); self.session.log.writes.lock().unwrap().starts_with(&start) } + + /// For each datagram sent, how many stream bytes had reached the transport first. + fn datagrams(&self) -> Vec { + self.session + .log + .datagrams() + .into_iter() + .map(|(written, _)| written) + .collect() + } + } + + /// Large enough for a one-byte datagram, too small for [`big_datagram`]. + const MAX_DATAGRAM: usize = 64; + + fn big_datagram() -> bytes::Bytes { + bytes::Bytes::from(vec![0u8; MAX_DATAGRAM]) + } + + fn write_datagram(track: &mut track::Producer, sequence: u64, payload: bytes::Bytes) { + track + .insert_datagram(sequence, Timestamp::from_millis(sequence).unwrap(), payload) + .unwrap(); + } + + /// A datagram too large for the transport is never sent, so it must not resolve the + /// start: that would drop an older group below it for nothing. + #[tokio::test] + async fn dropped_datagram_does_not_resolve_the_start() { + let mut track = track::Producer::new(Arc::new(broadcast::Info::default()), "test", None); + let subscriber = track.subscribe(None); + let session = Log::default(); + let (mut run, mut stream, log) = lite07_run( + SinkSession::new(session.clone()).with_datagrams(MAX_DATAGRAM), + subscriber, + ); + let mut run = std::pin::pin!(kio::wait(move |waiter| run.poll(&mut stream, waiter))); + + write_datagram(&mut track, 10, big_datagram()); + assert!(futures::poll!(run.as_mut()).is_pending()); + write_group(&mut track, 5, 5); + assert!(futures::poll!(run.as_mut()).is_pending()); + + // SUBSCRIBE_START at 5 from origin 9. + assert!( + log.writes.lock().unwrap().starts_with(&[0, 2, 5, 9]), + "resolved the start from the dropped datagram" + ); + assert!(session.datagrams().is_empty()); + } + + /// A datagram that goes first resolves the start the way a group does, under the + /// source's start, and follows its SUBSCRIBE_START onto the transport. + #[tokio::test] + async fn first_datagram_resolves_the_start_under_the_source() { + let mut track = track::Producer::new(Arc::new(broadcast::Info::default()), "test", None); + let mut relay = RelayRun::new(&mut track, 0).await; + + write_datagram(&mut track, 10, bytes::Bytes::from_static(b"x")); + relay.settle(); + assert!( + relay.session.log.writes.lock().unwrap().is_empty(), + "started before the source" + ); + assert!(relay.datagrams().is_empty(), "sent a datagram ahead of the start"); + + track.start_at(3).unwrap(); + relay.settle(); + assert!(relay.started_at(3), "resolved the start from the datagram alone"); + let start = relay.session.log.writes.lock().unwrap().len(); + assert_eq!(relay.datagrams(), [start]); + } + + /// While the first group waits on the source, a datagram must not go out ahead of the + /// SUBSCRIBE_START it follows. + #[tokio::test] + async fn held_first_group_holds_datagrams_back() { + let mut track = track::Producer::new(Arc::new(broadcast::Info::default()), "test", None); + let mut relay = RelayRun::new(&mut track, 0).await; + + write_group(&mut track, 5, 5); + relay.settle(); + write_datagram(&mut track, 6, bytes::Bytes::from_static(b"x")); + relay.settle(); + assert!(relay.datagrams().is_empty(), "sent a datagram ahead of the start"); + + track.start_at(0).unwrap(); + relay.settle(); + assert!(relay.started_at(0)); + let [written] = relay.datagrams()[..] else { + panic!("the datagram was not sent"); + }; + assert!(written > 0, "sent a datagram ahead of the start"); } /// A SUBSCRIBE_UPDATE landing while the first group waits on the source's start keeps diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 7dca60627d7..1f04c56d3de 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -461,8 +461,10 @@ impl Subscriber { }; // A datagram cannot wait for its subscription's SUBSCRIBE_OK like a group stream - // does, so until the origin it names is admitted, the datagram is dropped. - if self.version.has_origin() && !entry.producer.provenance().is_admitted() { + // does, so until that reply named an origin the front admits, the datagram is + // dropped. The track's provenance alone is not enough: a FETCH_OK on the same + // track can admit it while this subscription's own reply may still name another. + if self.version.has_origin() && !(*entry.started.read() && entry.producer.provenance().is_admitted()) { return Ok(()); } @@ -1777,9 +1779,10 @@ mod tests { } /// On lite-07 a datagram carries no origin of its own, so until its subscription's - /// origin is admitted it is dropped rather than risk splicing another origin's content. + /// SUBSCRIBE_OK named an origin the front admits, it is dropped rather than risk + /// splicing another origin's content. #[test] - fn datagram_waits_for_the_admitted_origin() { + fn datagram_waits_for_its_start_and_the_admitted_origin() { let version = Version::Lite07; let origin = origin::Config::new(crate::Hop::new(1).unwrap()).produce(); let subscriber = Subscriber::new(SubscriberConfig { @@ -1798,13 +1801,14 @@ mod tests { let producer = broadcast.create_track("datagrams", None).unwrap(); let provenance = producer.provenance(); let mut received = producer.subscribe(None); + let started = kio::Shared::new(false); subscriber.subscribes.lock().insert( 7, TrackEntry { producer, timescale: Some(Timescale::default()), tail: Default::default(), - started: kio::Shared::new(false), + started: started.clone(), }, ); @@ -1823,16 +1827,27 @@ mod tests { let named = crate::Hop::new(42).unwrap(); provenance.name(named).unwrap(); provenance.admit(crate::Hop::new(43).unwrap()); + *started.lock() = true; subscriber.route_datagram(payload(1)).unwrap(); assert!( received.recv_datagram().now_or_never().is_none(), "delivered before admission" ); + // Admitted, as a FETCH_OK on the same track would leave it, but this + // subscription's own SUBSCRIBE_OK has not arrived. + *started.lock() = false; provenance.admit(named); subscriber.route_datagram(payload(2)).unwrap(); + assert!( + received.recv_datagram().now_or_never().is_none(), + "delivered before its own SUBSCRIBE_OK" + ); + + *started.lock() = true; + subscriber.route_datagram(payload(3)).unwrap(); let datagram = received.recv_datagram().now_or_never().unwrap().unwrap().unwrap(); - assert_eq!(datagram.sequence, 2); + assert_eq!(datagram.sequence, 3); } /// A lite-05 subscribe still waiting on TRACK_INFO is not in the subscribe map. diff --git a/rs/moq-net/src/lite/test_transport.rs b/rs/moq-net/src/lite/test_transport.rs index ff69af76af1..7b5590cb2ab 100644 --- a/rs/moq-net/src/lite/test_transport.rs +++ b/rs/moq-net/src/lite/test_transport.rs @@ -45,6 +45,7 @@ pub struct Log { closes: Arc>>, bi_opens: Arc, priorities: Arc>>, + datagrams: Arc>>, } impl Log { @@ -80,6 +81,20 @@ impl Log { pub fn bi_opens(&self) -> usize { self.bi_opens.load(Ordering::Relaxed) } + + /// The datagrams sent, each with how many stream bytes had been written before it, so a + /// test can tell what reached the transport ahead of it. + pub fn datagrams(&self) -> Vec<(usize, bytes::Bytes)> { + self.datagrams.lock().unwrap().clone() + } + + fn send_datagram(&self, payload: &[u8]) { + let written = self.writes.lock().unwrap().len(); + self.datagrams + .lock() + .unwrap() + .push((written, bytes::Bytes::copy_from_slice(payload))); + } } pub struct SinkSend { @@ -344,6 +359,8 @@ pub struct SinkSession { /// with no congestion controller exposed. Shared and mutable so a test can /// change it mid-session, the way a real transport's figures move. stats: Arc>, + /// Set by [`Self::with_datagrams`]. Zero, a transport without datagrams, by default. + max_datagram_size: usize, } impl SinkSession { @@ -372,6 +389,12 @@ impl SinkSession { *self.stats.lock().unwrap() = stats; } + /// Carry datagrams up to `max` bytes, recording each one sent in the [`Log`]. + pub fn with_datagrams(mut self, max: usize) -> Self { + self.max_datagram_size = max; + self + } + /// Report `protocol` as the negotiated ALPN. pub fn with_protocol(mut self, protocol: &'static str) -> Self { self.protocol = Some(protocol); @@ -475,7 +498,8 @@ impl poll::Session for SinkSession { Poll::Ready(Ok(send)) } - fn poll_send_datagram(&mut self, _cx: &mut Context<'_>, _payload: &[u8]) -> Poll> { + fn poll_send_datagram(&mut self, _cx: &mut Context<'_>, payload: &[u8]) -> Poll> { + self.log.send_datagram(payload); Poll::Ready(Ok(())) } @@ -484,7 +508,7 @@ impl poll::Session for SinkSession { } fn max_datagram_size(&self) -> usize { - 0 + self.max_datagram_size } fn protocol(&self) -> Option<&str> { @@ -604,6 +628,8 @@ pub struct ScriptedSession { /// Scripts the peer pushes at us on unidirectional streams, popped by `accept_uni` /// in order. See [`Self::with_incoming_unis`]. incoming_unis: Arc>>>, + /// Set by [`Self::with_datagrams`]. Zero, a transport without datagrams, by default. + max_datagram_size: usize, } impl ScriptedSession { @@ -616,9 +642,16 @@ impl ScriptedSession { open_gate: None, park: kio::Park::default(), incoming_unis: Arc::new(Mutex::new(std::collections::VecDeque::new())), + max_datagram_size: 0, } } + /// Carry datagrams up to `max` bytes, recording each one sent in the [`Log`]. + pub fn with_datagrams(mut self, max: usize) -> Self { + self.max_datagram_size = max; + self + } + /// Have the peer open one unidirectional stream per script, in order, each replaying /// its bytes and then going quiet (or reporting EOF, on an `eof` session). /// @@ -729,7 +762,8 @@ impl poll::Session for ScriptedSession { Poll::Ready(Ok(SinkSend::new(self.log.clone()))) } - fn poll_send_datagram(&mut self, _cx: &mut Context<'_>, _payload: &[u8]) -> Poll> { + fn poll_send_datagram(&mut self, _cx: &mut Context<'_>, payload: &[u8]) -> Poll> { + self.log.send_datagram(payload); Poll::Ready(Ok(())) } @@ -738,7 +772,7 @@ impl poll::Session for ScriptedSession { } fn max_datagram_size(&self) -> usize { - 0 + self.max_datagram_size } fn protocol(&self) -> Option<&str> { From 013a0617ce0ebbd453d9a6f3843dc2d2a9072df6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 11:18:55 -0700 Subject: [PATCH 3/4] fix(lite): drop datagrams below the subscription's floor Groups below the committed SUBSCRIBE_START were already dropped, but a datagram below it was still sent after the subscriber had counted that sequence as missed. Both publishers now check datagrams against the same floor the group cursor uses, so a widening update lowers both together. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 5 ++++ js/net/src/lite/publisher.test.ts | 33 +++++++++++++++++++++++++ js/net/src/lite/publisher.ts | 7 ++++++ js/net/src/track.ts | 1 + rs/moq-net/src/lite/publisher.rs | 41 +++++++++++++++++++++++++++++-- rs/moq-net/src/model/resume.rs | 5 ++++ rs/moq-net/src/model/track.rs | 10 ++++++++ rs/moq-net/tests/datagram.rs | 19 +++++++------- 8 files changed, 110 insertions(+), 11 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index e809fe0e98c..27ebec01f8b 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -160,6 +160,8 @@ export const hooks: { * `setGroups`, which never rewinds. */ replaceGroups: (subscriber: Subscriber, groups: Groups) => void; + /** The lowest group sequence a serving cursor delivers, which datagrams bypass. */ + groupFloor: (subscriber: Subscriber) => number; /** Return a group's first timestamp, retained even after its first frame is read. */ groupTimestamp: (group: GroupConsumer) => Timestamp | undefined; groupLatest: (group: GroupConsumer) => Timestamp | undefined; @@ -200,6 +202,9 @@ export const hooks: { replaceGroups: () => { throw new Error("track.ts not loaded"); }, + groupFloor: () => { + throw new Error("track.ts not loaded"); + }, groupTimestamp: () => { throw new Error("group.ts not loaded"); }, diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 3b0fd73f25c..dd473b3b821 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -10,6 +10,7 @@ import { Reader, Stream, Writer } from "../stream.ts"; import { Milli, Timestamp } from "../time.ts"; import { DEFAULT_MAX_AGE_MS } from "../track.ts"; import { AnnounceRequest } from "./announce.ts"; +import { Datagram as DatagramMessage } from "./datagram.ts"; import { Fetch } from "./fetch.ts"; import { Group as GroupMessage } from "./group.ts"; import { sendOrder } from "./priority.ts"; @@ -574,6 +575,7 @@ async function servedSubscription( const serving = publisher.runSubscribe(msg, server); const opened = pair.client.incomingUnidirectionalStreams.getReader(); + const datagrams = pair.client.datagrams.readable.getReader(); return { client, @@ -618,9 +620,21 @@ async function servedSubscription( async servedSequence(): Promise { return (await this.servedGroup())?.sequence; }, + // The next datagram's sequence, or undefined once the publisher has gone idle. + async sentDatagram(): Promise { + let timer: ReturnType | undefined; + const idle = new Promise((resolve) => { + timer = setTimeout(() => resolve(undefined), IDLE_MS); + }); + const next = await Promise.race([datagrams.read(), idle]); + clearTimeout(timer); + if (!next || next.done) return undefined; + return (await DatagramMessage.decode(next.value, version)).sequence; + }, async close() { // Settles any pending read as well as dropping the stream. await opened.cancel(); + await datagrams.cancel(); publisher.close(); client.close(); }, @@ -691,6 +705,25 @@ test("lite draft-05: a dropped datagram does not resolve the start", async () => } }); +// SUBSCRIBE_START promises nothing below the start, so a datagram below it is dropped like a +// group there would be. +test("lite draft-05: a datagram below the announced start is not sent", async () => { + const sub = await servedSubscription(); + try { + sub.serve(5); + expect(await sub.servedSequence()).toBe(5); + const resp = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_05); + if (!("start" in resp)) throw new Error("expected SUBSCRIBE_START"); + expect(resp.start.group).toBe(5); + + sub.datagram(3); + sub.datagram(6); + expect(await sub.sentDatagram()).toBe(6); + } finally { + await sub.close(); + } +}); + // The datagram and group loops race to claim SUBSCRIBE_START, and the START write runs a few // turns after the claim. Whichever loop wins, no group goes out below the start it announced, // even one popped before that write ran. Sweeping the group's arrival across those turns diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 9f69caa4b4a..1c1b9b523ba 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1021,6 +1021,13 @@ export class Publisher { const datagram = await track.recvDatagram(); if (!datagram) return; // Track finished; #runTrack tears the subscription down. + // Below the floor, as a group there is: SUBSCRIBE_START promised nothing below + // the start. + if (datagram.sequence < hooks.groupFloor(track)) { + console.debug(`dropping datagram below the start: sub=${sub} sequence=${datagram.sequence}`); + continue; + } + // Convert the timestamp to the track's advertised timescale, matching #serveGroup. const ts = Math.round(datagram.timestamp.as(timescale)); const body = new DatagramMessage(sub, datagram.sequence, ts, datagram.payload).encode(this.version); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 52dc4034e01..27a51dac09b 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1088,6 +1088,7 @@ export class Subscriber { subscriber.#enforceLatency = false; }; hooks.replaceGroups = (subscriber, groups) => subscriber.#replaceGroups(groups); + hooks.groupFloor = (subscriber) => subscriber.#cursor.peek().start; // The sequence cursor lives here (it shares the buffer and the drift anchor with // the arrival cursor); `Ordered` is the handle that reaches it. ordered_ = { diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 8aa080df133..ab4720f80c8 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2535,8 +2535,12 @@ impl TrackRun { } Recv::Datagram(datagram) => { let sequence = datagram.sequence; - // A dropped datagram is never sent, so it cannot resolve the start. - if let Some(body) = self.ctx.encode_datagram(datagram) { + // Below the floor, as a group there is: SUBSCRIBE_START promised nothing + // below the start. A dropped datagram is never sent, so it cannot resolve + // the start either. + if sequence < self.track.floor() { + tracing::debug!(subscribe = self.ctx.id, track = %self.ctx.track_name, sequence, "dropping datagram below the start"); + } else if let Some(body) = self.ctx.encode_datagram(datagram) { match self.emit_range && !self.start_sent { true => self.first = Some(First::Datagram { sequence, body }), false => self.ctx.send_datagram(&body), @@ -3605,6 +3609,39 @@ mod serve_group_test { assert!(session.datagrams().is_empty()); } + /// SUBSCRIBE_START promises nothing below the start, so a datagram below it is dropped + /// like a group there would be. + #[tokio::test] + async fn datagram_below_the_start_is_not_sent() { + let mut track = track::Producer::new(Arc::new(broadcast::Info::default()), "test", None); + let subscriber = track.subscribe(None); + let session = Log::default(); + let (mut run, mut stream, log) = lite07_run( + SinkSession::new(session.clone()).with_datagrams(MAX_DATAGRAM), + subscriber, + ); + let mut run = std::pin::pin!(kio::wait(move |waiter| run.poll(&mut stream, waiter))); + + write_group(&mut track, 5, 5); + assert!(futures::poll!(run.as_mut()).is_pending()); + assert!(log.writes.lock().unwrap().starts_with(&[0, 2, 5, 9])); + + write_datagram(&mut track, 3, bytes::Bytes::from_static(b"x")); + write_datagram(&mut track, 6, bytes::Bytes::from_static(b"x")); + assert!(futures::poll!(run.as_mut()).is_pending()); + + let sent: Vec = session + .datagrams() + .into_iter() + .map(|(_, mut body)| { + >::decode(&mut body, Version::Lite07) + .unwrap() + .sequence + }) + .collect(); + assert_eq!(sent, [6], "sent a datagram below the start"); + } + /// A datagram that goes first resolves the start the way a group does, under the /// source's start, and follows its SUBSCRIBE_START onto the transport. #[tokio::test] diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 089c361fafc..dd9157cdabb 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -2377,6 +2377,11 @@ impl Subscriber { } } + /// The lowest sequence this subscriber surfaces. See [`track::Subscriber::floor`]. + pub(crate) fn floor(&self) -> u64 { + self.min_sequence + } + /// Raise the floor to `sequence`, keeping any higher floor already set. See /// [`track::Subscriber::raise_start_to`]. pub(crate) fn raise_start_to(&mut self, sequence: u64) { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 8e0dbfd3861..0d86a810a0d 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -4126,6 +4126,16 @@ impl Subscriber { } } + /// The lowest group sequence this subscriber surfaces, as [`Self::start_at`] and + /// [`Self::raise_start_to`] left it. Datagrams bypass it, so a wire publisher that + /// promised a start checks them against it. + pub(crate) fn floor(&self) -> u64 { + match &self.inner { + SubscriberKind::Plain(plain) => plain.min_sequence, + SubscriberKind::Spliced(spliced) => spliced.floor(), + } + } + /// Raise the read cursor's floor to `sequence`, keeping any higher floor already set. /// /// The spliced layer positions a segment's inner cursor with this instead of diff --git a/rs/moq-net/tests/datagram.rs b/rs/moq-net/tests/datagram.rs index 3cd596cf258..4591d2d4108 100644 --- a/rs/moq-net/tests/datagram.rs +++ b/rs/moq-net/tests/datagram.rs @@ -191,7 +191,8 @@ async fn ietf_does_not_deliver_datagrams() { .expect("timed out"); } -/// Explicit insert keeps the origin sequence on the lite wire, including a gap. +/// Explicit insert keeps the origin sequence on the lite wire, including a gap. The first +/// datagram resolves SUBSCRIBE_START, so the later one is above it (one below is dropped). #[tokio::test] async fn inserted_sequences_survive_the_lite_wire() { tokio::time::timeout(TEST_TIMEOUT, async { @@ -200,32 +201,32 @@ async fn inserted_sequences_survive_the_lite_wire() { fixture .producer .insert_datagram( - 5, - Timestamp::from_millis(5).unwrap(), + 2, + Timestamp::from_millis(2).unwrap(), bytes::Bytes::from_static(PAYLOAD), ) .unwrap(); fixture .producer .insert_datagram( - 2, - Timestamp::from_millis(2).unwrap(), + 5, + Timestamp::from_millis(5).unwrap(), bytes::Bytes::from_static(PAYLOAD), ) .unwrap(); let first = fixture.subscriber.recv_datagram().await.unwrap().unwrap(); - assert_eq!(first.sequence, 5); + assert_eq!(first.sequence, 2); assert_eq!( first.timestamp, - Timestamp::from_millis(5).unwrap(), + Timestamp::from_millis(2).unwrap(), "timestamp did not survive the wire" ); assert_eq!(&first.payload[..], PAYLOAD); let second = fixture.subscriber.recv_datagram().await.unwrap().unwrap(); - assert_eq!(second.sequence, 2); - assert_eq!(second.timestamp, Timestamp::from_millis(2).unwrap()); + assert_eq!(second.sequence, 5); + assert_eq!(second.timestamp, Timestamp::from_millis(5).unwrap()); assert_eq!(&second.payload[..], PAYLOAD); }) .await From fd97159e16c7160fed5d4463f9ff6078266d9f30 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 11:51:02 -0700 Subject: [PATCH 4/4] quest(m0): drop the merged js-origin child; size the Wildcard line #4599 deleted quest/m0/wildcard/js-origin.md but the line README still listed it. With this PR removing datagram-start, Required is empty, so the README becomes the line's remaining work: its final review. Co-Authored-By: Claude Opus 5.5 --- quest/m0/wildcard/README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/quest/m0/wildcard/README.md b/quest/m0/wildcard/README.md index d97084c4db8..735fade6eec 100644 --- a/quest/m0/wildcard/README.md +++ b/quest/m0/wildcard/README.md @@ -1,4 +1,4 @@ -# Wildcard advertisements +# [S] Wildcard advertisements ## Goal @@ -189,10 +189,6 @@ announcement shadows it. A claim names no generation, so a client that must distinguish recording generations reads the catalog's archive entry ([archive](/quest/m1/archive/README.md)) rather than announce state. -## Required - -- [JS origin granularity](/quest/m0/wildcard/js-origin.md) - `@moq/net` tracks a reply's origin at the same granularity as Rust - ## Related - [archive](/quest/m1/archive/README.md) - an archive claims the root, and its