From acbe2e9eaba1efc9d55ca2b95da55018a06b189a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 02:58:23 -0700 Subject: [PATCH 01/16] chore(quest): claim quest/m2/pipeline-requests/subscribe.md Quest-Claim: 114131f35d210305 From bd26003814ea0cd21e8479b90539b32316ec3ec6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 03:59:06 -0700 Subject: [PATCH 02/16] feat(net): send SUBSCRIBE alongside TRACK in Rust A lite-05+ subscriber opens its SUBSCRIBE with the TRACK instead of a round trip after TRACK_INFO. Group streams that beat the info park unread after their header and open in arrival order on the accept. The aggregate demand is capped only by the cache ceiling, so accepting the info sends no update, and TRACK_INFO goes out ahead of every group stream. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/coding/stream.rs | 9 +- rs/moq-net/src/coding/writer.rs | 2 +- rs/moq-net/src/ietf/subscriber.rs | 17 +- rs/moq-net/src/lite/publisher.rs | 63 +- rs/moq-net/src/lite/subscriber.rs | 999 ++++++++++++++---- rs/moq-net/src/lite/test_transport.rs | 9 +- rs/moq-net/src/model/track.rs | 101 +- .../integration/finished_broadcast_mock.rs | 14 +- 8 files changed, 988 insertions(+), 226 deletions(-) diff --git a/rs/moq-net/src/coding/stream.rs b/rs/moq-net/src/coding/stream.rs index 93699c86f6..24e1f1d8a6 100644 --- a/rs/moq-net/src/coding/stream.rs +++ b/rs/moq-net/src/coding/stream.rs @@ -19,6 +19,11 @@ use crate::coding::{Reader, StreamCodes, Writer}; /// round-robin share: a packet or two, which is all it needs. const CONTROL_SEND_ORDER: u8 = u8::MAX; +/// The send order a TRACK_INFO answer goes at: ahead of every group stream, unlike other +/// control. A subscriber that sent its SUBSCRIBE alongside the TRACK holds the groups that +/// beat the info unread, so the info is all that stands between it and them. +pub(crate) const TRACK_INFO_SEND_ORDER: i64 = CONTROL_SEND_ORDER as i64 + 1; + /// A [Writer] and [Reader] pair for a single stream. pub struct Stream { pub writer: Writer, @@ -176,7 +181,7 @@ mod tests { assert_eq!( log.priorities(), - vec![CONTROL_SEND_ORDER], + vec![i64::from(CONTROL_SEND_ORDER)], "a control stream left at the transport default loses to every group", ); } @@ -194,7 +199,7 @@ mod tests { assert_eq!( log.priorities(), - vec![CONTROL_SEND_ORDER], + vec![i64::from(CONTROL_SEND_ORDER)], "an accepted control stream left at the transport default loses to every group", ); } diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index d158ee2f78..1f972ff283 100644 --- a/rs/moq-net/src/coding/writer.rs +++ b/rs/moq-net/src/coding/writer.rs @@ -375,7 +375,7 @@ mod tests { writer.set_priority(send_order.into()); } - assert_eq!(log.priorities(), (0u8..=255).collect::>()); + assert_eq!(log.priorities(), (0i64..=255).collect::>()); } /// A message the stream has not taken whole cannot be finished, so closing resets rather diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index f70b2ec62b..a9f64c0918 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -13331,12 +13331,23 @@ mod stitch_tests { original.push(datagram); } track.finish().unwrap(); - let (served, _sessions) = match hop { + let (served, _sessions, _copy) = match hop { true => { let (served, sessions) = crate::lite::ietf::test_hop(&origin, "room", [true, true]).await; - (served, Some(sessions)) + // The hop learns the track's info first, and holds the copy for the SUBSCRIBE + // below: a SUBSCRIBE sent alongside the TRACK has the cache replayed as + // datagrams before the TRACK_INFO they are read with, and they are dropped. + let copy = served + .consume() + .request("room", None) + .await + .unwrap() + .track("video") + .unwrap(); + copy.info().await.unwrap(); + (served, Some(sessions), Some(copy)) } - false => (origin, None), + false => (origin, None, None), }; let downstream = ScriptedSession::per_stream(vec![vec![]]).with_datagrams(65535); let peer = peer::PeerSetup::default(); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index dfc27f6605..3baaac60e8 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -119,7 +119,9 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option /// must not read as a request to replay the whole cache on join. /// /// `latest` is the newest group when the SUBSCRIBE arrived, not once the subscription -/// resolved: anything written in between is newer, so it is delivered. +/// resolved: anything written in between is newer, so it is delivered. On lite-05, a +/// SUBSCRIBE sent alongside its TRACK can arrive before the track resolves, when no group +/// is known yet: the newest once it resolves stands in, as it would for one sent after. fn cursor_floor(version: Version, start_group: Option, latest: Option) -> u64 { if version.resolves_start() || start_group.is_some() { return 0; @@ -1838,6 +1840,8 @@ impl Request for TrackInfoServe { let info = ready!(self.querying.poll_ok(waiter))?; crate::ietf::flate::egress(&info, shared.version.has_compression())?; + // Ahead of the track's groups, which a subscriber holds unread until it lands. + writer.set_priority(crate::coding::TRACK_INFO_SEND_ORDER); // TRACK_INFO only flows on Lite05+ (the encode errors otherwise). writer.buffer(&lite::TrackInfo::serve(&info, shared.version, ietf))?; if ietf { @@ -2100,6 +2104,10 @@ impl Request for SubscribeServe { ietf, }; + let latest = match shared.version.has_track_stream() { + true => latest.or_else(|| track.latest_sequence()), + false => latest, + }; let floor = cursor_floor(shared.version, bounds.groups().start(), latest); let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); if let Some(update) = update { @@ -8068,6 +8076,59 @@ mod tests { } } + /// TRACK_INFO goes out ahead of every group stream, the most urgent included: a subscriber + /// that sent its SUBSCRIBE alongside holds the groups that beat it unread. + #[moq_clock::test] + async fn track_info_outranks_the_track_groups() { + let version = Version::Lite07; + let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); + let broadcast = origin.create_broadcast("room").unwrap(); + let _track = broadcast.create_track("video", crate::track::Info::timed()).unwrap(); + broadcast.announce(Default::default()).unwrap(); + let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); + peer_setup.set(crate::lite::Setup::default()); + let (_, goaway) = crate::goaway::Handle::new(true); + let publisher = Publisher::new(PublisherConfig { + estimate: kio::Producer::default().weak(), + runtime: crate::time::sim(), + session: crate::driver::Transport::new(ScriptedSession::new(Vec::new())), + origin: origin.consume(), + version, + peer_setup, + goaway, + peer_hop: None, + subscriptions: Default::default(), + auth: crate::auth::Link::new(false), + peer_grant: Default::default(), + client: false, + }); + + let mut script = Vec::new(); + lite::Track { + epoch: None, + broadcast: crate::Path::from_static("room"), + track: "video".into(), + } + .encode(&mut crate::coding::Encoder::new(&mut script, version.into()), version) + .unwrap(); + let session = ScriptedSession::new(script); + let (send, recv) = kio::wait(|waiter| { + ::poll_open_bi(&session, waiter, Default::default()) + }) + .await + .unwrap(); + let stream = Stream:: { + writer: Writer::new(crate::transport::boxed::SendStream::new(send), version), + reader: crate::coding::Reader::new(crate::transport::boxed::RecvStream::new(recv), version), + }; + let mut serve = RequestServe::::new(publisher.shared.clone(), stream); + assert!(!drive(&mut serve).await, "the TRACK stream is held"); + + let top = i64::from(PriorityHandle::send_order_of(0)); + assert_eq!(session.log.priorities(), [crate::coding::TRACK_INFO_SEND_ORDER]); + assert!(crate::coding::TRACK_INFO_SEND_ORDER > top); + } + /// An answered TRACK stream holds its track until the requester closes its side, by /// FIN or reset, and the hold owes nothing a draining close would wait for. A requester /// that leaves while the reply is still blocked on flow control lets the track go too. diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 8dd35ae830..2a9f2a181c 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -71,8 +71,8 @@ pub(super) struct Subscriber { // local and is never written into a hop chain. session_origin: crate::Hop, subscribes: Lock>, - /// Why this session ended, once it has. A track still waiting on TRACK_INFO is - /// not in [`Self::subscribes`], so dropping its request reads this instead of + /// Why this session ended, once it has. A track still waiting on TRACK_INFO has no + /// copy in [`Self::subscribes`], so dropping its request reads this instead of /// becoming [`Error::Dropped`]. ended: Lock>, next_id: Arc, @@ -81,6 +81,8 @@ pub(super) struct Subscriber { peer_setup: super::PeerSetup, /// GROUP streams parked until the peer's SETUP says how to read their header. setup_waits: Arc, + /// GROUP streams parked until their subscription's TRACK_INFO says how to read them. + park_waits: Arc, /// Local policy overriding the peer's declared egress price. See `poll_link_cost`. cost: Option, /// Sources minted by the announce half, drained by the driver into @@ -92,16 +94,200 @@ pub(super) struct Subscriber { pub(super) announces: crate::session::Slots, } -#[derive(Clone)] struct TrackEntry { - producer: track::Producer, - /// How to read the track, from its TRACK_INFO, known before the SUBSCRIBE is even - /// opened, so group streams decode frames without blocking. - framing: lite::Framing, + /// Where the subscription's groups go, once its TRACK_INFO says how to read them. A + /// SUBSCRIBE sent alongside its TRACK registers before. + copy: Option, + /// The group streams that arrived before the copy, while any wait. + parked: Option>, /// The groups received so far, so the subscription's end can wait for the ones owed. tail: kio::Producer, } +/// A subscription's accepted copy of its track. +struct Accepted { + producer: track::Producer, + /// How to read the track, from its TRACK_INFO. + framing: lite::Framing, +} + +/// Group streams that beat their subscription's TRACK_INFO, each parked unread after its +/// header, which flow control bounds. The accept opens their groups at once, in the order +/// they arrived, so the copy receives them as it would have without the wait, and each +/// stream then takes its own. +#[derive(Default)] +struct Parked { + /// The headers still waiting on the accept, by ticket, in arrival order. + waiting: Vec<(u64, lite::Group)>, + /// What the accept opened for each, until its stream takes it. + opened: HashMap>, + next: u64, +} + +/// A group stream's group, opened in its subscription's copy for the stream to fill. +struct Opened { + group: group::Producer, + track: track::Producer, + framing: lite::Framing, + reading: Reading, +} + +impl Drop for TrackEntry { + fn drop(&mut self) { + // Groups the accept opened for streams that never took them end as cancelled with + // the subscription, rather than dropped unfinished. + let Some(parked) = &self.parked else { + return; + }; + let opened = match parked.write() { + Ok(mut state) => std::mem::take(&mut state.opened), + Err(_) => return, + }; + for opened in opened.into_values().flatten() { + let _ = opened.group.abort(Error::Cancel); + } + } +} + +/// Counts one GROUP stream parked on its subscription for as long as it waits. +struct ParkWait(Arc); + +impl ParkWait { + fn new(waits: &Arc) -> Self { + waits.fetch_add(1, atomic::Ordering::Relaxed); + Self(waits.clone()) + } +} + +impl Drop for ParkWait { + fn drop(&mut self) { + self.0.fetch_sub(1, atomic::Ordering::Relaxed); + } +} + +impl TrackEntry { + fn new(copy: Option, tail: kio::Producer) -> Self { + Self { + copy, + parked: None, + tail, + } + } + + #[cfg(test)] + fn accepted(producer: track::Producer, framing: lite::Framing, tail: kio::Producer) -> Self { + Self::new(Some(Accepted { producer, framing }), tail) + } + + /// Park a group stream whose `hdr` arrived before the copy, returning its ticket. + fn park(&mut self, hdr: &lite::Group) -> (kio::Consumer, u64) { + let parked = self.parked.get_or_insert_with(Default::default); + let ticket = match parked.write() { + Ok(mut state) => { + let ticket = state.next; + state.next += 1; + state.waiting.push((ticket, hdr.clone())); + ticket + } + Err(_) => unreachable!("open while the entry holds it"), + }; + (parked.consume(), ticket) + } + + /// Give the copy to the subscription, opening the groups of the streams parked on it. + fn accept(&mut self, copy: Accepted, version: Version, now: crate::time::Instant) { + self.copy = Some(copy); + let Some(parked) = self.parked.clone() else { + return; + }; + let waiting = match parked.write() { + Ok(mut state) => std::mem::take(&mut state.waiting), + Err(_) => return, + }; + let opened: Vec<_> = waiting + .into_iter() + .map(|(ticket, hdr)| (ticket, self.open(&hdr, version, now))) + .collect(); + if let Ok(mut state) = parked.write() { + state.opened.extend(opened); + } + } + + /// Take what the accept opened for the parked stream `ticket`. + fn unpark(&mut self, ticket: u64) -> Result { + let Some(parked) = &self.parked else { + return Err(Error::Cancel); + }; + let (opened, done) = match parked.write() { + Ok(mut state) => { + let opened = state.opened.remove(&ticket).unwrap_or(Err(Error::Cancel)); + (opened, state.opened.is_empty() && state.waiting.is_empty()) + } + Err(_) => (Err(Error::Cancel), false), + }; + if done { + self.parked = None; + } + opened + } + + /// Open the group of a stream whose header is `hdr` in the copy. + fn open(&mut self, hdr: &lite::Group, version: Version, now: crate::time::Instant) -> Result { + let Some(Accepted { producer, framing }) = &mut self.copy else { + return Err(Error::Cancel); + }; + let group_info = group::Info { sequence: hdr.sequence }; + // Stats (groups/frames/bytes) are counted in the model as the group + // is written, through the tagged `track::Producer`. The track counts + // it as content once its first frame lands. + let received = producer.create_group_at(group_info, hdr.frame_start); + // The subscription's end waits until this stream is read. Counted + // whatever the cache said, since the publisher counts every stream it + // opens (a resubscribe resends a cached group), and only after the + // cache holds the group, so a fetch held for the feed that sees it + // accounted also finds it. + let reading = Reading::open(&self.tail, Some(hdr.sequence), now); + // A route's first group says where its live feed is, when its answer does + // not: even one the cache already holds, as an idle copy asked from its + // head gets back. The copy goes live once the cache shows that group. + // Lite-07's answer does, and a group can overtake it: on a descending + // track the first to land is the newest, not where the feed starts. + if !producer.is_live() && !version.has_largest() { + producer.set_live( + Some(Position { + group: hdr.sequence, + frame: hdr.frame_start, + }), + None, + ); + } + let group = match received { + Ok(group) => group, + // The group is at or past the end the publisher declared, which no + // later stream can repair. Before lite-05 only a local finish sets + // an end, and lite-05 specified an inclusive one, so its last + // group lands on it: drop only that stream there. + Err(Error::Closed) + if !matches!( + version, + Version::Lite01 | Version::Lite02 | Version::Lite03 | Version::Lite04 | Version::Lite05 + ) => + { + tracing::warn!(group = hdr.sequence, "group past the declared end of track"); + let _ = producer.clone().abort(Error::ProtocolViolation); + return Err(Error::ProtocolViolation); + } + Err(err) => return Err(err), + }; + Ok(Opened { + group, + track: producer.clone(), + framing: *framing, + reading, + }) + } +} + impl Subscriber { pub fn new(config: SubscriberConfig) -> Self { // Identity for incoming-hop loop detection. Derived from the local @@ -123,6 +309,7 @@ impl Subscriber { version: config.version, peer_setup: config.peer_setup, setup_waits: Default::default(), + park_waits: Default::default(), cost: config.cost, sources: kio::Queue::new(), going_away: config.going_away, @@ -488,16 +675,26 @@ impl Subscriber { let mut subscribes = self.subscribes.lock(); // An unknown subscription's datagram is dropped below, however it parses. let dg = lite::Datagram::decode(payload, self.version, ietf, |id| { - subscribes.get(&id).map(|entry| entry.framing).unwrap_or_default() + subscribes + .get(&id) + .and_then(|entry| entry.copy.as_ref()) + .map(|copy| copy.framing) + .unwrap_or_default() })?; - let Some(entry) = subscribes.get_mut(&dg.subscribe) else { - // Unknown or already-closed subscription: drop the datagram. + // Unknown or already-closed subscription, or one whose TRACK_INFO has not landed + // yet, which a datagram cannot wait for: drop the datagram. + let Some(TrackEntry { + copy: Some(Accepted { producer, framing }), + tail, + .. + }) = subscribes.get_mut(&dg.subscribe) + else { return Ok(()); }; let timestamp = dg .timestamp - .zip(entry.framing.timescale) + .zip(framing.timescale) .map(|(value, scale)| Timestamp::new(value, scale)) .transpose() .map_err(|_| Error::BoundsExceeded(crate::coding::BoundsExceeded))? @@ -505,12 +702,12 @@ impl Subscriber { .filter(|_| self.version.has_media_time()); // A datagram is never owed a stream, so it never holds the subscription's end open. - if let Ok(mut tail) = entry.tail.write() { + if let Ok(mut tail) = tail.write() { tail.account(dg.sequence..dg.sequence.saturating_add(1), self.runtime.now()); } // A datagram says where the live feed is as well as a group does. Inserted first, so // the copy goes live with it already showing. - let live = entry.producer.is_live(); + let live = producer.is_live(); let datagram = crate::model::datagram::Stored { datagram: crate::Datagram { sequence: dg.sequence, @@ -521,12 +718,12 @@ impl Subscriber { deflated: None, }; match dg.inflated { - Some(size) => entry.producer.insert_deflated_datagram(datagram, size)?, - None => entry.producer.insert_wire_datagram(datagram)?, + Some(size) => producer.insert_deflated_datagram(datagram, size)?, + None => producer.insert_wire_datagram(datagram)?, } // A datagram-only feed may never answer with SUBSCRIBE_START, which waits for a group. if !live { - entry.producer.set_live(Some(Position::group(dg.sequence)), None); + producer.set_live(Some(Position::group(dg.sequence)), None); } Ok(()) } @@ -552,10 +749,15 @@ impl SubscriptionCleanup { /// and what arrived stays servable to readers still behind rather than handed to /// another route. fn abort(&self, err: &Error) { - for (_, entry) in self.0.lock().drain() { + for (_, mut entry) in self.0.lock().drain() { + // A track still waiting on TRACK_INFO has no copy yet: its request rejects with + // the session's end instead. + let Some(Accepted { producer, .. }) = entry.copy.take() else { + continue; + }; let _ = match entry.tail.read().is_ended() { - true => entry.producer.finish(), - false => entry.producer.abort(err.clone()), + true => producer.finish(), + false => producer.abort(err.clone()), }; } } @@ -622,6 +824,13 @@ impl SubscriberDriver { pub fn poll_drain(&mut self, waiter: &kio::Waiter) -> Poll<()> { let closed = self.uni.poll(waiter).is_ready(); let _ = self.uni.children.poll(waiter); + // Streams parked on a TRACK_INFO wait for their source to read it, which accepts them + // or ends their subscription. Either wakes this. + let parked = self.subscriber.park_waits.load(atomic::Ordering::Relaxed); + if closed && parked > 0 && self.uni.children.len() == parked { + let _ = self.sources.poll(waiter); + return Poll::Pending; + } // Once every remaining stream waits on a SETUP that never arrived, none of // them can deliver it: the peer can open no more. let stuck = self.subscriber.peer_setup.ietf().is_none() @@ -890,6 +1099,8 @@ struct GroupRecv { state: GroupRecvState, /// Held while parked on the peer's SETUP, so a drain can tell it never arrives. setup_wait: Option, + /// Held while parked on the subscription's TRACK_INFO, so a drain reads it first. + park_wait: Option, } /// Counts one GROUP stream parked on the peer's SETUP for as long as it lives. @@ -916,11 +1127,12 @@ impl GroupRecv { reader, state: GroupRecvState::Start, setup_wait: None, + park_wait: None, } } fn poll_serve(&mut self, waiter: &kio::Waiter) -> Poll> { - let (ietf, hdr) = wait!(Header => { + let header: (bool, lite::Group) = wait!(Header => { // Before the header, which a wait after it would lose. let Poll::Ready(ietf) = self.subscriber.peer_setup.poll_ietf(waiter) else { let waits = &self.subscriber.setup_waits; @@ -931,54 +1143,47 @@ impl GroupRecv { self.reader.poll_decode::(waiter).map_ok(|hdr| (ietf, hdr)) })?; - let (group, track, framing, reading) = { + // A SUBSCRIBE sent alongside its TRACK may be served before the TRACK_INFO that says + // how to read it: the stream parks, unread, until the accept opens its group. + let parked: Option<(kio::Consumer, u64)> = { let mut subs = self.subscriber.subscribes.lock(); - let entry = subs.get_mut(&hdr.subscribe).ok_or(Error::Cancel)?; + match subs.get_mut(&header.1.subscribe) { + Some(entry) if entry.copy.is_none() => { + self.park_wait = Some(ParkWait::new(&self.subscriber.park_waits)); + Some(entry.park(&header.1)) + } + _ => None, + } + }; + // Closed once the subscription went, which the lookup below reports. + let _ = wait!(Accept => match parked { + Some((parked, ticket)) => parked + .poll(waiter, |parked| match parked.opened.contains_key(ticket) { + true => Poll::Ready(()), + false => Poll::Pending, + }) + .map(|res| res.is_ok()), + None => Poll::Ready(true), + }); + self.park_wait = None; + let (ietf, hdr) = header; - let group_info = group::Info { sequence: hdr.sequence }; - // Stats (groups/frames/bytes) are counted in the model as the group - // is written, through the tagged `track::Producer`. The track counts - // it as content once its first frame lands. - let received = entry.producer.create_group_at(group_info, hdr.frame_start); - // The subscription's end waits until this stream is read. Counted - // whatever the cache said, since the publisher counts every stream it - // opens (a resubscribe resends a cached group), and only after the - // cache holds the group, so a fetch held for the feed that sees it - // accounted also finds it. - let reading = Reading::open(&entry.tail, Some(hdr.sequence), self.subscriber.runtime.now()); - // A route's first group says where its live feed is, when its answer does - // not: even one the cache already holds, as an idle copy asked from its - // head gets back. The copy goes live once the cache shows that group. - // Lite-07's answer does, and a group can overtake it: on a descending - // track the first to land is the newest, not where the feed starts. - if !entry.producer.is_live() && !self.subscriber.version.has_largest() { - entry.producer.set_live( - Some(Position { - group: hdr.sequence, - frame: hdr.frame_start, - }), - None, - ); + let opened = { + let mut subs = self.subscriber.subscribes.lock(); + let entry = subs.get_mut(&hdr.subscribe).ok_or(Error::Cancel)?; + match &parked { + Some((_, ticket)) => entry.unpark(*ticket), + None => entry.open(&hdr, self.subscriber.version, self.subscriber.runtime.now()), } - let group = match received { - Ok(group) => group, - // The group is at or past the end the publisher declared, which no - // later stream can repair. Before lite-05 only a local finish sets - // an end, and lite-05 specified an inclusive one, so its last - // group lands on it: drop only that stream there. - Err(Error::Closed) - if !matches!( - self.subscriber.version, - Version::Lite01 | Version::Lite02 | Version::Lite03 | Version::Lite04 | Version::Lite05 - ) => - { - tracing::warn!(group = hdr.sequence, "group past the declared end of track"); - let _ = entry.producer.clone().abort(Error::ProtocolViolation); - return Poll::Ready(Err(Error::ProtocolViolation)); - } - Err(err) => return Poll::Ready(Err(err)), - }; - (group, entry.producer.clone(), entry.framing, reading) + }; + let Opened { + group, + track, + framing, + reading, + } = match opened { + Ok(opened) => opened, + Err(err) => return Poll::Ready(Err(err)), }; let (priority, order) = track .subscription() @@ -986,9 +1191,8 @@ impl GroupRecv { self.reader .set_priority(crate::priority::recv_group(priority, hdr.sequence, order)); - // The timescale came from TRACK_INFO (read before this subscription was - // even registered), so frames decode immediately. No SUBSCRIBE_OK to - // wait on. + // The timescale came from TRACK_INFO, so frames decode immediately. No + // SUBSCRIBE_OK to wait on. // Guarded: dropping this machine mid-group is a cancellation, not a clean end. let group: crate::recv::Group = crate::recv::Group::new(group); let track: track::Producer = track; @@ -2896,15 +3100,15 @@ mod tests { let mut consumer = track.consume().subscribe(None).await.unwrap(); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track, - framing: lite::Framing { + TrackEntry::accepted( + track, + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - }, + Default::default(), + ), ); let mut accept = UniAccept::new(subscriber, early); assert!(accept.poll(&kio::Waiter::noop()).is_pending()); @@ -2956,15 +3160,15 @@ mod tests { track.finish_at(Position::group(3)).unwrap(); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track.clone(), - framing: lite::Framing { + TrackEntry::accepted( + track.clone(), + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - }, + Default::default(), + ), ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); @@ -3057,14 +3261,10 @@ mod tests { "video", info, ); - subscriber.subscribes.lock().insert( - 7, - TrackEntry { - producer: track.clone(), - framing, - tail: Default::default(), - }, - ); + subscriber + .subscribes + .lock() + .insert(7, TrackEntry::accepted(track.clone(), framing, Default::default())); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let mut group = GroupRecv::new( @@ -3242,15 +3442,15 @@ mod tests { before.finish().unwrap(); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track.clone(), - framing: lite::Framing { + TrackEntry::accepted( + track.clone(), + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - }, + Default::default(), + ), ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); @@ -3352,15 +3552,15 @@ mod tests { let tail = kio::Producer::new(Tail::default()); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track.clone(), - framing: lite::Framing { + TrackEntry::accepted( + track.clone(), + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: tail.clone(), - }, + tail.clone(), + ), ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); @@ -3830,15 +4030,15 @@ mod tests { let mut received = producer.subscribe(None); subscriber.subscribes.lock().insert( 7, - TrackEntry { + TrackEntry::accepted( producer, - framing: lite::Framing { + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - }, + Default::default(), + ), ); let payload = |sequence, size| { @@ -4087,6 +4287,221 @@ mod tests { } } + /// A subscriber on `version` over `session`, whose peer's SETUP was already read. + fn pipelining(session: crate::driver::Transport, version: Version) -> Subscriber { + let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); + peer_setup.set(lite::Setup::default()); + Subscriber::new(SubscriberConfig { + runtime: crate::time::sim(), + session, + origin: origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + recv_bandwidth: None, + version, + peer_setup, + peer_hop: None, + cost: None, + going_away: Default::default(), + auth: crate::auth::Link::new(false), + }) + } + + /// A subscription to `video` over `subscriber`, waiting on the run that serves it. + fn subscribing( + subscriber: &Subscriber, + subscription: Subscription, + ) -> (kio::Pending, TrackServeRun) { + let broadcast = crate::broadcast::Info::new().produce(); + let mut dynamic = broadcast.requests(); + let mut waiting = broadcast.consume().track("video").unwrap().subscribe(subscription); + assert!((&mut waiting).now_or_never().is_none(), "waiting on the publisher"); + let request = dynamic.requested_track().now_or_never().unwrap().expect("request"); + let serve = TrackServe { + validity: Default::default(), + route: None, + subscriber: subscriber.clone(), + path: Path::from_static("room"), + epoch: None, + name: "video".to_string(), + }; + (waiting, TrackServeRun::new(serve, request)) + } + + /// What `session` wrote on its `stream`th send stream. + fn sent(log: &crate::lite::test_transport::Log, stream: usize) -> Vec { + log.trail() + .into_iter() + .filter(|(id, _)| *id == stream) + .filter_map(|(_, sent)| match sent { + crate::lite::test_transport::Sent::Write(bytes) => Some(bytes), + _ => None, + }) + .flatten() + .collect() + } + + /// The SUBSCRIBE a request stream carried, and whether anything followed it. + fn sent_subscribe(wire: &[u8], version: Version) -> (lite::Subscribe<'static>, bool) { + let mut wire = wire; + assert_eq!( + crate::coding::decode_buf(&mut wire, version, |r, v| lite::ControlType::decode(r, v)).unwrap(), + lite::ControlType::Subscribe + ); + let msg = crate::coding::decode_from(&mut wire, version, lite::Subscribe::decode).unwrap(); + (msg.into_owned(), !wire.is_empty()) + } + + /// On lite-05 and later the SUBSCRIBE goes out beside its TRACK, registered before the + /// TRACK_INFO its copy is accepted with. + #[moq_clock::test] + async fn a_subscribe_goes_out_with_its_track() { + for version in [Version::Lite05, Version::Lite06, Version::Lite07] { + let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![Vec::new(), Vec::new()]); + let subscriber = pipelining(crate::driver::Transport::new(session.clone()), version); + let (mut waiting, mut run) = subscribing(&subscriber, Subscription::default().with_priority(3)); + assert!(kio::Task::poll(&mut run, &kio::Waiter::noop()).is_pending()); + + let track = sent(&session.log, 0); + let mut track = track.as_slice(); + assert_eq!( + crate::coding::decode_buf(&mut track, version, |r, v| lite::ControlType::decode(r, v)).unwrap(), + lite::ControlType::Track, + "{version}: the TRACK opens first" + ); + let (msg, trailing) = sent_subscribe(&sent(&session.log, 1), version); + assert_eq!((msg.id, msg.track.as_ref(), msg.priority), (0, "video", 3), "{version}"); + assert!(!trailing, "{version}"); + assert!( + subscriber.subscribes.lock().get(&0).is_some_and(|entry| entry.copy.is_none()), + "{version}: registered with its copy pending" + ); + assert!((&mut waiting).now_or_never().is_none(), "{version}: no TRACK_INFO yet"); + } + } + + /// Accepting the TRACK_INFO sends no SUBSCRIBE_UPDATE, even when the publisher keeps + /// groups for less than the subscriber waits: the publisher applies its own max age. + #[moq_clock::test] + async fn accepting_track_info_sends_no_subscribe_update() { + for version in [Version::Lite05, Version::Lite06, Version::Lite07] { + let mut info = Vec::new(); + lite::TrackInfo { + refresh: None, + priority: 0, + max_age: Some(Duration::from_secs(1)), + timescale: Some(Timescale::MILLI), + compress: false, + } + .encode(&mut crate::coding::Encoder::new(&mut info, version.into()), version) + .unwrap(); + let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![info, Vec::new()]); + let subscriber = pipelining(crate::driver::Transport::new(session.clone()), version); + let wait = Duration::from_secs(10); + let (waiting, mut run) = subscribing(&subscriber, Subscription::default().with_max_delay(wait)); + for _ in 0..4 { + let _ = kio::Task::poll(&mut run, &kio::Waiter::noop()); + } + waiting.now_or_never().expect("accepted").unwrap(); + + let (msg, trailing) = sent_subscribe(&sent(&session.log, 1), version); + assert_eq!(msg.max_delay, wait, "{version}: the subscriber's own max delay"); + assert!(!trailing, "{version}: a SUBSCRIBE_UPDATE followed the accept"); + } + } + + /// A TRACK reset while its SUBSCRIBE is live rejects the request, so the origin can try + /// another route, and resets the SUBSCRIBE. + #[moq_clock::test] + async fn a_reset_track_rejects_the_request_and_resets_its_subscribe() { + for version in [Version::Lite05, Version::Lite06, Version::Lite07] { + let session = crate::lite::test_transport::ScriptedSession::per_stream_reset(vec![Vec::new(), Vec::new()]); + let subscriber = pipelining(crate::driver::Transport::new(session.clone()), version); + let (waiting, mut run) = subscribing(&subscriber, Subscription::default()); + assert!(kio::Task::poll(&mut run, &kio::Waiter::noop()).is_ready()); + + assert!(waiting.now_or_never().expect("rejected").is_err(), "{version}"); + assert!( + session + .log + .trail() + .iter() + .any(|(stream, sent)| *stream == 1 && matches!(sent, crate::lite::test_transport::Sent::Reset(_))), + "{version}: the SUBSCRIBE was not reset" + ); + assert!(subscriber.subscribes.lock().is_empty(), "{version}"); + } + } + + /// A group stream that beats its subscription's TRACK_INFO waits after its header, + /// unread, and the accept opens the groups that waited in the order they arrived. + #[moq_clock::test] + async fn a_group_that_beats_its_track_info_waits_for_it() { + let version = Version::Lite07; + let group = |sequence: u64, payload: u8| { + let mut bytes = Vec::new(); + let mut w = crate::coding::Encoder::new(&mut bytes, version.into()); + lite::DataType::Group.encode(&mut w, version).unwrap(); + lite::Group { + subscribe: 0, + sequence, + frame_start: 0, + } + .encode(&mut w, version) + .unwrap(); + w.varint(0).unwrap(); + w.varint(1).unwrap(); + w.u8(payload); + bytes + }; + let session = crate::lite::test_transport::ScriptedSession::eof(Vec::new()) + .with_incoming_unis(vec![group(3, 30), group(2, 20)]); + let subscriber = pipelining(crate::driver::Transport::new(session.clone()), version); + subscriber + .subscribes + .lock() + .insert(0, TrackEntry::new(None, Default::default())); + let mut accept = UniAccept::new(subscriber.clone(), Vec::new()); + assert!(accept.poll(&kio::Waiter::noop()).is_pending()); + assert_eq!(subscriber.park_waits.load(atomic::Ordering::Relaxed), 2); + assert!(session.log.stops().is_empty(), "a parked stream is kept"); + + let track = crate::track::Producer::new( + Arc::new(crate::broadcast::Info::default()), + "video", + crate::track::Info::default().with_timescale(Timescale::MILLI), + ); + // Every group, oldest first, however late. + let every = Subscription::default() + .with_order(crate::track::Order::Ascending) + .with_start(0) + .with_max_delay(Duration::from_secs(60)); + let mut consumer = track.consume().subscribe(every).await.unwrap(); + let framing = lite::Framing::new(track.info(), version); + subscriber.subscribes.lock().get_mut(&0).unwrap().accept( + Accepted { + producer: track.clone(), + framing, + }, + version, + subscriber.runtime.now(), + ); + // Both opened at the accept, the first to arrive first. + assert_eq!(track.latest_sequence(), Some(3)); + + assert!(accept.poll(&kio::Waiter::noop()).is_pending()); + assert_eq!(subscriber.park_waits.load(atomic::Ordering::Relaxed), 0); + let mut read = Vec::new(); + for _ in 0..2 { + let mut group = consumer.recv_group().await.unwrap().unwrap(); + read.push((group.sequence, group.read_frame().await.unwrap().unwrap().payload[0])); + } + read.sort_unstable(); + assert_eq!(read, [(2, 20), (3, 30)]); + assert!( + subscriber.subscribes.lock().get(&0).unwrap().parked.is_none(), + "released once every stream took its group" + ); + } + /// A lite-07 source of `room` over `session`, a consumer to list it through, and the /// route that keeps it served. fn listed_source( @@ -6134,15 +6549,15 @@ mod tests { ); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track.clone(), - framing: lite::Framing { + TrackEntry::accepted( + track.clone(), + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - }, + Default::default(), + ), ); let mut held = Vec::new(); for start in (1..=8).rev() { @@ -6709,39 +7124,8 @@ impl TrackServe { pref: Option, supports_update: bool, ) -> Result { - // An empty half-open range asks for nothing, and the wire cannot say that: its - // bounds are inclusive, so the nearest encoding either hands back the position - // the caller excluded or inverts the range outright once the two bounds meet. No - // demand at all is the faithful translation. An absent start is the live edge, wherever that lands, so the only end that is - // certainly empty is the very first position, which is what `Position::default()` - // stands in for. - let pref = pref.filter(|sub| sub.end().is_none_or(|end| end > sub.start().unwrap_or_default())); - let ranged = self.subscriber.version.has_ranges(); - let request = match pref { - Some(subscription) => { - // Lite-07 carries every range asked for; an older wire one span covering them. - let mut subscription = match ranged { - true => subscription, - false => wire_span(subscription), - }; - // An idle copy is asked from the head of the newest group it cached when - // the subscriber has no start of its own: a quiet track (a catalog) sends - // that group again, which says the cache is current. A lite-07 list keeps - // any range of its own, so only one asking just for the live or oldest group - // counts. - let idle = subscription.start().is_none() - && (!ranged - || subscription - .ranges - .iter() - .all(|range| range.end.is_none() && matches!(range.start, Anchor::Relative(0 | u64::MAX)))); - if matches!(sub, Sub::None) && idle { - subscription = subscription.with_start(producer.idle_newest()); - } - wire_ranges(&subscription, producer.info().timescale)?.map(|ranges| (subscription, ranges)) - } - None => None, - }; + let opening = matches!(sub, Sub::None); + let request = self.wire_request(pref, opening, producer.idle_newest(), producer.info().timescale)?; match request { Some((subscription, ranges)) => { @@ -6807,15 +7191,71 @@ impl TrackServe { } } + /// What a SUBSCRIBE carries for the demand `pref`: the subscription and its ranges, or + /// `None` when it asks for nothing. + /// + /// `opening` when this opens a subscription for a copy whose newest cached group, while + /// idle, is `newest`. `timescale` is the track's, which only a time anchor needs. + fn wire_request( + &self, + pref: Option, + opening: bool, + newest: Option, + timescale: Option, + ) -> Result)>, Error> { + // An empty half-open range asks for nothing, and the wire cannot say that: its + // bounds are inclusive, so the nearest encoding either hands back the position + // the caller excluded or inverts the range outright once the two bounds meet. No + // demand at all is the faithful translation. An absent start is the live edge, wherever that lands, so the only end that is + // certainly empty is the very first position, which is what `Position::default()` + // stands in for. + let Some(subscription) = pref.filter(|sub| sub.end().is_none_or(|end| end > sub.start().unwrap_or_default())) + else { + return Ok(None); + }; + let ranged = self.subscriber.version.has_ranges(); + // Lite-07 carries every range asked for; an older wire one span covering them. + let mut subscription = match ranged { + true => subscription, + false => wire_span(subscription), + }; + // An idle copy is asked from the head of the newest group it cached when + // the subscriber has no start of its own: a quiet track (a catalog) sends + // that group again, which says the cache is current. A lite-07 list keeps + // any range of its own, so only one asking just for the live or oldest group + // counts. + let idle = subscription.start().is_none() + && (!ranged + || subscription + .ranges + .iter() + .all(|range| range.end.is_none() && matches!(range.start, Anchor::Relative(0 | u64::MAX)))); + if opening && idle { + subscription = subscription.with_start(newest); + } + Ok(wire_ranges(&subscription, timescale)?.map(|ranges| (subscription, ranges))) + } + + /// Open the SUBSCRIBE for `pref` alongside the TRACK still waiting on its TRACK_INFO, + /// registered with its copy pending. `None` when it asks for nothing, or names a time + /// that needs the track's timescale, so it waits for the info. + fn pipeline(&self, pref: Subscription) -> Result, Error> { + // A copy not accepted yet caches nothing. + let (subscription, ranges) = match self.wire_request(Some(pref.clone()), true, None, None) { + Ok(Some(request)) => request, + Ok(None) | Err(Error::TimestampMismatch) => return Ok(None), + Err(err) => return Err(err), + }; + let establish = self.register(subscription, ranges, None); + Ok(Some(Pipelined { + pref, + state: PipelinedState::Opening(establish), + })) + } + /// Allocate the id, set the demand floor, and register the subscription, so the /// returned [`Establish`] can put the SUBSCRIBE on the wire. /// - /// Registration happens here, before any of it reaches the transport: `id` is - /// live the moment the peer reads it, and a publisher may serve its first group - /// immediately, so a late insert races the group stream (a group whose id isn't - /// in the map yet is dropped, stalling the track forever). The caller - /// deregisters `id` if the establish fails. - /// /// The subscription's bounds come straight from the demand aggregate. fn prepare_establish( &self, @@ -6823,30 +7263,63 @@ impl TrackServe { subscription: Subscription, ranges: Vec, ) -> Establish { - let id = self.subscriber.next_id.fetch_add(1, atomic::Ordering::Relaxed); + self.request_floor(producer, subscription.start()); + let framing = lite::Framing::new(producer.info(), self.subscriber.version); + self.register( + subscription, + ranges, + Some(Accepted { + producer: producer.clone(), + framing, + }), + ) + } - // The floor tracks the requested start until this subscription's own - // SUBSCRIBE_START refines it: the peer never serves below the request, a - // previous subscription's declaration must not outlive its demand, and - // live-edge demand (None) starts with no floor at all. Lite-06+ only resolves - // the start with its SUBSCRIBE_START, so until then the floor is just a request. - let floor = subscription.start(); + /// The floor tracks the requested start until this subscription's own + /// SUBSCRIBE_START refines it: the peer never serves below the request, a + /// previous subscription's declaration must not outlive its demand, and + /// live-edge demand (None) starts with no floor at all. Lite-06+ only resolves + /// the start with its SUBSCRIBE_START, so until then the floor is just a request. + fn request_floor(&self, producer: &mut track::Producer, floor: Option) { let _ = match self.subscriber.version.resolves_start() { true => producer.request_start(floor), false => producer.start_at(floor), }; + } + + /// Give a pipelined subscription its accepted copy, the target its parked group streams + /// resume into. + fn accept_pipelined(&self, id: u64, producer: &track::Producer) { + let framing = lite::Framing::new(producer.info(), self.subscriber.version); + let mut subscribes = self.subscriber.subscribes.lock(); + let Some(entry) = subscribes.get_mut(&id) else { + return; + }; + let copy = Accepted { + producer: producer.clone(), + framing, + }; + entry.accept(copy, self.subscriber.version, self.subscriber.runtime.now()); + } + + /// Allocate the id and register the subscription, so the returned [`Establish`] can + /// put the SUBSCRIBE on the wire. + /// + /// Registration happens here, before any of it reaches the transport: `id` is + /// live the moment the peer reads it, and a publisher may serve its first group + /// immediately, so a late insert races the group stream (a group whose id isn't + /// in the map yet is dropped, stalling the track forever). The caller + /// deregisters `id` if the establish fails. + fn register(&self, subscription: Subscription, ranges: Vec, copy: Option) -> Establish { + let id = self.subscriber.next_id.fetch_add(1, atomic::Ordering::Relaxed); tracing::debug!(id, broadcast = %self.subscriber.log_path(&self.path), track = %self.name, "subscribe started"); let tail = kio::Producer::new(Tail::new(tail::grace(subscription.budget()))); - self.subscriber.subscribes.lock().insert( - id, - TrackEntry { - producer: producer.clone(), - framing: lite::Framing::new(producer.info(), self.subscriber.version), - tail: tail.clone(), - }, - ); + self.subscriber + .subscribes + .lock() + .insert(id, TrackEntry::new(copy, tail.clone())); let session = self.subscriber.session.clone(); Establish { @@ -7047,6 +7520,57 @@ impl Establish { } } +/// A SUBSCRIBE sent alongside its TRACK, before the TRACK_INFO its copy is accepted with. +struct Pipelined { + /// The demand it asked for, which later demand changes are measured against. + pref: Subscription, + state: PipelinedState, +} + +#[expect( + clippy::large_enum_variant, + reason = "a state machine's enum is its storage: one transient instance per stream, so the big variant is the working state, not padding held in bulk" +)] +enum PipelinedState { + Opening(Establish), + Open(SubStream), +} + +impl Pipelined { + fn id(&self) -> u64 { + match &self.state { + PipelinedState::Opening(establish) => establish.id, + PipelinedState::Open(active) => active.id, + } + } + + /// Drive the SUBSCRIBE onto the wire; its responses wait for the accept. Only a failure + /// to send it is ready. + fn poll(&mut self, waiter: &kio::Waiter) -> Poll { + let PipelinedState::Opening(establish) = &mut self.state else { + return Poll::Pending; + }; + match ready!(establish.poll(waiter)) { + Ok(active) => { + self.state = PipelinedState::Open(active); + Poll::Pending + } + Err(err) => Poll::Ready(err), + } + } + + /// Give up on the subscription with its track, resetting it with `err`. + fn abort(self, serve: &TrackServe, err: &Error) { + match self.state { + PipelinedState::Opening(establish) => establish.abort(err), + PipelinedState::Open(active) => { + serve.subscriber.remove_subscribe(active.id); + active.stream.writer.abort(err); + } + } + } +} + /// Drives one [`TrackServe`]: the TRACK_INFO fetch, then the serve loop, then /// the teardown that decides how the origin sees this copy end. struct TrackServeRun { @@ -7062,11 +7586,14 @@ struct TrackServeRun { )] enum TrackRunState { /// Lite05+ learns the track's immutable properties once, up front, via a - /// TRACK stream. The timescale then flows into every SUBSCRIBE and FETCH - /// without a per-response header. + /// TRACK stream. The timescale then flows into every FETCH without a + /// per-response header. A SUBSCRIBE goes out alongside, rather than a round + /// trip later: it needs the info only to read what it receives. Info { request: Option, info: TrackInfoFetch, + /// The SUBSCRIBE sent alongside, once anyone subscribes. + pipelined: Option, }, Serve(ServeLoop), /// Preserve the lite07 completion FIN until the publisher acknowledges it. @@ -7080,6 +7607,7 @@ impl TrackServeRun { TrackRunState::Info { request: Some(request), info: TrackInfoFetch::new(&serve), + pipelined: None, } } else { // Older wires declare no publisher retention limit, and no timeline: their @@ -7096,10 +7624,14 @@ impl TrackServeRun { fn revoke(&mut self) { tracing::info!(broadcast = %self.serve.subscriber.log_path(&self.serve.path), track = %self.serve.name, "subscription no longer authorized"); match std::mem::replace(&mut self.state, TrackRunState::Done) { - TrackRunState::Info { request, .. } => { + TrackRunState::Info { request, pipelined, .. } => { if let Some(request) = request { request.reject(Error::Unauthorized); } + // Reset like a live one. + if let Some(pipelined) = pipelined { + pipelined.abort(&self.serve, &Error::Unauthorized); + } } TrackRunState::Serve(serve_loop) => { let _ = serve_loop.serving.abort(Error::Unauthorized); @@ -7132,7 +7664,13 @@ impl TrackServeRun { let pending = request.as_ref().expect("request pending"); ready!(crate::util::poll_unwanted(&mut pending.demand(), waiter)); if pending.reject_unused(Error::Cancel) { - self.state = TrackRunState::Done; + if let TrackRunState::Info { + pipelined: Some(pipelined), + .. + } = std::mem::replace(&mut self.state, TrackRunState::Done) + { + pipelined.abort(&self.serve, &Error::Cancel); + } return Poll::Ready(()); } // Demand returned in the gap and won inside `reject_unused`: poll again so the @@ -7162,10 +7700,33 @@ impl kio::Task for TrackServeRun { } loop { match &mut self.state { - TrackRunState::Info { request, info } => { + TrackRunState::Info { + request, + info, + pipelined, + } => { // Abandonment was checked at the top of this poll, arming its wait. - let res = ready!(info.poll_fetch(&self.serve, waiter)); + let pending = request.as_mut().expect("request pending"); + // Demand that arrives before the info goes upstream with it, the TRACK + // first. One that needs the info to say what it asks for waits for it. + let mut started = Ok(()); + if pipelined.is_none() + && let Poll::Ready(Some(pref)) = pending.poll_subscription_changed(waiter) + { + started = self.serve.pipeline(pref).map(|subscribing| *pipelined = subscribing); + } + let fetched = info.poll_fetch(&self.serve, waiter); + let failed = match pipelined { + Some(subscribing) => subscribing.poll(waiter), + None => Poll::Pending, + }; + let res = match (started, failed, fetched) { + (Err(err), _, _) | (_, Poll::Ready(err), _) => Err(err), + (_, _, Poll::Ready(res)) => res, + (_, _, Poll::Pending) => return Poll::Pending, + }; let request = request.take().expect("request pending"); + let pipelined = pipelined.take(); match res { Ok((mut info, held)) => { // Lite05/06 timestamps may be send times, so the copy is untimed @@ -7175,6 +7736,9 @@ impl kio::Task for TrackServeRun { } let mut serve_loop = ServeLoop::new(&self.serve, request, info); serve_loop.held = Some(held); + if let Some(pipelined) = pipelined { + serve_loop.adopt(&self.serve, pipelined); + } self.state = TrackRunState::Serve(serve_loop); } Err(err) => { @@ -7182,7 +7746,10 @@ impl kio::Task for TrackServeRun { tracing::warn!(broadcast = %self.serve.subscriber.log_path(&self.serve.path), track = %self.serve.name, %err, "track info failed"); // Rejecting the request lets the origin retry (bounded) on // another source; waiting subscribers stall rather than error - // meanwhile. + // meanwhile. The SUBSCRIBE sent alongside ends with it. + if let Some(pipelined) = pipelined { + pipelined.abort(&self.serve, &err); + } request.reject(err); self.state = TrackRunState::Done; return Poll::Ready(()); @@ -7241,15 +7808,19 @@ impl kio::Task for TrackServeRun { impl Drop for TrackServeRun { fn drop(&mut self) { - // Still waiting on TRACK_INFO: the request never reached the subscribe map, - // so the driver's abort cannot see it. Reject with the session's error. - let TrackRunState::Info { request, .. } = &mut self.state else { + // Still waiting on TRACK_INFO: the request has no copy for the driver's abort to + // end. Reject with the session's error. + let TrackRunState::Info { request, pipelined, .. } = &mut self.state else { return; }; let Some(request) = request.take() else { return; }; - request.reject(self.serve.subscriber.end_reason()); + let err = self.serve.subscriber.end_reason(); + if let Some(pipelined) = pipelined.take() { + pipelined.abort(&self.serve, &err); + } + request.reject(err); } } @@ -7283,12 +7854,10 @@ impl TrackInfoFetch { ) -> Poll> { #[every_poll] self.validity.check(waiter)?; + // A withdrawn route fails an info still owed. One already delivered stays usable, like + // one read before the withdrawal: a SUBSCRIBE sent alongside may already be served. #[every_poll] - if serve - .route - .as_ref() - .is_some_and(|route| route.poll_closed(waiter).is_ready()) - { + if !matches!(self.state, TrackInfoState::Read(_)) && withdrawn(serve, waiter) { return Poll::Ready(Err(Error::Unroutable)); } @@ -7307,8 +7876,14 @@ impl TrackInfoFetch { if let Poll::Ready(err) = self.closed.poll_closed(waiter) { return Poll::Ready(Err(crate::lite::error::from_transport(err))); } - let ietf = ready!(serve.subscriber.peer_setup.poll_ietf(waiter)); - stream.reader.poll_decode::(waiter).map_ok(|info| (ietf, info)) + let read = match serve.subscriber.peer_setup.poll_ietf(waiter) { + Poll::Ready(ietf) => stream.reader.poll_decode::(waiter).map_ok(|info| (ietf, info)), + Poll::Pending => Poll::Pending, + }; + if read.is_pending() && withdrawn(serve, waiter) { + return Poll::Ready(Err(Error::Unroutable)); + } + read })?; // Publisher Max Age rides on the wire, so the local retention @@ -7336,6 +7911,28 @@ impl TrackInfoFetch { } } +/// Ready once no group stream that beat `sub`'s accept still waits to be read: they arrived +/// before whatever ends the subscription, so they are delivered first. +fn poll_unparked(sub: &Sub, serve: &TrackServe, waiter: &kio::Waiter) -> Poll<()> { + let Sub::Active(active) = sub else { + return Poll::Ready(()); + }; + match serve.subscriber.subscribes.lock().get(&active.id) { + Some(TrackEntry { + parked: Some(parked), .. + }) => parked.poll_closed(waiter), + _ => Poll::Ready(()), + } +} + +/// Whether the advertisement that minted `serve`'s source was withdrawn. +fn withdrawn(serve: &TrackServe, waiter: &kio::Waiter) -> bool { + serve + .route + .as_ref() + .is_some_and(|route| route.poll_closed(waiter).is_ready()) +} + /// An answered TRACK stream, kept open as interest in the track: the publisher holds the /// track for us until we FIN it, which dropping this does. Held until the SUBSCRIBE that /// follows has its first response, or until nobody wants this copy, so demand never @@ -7385,6 +7982,10 @@ struct ServeLoop { /// The ranges of a bounded lite-07 subscription the publisher covered, while the /// demand still asks for just them: subscribing again would only resend them. covered: Option>, + /// A pipelined subscription whose group streams stay parked until the responses that + /// beat the accept are read, as they would have been without the wait: a + /// SUBSCRIBE_START says where the feed starts before its first group does. + parked: Option, /// Our side of the covered subscription, finished and held until the publisher reads /// the FIN, since dropping it would reset it. closing: Option>, @@ -7658,6 +8259,30 @@ impl ServeLoop { held: None, covered: None, closing: None, + parked: None, + } + } + + /// Resume the parked group streams into the accepted copy. + fn unpark(&mut self, serve: &TrackServe) { + if let Some(id) = self.parked.take() { + serve.accept_pipelined(id, &self.serving); + } + } + + /// Take over the SUBSCRIBE sent alongside the TRACK, now that its copy is accepted. Its + /// demand counts as asked for, so only a change since goes out as an update. + fn adopt(&mut self, serve: &TrackServe, pipelined: Pipelined) { + let floor = match &pipelined.state { + PipelinedState::Opening(establish) => establish.subscription.start(), + PipelinedState::Open(active) => active.requested, + }; + serve.request_floor(&mut self.serving, floor); + self.parked = Some(pipelined.id()); + self.serving.seen_subscription(pipelined.pref); + match pipelined.state { + PipelinedState::Opening(establish) => self.mode = ServeMode::Establish(establish), + PipelinedState::Open(active) => self.sub = Sub::Active(active), } } @@ -7687,12 +8312,7 @@ impl ServeLoop { fn poll(&mut self, serve: &TrackServe, waiter: &kio::Waiter) -> Poll { // Immutable TRACK_INFO stays usable locally after withdrawal. Its wire hold // is only a bridge to SUBSCRIBE while this source is still advertised. - if self.held.is_some() - && serve - .route - .as_ref() - .is_some_and(|route| route.poll_closed(waiter).is_ready()) - { + if self.held.is_some() && withdrawn(serve, waiter) { self.held = None; } let mut budget = kio::coop::Budget::new(32); @@ -7713,6 +8333,10 @@ impl ServeLoop { } } ServeMode::Tail { settle, owed, track } => { + // The tail waits for groups, parked ones included. + if let Some(id) = self.parked.take() { + serve.accept_pipelined(id, &self.serving); + } let _ = self.fetches.poll(waiter); self.feed_fetches .poll(&self.sub, serve.subscriber.runtime.now(), waiter); @@ -7756,6 +8380,9 @@ impl ServeLoop { // finishes it, as `SubscriptionCleanup` does, rather than hand it to // another route. A copy whose subscription alone ended is handed back. if let Poll::Ready(err) = self.closed.poll_closed(waiter) { + if poll_unparked(&self.sub, serve, waiter).is_pending() { + return Poll::Pending; + } return Poll::Ready(match track { true => ServeEnd::Finished, false => ServeEnd::GiveBack(crate::lite::error::from_transport(err)), @@ -8053,9 +8680,15 @@ impl ServeLoop { } } + // Every response that beat the accept is read. + self.unpark(serve); + // (5) The session died: hand the track back for another route, with // the session's error in case none takes it. if let Poll::Ready(err) = self.closed.poll_closed(waiter) { + if poll_unparked(&self.sub, serve, waiter).is_pending() { + return Poll::Pending; + } return Poll::Ready(ServeEnd::GiveBack(crate::lite::error::from_transport(err))); } diff --git a/rs/moq-net/src/lite/test_transport.rs b/rs/moq-net/src/lite/test_transport.rs index a17bf539c6..9c9e8a7917 100644 --- a/rs/moq-net/src/lite/test_transport.rs +++ b/rs/moq-net/src/lite/test_transport.rs @@ -72,7 +72,7 @@ pub struct Log { closed_at: Arc>>, bi_opens: Arc, pub request_attempts: Arc, - priorities: Arc>>, + priorities: Arc>>, /// Hands each send group its id. group_ids: Arc, groups: Arc>>, @@ -137,7 +137,7 @@ impl Log { /// Every value handed to the transport's `set_priority`, in call order. These are /// trait-contract send orders (higher = transmitted first), recorded so tests can /// verify priority conversions at their protocol boundaries. - pub fn priorities(&self) -> Vec { + pub fn priorities(&self) -> Vec { self.priorities.lock().unwrap().clone() } @@ -342,7 +342,10 @@ impl poll::SendStream for SinkSend { self.log.orders.lock().unwrap().push((self.id, order)); return; } - let order = u8::try_from(order).expect("moq-net sends u8 send orders outside a group"); + assert!( + (0..=crate::coding::TRACK_INFO_SEND_ORDER).contains(&order), + "moq-net sends u8 send orders outside a group, and TRACK_INFO just above them" + ); self.log.priorities.lock().unwrap().push(order); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index da4102ad63..edd4d6bf37 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1251,17 +1251,24 @@ impl TrackState { self.lookup.get(&sequence)?.copies().find(|group| group.covers(frames)) } - /// The local retention bound, or `None` when unknown or unlimited. - /// Bounds the aggregate subscription without changing the publisher's metadata. + /// The local retention bound, or `None` when unknown or unlimited: how long a reader + /// may wait for a late group, without changing the publisher's metadata. fn max_age_bound(&self) -> Option { let published = self.info.as_ref()?.max_age; - let ceiling = self.broadcast.cache_duration; match published { - Some(age) => Some(age.min(ceiling)), - None => (ceiling != Duration::MAX).then_some(ceiling), + Some(age) => Some(age.min(self.broadcast.cache_duration)), + None => self.demand_bound(), } } + /// The bound on the aggregate subscription: the local cache ceiling alone, or `None` + /// when unlimited. It needs no info, so a track asks upstream for the same demand + /// before and after its info lands, and the publisher enforces its own max age. + fn demand_bound(&self) -> Option { + let ceiling = self.broadcast.cache_duration; + (ceiling != Duration::MAX).then_some(ceiling) + } + /// The live edge a subscription served `servable` measures drift against: the /// highest-sequence group in it that has presented a frame, or `None` while nothing /// stamped is servable (an unstamped track drives no media-time expiry; the wall @@ -3718,12 +3725,13 @@ impl Producer { /// when there are no live subscribers. Unlike [`Self::subscription`], this /// doesn't wait for a change or advance the change cursor. /// - /// The aggregate's [`Subscription::max_delay`] is clamped to this track's - /// [`Info::max_age`]: no subscriber can wait for a late group longer than the - /// publisher keeps it. + /// The aggregate's [`Subscription::max_delay`] is the subscribers' own, capped only by + /// the local cache ceiling ([`broadcast::Info::cache_duration`]). The publisher + /// enforces its own [`Info::max_age`], which each reader's lateness budget also + /// applies, so the demand asked upstream is the same before and after the info lands. pub fn subscription(&self) -> Option { let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); snapshot_subscription(&subs, bound) } @@ -3741,12 +3749,20 @@ impl Producer { // Read the bound before locking `subs`, so the aggregation never nests the two locks. let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); poll_combined_changed(&subs, bound, &mut self.prev_subscription, waiter).map(Ok) } + /// Count `seen` as the aggregate already acted on, so + /// [`Self::poll_subscription_changed`] reports only a change from it: the demand a + /// subscriber session asked upstream for before accepting the track. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn seen_subscription(&mut self, seen: Subscription) { + self.prev_subscription = Some(seen); + } + /// Create a [`Requests`] handle that serves on-demand fetches of uncached /// (old) groups. Most producers never need this; a relay creates one to fetch /// past groups from upstream. @@ -3981,15 +3997,18 @@ fn snapshot_subscription(subs: &kio::Lock, bound: Option) -> Duration { if let Some(bound) = bound { max_delay = max_delay.min(bound); @@ -3997,7 +4016,7 @@ fn clamp_max_delay(mut max_delay: Duration, bound: Option) -> Duration max_delay } -/// Clamp the aggregate's max delay budget to the publisher's window; see [`clamp_max_delay`]. +/// Clamp the aggregate's max delay budget to `bound`; see [`clamp_max_delay`]. fn clamp_combined(combined: Option, bound: Option) -> Option { let mut combined = combined?; if bound.is_some() { @@ -4083,7 +4102,7 @@ impl TrackWeak { /// The readers' aggregate demand and contribution count, or `None` without one. pub(super) fn subscription(&self) -> Option<(Subscription, usize)> { let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); let subs = subs.lock(); Some((clamp_combined(subs.combined(), bound)?, subs.len())) @@ -8184,7 +8203,7 @@ impl Request { /// or `None` if nobody is waiting. Useful for sizing the track before accepting. pub fn subscription(&self) -> Option { let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); snapshot_subscription(&subs, bound) } @@ -8199,7 +8218,7 @@ impl Request { /// Poll for a changed subscription. pub fn poll_subscription_changed(&mut self, waiter: &kio::Waiter) -> Poll> { let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); poll_combined_changed(&subs, bound, &mut self.prev_subscription, waiter) @@ -10590,10 +10609,10 @@ mod test { #[test] fn max_delay_clamped_to_cache() { - let producer = track_producer("test", Info::timed().with_max_age(Duration::from_secs(2))); + let producer = track_producer_capped("test", Info::timed(), Duration::from_secs(2)); // A max delay budget beyond the cache is capped in the aggregate; a group can't be - // waited for longer than the publisher keeps it. The subscriber's own preference + // waited for longer than the cache keeps it. The subscriber's own preference // is stored verbatim, so what it asked for stays readable. let mut subscriber = producer.subscribe(Subscription::default().with_max_delay(Duration::from_secs(10))); assert_eq!(subscriber.subscription().max_delay, Some(Duration::from_secs(10))); @@ -10682,7 +10701,7 @@ mod test { #[test] fn max_delay_clamped_via_every_update_path() { - let producer = track_producer("test", Info::timed().with_max_age(Duration::from_secs(2))); + let producer = track_producer_capped("test", Info::timed(), Duration::from_secs(2)); let over = Subscription::default().with_max_delay(Duration::from_secs(10)); // The clamp lives in the aggregation, so it applies no matter which entry point @@ -10699,7 +10718,7 @@ mod test { #[test] fn max_delay_aggregate_clamps_across_subscribers() { - let producer = track_producer("test", Info::timed().with_max_age(Duration::from_secs(2))); + let producer = track_producer_capped("test", Info::timed(), Duration::from_secs(2)); // The aggregate takes the max, then clamps once. Equivalent to clamping each // subscriber first, since `min` distributes over `max`. @@ -10709,6 +10728,36 @@ mod test { assert_eq!(producer.subscription().unwrap().max_delay, Some(Duration::from_secs(2))); } + /// The publisher enforces its own max age, so the demand asked of it is the subscribers' + /// own, and a request asks for the same before and after its info lands. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn the_aggregate_is_not_clamped_to_the_publisher_max_age() { + let broadcast = broadcast::Info { + cache_duration: Duration::from_secs(30), + ..Default::default() + } + .produce(); + let mut dynamic = broadcast.requests(); + let consumer = broadcast.consume(); + let mut waiting = std::pin::pin!( + consumer + .track("test") + .unwrap() + .subscribe(Subscription::default().with_max_delay(Duration::from_secs(60))) + ); + assert!(futures::poll!(waiting.as_mut()).is_pending()); + let request = dynamic.requested_track().await.unwrap(); + // Capped by the local ceiling alone. + let before = request.subscription().unwrap().max_delay; + assert_eq!(before, Some(Duration::from_secs(30))); + + let producer = request.accept(Info::timed().with_max_age(Duration::from_secs(2))); + assert_eq!(producer.subscription().unwrap().max_delay, before); + assert_eq!(producer.state.read().max_age_bound(), Some(Duration::from_secs(2))); + waiting.await.unwrap(); + } + #[test] fn churned_subscribers_do_not_accumulate() { let producer = track_producer("test", Info::timed()); @@ -11902,8 +11951,8 @@ mod test { #[cfg(feature = "async")] fn max_age_bounds_the_budget() { // The publisher only keeps a group around for 500ms, so a subscriber asking to - // wait ten seconds for one still gives up at 500ms: the same clamp the aggregate - // applies, on the subscriber's own side of it. + // wait ten seconds for one still gives up at 500ms: the window the publisher + // applies to the aggregate, on the subscriber's own side of it. let mut producer = track_producer("test", Info::timed().with_max_age(Duration::from_millis(500))); append_at(&mut producer, 0); append_at(&mut producer, 1000); diff --git a/rs/moq-net/tests/integration/finished_broadcast_mock.rs b/rs/moq-net/tests/integration/finished_broadcast_mock.rs index a611695599..c83078644f 100644 --- a/rs/moq-net/tests/integration/finished_broadcast_mock.rs +++ b/rs/moq-net/tests/integration/finished_broadcast_mock.rs @@ -55,14 +55,14 @@ async fn round(finish_broadcast: bool) -> (Vec>, Option) .expect("resolve timeout") .expect("broadcast resolves"); + // Resolved before the ending starts: a SUBSCRIBE sent alongside its TRACK reaches the + // publisher before the subscriber has the TRACK_INFO it accepts the track with. + let subscription = moq_net::track::Subscription::default().with_range(0..); + let mut sub = moq_clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(subscription)) + .await + .expect("subscribe timeout") + .expect("subscribe"); let reader = moq_clock::sim::spawn(async move { - let subscription = moq_net::track::Subscription::default().with_range(0..); - let mut sub = remote - .track("video") - .unwrap() - .subscribe(subscription) - .await - .expect("subscribe"); let mut got = Vec::new(); loop { let mut group = match sub.recv_group().await { From 2447b090c491ea95a430a9a45d230991c3561995 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 04:06:32 -0700 Subject: [PATCH 03/16] feat(js/net): send SUBSCRIBE alongside TRACK A lite-05+ subscriber writes its SUBSCRIBE while its TRACK is in flight. A group stream that beats the TRACK_INFO waits for it unread, a datagram is dropped, and a failed TRACK rejects the request and resets the SUBSCRIBE. The aggregate demand is no longer clamped to the track's maxAge, so accepting the info sends no update. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscriber.test.ts | 113 ++++++++++++++++++++-- js/net/src/lite/subscriber.ts | 145 ++++++++++++++++++---------- js/net/src/request-deadline.test.ts | 19 ++-- js/net/src/track.test.ts | 14 ++- js/net/src/track.ts | 7 +- 5 files changed, 226 insertions(+), 72 deletions(-) diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 372721173e..d4ce04ae9c 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -29,6 +29,7 @@ import { Probe } from "./probe.ts"; import { StreamId } from "./stream.ts"; import { encodeSubscribeResponse, + Subscribe, SubscribeDrop, SubscribeEnd, SubscribeOk, @@ -1087,7 +1088,7 @@ test.each([ }); // A setup that outlived its deadline is over: its TRACK stream is reset, even one still waiting for a -// slot, and the subscription is neither registered again nor sent as a SUBSCRIBE. +// slot, along with the SUBSCRIBE sent beside it, and the subscription is not registered again. test.each([ ["lite-05 subscribe waiting on the TRACK_INFO", Version.DRAFT_05, false], ["lite-06 subscribe waiting on the TRACK_INFO", Version.DRAFT_06, false], @@ -1109,7 +1110,8 @@ test.each([ if (parked) streams[0].release(); await streams[0].aborted; - expect(streams.length).toBe(1); + await streams[1].aborted; + expect(streams.length).toBe(2); // A GROUP for a forgotten id is ignored without touching its stream. const touched: PropertyKey[] = []; @@ -1183,6 +1185,104 @@ test("a lite subscribe whose SUBSCRIBE write stalls past the deadline closes its } }); +// On lite-05 and later a SUBSCRIBE goes out beside its TRACK, before the TRACK_INFO, and accepting +// the info sends no SUBSCRIBE_UPDATE even when the publisher keeps groups for less than the +// subscriber waits. +test.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])( + "lite %s: a subscribe goes out with its track", + async (version) => { + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const track = subscriber + .consume(Path.from("room")) + .track("video") + .subscribe({ priority: 3, maxDelay: Milli(10_000) }); + try { + await opened(0); + await streams[0].reading; + await (await opened(1)).writing; + const sent = new Reader(undefined, new Uint8Array(Buffer.concat(streams[0].written)), version); + expect(await sent.u53()).toBe(StreamId.Track); + await drainReactions(); + const wire = () => new Reader(undefined, new Uint8Array(Buffer.concat(streams[1].written)), version); + let subscribe = wire(); + expect(await subscribe.u53()).toBe(StreamId.Subscribe); + const msg = await Subscribe.decode(subscribe, version); + expect([msg.id, msg.track, msg.priority, msg.maxDelay]).toEqual([0n, "video", 3, 10_000]); + + const chunks: Uint8Array[] = []; + const writer = new Writer( + new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), + version, + ); + await new TrackInfo({ timescale: 1000, maxAge: 1000 }).encode(writer, version); + for (const chunk of chunks) streams[0].inbound.enqueue(chunk); + await track.info(); + await drainReactions(); + subscribe = wire(); + await subscribe.u53(); + await Subscribe.decode(subscribe, version); + expect(await subscribe.done()).toBe(true); + } finally { + track.close(); + subscriber.close(); + } + }, +); + +// A GROUP that beats its subscription's TRACK_INFO waits unread for it, then is read with it. +test("a group that beats its track info waits for it", async () => { + const version = Version.DRAFT_07; + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const track = subscriber.consume(Path.from("room")).track("video").subscribe(); + try { + await opened(0); + await (await opened(1)).writing; + const touched: PropertyKey[] = []; + const stream = await groupStream(0, version); + const reader = new Proxy(stream, { + get: (target, key) => { + touched.push(key); + const value = Reflect.get(target, key, target); + return typeof value === "function" ? value.bind(target) : value; + }, + }); + const receiving = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0 }), reader); + await drainReactions(); + expect(touched).toEqual([]); + + await answerTrackInfo(streams[0], version, 1000); + await receiving; + const group = await track.recvGroup(); + expect(group?.sequence).toBe(0); + expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([0])); + } finally { + track.close(); + subscriber.close(); + } +}); + +// A TRACK reset while its SUBSCRIBE is live fails the subscription, so the origin can try another +// route, and resets the SUBSCRIBE. +test("a reset track rejects the subscription and resets its subscribe", async () => { + const warn = spyOn(console, "warn").mockImplementation(() => {}); + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); + const track = subscriber.consume(Path.from("room")).track("video").subscribe(); + try { + await opened(0); + await (await opened(1)).writing; + streams[0].inbound.error(new Error("peer reset")); + expect(await track.closed).toBeInstanceOf(Error); + await streams[1].aborted; + } finally { + track.close(); + subscriber.close(); + warn.mockRestore(); + } +}); + test("a fetch started after the subscriber closes rejects without opening a stream", async () => { const { quic, streams } = fakeSession(); const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n)); @@ -1194,7 +1294,7 @@ test("a fetch started after the subscriber closes rejects without opening a stre }); // The grant watch is armed before the first check, so a shrink while TRACK_INFO is still in -// flight refuses the subscription rather than opening a SUBSCRIBE the grant no longer covers. +// flight refuses the subscription and resets the SUBSCRIBE sent beside its TRACK. test("a grant that shrinks while the subscription sets up refuses it", async () => { const { quic, streams, opened } = fakeSession(); const scoped = (prefix: string): Grant => ({ @@ -1223,8 +1323,8 @@ test("a grant that shrinks while the subscription sets up refuses it", async () const err = await next; expect(err).toBeInstanceOf(StreamError); expect((err as StreamError).code).toBe(StreamCode.Unauthorized); - // Nothing past the TRACK stream reached the wire. - expect(streams.length).toBe(1); + await streams[1].aborted; + expect(streams.length).toBe(2); track.close(); }); @@ -2513,8 +2613,9 @@ test("closing a broadcast while TRACK_INFO is pending leaves no unhandled reject broadcast.close(); track.close(); await streams[0].aborted; + await streams[1].aborted; await drainReactions(); - expect(streams).toHaveLength(1); + expect(streams).toHaveLength(2); expect(unhandled).toEqual([]); } finally { process.off("unhandledRejection", onUnhandled); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 1a009cf430..c2e21a9a90 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -175,6 +175,9 @@ export class Subscriber { // Registered only after immutable TRACK_INFO or the older drafts' defaults resolve, // so every incoming group and datagram already knows its timestamp framing. #subscribes = new Map(); + // A SUBSCRIBE sent alongside its TRACK, until its TRACK_INFO lands: settles with the + // entry, or `undefined` if it never does. + #pending = new Map>(); #subscribeNext = 0n; // Dedup consumed broadcasts per path: repeat consume() calls share one subscription. @@ -1049,16 +1052,16 @@ export class Subscriber { } } - // Determine the track's immutable properties, accept the request (so the - // application's track.Subscriber resolves and incoming groups have a producer to - // write into), register it, then open the subscribe stream. `state.stream` and - // `state.track` are populated as soon as each stream opens so the caller can clean - // them up on timeout even before this promise settles. + // Open the subscribe stream and accept the request (so the application's track.Subscriber + // resolves and incoming groups have a producer to write into) with the track's immutable + // properties. `state.stream` and `state.track` are populated as soon as each stream opens + // so the caller can clean them up on timeout even before this promise settles. // - // On lite-05+ the properties come from a TRACK stream opened first, held open until - // the SUBSCRIBE is answered, and the SUBSCRIBE is accepted implicitly (no - // SUBSCRIBE_OK). Older drafts carry no per-track properties, so they resolve to - // defaults and just drain SUBSCRIBE_OK. + // On lite-05+ the properties come from a TRACK stream, held open until the SUBSCRIBE is + // answered, and the SUBSCRIBE goes out alongside it rather than a round trip after: it is + // accepted implicitly (no SUBSCRIBE_OK), and needs the info only to read what it receives. + // A group stream that beats the info waits on `#pending`, unread. Older drafts carry no + // per-track properties, so they resolve to defaults and just drain SUBSCRIBE_OK. async #openSubscribe( state: SubscribeSetup, msg: Subscribe, @@ -1067,44 +1070,84 @@ export class Subscriber { deadline: Deadline, ): Promise<{ stream: Stream; entry: SubscribeEntry }> { state.cancel.signal.throwIfAborted(); - let producer: track.Producer; - let drainOk = false; - let compress = false; - let timescale = 0; - - if (hasTrackStream(this.version)) { - // Fetch the immutable properties once via the TRACK stream. - const { info, stream } = await this.#trackInfo( - msg.broadcast, - msg.epoch, - msg.track, - state.cancel.signal, - deadline, - ); - state.track = stream; + + if (!hasTrackStream(this.version)) { + // Older drafts negotiate nothing per-track: verbatim frames with no timeline. + const entry = this.#register(id, request.accept(), { compress: false, timescale: 0 }, msg, request, state); + const stream = await this.#sendSubscribe(state, msg, deadline); + // The first response MUST be a SUBSCRIBE_OK (older drafts only). + const resp = await decodeSubscribeResponse(stream.reader, this.version); + if (!("ok" in resp)) { + throw new Error("first subscribe response must be SUBSCRIBE_OK"); + } + return { stream, entry }; + } + + // The TRACK opens first, then the SUBSCRIBE, which is waiting on before it is written. + const info = this.#trackInfo(msg.broadcast, msg.epoch, msg.track, state.cancel.signal, deadline); + let settle: (entry: SubscribeEntry | undefined) => void = () => {}; + this.#pending.set( + id, + new Promise((resolve) => { + settle = resolve; + }), + ); + const sending = this.#sendSubscribe(state, msg, deadline); + try { + // Either failing fails the subscription: a reset TRACK rejects the request, so the + // origin can try another route, and the caller resets the SUBSCRIBE. + const [answered, stream] = await Promise.all([info, sending]); + state.track = answered.stream; // The deadline passed as TRACK_INFO landed: the request is already rejected, so don't - // register it again or send its SUBSCRIBE. + // accept it again. state.cancel.signal.throwIfAborted(); - producer = request.accept(this.#toModelInfo(info)); - compress = info.compress; - timescale = info.timescale ?? 0; - } else { - // Older drafts negotiate nothing per-track: verbatim frames with no timeline. - producer = request.accept(); - drainOk = true; + const producer = request.accept(this.#toModelInfo(answered.info)); + const entry = this.#register( + id, + producer, + { compress: answered.info.compress, timescale: answered.info.timescale ?? 0 }, + msg, + request, + state, + ); + settle(entry); + return { stream, entry }; + } catch (err) { + settle(undefined); + // Whichever exchange outlived the other is no longer wanted. + void info.then( + ({ stream }) => stream.abort(error(err)), + () => {}, + ); + void sending.then( + (stream) => stream.abort(error(err)), + () => {}, + ); + throw err; + } finally { + this.#pending.delete(id); } + } + + // Register a subscription's accepted copy, so incoming groups and datagrams find it. + #register( + id: bigint, + producer: track.Producer, + framing: { compress: boolean; timescale: number }, + msg: Subscribe, + request: track.Request, + state: SubscribeSetup, + ): SubscribeEntry { // Lite-06+ resolves where the feed starts only with SUBSCRIBE_START, so a relay serving // this copy holds its first group until the answer arrives. if (resolvesStart(this.version)) hooks.startTrack(producer, { requested: msg.startGroup }); - // Register before opening SUBSCRIBE so a racing GROUP stream finds the entry. const entry: SubscribeEntry = { track: producer, - compress, - timescale, + ...framing, // The effective max delay is the stopgap grace: the wrong clock (it bounds // presentation-time drift), but it is how long the subscriber was willing to wait - // for a late group anyway. Already the smaller of the subscriber's and the track's. + // for a late group anyway. tail: new Tail({ grace: () => grace(producer.subscription.peek()?.maxDelay ?? Milli.zero), }), @@ -1115,23 +1158,19 @@ export class Subscriber { }; this.#subscribes.set(id, entry); state.producer = producer; + return entry; + } - state.stream = await deadline.open(Stream.open(this.#quic, { version: this.version, ...requestOptions })); + // Open the subscribe stream and write the SUBSCRIBE on it. + async #sendSubscribe(state: SubscribeSetup, msg: Subscribe, deadline: Deadline): Promise { + const stream = await deadline.open(Stream.open(this.#quic, { version: this.version, ...requestOptions })); + state.stream = stream; // The deadline passed while the open waited: the late-setup handler resets the stream, so // don't send a SUBSCRIBE on it first. state.cancel.signal.throwIfAborted(); - await state.stream.writer.u53(StreamId.Subscribe); - await msg.encode(state.stream.writer, this.version); - - if (drainOk) { - // The first response MUST be a SUBSCRIBE_OK (older drafts only). - const resp = await decodeSubscribeResponse(state.stream.reader, this.version); - if (!("ok" in resp)) { - throw new Error("first subscribe response must be SUBSCRIBE_OK"); - } - } - - return { stream: state.stream, entry }; + await stream.writer.u53(StreamId.Subscribe); + await msg.encode(stream.writer, this.version); + return stream; } // Opens a TRACK stream and reads the single TRACK_INFO. Lite-05+ only. The stream @@ -1692,7 +1731,9 @@ export class Subscriber { * @internal */ async runGroup(group: GroupMessage, stream: Reader) { - const entry = this.#subscribes.get(group.subscribe); + // One that beats its subscription's TRACK_INFO waits for it unread, which flow control + // bounds. Waiters resume in the order they arrived. + const entry = this.#subscribes.get(group.subscribe) ?? (await this.#pending.get(group.subscribe)); if (!entry) { if (group.subscribe >= this.#subscribeNext) { throw new Error(`unknown subscription: id=${group.subscribe}`); @@ -1796,8 +1837,10 @@ export class Subscriber { compressed: this.#subscribes.get(id)?.compress ?? false, })); + // Unknown or already-closed subscription, or one whose TRACK_INFO has not landed yet, + // which a datagram cannot wait for. const entry = this.#subscribes.get(dg.subscribe); - if (!entry) return; // Unknown or already-closed subscription. + if (!entry) return; const scale = entry.timescale; diff --git a/js/net/src/request-deadline.test.ts b/js/net/src/request-deadline.test.ts index 61391a7cb1..a71dc52985 100644 --- a/js/net/src/request-deadline.test.ts +++ b/js/net/src/request-deadline.test.ts @@ -12,13 +12,14 @@ import * as Path from "./path.ts"; // Zero stream credit can be rejected immediately or queued by a transport that ignores // waitUntilAvailable. Once a slot returns, observe both resets and any unwanted bytes. function limited(mode: "refuse" | "queue" | "open") { - const slot = Promise.withResolvers(); + const slot = Promise.withResolvers(); const created = Promise.withResolvers(); const aborted = Promise.withResolvers(); const cancelled = Promise.withResolvers(); const written: Uint8Array[] = []; const options: unknown[] = []; - const stream = { + // Each open gets its own stream; the first abort and cancel among them resolve. + const stream = () => ({ readable: new ReadableStream({ cancel: cancelled.resolve }), writable: new WritableStream({ abort: aborted.resolve, @@ -26,14 +27,14 @@ function limited(mode: "refuse" | "queue" | "open") { written.push(new Uint8Array(bytes)); }, }), - }; + }); const quic = { closed: new Promise(() => {}), createBidirectionalStream: (opts: unknown) => { options.push(opts); created.resolve(); if (mode === "refuse") return Promise.reject(new DOMException("stream limit reached", "NetworkError")); - return mode === "open" ? Promise.resolve(stream) : slot.promise; + return mode === "open" ? Promise.resolve(stream()) : slot.promise.then(stream); }, } as unknown as WebTransport; return { @@ -43,7 +44,7 @@ function limited(mode: "refuse" | "queue" | "open") { cancelled: cancelled.promise, written, options, - release: () => slot.resolve(stream), + release: () => slot.resolve(), }; } @@ -73,7 +74,11 @@ for (const protocol of ["lite", "ietf"] as const) { } else { expect(failure?.name).toBe("NoStreamCredit"); } - expect(transport.options).toEqual([{ sendOrder: undefined, waitUntilAvailable: false }]); + // A lite subscription opens its SUBSCRIBE alongside its TRACK, under the one deadline. + const opened = protocol === "lite" ? 2 : 1; + expect(transport.options).toEqual( + Array.from({ length: opened }, () => ({ sendOrder: undefined, waitUntilAvailable: false })), + ); if (mode === "queue") { transport.release(); await transport.aborted; @@ -85,7 +90,7 @@ for (const protocol of ["lite", "ietf"] as const) { await transport.cancelled; } await clock.advance(Nano.fromMilli(Milli(30_000))); - expect(transport.options).toHaveLength(1); + expect(transport.options).toHaveLength(opened); } finally { track.close(); broadcast.close(); diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 479be62d51..1dd781e18d 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -563,14 +563,17 @@ test("an unfloored subscriber clears a resume floor above the live edge", () => expect(producer.subscription.peek()?.groups?.start).toBeUndefined(); }); -test("the producer aggregate is clamped without changing subscriber options", async () => { +test("the producer aggregate is not clamped to the track's maxAge", () => { + // The publisher applies its own, so a subscriber asking upstream before the info lands + // asks for the same demand after it. const producer = new TrackProducer("test"); const track = producer.subscribe({ maxDelay: Milli(10_000) }); - const clamped = producer.subscription.changed(); + const before = producer.subscription.peek(); producer.accept({ timescale: Timescale.MILLI, maxAge: Milli(2_000) }); - expect((await clamped)?.maxDelay).toBe(Milli(2_000)); + expect(producer.subscription.peek()).toEqual(before); + expect(producer.subscription.peek()?.maxDelay).toBe(Milli(10_000)); expect(track.subscription.peek()?.maxDelay).toBe(Milli(10_000)); }); @@ -840,9 +843,10 @@ test("the default subscription skips superseded groups and a bare options object // No options means the default subscription, whose zero budget keeps only the newest group. expect((await explicit.recvGroup())?.sequence).toBe(2); expect((await implicit.recvGroup())?.sequence).toBe(2); - // An omitted maxDelay leaves the publisher's retention as the only bound. + // An omitted maxDelay leaves the publisher's retention as the only bound, which the + // aggregate leaves to the publisher. for (const sequence of [0, 1, 2]) expect((await bare.recvGroup())?.sequence).toBe(sequence); - expect(producer.subscription.peek()?.maxDelay).toBe(Milli(5000)); + expect(producer.subscription.peek()?.maxDelay).toBe(Milli(Number.MAX_SAFE_INTEGER)); producer.finish(); producer.close(); }); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index fb3a911886..2c7fb1dae4 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1087,12 +1087,13 @@ export class Producer { // Recompute from every live sink because an update or close can narrow as well as widen // the aggregate. The wire layer observes this signal and emits SUBSCRIBE_UPDATE. + // + // The max delay is the subscribers' own, not clamped to the track's maxAge: the publisher + // applies its own, as each reader's budget does, so the demand asked upstream is the same + // before and after the info lands. #updateSubscription(): void { const combined = combineSubscriptions(this.#sinks); this.#settle(); - const retained = this.#state.info.peek()?.maxAge; - if (combined && retained !== undefined) - combined.maxDelay = Milli(Math.min(combined.maxDelay ?? Milli.zero, retained)); this.#state.update.set(combined); // The requested floor bounds what a pending tail owes. this.#owe(); From 2edc1500f30e21115f89b0b45155bf41aa37812a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 04:14:14 -0700 Subject: [PATCH 04/16] test(net): cover each TRACK and SUBSCRIBE arrival order Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 6 +- rs/moq-net/tests/integration/main.rs | 1 + .../tests/integration/subscribe_with_track.rs | 159 ++++++++++++++++++ rs/moq-net/tests/integration/support/mock.rs | 28 +++ 4 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 rs/moq-net/tests/integration/subscribe_with_track.rs diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 2a9f2a181c..7519a017a9 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -4371,7 +4371,11 @@ mod tests { assert_eq!((msg.id, msg.track.as_ref(), msg.priority), (0, "video", 3), "{version}"); assert!(!trailing, "{version}"); assert!( - subscriber.subscribes.lock().get(&0).is_some_and(|entry| entry.copy.is_none()), + subscriber + .subscribes + .lock() + .get(&0) + .is_some_and(|entry| entry.copy.is_none()), "{version}: registered with its copy pending" ); assert!((&mut waiting).now_or_never().is_none(), "{version}: no TRACK_INFO yet"); diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 94b3330cdc..0e2264dd19 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -59,6 +59,7 @@ mod stream_credit; mod subscribe_priority; mod subscribe_ranges; mod subscribe_tracks; +mod subscribe_with_track; mod subscriber_max_age; mod subscription_end_integrity; #[cfg(feature = "async")] diff --git a/rs/moq-net/tests/integration/subscribe_with_track.rs b/rs/moq-net/tests/integration/subscribe_with_track.rs new file mode 100644 index 0000000000..c71eac40c3 --- /dev/null +++ b/rs/moq-net/tests/integration/subscribe_with_track.rs @@ -0,0 +1,159 @@ +#![cfg(feature = "async")] + +//! On moq-lite 05 and later a subscriber opens its TRACK and SUBSCRIBE streams together, +//! so first data arrives one round trip after asking, per hop, rather than two. +//! +//! QUIC does not order streams, so either may reach the publisher first, or the SUBSCRIBE a +//! round trip after the TRACK, as from a subscriber that waited for TRACK_INFO. Whichever +//! it is, the reader gets the track, including when the SUBSCRIBE lands first and the +//! publisher's groups beat the TRACK_INFO they are read with. Lite-03/04 carry no TRACK +//! stream and are unchanged. + +use crate::support; + +use std::time::Duration; + +use moq_net::{Hop, Timestamp, Version, origin}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(10); +const LATENCY: Duration = Duration::from_millis(10); +const RTT: Duration = Duration::from_millis(20); +const VERSIONS: [&str; 5] = [ + "moq-lite-03", + "moq-lite-04", + "moq-lite-05", + "moq-lite-06", + "moq-lite-07-wip", +]; + +/// How the reader's TRACK and SUBSCRIBE reach the first hop. +#[derive(Debug, Clone, Copy)] +enum Arrival { + /// As sent: together. + Together, + /// The SUBSCRIBE a round trip after the TRACK, once its TRACK_INFO is back. + Serial, + /// The SUBSCRIBE first, so the publisher's groups head back before its TRACK_INFO. + SubscribeFirst, +} + +fn produce_origin(hop: u64) -> origin::Producer { + let (producer, driver) = origin::Producer::new(origin::Config::new(Hop::new(hop).unwrap())); + support::harness::spawn(driver); + producer +} + +/// `subscriber` reads what `publisher` serves, over a link with [`LATENCY`] each way. +async fn link(version: Version, publisher: &origin::Producer, subscriber: &origin::Producer) -> MockPair { + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(subscriber.clone()); + options.latency = LATENCY; + connect_mock(options).await +} + +/// A reader subscribes through `relays` relays and reads a cached group, then a live one. +/// Returns how long the cached group's first frame took. +async fn first_frame(version: &str, relays: usize, arrival: Arrival) -> Duration { + let version: Version = version.parse().unwrap(); + let label = format!("{version} over {relays} relays, {arrival:?}"); + + let nodes: Vec<_> = (1..=relays as u64 + 2).map(produce_origin).collect(); + let mut links = Vec::new(); + for pair in nodes.windows(2) { + links.push(link(version, &pair[0], &pair[1]).await); + } + let reader = &links.last().unwrap().client_transport; + + let broadcast = nodes[0].create_broadcast("room").unwrap(); + let track = broadcast + .create_track( + "video", + moq_net::track::Info::default().with_timescale(moq_net::Timescale::MILLI), + ) + .unwrap(); + let mut cached = track.append_group().unwrap(); + cached.write_frame(Timestamp::ZERO, b"cached".as_ref()).unwrap(); + cached.finish().unwrap(); + broadcast.announce(Default::default()).unwrap(); + + let consumer = nodes.last().unwrap().consume(); + let remote = consumer.follow("room", None).unwrap().available().await.unwrap(); + + if !matches!(arrival, Arrival::Together) { + reader.hold_bidis(); + } + let start = moq_clock::now(); + let subscribing = moq_clock::sim::spawn(remote.track("video").unwrap().subscribe(None)); + match arrival { + Arrival::Together => {} + Arrival::Serial => { + moq_clock::sleep(Duration::from_millis(1)).await; + reader.release_oldest_bidi(); + moq_clock::sleep(RTT).await; + reader.release_bidis(); + } + Arrival::SubscribeFirst => { + moq_clock::sleep(Duration::from_millis(1)).await; + reader.release_bidis_reversed(); + } + } + let mut subscriber = moq_clock::timeout(TIMEOUT, subscribing) + .await + .unwrap_or_else(|_| panic!("{label}: subscribe stalled")) + .unwrap() + .unwrap_or_else(|err| panic!("{label}: subscribe failed: {err}")); + + let mut group = moq_clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .unwrap_or_else(|_| panic!("{label}: no group arrived")) + .unwrap() + .expect("the track is live"); + let frame = group.read_frame().await.unwrap().expect("the group has a frame"); + let elapsed = moq_clock::now() - start; + assert_eq!(frame.payload.as_ref(), b"cached", "{label}"); + + let mut live = track.append_group().unwrap(); + live.write_frame(Timestamp::from_millis(1).unwrap(), b"live".as_ref()) + .unwrap(); + live.finish().unwrap(); + let mut group = moq_clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .unwrap_or_else(|_| panic!("{label}: the live group did not arrive")) + .unwrap() + .expect("the track is live"); + let frame = group.read_frame().await.unwrap().expect("the group has a frame"); + assert_eq!(frame.payload.as_ref(), b"live", "{label}"); + elapsed +} + +#[moq_clock::test] +async fn every_arrival_order_delivers() { + for version in VERSIONS { + let pipelined = !matches!(version, "moq-lite-03" | "moq-lite-04"); + for relays in 0..=2 { + first_frame(version, relays, Arrival::Together).await; + // Without a TRACK stream there is nothing to reorder. + if pipelined { + first_frame(version, relays, Arrival::Serial).await; + first_frame(version, relays, Arrival::SubscribeFirst).await; + } + } + } +} + +/// The first frame arrives one round trip after asking, per hop. +#[moq_clock::test] +async fn first_frame_takes_one_round_trip_per_hop() { + for version in ["moq-lite-05", "moq-lite-06", "moq-lite-07-wip"] { + for relays in 0..=2 { + let elapsed = first_frame(version, relays, Arrival::Together).await; + let hops = relays as u32 + 1; + assert!( + elapsed <= RTT * hops, + "{version} over {relays} relays: first frame after {elapsed:?}" + ); + } + } +} diff --git a/rs/moq-net/tests/integration/support/mock.rs b/rs/moq-net/tests/integration/support/mock.rs index 1b96c550ea..a0c3483a7a 100644 --- a/rs/moq-net/tests/integration/support/mock.rs +++ b/rs/moq-net/tests/integration/support/mock.rs @@ -777,6 +777,34 @@ impl MockSession { } } + /// Deliver only the oldest held bidi stream, and keep holding the rest. + pub fn release_oldest_bidi(&self) { + let link = &self.side.out; + link.settle(); + let mut held = link.held_bi.lock().unwrap(); + let held = held.as_mut().expect("not holding"); + if !held.is_empty() { + let _ = link.ready_bi.try_push(held.remove(0)); + } + } + + /// Deliver the held bidi streams newest first, and stop holding. + pub fn release_bidis_reversed(&self) { + let link = &self.side.out; + link.settle(); + for stream in link + .held_bi + .lock() + .unwrap() + .take() + .unwrap_or_default() + .into_iter() + .rev() + { + let _ = link.ready_bi.try_push(stream); + } + } + /// Park every uni stream this side opens from now on, like a peer that has granted /// no more stream credit. pub fn withhold_unis(&self) { From 3a7274728000a55c2cf2627b41e5dd05a2a1f0d9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 04:43:54 -0700 Subject: [PATCH 05/16] test(relay): let an undeafened subscription see the latest group first Co-Authored-By: Claude Opus 5.5 --- rs/moq-relay/tests/integration/auth_lifetime.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/rs/moq-relay/tests/integration/auth_lifetime.rs b/rs/moq-relay/tests/integration/auth_lifetime.rs index 24a1e59026..5aa0127c75 100644 --- a/rs/moq-relay/tests/integration/auth_lifetime.rs +++ b/rs/moq-relay/tests/integration/auth_lifetime.rs @@ -886,12 +886,17 @@ async fn a_rechecked_grant_resizes_live_sessions() { .await .expect("subscribe"); send(0, 1); - let group = tokio::time::timeout(TIMEOUT, audio.recv_group()) - .await - .expect("recv_group timeout") - .expect("recv_group failed") - .expect("the undeafened track closed"); - assert!(group.sequence >= 1, "{scheme}"); + // The SUBSCRIBE went out with the TRACK, so the latest group when it arrived may come first. + loop { + let group = tokio::time::timeout(TIMEOUT, audio.recv_group()) + .await + .expect("recv_group timeout") + .expect("recv_group failed") + .expect("the undeafened track closed"); + if group.sequence >= 1 { + break; + } + } for (session, what) in [(pub_session, "publisher"), (sub_session, "subscriber")] { assert!( From ccc77d1a59191f0e977325516b9bb20bde3b4f55 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:03:31 -0700 Subject: [PATCH 06/16] chore(quest): finish SUBSCRIBE goes out with TRACK Co-Authored-By: Claude Opus 5.5 --- quest/m2/pipeline-requests/README.md | 1 - quest/m2/pipeline-requests/fetch.md | 7 +- quest/m2/pipeline-requests/subscribe.md | 93 ------------------------- 3 files changed, 6 insertions(+), 95 deletions(-) delete mode 100644 quest/m2/pipeline-requests/subscribe.md diff --git a/quest/m2/pipeline-requests/README.md b/quest/m2/pipeline-requests/README.md index ff7f9888c8..d0d1c7b45c 100644 --- a/quest/m2/pipeline-requests/README.md +++ b/quest/m2/pipeline-requests/README.md @@ -24,5 +24,4 @@ and two relay hops, before and after the line. ## Required -- [SUBSCRIBE goes out with TRACK](/quest/m2/pipeline-requests/subscribe.md) - subscribers open TRACK and SUBSCRIBE together in Rust and JS - [Pipelined first FETCH](/quest/m2/pipeline-requests/fetch.md) - a fetch-only reader's first FETCH goes out with the info request, on lite and moq-transport diff --git a/quest/m2/pipeline-requests/fetch.md b/quest/m2/pipeline-requests/fetch.md index 9429ef0be4..0de91ffc73 100644 --- a/quest/m2/pipeline-requests/fetch.md +++ b/quest/m2/pipeline-requests/fetch.md @@ -48,6 +48,12 @@ What needs the info is decoding the response and approving the group, so: The line's shared decisions apply: early response data stays unread in QUIC, a failed info fails the fetch, and legacy serial peers keep working. +The SUBSCRIBE half landed in +[moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328): lite's +`TrackServeRun` opens a `Pipelined` SUBSCRIBE beside its TRACK, registered +with its copy pending, and parks group streams that beat the info +(`Parked`); a pipelined FETCH can take the same shape. + Tests: a fetch-only reader's FETCH is on the wire before the info answer; a refused info drops the fetch and retries the next route; a group is never released before its route's info passes; a serial peer against a pipelining @@ -57,5 +63,4 @@ Public API: none. Wire: none (ordering only). ## Related -- [Pipelined SUBSCRIBE](/quest/m2/pipeline-requests/subscribe.md) - the same change for subscriptions - [Retired requests](https://github.com/moq-dev/moq/pull/5294) - staged requests retain their selected instance; retries cannot resolve a replacement by path diff --git a/quest/m2/pipeline-requests/subscribe.md b/quest/m2/pipeline-requests/subscribe.md deleted file mode 100644 index 85cc7d033f..0000000000 --- a/quest/m2/pipeline-requests/subscribe.md +++ /dev/null @@ -1,93 +0,0 @@ -# [L] SUBSCRIBE goes out with TRACK - -## Goal - -On moq-lite 05 and later, a subscriber opens its TRACK and SUBSCRIBE streams -together instead of waiting for TRACK_INFO before sending SUBSCRIBE, in Rust -and JS, at every hop: the viewer's session and each relay's upstream session. -First data arrives one round trip sooner per hop. A peer that still sends -them serially, and lite-03/04 with no track stream, keep working unchanged. - -## Plan - -Decided 2026-10-08 in a `/quest-plan` interview (paper trail in the PR that -added this quest). It builds on #5053 (merged) and reverses that PR's "not -pipelining" decision: the draft already allows it -(`drafts/draft-lcurley-moq-lite.md`, Track Stream: the subscriber "MAY open -the Track and Subscribe streams concurrently"), and the round trip is not -worth keeping. - -Facts (2026-10-08, `origin/main` 11ee77d5d, after #5053): - -- Rust: `TrackServeRun::poll` waits on `info.poll_fetch` before building its - `ServeLoop` (`rs/moq-net/src/lite/subscriber.rs:4144`), and only then does - `begin_subscription` and `Establish` write SUBSCRIBE. -- JS: `#openSubscribe` awaits `#trackInfo` before `Stream.open` and the - SUBSCRIBE write (`js/net/src/lite/subscriber.ts:704`). The receive path - already waits for the timescale (`runGroup`) and drops datagrams that beat - TRACK_INFO. -- No SUBSCRIBE field needs TRACK_INFO, and neither publisher needs a TRACK - before a SUBSCRIBE (`SubscribeServe::start` subscribes the model track - directly). -- The relay front already hands demand to a copy before splicing it - (`model/origin.rs` `Action::Query`), so the gap is in the session. -- Both stacks route groups only to an accepted subscription. Rust's - `ServeLoop::new` accepts the request with the info, and - `begin_subscription` registers a `TrackEntry` holding that producer and its - timescale; a group whose id has no entry is cancelled, not left unread - (`subscriber.rs:816`). JS `runGroup` (`subscriber.ts:1145`) drops a group - for a past id with no registered subscription (an id it has not allocated - yet is a protocol error), and writes the group before it - waits on the timescale. - -Decisions: - -- Both streams open at once. Group streams that arrive before TRACK_INFO stay - unread in QUIC until it lands (flow control bounds them; no buffering, no - copies). Datagrams that beat it are dropped, as JS does today. Rejected: - buffering decoded bytes in memory. -- Each subscription gets a pending state before accept, in both stacks: - SUBSCRIBE is built from the request's subscription before the info is - known, and its entry is registered under the id before SUBSCRIBE is written, - with the producer and timescale deferred. A group stream that arrives early - parks after its header and resumes on accept. -- A TRACK that fails or is reset while its SUBSCRIBE is live rejects the - request, so the origin fails over as today, and resets the SUBSCRIBE - stream. Rejected: keeping the subscription and learning the info another - way. -- Only the max age SUBSCRIBE and SUBSCRIBE_UPDATE carry on the wire stops - being clamped by TRACK_INFO (`max_age_bound()`, `model/track.rs`): it is the - subscribers' own, still capped by the local cache ceiling (which needs no - info), and the publisher enforces its own. The per-reader lateness budget - (`is_expired`) and the drift check (`poll_drift`) keep `max_age_bound()`. - Accepting TRACK_INFO never emits a SUBSCRIBE_UPDATE. Reword the docs that - promise a clamped aggregate (`track::Producer::subscription`, - `max_age_bounds_the_budget`). Rejected: a SUBSCRIBE_UPDATE once TRACK_INFO - lands. Narrowed 2026-10-08 from review. This agrees with - the shared staleness rule (`Subscription::max_delay`): both budgets apply one - staleness rule, so a group is stale under the smaller budget exactly when it - is stale under either. -- Publishers send a track's TRACK_INFO at a higher stream priority than that - track's groups, in Rust and JS, as the draft's SHOULD asks, so the unread - wait stays one round trip. -- Keep #5053's held TRACK stream: demand still has to hold across either - arrival order and for serial peers. -- Legacy: mixed-mode integration tests in Rust and JS, a serial subscriber - against a pipelining publisher and relay and the reverse, on lite-05/06/07, - with lite-03/04 unchanged. `just test interop --all` covers the - cross-language pairs. - -Tests also: SUBSCRIBE is on the wire before TRACK_INFO; groups that arrive -first are read only after it; a reset TRACK rejects the request and the -origin fails over; accepting TRACK_INFO sends no SUBSCRIBE_UPDATE; TRACK_INFO -outranks the track's groups. Measure time to first frame across one and two -relay hops before and after. - -Public API: none. Wire: none (ordering only; the draft already permits it). -Decided 2026-10-08: pipelining is ordering-only, so it neither waits on nor -blocks the Related quests below; whichever lands second rebases. - -## Related - -- [Pipelined first FETCH](/quest/m2/pipeline-requests/fetch.md) - the same change for fetch-only readers -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - replaces the SUBSCRIBE floor with ranges From a1f37497e175606568dd7d4c5144df198198ef11 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:09:35 -0700 Subject: [PATCH 07/16] fix(js/net): read a datagram with the entry it was decoded with Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscriber.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index c2e21a9a90..d1d33e17ea 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1831,16 +1831,16 @@ export class Subscriber { // datagram (best-effort) if the subscription is unknown or closed. async #routeDatagram(payload: Uint8Array): Promise { if (payload.byteLength > DatagramMessage.MAX_SIZE) return; - // An unknown subscription's datagram is dropped below, however it parses. - const dg = await DatagramMessage.decode(payload, this.version, (id) => ({ - timed: !!this.#subscribes.get(id)?.timescale, - compressed: this.#subscribes.get(id)?.compress ?? false, - })); - - // Unknown or already-closed subscription, or one whose TRACK_INFO has not landed yet, - // which a datagram cannot wait for. - const entry = this.#subscribes.get(dg.subscribe); - if (!entry) return; + // Read with the entry it was decoded with: one registered while the decode awaited would + // read a datagram parsed with the defaults. An unknown subscription's datagram, or one + // whose TRACK_INFO had not landed, which a datagram cannot wait for, is dropped below. + let entry: SubscribeEntry | undefined; + const dg = await DatagramMessage.decode(payload, this.version, (id) => { + entry = this.#subscribes.get(id); + return { timed: !!entry?.timescale, compressed: entry?.compress ?? false }; + }); + // Gone since, too: the subscription ended while the decode awaited. + if (!entry || this.#subscribes.get(dg.subscribe) !== entry) return; const scale = entry.timescale; From 59d15b18bcec02fc7290448eb5fc23ad56730498 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 22:34:15 -0700 Subject: [PATCH 08/16] fix(js/net): join at the latest group and hold the answered TRACK A lite-05 SUBSCRIBE sent alongside its TRACK can reach a JS publisher before an on-demand track resolves, when no group is known: re-read the latest group once the track resolves, as the Rust publisher does, so a pre-06 "no start" does not replay the cache. The subscriber records the TRACK stream as soon as TRACK_INFO lands, so a cancellation while the SUBSCRIBE still waits for a stream closes it instead of leaving it open behind a create that may never finish. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 41 +++++++++++++++++++++++++++--- js/net/src/lite/publisher.ts | 13 +++++++++- js/net/src/lite/subscriber.test.ts | 36 ++++++++++++++++++++++++++ js/net/src/lite/subscriber.ts | 16 +++++++++--- 4 files changed, 98 insertions(+), 8 deletions(-) diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 6d5a0ea0d5..92bc728d21 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -16,6 +16,7 @@ import { Front } from "../resume.ts"; import { Reader, Stream, Writer } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; import { Subscriber as TrackSubscriber } from "../track.ts"; +import { wireOf } from "../wire.ts"; import { type AnnounceBroadcast, AnnounceOk, AnnounceRequest, decodeAnnounceBroadcast } from "./announce.ts"; import { Datagram as DatagramMessage } from "./datagram.ts"; import { Fetch } from "./fetch.ts"; @@ -696,6 +697,8 @@ async function servedSubscription( // Answer the subscription with a front's broadcast, as an application forwarding a // routed one does, so the track reads through the front's serving route. routed?: boolean; + // Leave the track unresolved until `accept`, as an on-demand track the SUBSCRIBE beats. + pending?: boolean; } = {}, ) { const version = options.version ?? Version.DRAFT_05; @@ -720,7 +723,10 @@ async function servedSubscription( } else { broadcast = publish(origin, Path.from("test")); } - const track = broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: options.maxAge }); + const info = { timescale: Timescale.MILLI, maxAge: options.maxAge }; + let track = options.pending ? undefined : broadcast.createTrack("video", info); + // Pulled before the SUBSCRIBE arrives: a broadcast nobody pulls refuses an unknown track. + const request = options.pending ? wireOf(broadcast).requested() : undefined; const client = await Stream.open(pair.client, { version: version }); @@ -752,16 +758,29 @@ async function servedSubscription( const opened = pair.client.incomingUnidirectionalStreams.getReader(); + const resolved = () => { + if (!track) throw new Error("the track is still pending"); + return track; + }; + return { client, datagrams: pair.client.datagrams.readable, - track, + get track() { + return resolved(); + }, serving, parked: gate.parked, release: gate.release, + // Resolve a pending track with the publisher's request for it. + async accept() { + const pulled = await request; + if (!pulled) throw new Error("the publisher never requested the track"); + track = pulled.accept(info); + }, // Stamped by sequence, so a group served out of order still starts in order. serve(sequence: number) { - const group = track.createGroup(sequence); + const group = resolved().createGroup(sequence); for (const frame of frames) group.writeFrame(textFrame(frame, Timestamp.fromMillis(sequence))); group.finish(); }, @@ -1331,6 +1350,22 @@ test("lite draft-05: an update dropping the floor re-pins to the latest group", } }); +// A SUBSCRIBE sent alongside its TRACK can beat the track resolving. Groups the track holds +// once it does are older than the join, so an absent Group Start is still the latest group. +test("lite draft-05: a subscribe that beats its track resolving joins at the latest group", async () => { + const sub = await servedSubscription({ pending: true }); + try { + await sub.accept(); + sub.serve(0); + sub.serve(1); + sub.serve(2); + expect(await sub.servedSequence()).toBe(2); + expect(await sub.servedSequence()).toBeUndefined(); + } finally { + await sub.close(); + } +}); + // On an empty track the re-pin has no latest group to name, so the first group the // track produces is served, even below the dropped floor. test("lite draft-05: an update dropping the floor on an empty track serves the next group", async () => { diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 88b7a1a6df..498992e34d 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -887,7 +887,8 @@ export class Publisher { }, }); serving(track); - const floor = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track)); + const latest = hooks.latestSequence(track); + let floor = cursorFloor(this.version, msg.startGroup, latest); track.withRange({ start: { included: floor }, end: endGroup === undefined ? undefined : { excluded: endGroup }, @@ -921,6 +922,16 @@ export class Publisher { // priority. const info = await untilAborted(track.info(), left.signal); const publisher = info.priority; + // On lite-05, a SUBSCRIBE sent alongside its TRACK can arrive before the track + // resolves, when no group is known yet: the newest once it resolves stands in, as it + // would for one sent after. + if (latest === undefined && hasTrackStream(this.version)) { + floor = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track)); + track.withRange({ + start: { included: floor }, + end: endGroup === undefined ? undefined : { excluded: endGroup }, + }); + } // Older drafts carry no timestamps. let stamp: Stamp | undefined; let compress = false; diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index d4ce04ae9c..bdd3830dad 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -1786,6 +1786,42 @@ test.each([ } }); +// A TRACK answered while its SUBSCRIBE still waits for a stream belongs to the subscription, so +// a withdrawal then closes it rather than leaving it to a create that may never open. +test("a withdrawal while the subscribe opens closes the answered track", async () => { + const version = Version.DRAFT_07; + const warn = spyOn(console, "warn").mockImplementation(() => {}); + const { quic, streams, opened } = fakeSession([2]); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const announced = subscriber.announced(); + try { + await opened(0); + const pool = await epochlessPool(version, streams, announced); + const held = subscriber.consume(Path.from("pool/job")); + const track = held.track("video").subscribe(); + const info = await opened(1); + await opened(2); + await info.reading; + await answerTrackInfo(info, version); + await drainReactions(); + + let finished = false; + void info.finished.then(() => { + finished = true; + }); + await pool.withdraw(); + expect(await track.closed).toBeInstanceOf(Error); + await drainReactions(); + expect(finished).toBe(true); + track.close(); + held.close(); + } finally { + announced.close(); + subscriber.close(); + warn.mockRestore(); + } +}); + // A TRACKS request without an epoch lists whichever instance won the path when it arrived, so // another instance taking the path refuses one still unanswered. test("a listing of an epochless handle replaced before its answer is refused", async () => { diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index d1d33e17ea..bc3af70bed 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1054,8 +1054,8 @@ export class Subscriber { // Open the subscribe stream and accept the request (so the application's track.Subscriber // resolves and incoming groups have a producer to write into) with the track's immutable - // properties. `state.stream` and `state.track` are populated as soon as each stream opens - // so the caller can clean them up on timeout even before this promise settles. + // properties. `state.stream` is populated as soon as it opens, and `state.track` once + // answered, so the caller can clean them up on timeout even before this promise settles. // // On lite-05+ the properties come from a TRACK stream, held open until the SUBSCRIBE is // answered, and the SUBSCRIBE goes out alongside it rather than a round trip after: it is @@ -1084,7 +1084,16 @@ export class Subscriber { } // The TRACK opens first, then the SUBSCRIBE, which is waiting on before it is written. - const info = this.#trackInfo(msg.broadcast, msg.epoch, msg.track, state.cancel.signal, deadline); + // The answered TRACK is held at once, so a cancellation while the SUBSCRIBE still opens + // closes it rather than waiting on an open that may never finish. + const info = this.#trackInfo(msg.broadcast, msg.epoch, msg.track, state.cancel.signal, deadline).then( + (answered) => { + state.track = answered.stream; + // Cancelled as it was answered: the caller may already have left. + if (state.cancel.signal.aborted) answered.stream.close(); + return answered; + }, + ); let settle: (entry: SubscribeEntry | undefined) => void = () => {}; this.#pending.set( id, @@ -1097,7 +1106,6 @@ export class Subscriber { // Either failing fails the subscription: a reset TRACK rejects the request, so the // origin can try another route, and the caller resets the SUBSCRIBE. const [answered, stream] = await Promise.all([info, sending]); - state.track = answered.stream; // The deadline passed as TRACK_INFO landed: the request is already rejected, so don't // accept it again. state.cancel.signal.throwIfAborted(); From 37e74667d8031e6f70497310632ac6f7bc42c2ac Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 23:10:28 -0700 Subject: [PATCH 09/16] fix(js/net): move the floor only when the resolved track changes it Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 498992e34d..3b56e592c0 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -925,8 +925,9 @@ export class Publisher { // On lite-05, a SUBSCRIBE sent alongside its TRACK can arrive before the track // resolves, when no group is known yet: the newest once it resolves stands in, as it // would for one sent after. - if (latest === undefined && hasTrackStream(this.version)) { - floor = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track)); + const resolved = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track)); + if (latest === undefined && hasTrackStream(this.version) && resolved !== floor) { + floor = resolved; track.withRange({ start: { included: floor }, end: endGroup === undefined ? undefined : { excluded: endGroup }, From 8ed41a964c3bb51975e7dab8d6edd0cb0f87fbb0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 01:06:24 -0700 Subject: [PATCH 10/16] fix(net): bound the session tail's grace by the track's max age The aggregate asked upstream is no longer clamped to TRACK_INFO's max age, but the session's tail and the JS track's arrival ledger read their grace from it. A subscriber deferring to the max age alone then waited out an unbounded grace for a group lost at the tail. Bound the grace locally by the max age, as before, without touching the demand sent upstream; a pipelined SUBSCRIBE takes the bound once its copy is accepted. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 8 ++++++++ js/net/src/lite/subscriber.ts | 5 ++--- js/net/src/track.test.ts | 3 +++ js/net/src/track.ts | 8 ++++++-- rs/moq-net/src/lite/subscriber.rs | 26 ++++++++++++++++++++------ rs/moq-net/src/model/track.rs | 11 +++++++++++ 6 files changed, 50 insertions(+), 11 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 0b29b82f1d..5f8a56568e 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -437,6 +437,11 @@ export const hooks: { * so it names no object; assigned by `track.ts`. */ reportedGroup: (producer: Producer, group: number) => void; + /** + * How long a session's tail waits for a late group: the subscribers' max delay, bounded by + * the track's max age; assigned by `track.ts`. + */ + tailGrace: (producer: Producer) => Milli; /** Snapshot the latest group sequence for wire positioning; assigned by `track.ts`. */ latestSequence: (subscriber: Subscriber) => number | undefined; /** @@ -650,6 +655,9 @@ export const hooks: { reportedGroup: () => { throw new Error("track.ts not loaded"); }, + tailGrace: () => { + throw new Error("track.ts not loaded"); + }, latestSequence: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index bc3af70bed..66aec84a4e 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -23,7 +23,6 @@ import { import * as netGroup from "../group.ts"; import { Cost, type Hop, MAX_HOPS, type Route, routesEqual, UNKNOWN_HOP } from "../hop.ts"; import { groupBounds, hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts"; -import { grace } from "../ledger.ts"; import type { Lister } from "../listing.ts"; import * as Path from "../path.ts"; import { sendOrder } from "../priority.ts"; @@ -1155,9 +1154,9 @@ export class Subscriber { ...framing, // The effective max delay is the stopgap grace: the wrong clock (it bounds // presentation-time drift), but it is how long the subscriber was willing to wait - // for a late group anyway. + // for a late group anyway, and no longer than the publisher keeps one. tail: new Tail({ - grace: () => grace(producer.subscription.peek()?.maxDelay ?? Milli.zero), + grace: () => hooks.tailGrace(producer), }), requested: msg.startGroup, bounded: msg.endGroup !== undefined, diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 1dd781e18d..7c87093aec 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -575,6 +575,8 @@ test("the producer aggregate is not clamped to the track's maxAge", () => { expect(producer.subscription.peek()).toEqual(before); expect(producer.subscription.peek()?.maxDelay).toBe(Milli(10_000)); expect(track.subscription.peek()?.maxDelay).toBe(Milli(10_000)); + // A late group is waited for no longer than the publisher keeps it. + expect(hooks.tailGrace(producer)).toBe(Milli(2_000)); }); test("nextGroup skips late arrivals", async () => { @@ -847,6 +849,7 @@ test("the default subscription skips superseded groups and a bare options object // aggregate leaves to the publisher. for (const sequence of [0, 1, 2]) expect((await bare.recvGroup())?.sequence).toBe(sequence); expect(producer.subscription.peek()?.maxDelay).toBe(Milli(Number.MAX_SAFE_INTEGER)); + expect(hooks.tailGrace(producer)).toBe(Milli(5000)); producer.finish(); producer.close(); }); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 2c7fb1dae4..3fec56fe2d 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -798,6 +798,7 @@ export class Producer { hooks.producerLargest = (producer) => producer.#state.largest; hooks.joinProducer = (producer) => producer.#join(); hooks.endTrack = (producer, end) => producer.#endPending(end); + hooks.tailGrace = (producer) => producer.#grace(); hooks.releaseTrack = (producer) => producer.#close(undefined); hooks.trackReleased = (producer) => producer.#state.closed; hooks.trackFinished = (producer) => producer.#state.end.peek() !== undefined; @@ -1590,9 +1591,12 @@ export class Producer { this.#owe(); } - // The grace the session's tail uses, from the max delay the track's subscribers ask for. + // The grace the session's tail uses, from the max delay the track's subscribers ask for, + // bounded by how long the publisher keeps a group. The demand asked upstream is not. #grace(): Milli { - return grace(this.#state.update.peek()?.maxDelay ?? Milli.zero); + const maxDelay = this.#state.update.peek()?.maxDelay ?? Milli.zero; + const maxAge = this.#state.info.peek()?.maxAge; + return grace(maxAge === undefined ? maxDelay : Milli(Math.min(maxDelay, maxAge))); } // Tell every reader whether the pending tail still owes a group below the end. diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 7519a017a9..9c36b91f6a 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -7151,7 +7151,7 @@ impl TrackServe { start..floor.unwrap_or(u64::MAX) }); if let Ok(mut tail) = active.tail.write() { - tail.set_grace(tail::grace(subscription.budget())); + tail.set_grace(producer.tail_grace(subscription.budget())); if let Some(groups) = lowered { tail.demand(groups, self.subscriber.runtime.now()); } @@ -7319,7 +7319,12 @@ impl TrackServe { tracing::debug!(id, broadcast = %self.subscriber.log_path(&self.path), track = %self.name, "subscribe started"); - let tail = kio::Producer::new(Tail::new(tail::grace(subscription.budget()))); + // A pipelined SUBSCRIBE has no copy, so no max age, until it is adopted. + let grace = match © { + Some(copy) => copy.producer.tail_grace(subscription.budget()), + None => tail::grace(subscription.budget()), + }; + let tail = kio::Producer::new(Tail::new(grace)); self.subscriber .subscribes .lock() @@ -8282,6 +8287,14 @@ impl ServeLoop { PipelinedState::Open(active) => active.requested, }; serve.request_floor(&mut self.serving, floor); + // The tail's grace waited on the max age the accepted copy now carries. + let tail = match &pipelined.state { + PipelinedState::Opening(establish) => &establish.tail, + PipelinedState::Open(active) => &active.tail, + }; + if let Ok(mut tail) = tail.write() { + tail.set_grace(self.serving.tail_grace(pipelined.pref.budget())); + } self.parked = Some(pipelined.id()); self.serving.seen_subscription(pipelined.pref); match pipelined.state { @@ -8637,7 +8650,7 @@ impl ServeLoop { tracing::debug!(broadcast = %serve.subscriber.log_path(&serve.path), track = %serve.name, "subscribe covered"); let budget = self.serving.subscription().map(|sub| sub.budget()); if let Ok(mut tail) = active.tail.write() { - tail.set_grace(tail::grace(budget.unwrap_or_default())); + tail.set_grace(self.serving.tail_grace(budget.unwrap_or_default())); tail.expire(serve.subscriber.runtime.now()); } self.mode = ServeMode::Tail { @@ -8665,9 +8678,10 @@ impl ServeLoop { // subscriber was willing to wait for a late group anyway. if let Ok(mut tail) = active.tail.write() { tail.end(); - tail.set_grace(tail::grace( - subscription.map(|sub| sub.budget()).unwrap_or_default(), - )); + tail.set_grace( + self.serving + .tail_grace(subscription.map(|sub| sub.budget()).unwrap_or_default()), + ); tail.expire(serve.subscriber.runtime.now()); } self.mode = ServeMode::Tail { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index edd4d6bf37..c5833c2b93 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3763,6 +3763,13 @@ impl Producer { self.prev_subscription = Some(seen); } + /// How long a subscriber session's tail waits for a late group: `budget`, bounded by how + /// long the publisher keeps one. The aggregate asked upstream is not bounded by it. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn tail_grace(&self, budget: Duration) -> Duration { + ledger::grace(clamp_max_delay(budget, self.state.read().max_age_bound())) + } + /// Create a [`Requests`] handle that serves on-demand fetches of uncached /// (old) groups. Most producers never need this; a relay creates one to fetch /// past groups from upstream. @@ -10755,6 +10762,10 @@ mod test { let producer = request.accept(Info::timed().with_max_age(Duration::from_secs(2))); assert_eq!(producer.subscription().unwrap().max_delay, before); assert_eq!(producer.state.read().max_age_bound(), Some(Duration::from_secs(2))); + // A session's tail still waits for a late group no longer than the publisher keeps it, + // even for a subscriber that defers to the max age alone. + assert_eq!(producer.tail_grace(Duration::from_secs(60)), Duration::from_secs(2)); + assert_eq!(producer.tail_grace(Duration::MAX), Duration::from_secs(2)); waiting.await.unwrap(); } From eefbf7d429c585e910fd6fd12376fa9c07f77ad6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 01:52:12 -0700 Subject: [PATCH 11/16] fix(js/net): floor an unresolved track at its newest group on every draft A SUBSCRIBE a JS relay serves from a copy still fetching upstream can beat the copy resolving on any pre-06 draft, not only a lite-05 one sent beside its TRACK. Re-read the newest group once the track resolves on all of them, and finish quest/m1/js-early-latest.md. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 34 +++++++++++++++++-------------- js/net/src/lite/publisher.ts | 9 ++++---- quest/m1/README.md | 1 - quest/m1/js-early-latest.md | 13 ------------ 4 files changed, 24 insertions(+), 33 deletions(-) delete mode 100644 quest/m1/js-early-latest.md diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 92bc728d21..4ee5633a81 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1350,21 +1350,25 @@ test("lite draft-05: an update dropping the floor re-pins to the latest group", } }); -// A SUBSCRIBE sent alongside its TRACK can beat the track resolving. Groups the track holds -// once it does are older than the join, so an absent Group Start is still the latest group. -test("lite draft-05: a subscribe that beats its track resolving joins at the latest group", async () => { - const sub = await servedSubscription({ pending: true }); - try { - await sub.accept(); - sub.serve(0); - sub.serve(1); - sub.serve(2); - expect(await sub.servedSequence()).toBe(2); - expect(await sub.servedSequence()).toBeUndefined(); - } finally { - await sub.close(); - } -}); +// A SUBSCRIBE can beat its track resolving, as one a relay serves from a copy still fetching +// upstream. Groups the track holds once it does are older than the join, so an absent Group +// Start is still the latest group. +test.each([Version.DRAFT_01, Version.DRAFT_03, Version.DRAFT_05])( + "lite %s: a subscribe that beats its track resolving joins at the latest group", + async (version) => { + const sub = await servedSubscription({ version, pending: true }); + try { + await sub.accept(); + sub.serve(0); + sub.serve(1); + sub.serve(2); + expect(await sub.servedHeader()).toBe(2); + expect(await sub.servedSequence()).toBeUndefined(); + } finally { + await sub.close(); + } + }, +); // On an empty track the re-pin has no latest group to name, so the first group the // track produces is served, even below the dropped floor. diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 3b56e592c0..13b15d52d7 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -922,11 +922,12 @@ export class Publisher { // priority. const info = await untilAborted(track.info(), left.signal); const publisher = info.priority; - // On lite-05, a SUBSCRIBE sent alongside its TRACK can arrive before the track - // resolves, when no group is known yet: the newest once it resolves stands in, as it - // would for one sent after. + // A SUBSCRIBE can arrive before its track resolves, such as one a relay serves from a + // copy still fetching upstream, or on lite-05 one sent alongside its TRACK, when no + // group is known yet: the newest once it resolves stands in, as it would for one + // sent after. const resolved = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track)); - if (latest === undefined && hasTrackStream(this.version) && resolved !== floor) { + if (latest === undefined && resolved !== floor) { floor = resolved; track.withRange({ start: { included: floor }, diff --git a/quest/m1/README.md b/quest/m1/README.md index fb790f804a..82183c6718 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -190,7 +190,6 @@ removed). - [The last AAC segment advertises its full duration](/quest/m1/hls-final-aac-segment.md) - a TS import's final audio segment's EXTINF matches what decodes - [A JS reader finishes after a clean close](/quest/m1/js-close-range-finish.md) - a range ending below the cache delivers what's held and then ends - [fMP4 decode times keep increasing](/quest/m1/fmp4-bframe-decode-time.md) - fragments shorter than a GOP of B-frame video never step `tfdt` backwards -- [A JS relay floors an unresolved copy at its newest group](/quest/m1/js-early-latest.md) - lite-01..05 subscribes without a start don't replay the cache from group 0 - [moq-quic deletes its remaining 0-RTT code](/quest/m1/quic-zero-rtt-leftovers.md) - only the code that drops a 0-RTT packet remains, with a test ## Related diff --git a/quest/m1/js-early-latest.md b/quest/m1/js-early-latest.md deleted file mode 100644 index b005bd2dec..0000000000 --- a/quest/m1/js-early-latest.md +++ /dev/null @@ -1,13 +0,0 @@ -# [S] A JS relay starts an unresolved copy's subscription at its newest group - -## Goal - -On lite-01..05, a SUBSCRIBE with no start group served by a JS publisher from a track copy that has no groups yet starts at the newest group once the copy resolves, instead of replaying the whole cache from group 0, matching the Rust fix in [moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328). - -## Plan - -Found by the agent on [moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328). `cursorFloor` (`js/net/src/lite/publisher.ts:419-423`) returns `latest ?? 0` and is called once right after subscribe (`:890`) with `hooks.latestSequence(track)` (`js/net/src/track.ts:2249`); a related fallback is at `:1290`. Rust's fix re-reads `latest_sequence()` once the track resolves. - -Decided 2026-10-30 (maintainer, follow-up planning after the /quest-afk run). Port the Rust rule rather than invent a JS one: an unresolved copy defers the floor until it resolves, then floors at the newest group. Test a JS relay serving a copy that resolves after the SUBSCRIBE arrives. - -Public API: none. Wire: none. From c5916e79bd1053a24fd4f12b301cd56f4a7723b8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 08:56:47 -0800 Subject: [PATCH 12/16] fix(net): deliver parked groups before a failed pipelined SUBSCRIBE gives the track back A lite-06 publisher resets the SUBSCRIBE with an errored end right after the cached groups, so with the SUBSCRIBE pipelined the reset can land alongside the TRACK_INFO, ahead of the parked group streams. The serve loop gave the track back at once and lost that history (errored_end::an_errored_end_keeps_its_history_across_a_hop). It now unparks and waits for them first, as it already did for a closed session. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index cc4cfb0a17..313f258e65 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -4279,17 +4279,15 @@ mod tests { let mut received = producer.subscribe(None); subscriber.subscribes.lock().insert( 1, - TrackEntry { + TrackEntry::accepted( producer, - framing: lite::Framing { + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - start: None, - first: None, - }, + Default::default(), + ), ); if version.has_padding() { @@ -4297,7 +4295,7 @@ mod tests { for body in [vec![1], vec![1; 1200], vec![1; 1201]] { subscriber.route_datagram(body.into()).unwrap(); assert!(received.recv_datagram().now_or_never().is_none()); - assert!(!subscriber.subscribes.lock().get(&1).unwrap().producer.is_live()); + assert!(!subscriber.subscribes.lock().get(&1).unwrap().copy.as_ref().unwrap().producer.is_live()); } } @@ -8555,6 +8553,9 @@ enum ServeMode { /// subscription, after which the copy stays for its readers. track: bool, }, + /// The upstream subscription failed with this error. Group streams that beat the accept + /// arrived before it, so they are delivered first, then the track is given back. + Failed(Error), } /// Fills a pre-lite-05 upstream has no FETCH for, held while the live subscription may @@ -8885,6 +8886,14 @@ impl ServeLoop { } } } + ServeMode::Failed(_) => { + self.unpark(serve); + ready!(poll_unparked(&self.sub, serve, waiter)); + let ServeMode::Failed(err) = std::mem::replace(&mut self.mode, ServeMode::Select) else { + unreachable!() + }; + return Poll::Ready(ServeEnd::GiveBack(err)); + } ServeMode::Tail { settle, owed, track } => { // The tail waits for groups, parked ones included. if let Some(id) = self.parked.take() { @@ -9243,7 +9252,8 @@ impl ServeLoop { } Err(err) => { tracing::warn!(broadcast = %serve.subscriber.log_path(&serve.path), track = %serve.name, %err, "subscribe error"); - return Poll::Ready(ServeEnd::GiveBack(err)); + self.mode = ServeMode::Failed(err); + continue; } } } From ff6aaede623d15dae1f9975f2e3f1d300948ef71 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 09:01:27 -0800 Subject: [PATCH 13/16] style: format the merge resolution Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscriber.ts | 2 +- rs/moq-net/src/lite/subscriber.rs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 29ad8b88f3..9d804d3888 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1871,7 +1871,7 @@ export class Subscriber { }); if (!dg) return; // Gone since, too: the subscription ended while the decode awaited. - if (!entry ||this.#subscribes.get(dg.subscribe) !== entry) return; + if (!entry || this.#subscribes.get(dg.subscribe) !== entry) return; const scale = entry.timescale; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 313f258e65..c688cf0e0e 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -4295,7 +4295,18 @@ mod tests { for body in [vec![1], vec![1; 1200], vec![1; 1201]] { subscriber.route_datagram(body.into()).unwrap(); assert!(received.recv_datagram().now_or_never().is_none()); - assert!(!subscriber.subscribes.lock().get(&1).unwrap().copy.as_ref().unwrap().producer.is_live()); + assert!( + !subscriber + .subscribes + .lock() + .get(&1) + .unwrap() + .copy + .as_ref() + .unwrap() + .producer + .is_live() + ); } } From 2b9b38f333acb2b0f4517efdb4fc1b398d61f43f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 09:49:03 -0800 Subject: [PATCH 14/16] fix(net): read every accepted group stream before a failed SUBSCRIBE gives the track back The failed drain stopped once the last parked stream took its group, before that stream had read its frames, so giving the track back truncated payloads still in flight. It now waits until the subscription's tail has no stream still being read, parked or not: a group ends on its own stream's FIN or reset, as the tail already holds. The errored-end test now delays every group's frames and FIN past the reset, with the SUBSCRIBE arriving beside and before the TRACK, and reads every frame. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 33 +++++++--- rs/moq-net/src/tail.rs | 5 ++ rs/moq-net/tests/integration/errored_end.rs | 69 ++++++++++++++++++--- 3 files changed, 90 insertions(+), 17 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index c688cf0e0e..8d5b3de058 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -8564,9 +8564,14 @@ enum ServeMode { /// subscription, after which the copy stays for its readers. track: bool, }, - /// The upstream subscription failed with this error. Group streams that beat the accept - /// arrived before it, so they are delivered first, then the track is given back. - Failed(Error), + /// The upstream subscription failed with `err`. Every group stream whose header arrived + /// before it, parked on the accept or not, is read to its end first, as a group ends on + /// its own stream's FIN or reset: then the track is given back. + Failed { + err: Error, + /// The subscription's tail, whose readers say which streams are still being read. + tail: kio::Consumer, + }, } /// Fills a pre-lite-05 upstream has no FETCH for, held while the live subscription may @@ -8897,10 +8902,21 @@ impl ServeLoop { } } } - ServeMode::Failed(_) => { + ServeMode::Failed { .. } => { + // The accept counts each parked stream as read until its group settles, so + // the wait below covers the ones it opens. self.unpark(serve); - ready!(poll_unparked(&self.sub, serve, waiter)); - let ServeMode::Failed(err) = std::mem::replace(&mut self.mode, ServeMode::Select) else { + let ServeMode::Failed { tail, .. } = &self.mode else { + unreachable!() + }; + ready!( + tail.poll(waiter, |tail| match tail.is_reading() { + true => Poll::Pending, + false => Poll::Ready(()), + }) + .map(|_| ()) + ); + let ServeMode::Failed { err, .. } = std::mem::replace(&mut self.mode, ServeMode::Select) else { unreachable!() }; return Poll::Ready(ServeEnd::GiveBack(err)); @@ -9263,7 +9279,10 @@ impl ServeLoop { } Err(err) => { tracing::warn!(broadcast = %serve.subscriber.log_path(&serve.path), track = %serve.name, %err, "subscribe error"); - self.mode = ServeMode::Failed(err); + self.mode = ServeMode::Failed { + err, + tail: active.tail.consume(), + }; continue; } } diff --git a/rs/moq-net/src/tail.rs b/rs/moq-net/src/tail.rs index 07b74137da..4eeb653279 100644 --- a/rs/moq-net/src/tail.rs +++ b/rs/moq-net/src/tail.rs @@ -92,6 +92,11 @@ impl Tail { self.ledger.folds_at(group, self.ledger.grace()) } + /// Whether a data stream whose header arrived is still being read. + pub fn is_reading(&self) -> bool { + self.active > 0 + } + /// Data streams received, whether they finished or were reset. pub fn streams(&self) -> u64 { self.streams diff --git a/rs/moq-net/tests/integration/errored_end.rs b/rs/moq-net/tests/integration/errored_end.rs index 0eaefbee34..fd5501e3f0 100644 --- a/rs/moq-net/tests/integration/errored_end.rs +++ b/rs/moq-net/tests/integration/errored_end.rs @@ -14,6 +14,7 @@ use support::harness::{MockConnectOptions, connect_mock}; const TIMEOUT: Duration = Duration::from_secs(10); const GROUPS: u64 = 2; +const FRAMES: [&[u8]; 2] = [b"first", b"second"]; fn produce_origin(hop: u64) -> moq_net::origin::Producer { let (producer, driver) = moq_net::origin::Producer::new(moq_net::origin::Config::new(Hop::new(hop).unwrap())); @@ -21,8 +22,9 @@ fn produce_origin(hop: u64) -> moq_net::origin::Producer { producer } -/// Read every group from the first, returning their sequences and how the read ended. -async fn read(broadcast: &moq_net::broadcast::Consumer) -> (Vec, Option) { +/// Read every group from the first, frame by frame, returning each group's sequence and +/// payloads, and how the read ended. +async fn read(broadcast: &moq_net::broadcast::Consumer) -> (Vec<(u64, Vec>)>, Option) { let subscription = Subscription::default() .with_range(0..) .with_max_delay(Duration::from_secs(30)); @@ -36,16 +38,43 @@ async fn read(broadcast: &moq_net::broadcast::Consumer) -> (Vec, Option got.push(group.sequence), + Ok(Some(mut group)) => { + let mut frames = Vec::new(); + while let Some(frame) = kio::clock::timeout(TIMEOUT, group.read_frame()) + .await + .expect("frame timeout") + .unwrap_or_else(|err| panic!("group {} cut short: {err:?}", group.sequence)) + { + frames.push(frame.payload.to_vec()); + } + got.push((group.sequence, frames)); + } Ok(None) => return (got, None), Err(err) => return (got, Some(err)), } } } +/// What [`read`] returns for the whole history. +fn history() -> Vec<(u64, Vec>)> { + (0..GROUPS) + .map(|sequence| (sequence, FRAMES.iter().map(|frame| frame.to_vec()).collect())) + .collect() +} + +/// Each group stream's header lands, but its frames and FIN only after the subscription's +/// end, as QUIC does not order streams. A stream whose header arrived still belongs to the +/// track, so the hop reads it to its end. With the SUBSCRIBE first, the publisher's groups +/// also beat its TRACK_INFO, so they park on it and the end lands as the accept unparks them. #[kio::test] async fn an_errored_end_keeps_its_history_across_a_hop() { - for name in ["moq-lite-06", "moq-lite-07-wip"] { + for (name, subscribe_first) in [ + ("moq-lite-06", false), + ("moq-lite-06", true), + ("moq-lite-07-wip", false), + ("moq-lite-07-wip", true), + ] { + let label = format!("{name} subscribe_first={subscribe_first}"); let version: Version = name.parse().unwrap(); let publisher = produce_origin(1); let broadcast = publisher.create_broadcast("bcast").unwrap(); @@ -54,7 +83,9 @@ async fn an_errored_end_keeps_its_history_across_a_hop() { broadcast.announce(Default::default()).unwrap(); for _ in 0..GROUPS { let mut group = track.append_group().unwrap(); - group.write_frame(Timestamp::ZERO, &b"frame"[..]).unwrap(); + for frame in FRAMES { + group.write_frame(Timestamp::ZERO, frame).unwrap(); + } group.finish().unwrap(); } track.finish_at(Position::group(GROUPS), Some(Error::App(7))).unwrap(); @@ -69,16 +100,34 @@ async fn an_errored_end_keeps_its_history_across_a_hop() { .expect("resolve timeout") .expect("broadcast resolves"); - let first = read(&remote).await; + session.server_transport.split_unis(); + if subscribe_first { + session.client_transport.hold_bidis(); + } + let release = async { + if subscribe_first { + kio::clock::sleep(Duration::from_millis(1)).await; + session.client_transport.release_bidis_reversed(); + } + kio::clock::sleep(Duration::from_millis(100)).await; + if name != "moq-lite-07-wip" { + assert!( + session.server_transport.resets().contains(&(64 + 7)), + "{label}: the reset was not sent before the frames" + ); + } + session.server_transport.release_split(); + }; + let (first, ()) = futures::join!(read(&remote), release); let later = read(&remote).await; for (what, (got, err)) in [("first", first), ("later", later)] { - assert_eq!(got, (0..GROUPS).collect::>(), "{name} {what}"); + assert_eq!(got, history(), "{label} {what}"); match name == "moq-lite-07-wip" { true => assert!( matches!(err, Some(Error::Stream(StreamError::App(7)))), - "{name} {what}: {err:?}" + "{label} {what}: {err:?}" ), - false => assert!(err.is_some(), "{name} {what}: ended cleanly"), + false => assert!(err.is_some(), "{label} {what}: ended cleanly"), } } // Lite-07 ends the subscription after SUBSCRIBE_END rather than resetting it. @@ -86,7 +135,7 @@ async fn an_errored_end_keeps_its_history_across_a_hop() { assert_eq!( reset, name != "moq-lite-07-wip", - "{name}: resets {:?}", + "{label}: resets {:?}", session.server_transport.resets() ); drop((session, track, broadcast, publisher, subscriber)); From 23c135ccb926eb2423737acd1eac396d2e242598 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 10:03:08 -0800 Subject: [PATCH 15/16] fix(net): release a failed subscription's copy once nobody holds it The failed drain waited only for its streams, so a stream the publisher never finished kept the subscription, its copy and its stream after every reader left. It now drives the fetches and takes the idle teardown once the copy is unwanted, as the tail does. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 105 ++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 8d5b3de058..0f7aeafc61 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -4237,6 +4237,92 @@ mod tests { } } + /// A failed subscription reads a stream whose header arrived to its end before it gives + /// the track back, but only while someone holds the copy: once nobody does, a stream the + /// publisher never finishes cannot keep the subscription. + #[kio::test] + async fn a_failed_subscription_drains_only_while_wanted() { + for wanted in [true, false] { + let version = Version::Lite06; + let mut responses = Vec::new(); + lite::SubscribeResponse::Start(lite::SubscribeStart { + largest: None, + starts: vec![Some(0)], + }) + .encode( + &mut crate::coding::Encoder::new(&mut responses, version.into()), + version, + ) + .unwrap(); + let session = crate::lite::test_transport::ScriptedSession::new(responses); + let subscriber = Subscriber::new(SubscriberConfig { + runtime: crate::time::sim(), + session: crate::driver::Transport::new(session.clone()), + origin: origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + recv_bandwidth: None, + version, + peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Address::Setup), + peer_hop: None, + cost: None, + going_away: Default::default(), + auth: crate::auth::Link::new(false), + }); + let serve = TrackServe { + validity: Default::default(), + route: None, + subscriber, + path: Path::from_static("room"), + epoch: None, + name: "video".to_string(), + }; + let broadcast = crate::broadcast::Info::new().produce(); + let serving = ServeLoop::new(&serve, broadcast.reserve_track("video").unwrap(), crate::track::Info::timed()); + let mut reader = broadcast + .consume() + .track("video") + .unwrap() + .subscribe(None) + .await + .unwrap(); + let gate = serve.gate(); + let mut running = TrackServeRun { + serve, + state: TrackRunState::Serve(serving), + gate, + }; + assert!(kio::Task::poll(&mut running, &kio::Waiter::noop()).is_pending()); + + // A group stream whose header arrived, still being read. + let reading = { + let subscribes = running.serve.subscriber.subscribes.lock(); + let (_, entry) = subscribes.iter().next().expect("subscribed"); + Reading::open(&entry.tail, Some(0), running.serve.subscriber.runtime.now()) + }; + session.close(crate::lite::test_transport::Close::ResetStream(64 + 7)); + assert!( + kio::Task::poll(&mut running, &kio::Waiter::noop()).is_pending(), + "wanted={wanted}: the stream being read holds the track" + ); + + match wanted { + true => { + drop(reading); + assert!(kio::Task::poll(&mut running, &kio::Waiter::noop()).is_ready()); + assert!(reader.recv_group().await.is_err(), "the track is given back failed"); + } + false => { + drop(reader); + assert!( + kio::Task::poll(&mut running, &kio::Waiter::noop()).is_ready(), + "nobody holds the copy" + ); + assert!(running.serve.subscriber.subscribes.lock().is_empty()); + drop(reading); + } + } + } + } + /// Both runtimes abort a track whose tail the other's publisher still owed when the /// session died. #[kio::test] @@ -8909,13 +8995,24 @@ impl ServeLoop { let ServeMode::Failed { tail, .. } = &self.mode else { unreachable!() }; - ready!( - tail.poll(waiter, |tail| match tail.is_reading() { + let drained = tail + .poll(waiter, |tail| match tail.is_reading() { true => Poll::Pending, false => Poll::Ready(()), }) - .map(|_| ()) - ); + .is_ready(); + let _ = self.fetches.poll(waiter); + self.feed_fetches + .poll(&self.sub, serve.subscriber.runtime.now(), waiter); + if !drained { + // The groups are drained only for a reader: once nobody holds the + // copy, a stream the publisher never finishes must not hold it. + if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() + { + return Poll::Ready(ServeEnd::Idle); + } + return Poll::Pending; + } let ServeMode::Failed { err, .. } = std::mem::replace(&mut self.mode, ServeMode::Select) else { unreachable!() }; From f1e690d6d11ce2600fbdd7210974255a4b4e5f7c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 10:07:07 -0800 Subject: [PATCH 16/16] style: format the failed-drain fix --- rs/moq-net/src/lite/subscriber.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index c8bc227be6..1ddef7bac5 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -4313,7 +4313,11 @@ mod tests { name: "video".to_string(), }; let broadcast = crate::broadcast::Info::new().produce(); - let serving = ServeLoop::new(&serve, broadcast.reserve_track("video").unwrap(), crate::track::Info::timed()); + let serving = ServeLoop::new( + &serve, + broadcast.reserve_track("video").unwrap(), + crate::track::Info::timed(), + ); let mut reader = broadcast .consume() .track("video") @@ -9044,8 +9048,7 @@ impl ServeLoop { if !drained { // The groups are drained only for a reader: once nobody holds the // copy, a stream the publisher never finishes must not hold it. - if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() - { + if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() { return Poll::Ready(ServeEnd::Idle); } return Poll::Pending;