From e528387867498a5caf35a26e4ac874242dbad783 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 03:28:10 -0700 Subject: [PATCH 1/7] chore(quest): claim quest/m2/pipeline-requests/fetch.md Quest-Claim: 0f1c7f0952e9f4c5 From cdc76247036eddcd88635f7b4438ca5ce6ea558e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 04:06:07 -0700 Subject: [PATCH 2/7] feat(net): send the first FETCH alongside the track's info request Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/ietf/publisher.rs | 77 ++++- rs/moq-net/src/ietf/subscriber.rs | 124 ++++++-- rs/moq-net/src/lite/subscriber.rs | 173 +++++++--- rs/moq-net/src/model/origin.rs | 20 +- rs/moq-net/src/model/resume.rs | 300 ++++++++++++++++-- rs/moq-net/src/model/track.rs | 75 ++++- rs/moq-net/src/util.rs | 4 + rs/moq-net/src/util/later.rs | 60 ++++ .../tests/integration/fetch_pipeline.rs | 131 ++++++++ rs/moq-net/tests/integration/main.rs | 1 + 10 files changed, 848 insertions(+), 117 deletions(-) create mode 100644 rs/moq-net/src/util/later.rs create mode 100644 rs/moq-net/tests/integration/fetch_pipeline.rs diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index e1e2dcd18b6..f5b2151a1de 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -175,7 +175,27 @@ async fn prepare_fetch( let reason = err.to_string(); (err, reason) }; - track.info().await.map_err(failed)?; + // Asking for the group needs nothing from the track's info, only judging and answering + // it: a relay's FETCH goes upstream alongside the request for the info, driven while + // that resolves. + let mut ahead = largest_filter.is_none().then(|| { + Ahead::Fetching(track.fetch_group( + range.start.group, + group::Fetch { + priority, + ..Default::default() + }, + )) + }); + let info = track.info(); + kio::wait(|waiter| { + if let Some(ahead) = &mut ahead { + ahead.poll(waiter); + } + info.poll_ok(waiter) + }) + .await + .map_err(failed)?; // The cache's newest object is Largest Object while a live feed writes the track, or once // the track ended at its newest group. A fetch-only copy retains its publisher's report // independently. A joining FETCH ends where its subscription starts instead. @@ -238,12 +258,36 @@ async fn prepare_fetch( range.until(range.start.group), priority, compression, + ahead, ) .await .map_err(failed)?; Ok(PreparedFetch { range, last }) } +/// A whole-group fetch made before the track's info resolved. +enum Ahead { + Fetching(kio::Pending), + Fetched(Result), +} + +impl Ahead { + fn poll(&mut self, waiter: &kio::Waiter) { + if let Self::Fetching(fetching) = self + && let Poll::Ready(result) = kio::Task::poll(&mut **fetching, waiter) + { + *self = Self::Fetched(result); + } + } + + async fn fetched(self) -> Result { + match self { + Self::Fetching(fetching) => fetching.await, + Self::Fetched(result) => result, + } + } +} + /// The group a FETCH answers with, read out of the cache so a later eviction cannot /// truncate what FETCH_OK already promised. struct FetchedGroup { @@ -311,7 +355,8 @@ impl FetchedGroup { /// `until`, or through the end of the group without one. /// /// This is a [`track::Consumer::fetch_group`], so a relay fetches a miss upstream. The -/// track's info resolves first, as it would for a SUBSCRIBE. +/// track's info resolves first, as it would for a SUBSCRIBE; `ahead` is the whole group +/// already asked for, used for a miss. async fn read_fetch( track: &track::Consumer, sequence: u64, @@ -319,6 +364,7 @@ async fn read_fetch( until: Option, priority: i8, compression: bool, + ahead: Option, ) -> Result { let info = track.info().await?; let cached = track.peek_group(sequence); @@ -335,16 +381,21 @@ async fn read_fetch( (None, Some(_)) => skip, (_, None) => 0, }; - let mut group = track - .fetch_group( - sequence, - group::Fetch { - priority, - frame_start, - ..Default::default() - }, - ) - .await?; + let mut group = match ahead { + Some(ahead) if frame_start == 0 => ahead.fetched().await?, + _ => { + track + .fetch_group( + sequence, + group::Fetch { + priority, + frame_start, + ..Default::default() + }, + ) + .await? + } + }; if compression { group = group.compressed(); } @@ -6677,7 +6728,7 @@ mod subscribe_cursor_test { open.write_frame(crate::Timestamp::ZERO, b"a".to_vec()).unwrap(); let consumer = track.consume(); - let read = moq_clock::sim::spawn(async move { read_fetch(&consumer, 0, 0, None, 0, false).await.map(|_| ()) }); + let read = moq_clock::sim::spawn(async move { read_fetch(&consumer, 0, 0, None, 0, false, None).await.map(|_| ()) }); moq_clock::sim::yield_now().await; // The track runs a full second past the group's successor. for ms in [1000, 2000] { diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index f70b2ec62b2..daf6ce16b36 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -18,6 +18,7 @@ use crate::{ use super::{Message, Version, cluster, error::request, group::ObjectExtensionsLength, peer, request_update}; use crate::tail::{Reading, Settle, Tail}; +use crate::util::Later; use kio::Lock; @@ -2890,7 +2891,7 @@ impl Subscriber { .is_none_or(|subscription| subscription.ranges.is_empty()) => { let Some(idle) = self - .track_status(&broadcast_path, &track_name, request, &mut gate) + .track_status(&broadcast_path, &track_name, request, &mut group_fetches, &mut gate) .await else { return; @@ -3772,7 +3773,7 @@ impl Subscriber { broadcast_path.clone(), track_name.to_owned(), requested, - timescale, + Later::Known(timescale), ); group_fetches.push(fetch); } @@ -4001,7 +4002,7 @@ impl Subscriber { Step::Fill(fill) => { let fetch = self.clone() - .serve_fill(broadcast_path.clone(), track_name.to_owned(), fill, idle.timescale); + .serve_fill(broadcast_path.clone(), track_name.to_owned(), fill, Later::Known(idle.timescale)); group_fetches.push(fetch); } Step::Subscribe => break true, @@ -4283,13 +4284,50 @@ impl Subscriber { /// Learn a track nobody subscribes to from TRACK_STATUS, accepting an idle copy that /// [`Self::linger`] serves fetches from and subscribes once someone does. `None` once /// refused or abandoned: a refused status fails the query-only or fetch-only request. + /// + /// Fetches go out meanwhile into `group_fetches`, reading their objects only once the + /// status says the timescale, and failing with it. async fn track_status( &mut self, broadcast_path: &Path, track_name: &str, request: track::Request, + group_fetches: &mut TaskSet, gate: &mut crate::auth::Gate, ) -> Option { + let timescale = crate::util::Learning::new(); + let learned = self + .learn_track_status(broadcast_path, track_name, request, group_fetches, ×cale, gate) + .await; + match learned { + Ok(idle) => { + timescale.resolve(Ok(idle.timescale)); + Some(idle) + } + Err(err) => { + // The fetches sent alongside fail with the status: let them answer first. + timescale.resolve(Err(err)); + self.drain(group_fetches).await; + None + } + } + } + + /// [`Self::track_status`]'s exchange, failing with why the request was refused or + /// abandoned. + + async fn learn_track_status( + &mut self, + broadcast_path: &Path, + track_name: &str, + request: track::Request, + group_fetches: &mut TaskSet, + timescale: &crate::util::Learning>, + gate: &mut crate::auth::Gate, + ) -> Result { + // Registered before accepting: `accept` releases the request's own fetch gate, and a + // cache-miss fetch queued in the gap would be drained as NotFound. + let dynamic = request.requests(); let validity = self.validity.start(); let opening = self.session.clone(); let mut stream = match kio::wait(|waiter| { @@ -4303,8 +4341,8 @@ impl Subscriber { { Ok(stream) => stream, Err(err) => { - request.reject(err); - return None; + request.reject(err.clone()); + return Err(err); } }; stream = stream.with_stats(self.origin.stats()); @@ -4323,14 +4361,15 @@ impl Subscriber { }, ); if let Err(err) = written { - request.reject(err); - return None; + request.reject(err.clone()); + return Err(err); } // Nobody wanting the track any more ends the wait on a peer that may never answer, // and the session ending fails it with the session's reason. let mut demand = request.demand(); let closed = self.session.clone(); + let this = self.clone(); let response = { let mut response = std::pin::pin!(async { kio::wait(|waiter| stream.writer.poll_flush(waiter)).await?; @@ -4354,6 +4393,17 @@ impl Subscriber { if let Poll::Ready(err) = closed.poll_closed(waiter) { return Poll::Ready(Some(SetupResponse::Cancelled(Error::from_transport(err)))); } + // A fetch needs nothing from the status to go out, only to read its objects. + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(waiter) { + let fetch = this.clone().serve_fill( + broadcast_path.clone(), + track_name.to_owned(), + fill, + timescale.later(), + ); + group_fetches.push(fetch); + } + let _ = group_fetches.poll(waiter); crate::util::poll_unwanted(&mut demand, waiter).map(|_| None) }) .await; @@ -4367,15 +4417,15 @@ impl Subscriber { let response = match response { Some(SetupResponse::Response(response)) => response, Some(SetupResponse::Cancelled(err)) => { - request.reject(err); + request.reject(err.clone()); stream.reader.abort(&Error::Cancel); let _ = stream.writer.close().await; - return None; + return Err(err); } None => { stream.reader.abort(&Error::Cancel); stream.writer.abort(&Error::Cancel); - return None; + return Err(Error::Cancel); } }; let _ = stream.writer.finish(); @@ -4391,13 +4441,12 @@ impl Subscriber { Ok(ok) => ok, Err(err) => { tracing::debug!(%err, broadcast = %crate::path::Rooted(self.origin.root(), broadcast_path), track = %track_name, "track status refused"); - request.reject(err); - return None; + request.reject(err.clone()); + return Err(err); } }; tracing::debug!(broadcast = %crate::path::Rooted(self.origin.root(), broadcast_path), track = %track_name, "track status known"); - let dynamic = request.requests(); // Nothing feeds this copy live: what it caches is fetched, never the live edge. Idle // from the accept, so no reader takes it as live in between. let track = request.idle().stage(accepted_info(&ok.properties, self.version)); @@ -4408,7 +4457,7 @@ impl Subscriber { track.report_object(largest.group, largest.object); } let track = track.commit(); - Some(Idle { + Ok(Idle { demand: track.demand(), track, dynamic, @@ -5326,7 +5375,7 @@ impl Subscriber { /// group always goes alone: a run including the group its source is still writing would /// hold the earlier groups until it finishes, or be cut short at the Largest Object by a /// publisher following draft-22 section 9.11. - async fn serve_fill(self, broadcast: Path, name: String, fill: track::Fill, timescale: Option) { + async fn serve_fill(self, broadcast: Path, name: String, fill: track::Fill, timescale: Later>) { let fill = std::sync::Arc::new(fill); let mut last = None; while fill.is_used() @@ -5353,7 +5402,7 @@ impl Subscriber { } last = Some(run.clone()); self.clone() - .run_range_fetch(broadcast.clone(), name.clone(), fill.clone(), run, timescale) + .run_range_fetch(broadcast.clone(), name.clone(), fill.clone(), run, timescale.clone()) .await; } } @@ -5362,7 +5411,7 @@ impl Subscriber { #[cfg(test)] async fn run_group_fetch(self, broadcast: Path, name: String, fill: track::Fill, timescale: Option) { let run = fill.start().group..fill.end(); - self.run_range_fetch(broadcast, name, std::sync::Arc::new(fill), run, timescale) + self.run_range_fetch(broadcast, name, std::sync::Arc::new(fill), run, Later::Known(timescale)) .await; } @@ -5380,7 +5429,7 @@ impl Subscriber { name: String, fill: std::sync::Arc, run: std::ops::Range, - timescale: Option, + timescale: Later>, ) { let validity = self.validity.start(); let last = run.end - 1; @@ -5400,6 +5449,10 @@ impl Subscriber { if fill.poll_unwanted_groups(run.clone(), waiter).is_ready() { return Poll::Ready(Err(Error::Cancel)); } + // A TRACK_STATUS sent alongside that failed fails the fetch. + if let Poll::Ready(Err(err)) = timescale.poll(waiter) { + return Poll::Ready(Err(err)); + } Stream::poll_open(&opening, self.version, waiter) }) .await @@ -5452,8 +5505,8 @@ impl Subscriber { fetch_type, range_filters: false, fill_timeout: false, - // The copy learned the track before any fetch, from SUBSCRIBE_OK or - // TRACK_STATUS_OK, so a FETCH_OK repeating it is waste. + // The copy learns the track from SUBSCRIBE_OK or TRACK_STATUS_OK, so a + // FETCH_OK repeating it is waste. properties_wanted: false, epoch: self.epoch.clone(), }, @@ -5477,6 +5530,9 @@ impl Subscriber { if let Poll::Ready(err) = fill.poll_aborted(waiter) { return Poll::Ready(SetupResponse::Cancelled(err)); } + if let Poll::Ready(Err(err)) = timescale.poll(waiter) { + return Poll::Ready(SetupResponse::Cancelled(err)); + } if let Poll::Ready(res) = response { return Poll::Ready(SetupResponse::Response(res)); } @@ -5503,6 +5559,32 @@ impl Subscriber { } }; + // The objects are stamped in the track's timescale, which the TRACK_STATUS sent + // alongside may still owe: they wait unread in their stream until it lands, and its + // failure fails the fetch. + let timescale = match kio::wait(|waiter| { + if let Err(err) = validity.check(waiter) { + return Poll::Ready(Err(err)); + } + if let Poll::Ready(err) = fill.poll_aborted(waiter) { + return Poll::Ready(Err(err)); + } + if let Poll::Ready(res) = timescale.poll(waiter) { + return Poll::Ready(res); + } + fill.poll_unwanted_groups(run.clone(), waiter).map(|_| Err(Error::Cancel)) + }) + .await + { + Ok(timescale) => timescale, + Err(err) => { + fill.reject_groups(run, err); + drop(registered); + self.cancel_group_fetch(stream).await; + return; + } + }; + // Draft-22's End Location is inclusive; older drafts spell a bounded object one past it. let mut end = ok.end_location; if draft22 { @@ -7699,7 +7781,7 @@ mod tests { SetupWait::StatusOpen | SetupWait::StatusWrite | SetupWait::StatusResponse => Box::pin(async { assert!( subscriber - .track_status(&Path::from_static("broadcast"), "video", request, &mut gate) + .track_status(&Path::from_static("broadcast"), "video", request, &mut TaskSet::owned(), &mut gate) .await .is_none() ); diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 8dd35ae830f..e182754007e 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -4003,7 +4003,7 @@ mod tests { let dynamic = track.requests(); let fetch = track.consume().fetch_group(4, None); let request = dynamic.requested_fill_group().await.unwrap(); - let mut run = FetchServeRun::new(serve, request, info); + let mut run = FetchServeRun::new(serve, request, FetchInfo::Known(info)); let waiter = kio::Waiter::noop(); assert!(kio::Task::poll(&mut run, &waiter).is_pending(), "{version}: {end}"); assert_eq!(session.log.bi_opens(), 0); @@ -7063,10 +7063,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. + /// without a per-response header. Fetches go out meanwhile, and read their + /// response only once the info lands. Info { request: Option, info: TrackInfoFetch, + dynamic: track::Requests, + fetches: kio::Tasks, + known: crate::util::Learning, }, Serve(ServeLoop), /// Preserve the lite07 completion FIN until the publisher acknowledges it. @@ -7078,8 +7082,13 @@ impl TrackServeRun { fn new(serve: TrackServe, request: track::Request) -> Self { let state = if serve.subscriber.version.has_track_stream() { TrackRunState::Info { + // Registered before accepting: `accept` releases the request's own fetch gate, + // and a cache-miss fetch queued in the gap would be drained as NotFound. + dynamic: request.requests(), request: Some(request), info: TrackInfoFetch::new(&serve), + fetches: kio::Tasks::new(), + known: crate::util::Learning::new(), } } else { // Older wires declare no publisher retention limit, and no timeline: their @@ -7096,7 +7105,9 @@ 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, known, .. } => { + // Each fetch sent alongside resets itself as it drops, seeing the denial. + known.resolve(Err(Error::Unauthorized)); if let Some(request) = request { request.reject(Error::Unauthorized); } @@ -7132,7 +7143,9 @@ 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 { known, .. } = std::mem::replace(&mut self.state, TrackRunState::Done) { + known.resolve(Err(Error::Cancel)); + } return Poll::Ready(()); } // Demand returned in the gap and won inside `reject_unused`: poll again so the @@ -7162,10 +7175,31 @@ impl kio::Task for TrackServeRun { } loop { match &mut self.state { - TrackRunState::Info { request, info } => { + TrackRunState::Info { + request: _, + info, + dynamic, + fetches, + known, + } => { + // A fetch needs nothing from TRACK_INFO to go out, only to read its answer. + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(waiter) { + fetches.push(FillServeRun::new(self.serve.clone(), fill, known.later())); + } + let _ = fetches.poll(waiter); // Abandonment was checked at the top of this poll, arming its wait. let res = ready!(info.poll_fetch(&self.serve, waiter)); - let request = request.take().expect("request pending"); + let TrackRunState::Info { + request, + dynamic, + fetches, + known, + .. + } = std::mem::replace(&mut self.state, TrackRunState::Done) + else { + unreachable!() + }; + let request = request.expect("request pending"); match res { Ok((mut info, held)) => { // Lite05/06 timestamps may be send times, so the copy is untimed @@ -7173,18 +7207,22 @@ impl kio::Task for TrackServeRun { if !self.serve.subscriber.version.has_media_time() { info.timescale = None; } - let mut serve_loop = ServeLoop::new(&self.serve, request, info); + let mut serve_loop = ServeLoop::adopt(&self.serve, request, info.clone(), dynamic, fetches); serve_loop.held = Some(held); + // Only once accepted: a fetch answered now caches into the accepted copy. + known.resolve(Ok(info)); self.state = TrackRunState::Serve(serve_loop); } Err(err) => { let err = lite::error::control(&mut self.serve.subscriber.session.clone(), err); tracing::warn!(broadcast = %self.serve.subscriber.log_path(&self.serve.path), track = %self.serve.name, %err, "track info failed"); + // The fetches sent alongside fail with it, resetting their streams. + known.resolve(Err(err.clone())); + drop(fetches); // Rejecting the request lets the origin retry (bounded) on // another source; waiting subscribers stall rather than error // meanwhile. request.reject(err); - self.state = TrackRunState::Done; return Poll::Ready(()); } } @@ -7243,13 +7281,15 @@ 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 { + let TrackRunState::Info { request, known, .. } = &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(); + known.resolve(Err(err.clone())); + request.reject(err); } } @@ -7621,10 +7661,21 @@ impl FeedFetches { impl ServeLoop { fn new(serve: &TrackServe, request: track::Request, info: track::Info) -> Self { - // Register the fetch handler before accepting: `accept` releases the - // request's own fetch gate, and a cache-miss fetch queued while TRACK_INFO - // was in flight would be drained as NotFound in the gap. + // Register the fetch handler before accepting: `accept` releases the request's own + // fetch gate, and a cache-miss fetch queued in the gap would be drained as NotFound. let dynamic = request.requests(); + Self::adopt(serve, request, info, dynamic, kio::Tasks::new()) + } + + /// Accept `request` with `info`, serving fetches through `dynamic`, registered before the + /// accept, and carrying on with the `fetches` already in flight. + fn adopt( + serve: &TrackServe, + request: track::Request, + info: track::Info, + dynamic: track::Requests, + fetches: kio::Tasks, + ) -> Self { // Lite-06+ resolves each subscription's start from its budget, so the floor // the SUBSCRIBE asks for is not where delivery begins until SUBSCRIBE_START says. let request = match serve.subscriber.version.resolves_start() { @@ -7648,7 +7699,7 @@ impl ServeLoop { serving, dynamic, sub: Sub::None, - fetches: kio::Tasks::new(), + fetches, feed_fetches: FeedFetches::new(&serve.subscriber.runtime), closed: serve.subscriber.session.clone(), supports_update: !matches!(serve.subscriber.version, Version::Lite01 | Version::Lite02), @@ -7808,8 +7859,11 @@ impl ServeLoop { match self.dynamic.poll_requested_fill(waiter) { Poll::Ready(Ok(fill)) => { if self.supports_fetch { - self.fetches - .push(FillServeRun::new(serve.clone(), fill, self.serving.info().clone())); + self.fetches.push(FillServeRun::new( + serve.clone(), + fill, + FetchInfo::Known(self.serving.info().clone()), + )); } else { // No FETCH on the wire: wait for the live feed, which a // subscription arriving alongside may still be opening. @@ -8066,17 +8120,36 @@ impl ServeLoop { } } +/// The track's info for a fetch: known, or still in the TRACK_INFO sent alongside it, which +/// the fetch's response waits for unread in the stream. +type FetchInfo = crate::util::Later; + +/// Why the TRACK_INFO a fetch was sent alongside failed, once it has. +fn info_failed(info: &FetchInfo) -> Option { + info.peek()?.err() +} + /// Serves one fill a group at a time, newest first for a descending reader, each with its /// own [`FetchServeRun`], until it is answered or nobody waits on it. struct FillServeRun { serve: TrackServe, fill: track::Fill, - info: track::Info, + info: FetchInfo, run: Option<(u64, FetchServeRun)>, } +impl Drop for FillServeRun { + fn drop(&mut self) { + // The fetch went out before TRACK_INFO, which failed: that is the answer for every + // group still owed, not a miss. + if let Some(err) = info_failed(&self.info) { + self.fill.refuse(err); + } + } +} + impl FillServeRun { - fn new(serve: TrackServe, fill: track::Fill, info: track::Info) -> Self { + fn new(serve: TrackServe, fill: track::Fill, info: FetchInfo) -> Self { Self { serve, fill, @@ -8121,14 +8194,14 @@ impl kio::Task for FillServeRun { /// Serves one downstream fetch end-to-end on its own bidi stream: send FETCH, /// then fill the group from the bare FRAME messages that follow. The track's -/// info comes from its TRACK_INFO (already known), and the group sequence -/// is implicit from the request. +/// info comes from its TRACK_INFO, which the response waits for unread if it +/// is still in flight, and the group sequence is implicit from the request. struct FetchServeRun { validity: crate::model::broadcast::RequestValidity, gate: crate::auth::Gate, serve: TrackServe, session: Transport, - info: track::Info, + info: FetchInfo, group: u64, state: FetchRunState, } @@ -8172,7 +8245,7 @@ impl FetchRunState { } impl FetchServeRun { - fn new(serve: TrackServe, request: track::GroupFill, info: track::Info) -> Self { + fn new(serve: TrackServe, request: track::GroupFill, info: FetchInfo) -> Self { let session = serve.subscriber.session.clone(); let group = request.sequence(); Self { @@ -8189,19 +8262,25 @@ impl FetchServeRun { impl Drop for FetchServeRun { fn drop(&mut self) { - // Dropped because the session stopped allowing the broadcast: reset with that - // reason, so neither the publisher nor the waiting reader reads a routine cancel. - // Any other drop keeps the default cancel. - if matches!(self.state, FetchRunState::Done) - || self - .serve - .subscriber - .auth - .allows(crate::auth::Direction::Subscribe, self.serve.path.as_str()) - { + // Dropped because the session stopped allowing the broadcast, or because the + // TRACK_INFO sent alongside failed: reset with that reason, so neither the publisher + // nor the waiting reader reads a routine cancel. Any other drop keeps the default + // cancel. + if matches!(self.state, FetchRunState::Done) { return; } - let err = Error::Unauthorized; + let err = match self + .serve + .subscriber + .auth + .allows(crate::auth::Direction::Subscribe, self.serve.path.as_str()) + { + false => Error::Unauthorized, + true => match info_failed(&self.info) { + Some(err) => err, + None => return, + }, + }; match std::mem::replace(&mut self.state, FetchRunState::Done) { FetchRunState::Open { request } => { if let Some(request) = request { @@ -8289,13 +8368,16 @@ impl kio::Task for FetchServeRun { // consumer), and is more reusable in the cache than the tail would have // been. Asking for the offset anyway would fail to encode and reject a // fetch we can serve. + // Until TRACK_INFO says, the group may be compressed, which only a whole + // group decodes: ask for all of it. + let plain = matches!(self.info.peek(), Some(Ok(info)) if !info.compress); let frame_start = match self.serve.subscriber.version.has_frame_bounds() { - true if !self.info.compress => request.frame_start(), + true if plain => request.frame_start(), true => 0, false => 0, }; - let frame_end = if self.serve.subscriber.version.has_frame_bounds() && !self.info.compress { + let frame_end = if self.serve.subscriber.version.has_frame_bounds() && plain { request.frame_end() } else { None @@ -8362,6 +8444,23 @@ impl kio::Task for FetchServeRun { }; } FetchRunState::Answer { stream, .. } => { + // The answer is framed in the track's units: it stays unread in the stream + // until the TRACK_INFO sent alongside lands. + let info = match ready!(self.info.poll(waiter)) { + Ok(info) => info, + Err(err) => { + let FetchRunState::Answer { + request, mut stream, .. + } = std::mem::replace(&mut self.state, FetchRunState::Done) + else { + unreachable!() + }; + stream.writer.abort(&err); + stream.reader.abort(&err); + request.expect("request pending").reject(err); + return Poll::Ready(()); + } + }; let ietf = ready!(self.serve.subscriber.peer_setup.poll_ietf(waiter)); // Lite has no FETCH_OK: a publisher without the group resets the // stream instead. Accepting before the first byte (or a FIN, for an @@ -8389,7 +8488,7 @@ impl kio::Task for FetchServeRun { // it. The track::Info only takes effect if the track isn't accepted yet // (a fetch with no live subscription); otherwise the group inherits the // accepted info. - let group_info = self.info.clone(); + let group_info = info.clone(); // The group's readers pick it up from the cache only when next polled, // so their demand outlives the request until then. let readers = request.readers(); @@ -8414,7 +8513,7 @@ impl kio::Task for FetchServeRun { producer: Box::new(crate::recv::Group::new(producer)), readers, ingest: Box::new(FrameIngest::new( - lite::Framing::new(&self.info, self.serve.subscriber.version), + lite::Framing::new(&info, self.serve.subscriber.version), self.serve.subscriber.version, ietf, frame_start, diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 105cb616a4b..f2b74574b7b 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -3122,6 +3122,9 @@ struct Asked { /// query. Kept once spliced until the last reader leaves, so the copy is never left /// with nobody subscribed while readers are still subscribing to it on their own. sub: Option>, + /// The copy staged for fetches while its info is in flight, withdrawn if it is never + /// spliced. + staged: super::resume::Staged, } /// The driver's side of one logical track: the handles behind the names the @@ -3157,9 +3160,9 @@ impl TrackIo { self.ready.wake_by_ref(); if let Some(request) = self.request.take() { let info = asked.info.clone().unwrap_or_default(); - self.accepted = Some(request.routes(self.routes.consume()).accept(info)); + self.accepted = Some(request.accept(info)); } - self.routes.serve(asked.copy.clone()); + self.routes.splice(asked.copy.clone(), asked.staged); self.copy = Some((asked.source, asked.copy, asked.query)); self.held = asked.sub; } @@ -3173,8 +3176,11 @@ impl TrackIo { self.copy = None; self.held = None; match self.request.take() { - // Ended before any route served it: nothing to read. - Some(request) => request.reject(result.err().unwrap_or(Error::NotFound)), + // Ended before any route served it: nothing to read, nor to fetch. + Some(request) => { + self.routes.end(result.clone()); + request.reject(result.err().unwrap_or(Error::NotFound)); + } None => { if let Some(accepted) = self.accepted.take() { let _ = match &result { @@ -3657,6 +3663,7 @@ async fn serve_front(task: FrontTask) -> Vec { let copy = query.track(); io.query = Some(Asked { source, + staged: io.routes.stage(copy.clone()), copy, query, info: None, @@ -3922,15 +3929,16 @@ async fn serve_front(task: FrontTask) -> Vec { let name: Arc = request.name().into(); let track_ready = crate::util::QueueWake::new(name.clone(), ready.clone()); track_ready.wake_by_ref(); + let routes = super::resume::Producer::new(); tracks.insert( name.clone(), TrackIo { ready: track_ready, park: Default::default(), weak: request.weak(), - request: Some(request), + request: Some(request.routes(routes.consume())), accepted: None, - routes: super::resume::Producer::new(), + routes, query: None, staged: None, copy: None, diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 4c2ce3b5d6a..bac050a3f27 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -65,6 +65,13 @@ struct Route { parked: Option, /// Whether the front may replace the copy, or how it finished. phase: Phase, + /// The copy the front asked for while its info is in flight, by stage id: a fetch goes + /// to it at once, and hands its group out only once that copy is spliced. + staged: Option<(u64, track::Consumer)>, + /// The stage the serving copy was spliced from, if any. + spliced: Option, + /// The id the next stage takes. + next_stage: u64, } #[derive(Default)] @@ -149,12 +156,45 @@ impl Producer { demand } + /// Stage `copy`, whose info is still in flight, for fetches until the returned + /// [`Staged`] is spliced with [`Self::splice`] or dropped. + pub(super) fn stage(&self, copy: track::Consumer) -> Staged { + let id = match self.state.write() { + Ok(mut route) => { + let id = route.next_stage; + route.next_stage += 1; + route.staged = Some((id, copy)); + id + } + Err(_) => u64::MAX, + }; + Staged { + id, + state: self.state.clone(), + } + } + /// Serve the track from `copy` from now on, subscribing every reader to it. + #[cfg(test)] pub(super) fn serve(&self, copy: track::Consumer) { + self.serve_from(copy, None); + } + + /// Serve the track from `copy`, the one `staged` was made for, subscribing every reader + /// to it: what was fetched from it is handed out from here on. + pub(super) fn splice(&self, copy: track::Consumer, staged: Staged) { + self.serve_from(copy, Some(staged.id)); + } + + fn serve_from(&self, copy: track::Consumer, stage: Option) { if let Ok(mut route) = self.state.write() { route.generation += 1; route.copy = Some(copy); route.parked = None; + route.spliced = stage; + if stage.is_some() && route.staged.as_ref().map(|(id, _)| *id) == stage { + route.staged = None; + } } let readers = { let mut readers = self.readers.lock().unwrap_or_else(PoisonError::into_inner); @@ -176,6 +216,7 @@ impl Producer { { route.generation += 1; route.parked = route.copy.take(); + route.spliced = None; } } @@ -212,6 +253,7 @@ impl Producer { if let Ok(mut route) = self.state.write() { route.copy = None; route.parked = None; + route.spliced = None; } } @@ -229,6 +271,24 @@ impl Drop for Producer { } } +/// A copy staged with [`Producer::stage`]. Dropping it unspliced (its info was refused, its +/// source left, or nobody reads the track) withdraws it, so a fetch sent to it asks the +/// next route instead. +pub(super) struct Staged { + id: u64, + state: kio::Producer, +} + +impl Drop for Staged { + fn drop(&mut self) { + if let Ok(mut route) = self.state.write() + && route.staged.as_ref().is_some_and(|(id, _)| *id == self.id) + { + route.staged = None; + } + } +} + /// A reader's handle on a logical track's [`Producer`]. #[derive(Clone)] pub(super) struct Consumer { @@ -2109,8 +2169,57 @@ pub(super) struct Fetching { sequence: u64, options: track::FetchOptions, logical: kio::Consumer, - /// The fetch in flight, and the route generation it went to. - pending: Option<(u64, kio::Pending)>, + /// The fetch in flight, and the copy it went to. + pending: Option, +} + +/// Which copy a fetch went to. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Target { + /// The serving copy of a route generation. + Serving(u64), + /// A staged copy, by stage id: its info is still in flight. + Staged(u64), +} + +struct Asked { + target: Target, + fetch: kio::Pending, + /// What a staged copy answered, held until that copy is spliced. + answer: Option>, +} + +impl Fetching { + /// How the staged copy last asked failed the fetch, if it did. + fn staged_failure(&mut self, waiter: &kio::Waiter) -> Option { + let asked = self.pending.as_mut()?; + if !matches!(asked.target, Target::Staged(_)) { + return None; + } + if asked.answer.is_none() + && let Poll::Ready(result) = kio::Task::poll(&mut *asked.fetch, waiter) + { + asked.answer = Some(result); + } + match &asked.answer { + Some(Err(err)) => Some(err.clone()), + _ => None, + } + } + + /// Poll for the serving copy or the staged one to change, or the track to end. + fn poll_moved(&self, generation: u64, staged: Option, waiter: &kio::Waiter) -> Poll<()> { + match self.route.state.poll(waiter, |route| { + let moved = route.generation != generation || route.staged.as_ref().map(|(id, _)| *id) != staged; + match moved || route.phase.terminal() { + true => Poll::Ready(()), + false => Poll::Pending, + } + }) { + Poll::Pending => Poll::Pending, + Poll::Ready(_) => Poll::Ready(()), + } + } } impl kio::Task for Fetching { @@ -2118,36 +2227,75 @@ impl kio::Task for Fetching { fn poll(&mut self, waiter: &kio::Waiter) -> Poll { loop { - let (generation, serving) = { + let (generation, serving, staged, spliced) = { let route = self.route.state.read(); - (route.generation, route.copy.clone()) + (route.generation, route.copy.clone(), route.staged.clone(), route.spliced) }; - let Some(serving) = serving else { - return match self.route.end() { - Some(Some(Err(err))) => Poll::Ready(Err(err)), - Some(_) => Poll::Ready(Err(Error::NotFound)), - None => { - ready!(self.route.poll_changed(generation, waiter)); - continue; + let staged_id = staged.as_ref().map(|(id, _)| *id); + // A fetch sent to a staged copy carries on once that copy is spliced. + if let Some(asked) = &mut self.pending + && serving.is_some() + && let Target::Staged(id) = asked.target + && spliced == Some(id) + { + asked.target = Target::Serving(generation); + } + // The serving copy answers; with none, the copy whose info is in flight is asked + // at once rather than a round trip later, and judged once it is spliced. + let (target, copy) = match (serving, staged) { + (Some(serving), _) => (Target::Serving(generation), serving), + (None, Some((id, copy))) if self.route.end().is_none() => (Target::Staged(id), copy), + (None, _) => { + let end = self.route.end(); + // A fetch whose staged copy failed reports that failure once no route + // follows, as one the serving copy failed does. + if end.is_some() + && let Some(err) = self.staged_failure(waiter) + { + return Poll::Ready(Err(err)); } - }; + return match end { + Some(Some(Err(err))) => Poll::Ready(Err(err)), + Some(_) => Poll::Ready(Err(Error::NotFound)), + None => { + ready!(self.poll_moved(generation, staged_id, waiter)); + continue; + } + }; + } }; - if self.route.poll_changed(generation, waiter).is_ready() && self.route.end().is_none() { + if self.poll_moved(generation, staged_id, waiter).is_ready() && self.route.end().is_none() { continue; } - if self.pending.as_ref().is_none_or(|(asked, _)| *asked != generation) { - self.pending = Some((generation, serving.fetch_range(self.sequence, self.options.clone()))); + if self.pending.as_ref().is_none_or(|asked| asked.target != target) { + self.pending = Some(Asked { + target, + fetch: copy.fetch_range(self.sequence, self.options.clone()), + answer: None, + }); } - let (_, pending) = self.pending.as_mut().expect("asked above"); - let result = match kio::Task::poll(&mut **pending, waiter) { - Poll::Ready(result) => result, - Poll::Pending => { - return match self.route.end() { - Some(Some(Err(err))) => Poll::Ready(Err(err)), - _ => Poll::Pending, - }; - } + let asked = self.pending.as_mut().expect("asked above"); + let result = match asked.answer.take() { + Some(result) => result, + None => match kio::Task::poll(&mut *asked.fetch, waiter) { + Poll::Ready(result) => result, + Poll::Pending => { + return match self.route.end() { + Some(Some(Err(err))) => Poll::Ready(Err(err)), + _ => Poll::Pending, + }; + } + }, }; + // Never handed out before its copy's info passes the front's check. + if let Target::Staged(_) = target { + asked.answer = Some(result); + return match self.route.end() { + Some(Some(Err(err))) => Poll::Ready(Err(err)), + _ => Poll::Pending, + }; + } + let serving = copy; return match result { Ok(group) => { // The copy may have widened its fill; the caller still reads only its range. @@ -2169,9 +2317,13 @@ impl kio::Task for Fetching { fetch: None, }))) } - // The route failed: ask the next one, unless none will come. + // The route failed: ask the next one, unless none will come. Kept as the answer, + // since the same copy is never asked twice. Err(err) if (route_failed(&err) || copy_failed(&serving)) && self.route.end().is_none() => { - ready!(self.route.poll_changed(generation, waiter)); + if let Some(asked) = &mut self.pending { + asked.answer = Some(Err(err)); + } + ready!(self.poll_moved(generation, staged_id, waiter)); continue; } Err(err) => Poll::Ready(Err(err)), @@ -2998,6 +3150,102 @@ mod test { assert!(fetched.read_frame().now_or_never().is_none()); } + /// A session's copy whose info is still in flight, and its fetch handler. + fn pending_copy() -> (track::Request, track::Requests) { + let request = track::Request::new(Arc::new(broadcast::Info::default()), "video"); + let dynamic = request.requests(); + (request, dynamic) + } + + /// The logical track `routes` serves, before any copy is spliced. + fn unaccepted(routes: &Producer) -> track::Request { + track::Request::new(Arc::new(broadcast::Info::default()), "video").routes(routes.consume()) + } + + /// A fetch reaches the copy whose info is in flight at once, but its group is handed out + /// only once the front splices that copy. + #[test] + #[cfg(feature = "async")] + fn a_fetch_goes_to_a_staged_copy_before_its_info() { + let routes = Producer::new(); + let logical = unaccepted(&routes); + let (request, dynamic) = pending_copy(); + let staged = routes.stage(request.consume()); + let mut fetching = logical.consume().fetch_group(0, None); + assert!((&mut fetching).now_or_never().is_none()); + let fill = dynamic + .requested_fill() + .now_or_never() + .expect("the fetch reached the staged copy") + .unwrap(); + + let copy = request.accept(crate::track::Info::timed()); + let mut group = fill.accept(0, crate::track::Info::timed()).unwrap(); + group.write_frame(ts(0), b"early".as_ref()).unwrap(); + group.finish().unwrap(); + assert!( + (&mut fetching).now_or_never().is_none(), + "a group is never handed out before its copy's info passes" + ); + + let _logical = logical.accept(crate::track::Info::timed()); + routes.splice(copy.consume(), staged); + let mut fetched = fetching.now_or_never().expect("spliced").unwrap(); + assert_eq!(read(&mut fetched), Some(b"early".to_vec())); + } + + /// A staged copy withdrawn before it is spliced (its info refused) answers nothing: the + /// fetch asks the next route. + #[test] + #[cfg(feature = "async")] + fn a_withdrawn_stage_refetches_from_the_next_route() { + let routes = Producer::new(); + let logical = unaccepted(&routes); + let (refused, refused_dynamic) = pending_copy(); + let staged = routes.stage(refused.consume()); + let mut fetching = logical.consume().fetch_group(0, None); + assert!((&mut fetching).now_or_never().is_none()); + let fill = refused_dynamic.requested_fill().now_or_never().unwrap().unwrap(); + let wrong = refused.accept(crate::track::Info::timed()); + let mut group = fill.accept(0, crate::track::Info::timed()).unwrap(); + group.write_frame(ts(0), b"wrong".as_ref()).unwrap(); + group.finish().unwrap(); + drop(staged); + drop(wrong); + + let (request, dynamic) = pending_copy(); + let staged = routes.stage(request.consume()); + assert!((&mut fetching).now_or_never().is_none()); + let fill = dynamic + .requested_fill() + .now_or_never() + .expect("asked again of the next route") + .unwrap(); + let copy = request.accept(crate::track::Info::timed()); + let mut group = fill.accept(0, crate::track::Info::timed()).unwrap(); + group.write_frame(ts(0), b"right".as_ref()).unwrap(); + group.finish().unwrap(); + let _logical = logical.accept(crate::track::Info::timed()); + routes.splice(copy.consume(), staged); + let mut fetched = fetching.now_or_never().expect("spliced").unwrap(); + assert_eq!(read(&mut fetched), Some(b"right".to_vec())); + } + + /// A logical track that ends before any copy is spliced fails a fetch waiting on it with + /// the front's error. + #[test] + fn an_unserved_track_fails_its_fetch_with_the_fronts_error() { + let routes = Producer::new(); + let logical = unaccepted(&routes); + let (request, _dynamic) = pending_copy(); + let staged = routes.stage(request.consume()); + let mut fetching = logical.consume().fetch_group(0, None); + assert!((&mut fetching).now_or_never().is_none()); + drop(staged); + routes.end(Err(Error::Unauthorized)); + assert!(matches!(fetching.now_or_never(), Some(Err(Error::Unauthorized)))); + } + /// Every run the reader gives up right now. fn dropped(sub: &mut track::Subscriber) -> Vec<(std::ops::Range, StreamError)> { let mut runs = Vec::new(); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index da4102ad639..2cf7d924207 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1968,15 +1968,23 @@ impl TrackState { /// A group that starts above `frame_start` is also replaceable: it cannot answer a /// request from there, so the wider producer takes the slot. Readers already /// draining the old one keep their own handle. + /// + /// A fetched backfill never holds the sequence against the feed: subscribers read only + /// the feed's groups, so its copy takes the slot, and the fetch's readers keep theirs. fn claim_sequence(&mut self, sequence: u64, frame_start: u64) -> Result<()> { - // Either copy that can still answer from `frame_start` takes the sequence. - if self.covering_group(sequence, frame_start).is_some() { + if self.holding_group(sequence, frame_start).is_some() { return Err(Error::Duplicate); } self.displace(sequence); Ok(()) } + /// The copy of the feed's `sequence` that still answers from `frame_start`, if any. + fn holding_group(&self, sequence: u64, frame_start: u64) -> Option<&group::Handle> { + self.lookup.get(&sequence).filter(|slot| slot.visible)?; + self.covering_group(sequence, frame_start) + } + /// Insert a freshly-created group into the cache. /// /// Updates the live edge, demoting the previous latest into the eviction order; @@ -3030,7 +3038,7 @@ impl Producer { } let start = received.unwrap_or(0); let content = if received.is_some() { - if state.covering_group(info.sequence, start).is_some() { + if state.holding_group(info.sequence, start).is_some() { return Err(Error::Duplicate); } state.content_for_copy(info.sequence)? @@ -5295,8 +5303,8 @@ impl kio::Task for Fetching { type Output = Result; fn poll(&mut self, waiter: &kio::Waiter) -> Poll { - // Made before a front accepted its logical track, which caches nothing: the - // serving route answers instead. + // Made before a front took its logical track, which caches nothing: the serving + // route answers instead. if self.resume.is_none() && let Some(routes) = self.state.read().routes.clone() { @@ -7990,9 +7998,6 @@ pub struct Request { // Whether the accepted track starts idle; see [`Self::idle`]. idle: bool, - // Served from a front's routes; see [`Self::routes`]. - routes: Option, - // A track the producer reserved, listed by its broadcast once accepted. list: Option, } @@ -8013,7 +8018,6 @@ impl Request { stats: stats::Scope::default(), requested_start: None, idle: false, - routes: None, list: None, } } @@ -8025,10 +8029,12 @@ impl Request { } /// Serve the track from a front's routes, read straight from their copies; see - /// [`super::resume`]. Applied atomically with [`Self::accept`], so no reader ever sees - /// the accepted track without it. - pub(super) fn routes(mut self, routes: super::resume::Consumer) -> Self { - self.routes = Some(routes); + /// [`super::resume`]. Applied at once, so a fetch made before [`Self::accept`] already + /// reaches the copy whose info is still in flight. + pub(super) fn routes(self, routes: super::resume::Consumer) -> Self { + if let Ok(mut state) = self.state.write() { + state.routes = Some(routes); + } self } @@ -8147,7 +8153,6 @@ impl Request { if self.idle { state.set_idle(); } - state.routes = self.routes; true } Err(_) => false, @@ -17671,6 +17676,48 @@ mod test { subscriber.assert_no_group(); } + /// The feed's copy of a group a fetch cached first takes the slot, so a subscriber reads + /// it, while the fetch's reader keeps its own copy, abandoned or not. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn the_feed_takes_a_group_a_fetch_cached_first() { + let (producer, _pool) = pooled_producer(1 << 40); + let dynamic = producer.requests(); + let consumer = producer.consume(); + let mut subscriber = producer.subscribe(replay()); + + let pending = consumer.fetch_group(0, None); + let req = dynamic + .requested_fill_group() + .now_or_never() + .expect("should not block") + .unwrap(); + let mut fetching = req.accept(Info::timed()).unwrap(); + fetching + .write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"snapshot")) + .unwrap(); + let mut fetched = pending.await.unwrap(); + subscriber.assert_no_group(); + + let mut live = producer.create_group_at(0u64.into(), 0).expect("the feed takes the slot"); + live.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"snapshot")) + .unwrap(); + let mut group = subscriber.assert_group(); + assert_eq!(group.sequence, 0); + assert_eq!(&group.read_frame().await.unwrap().unwrap().payload[..], b"snapshot"); + assert_eq!(&fetched.read_frame().await.unwrap().unwrap().payload[..], b"snapshot"); + + // The fetch giving up never cuts the group short for the subscriber. + drop(fetched); + fetching.abort(Error::Cancel).unwrap(); + live.finish().unwrap(); + assert!(group.read_frame().await.unwrap().is_none()); + assert!(matches!( + producer.create_group_at(0u64.into(), 0), + Err(Error::Duplicate) + )); + } + /// Fetched backfill isn't in arrival order, so it ages out through the eviction /// order instead of lingering until the track closes. #[moq_clock::test] diff --git a/rs/moq-net/src/util.rs b/rs/moq-net/src/util.rs index b453946afd8..e4ba9ed2358 100644 --- a/rs/moq-net/src/util.rs +++ b/rs/moq-net/src/util.rs @@ -8,8 +8,12 @@ use std::{collections::VecDeque, future::Future, pin::Pin, task::Poll}; mod cell; +#[cfg(feature = "async")] +mod later; mod wake; pub(crate) use cell::SendCell; +#[cfg(feature = "async")] +pub(crate) use later::{Later, Learning}; pub(crate) use wake::QueueWake; /// Prove a future's native Send bound here rather than inside every caller's future. diff --git a/rs/moq-net/src/util/later.rs b/rs/moq-net/src/util/later.rs new file mode 100644 index 00000000000..358bfa6311b --- /dev/null +++ b/rs/moq-net/src/util/later.rs @@ -0,0 +1,60 @@ +use std::task::Poll; + +use crate::Error; + +/// What one request learns for the requests sent alongside it, which wait on it only once +/// they need it: a track's info, say, that a fetch's answer is read in. +pub(crate) struct Learning(kio::Producer>>); + +impl Learning { + pub fn new() -> Self { + Self(kio::Producer::new(None)) + } + + /// A handle to wait on the answer. + pub fn later(&self) -> Later { + Later::Pending(self.0.consume()) + } + + /// Answer every waiter; only the first answer counts. + pub fn resolve(&self, result: Result) { + if let Ok(mut learned) = self.0.write() + && learned.is_none() + { + *learned = Some(result); + } + } +} + +/// A value already known, or one a [`Learning`] still owes. +#[derive(Clone)] +pub(crate) enum Later { + Known(T), + Pending(kio::Consumer>>), +} + +impl Later { + /// The answer, if it came. + pub fn peek(&self) -> Option> { + match self { + Self::Known(value) => Some(Ok(value.clone())), + Self::Pending(learned) => learned.read().clone(), + } + } + + pub fn poll(&self, waiter: &kio::Waiter) -> Poll> { + let learned = match self { + Self::Known(value) => return Poll::Ready(Ok(value.clone())), + Self::Pending(learned) => learned, + }; + match learned.poll(waiter, |learned| match &**learned { + Some(result) => Poll::Ready(result.clone()), + None => Poll::Pending, + }) { + Poll::Ready(Ok(result)) => Poll::Ready(result), + // Gone unanswered: the request it waited on was abandoned. + Poll::Ready(Err(_)) => Poll::Ready(Err(Error::Cancel)), + Poll::Pending => Poll::Pending, + } + } +} diff --git a/rs/moq-net/tests/integration/fetch_pipeline.rs b/rs/moq-net/tests/integration/fetch_pipeline.rs new file mode 100644 index 00000000000..d1877d03d36 --- /dev/null +++ b/rs/moq-net/tests/integration/fetch_pipeline.rs @@ -0,0 +1,131 @@ +#![cfg(feature = "async")] + +//! A fetch-only reader's first FETCH goes upstream with the request for the track's info +//! (TRACK on moq-lite, TRACK_STATUS on moq-transport) at every hop, rather than a round trip +//! after it. The fetched group still waits for that info before it is handed out; see +//! `model::resume`'s staged copies. + +use crate::support; + +use std::time::Duration; + +use moq_net::{Error, Hop, Timestamp, Version}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(5); + +const VERSIONS: &[&str] = &["moq-lite-05", "moq-lite-07-wip", "moq-transport-18", "moq-transport-22"]; + +fn timed() -> moq_net::track::Info { + moq_net::track::Info::default().with_timescale(moq_net::Timescale::MILLI) +} + +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())); + support::harness::spawn(driver); + producer +} + +async fn link(version: Version, from: &moq_net::origin::Producer, to: &moq_net::origin::Producer) -> MockPair { + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(from.consume()); + options.client_subscribe = Some(to.clone()); + connect_mock(options).await +} + +/// What keeps the publisher and the hops to the reader up. +struct Held { + _publisher: moq_net::origin::Producer, + _broadcast: moq_net::origin::Publication, + _links: Vec, +} + +/// A publisher whose track is requested on demand, so the test decides when its info goes +/// out; a reader `relays` hops away fetches its group 0. Returns the publisher's pending +/// request once the fetch reached it, before anything answered the info. +async fn fetch_ahead( + version: &str, + relays: u64, +) -> ( + moq_net::track::Request, + moq_net::track::Fill, + moq_clock::sim::JoinHandle>, + Held, +) { + let version: Version = version.parse().unwrap(); + let publisher = produce_origin(1); + let broadcast = publisher.create_broadcast("bcast").unwrap(); + let mut dynamic = broadcast.requests(); + broadcast.announce(Default::default()).unwrap(); + + let mut links = Vec::new(); + let mut upstream = publisher.clone(); + for hop in 0..=relays { + let next = produce_origin(hop + 2); + links.push(link(version, &upstream, &next).await); + upstream = next; + } + let remote = moq_clock::timeout(TIMEOUT, upstream.consume().follow("bcast", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves"); + let fetching = moq_clock::sim::spawn(async move { remote.track("video").unwrap().fetch_group(0, None).await }); + + let request = moq_clock::timeout(TIMEOUT, dynamic.requested_track()) + .await + .expect("the info request arrives") + .unwrap(); + let fill = moq_clock::timeout(TIMEOUT, request.requests().requested_fill()) + .await + .unwrap_or_else(|_| panic!("{version}: the FETCH waited for the info")) + .unwrap(); + let held = Held { + _publisher: publisher, + _broadcast: broadcast, + _links: links, + }; + (request, fill, fetching, held) +} + +/// The FETCH is on the wire before the info is answered, at every hop, and the group comes +/// back once it is. +#[moq_clock::test] +async fn the_first_fetch_goes_out_with_the_info_request() { + for version in VERSIONS { + for relays in [0, 1] { + let (request, fill, fetching, _held) = fetch_ahead(version, relays).await; + let _track = request.accept(timed()); + let mut group = fill.accept(0, timed()).unwrap(); + group.write_frame(Timestamp::ZERO, b"early".as_ref()).unwrap(); + group.finish().unwrap(); + let mut fetched = moq_clock::timeout(TIMEOUT, fetching) + .await + .expect("the fetch resolves") + .expect("fetch panicked") + .unwrap_or_else(|err| panic!("{version}, {relays} relays: {err:?}")); + let frame = moq_clock::timeout(TIMEOUT, fetched.read_frame()) + .await + .expect("read timeout") + .unwrap() + .expect("a frame"); + assert_eq!(&frame.payload[..], b"early", "{version}, {relays} relays"); + } + } +} + +/// A refused info fails the fetch sent alongside it rather than leaving it waiting. +#[moq_clock::test] +async fn a_refused_info_fails_the_fetch_sent_with_it() { + for version in VERSIONS { + for relays in [0, 1] { + let (request, fill, fetching, _held) = fetch_ahead(version, relays).await; + request.reject(Error::NotFound); + let res = moq_clock::timeout(TIMEOUT, fetching) + .await + .unwrap_or_else(|_| panic!("{version}, {relays} relays: the fetch never ended")) + .expect("fetch panicked"); + assert!(res.is_err(), "{version}, {relays} relays: fetched past a refused info"); + drop(fill); + } + } +} diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 94b3330cdc3..6d729e799f7 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -25,6 +25,7 @@ mod fetch_cancel; mod fetch_fanout; mod fetch_from_feed; mod fetch_only; +mod fetch_pipeline; mod finished_broadcast_mock; mod finite_relay; mod goaway; From 4c5d20bbb919c47ad67c3c967dfbc2c5bad3d4f1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 04:11:20 -0700 Subject: [PATCH 3/7] feat(js): send the first FETCH alongside TRACK Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscriber.test.ts | 53 +++++++++++++++++++++++-- js/net/src/lite/subscriber.ts | 61 +++++++++++++++++------------ js/net/src/request-deadline.test.ts | 17 +++++--- js/net/src/resume.test.ts | 37 +++++++++++++++++ js/net/src/resume.ts | 20 ++++++++-- js/net/src/util/deadline.ts | 9 +++-- 6 files changed, 156 insertions(+), 41 deletions(-) diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 372721173eb..34af0137d5c 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -16,6 +16,7 @@ import { StreamError, TimestampMismatch, } from "../error.ts"; +import type { Consumer as GroupConsumer } from "../group.ts"; import { HopSchema, isAnonymous, MAX_HOPS, Route, UNKNOWN_HOP } from "../hop.ts"; import { hooks } from "../internal.ts"; import { GRACE } from "../ledger.ts"; @@ -1241,6 +1242,48 @@ test("an already-aborted fetch rejects without opening a stream", async () => { expect(streams.length).toBe(0); }); +// A fetch-only reader's FETCH goes out alongside the TRACK stream, not a round trip after it, +// and its response is read only once TRACK_INFO says the timescale. +test("the first FETCH goes out before TRACK_INFO answers", async () => { + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n)); + + let fetched: GroupConsumer | undefined; + const fetch = subscriber.fetchGroup(Path.from("room"), "video", 0).then((group) => { + fetched = group; + return group; + }); + await opened(0); + await opened(1); + await streams[1].reading; + + // An empty-group FIN accepts the fetch, but nothing is handed out before the info. + streams[1].inbound.close(); + for (let i = 0; i < 64; i++) await Promise.resolve(); + expect(fetched).toBeUndefined(); + + await answerTrackInfo(streams[0]); + expect(await (await fetch).readFrame()).toBeUndefined(); + + subscriber.close(); +}); + +test("a failed TRACK_INFO fails the FETCH sent alongside it", async () => { + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n)); + + const fetch = subscriber.fetchGroup(Path.from("room"), "video", 0).catch((err: unknown) => err); + await opened(0); + await opened(1); + await streams[1].reading; + + streams[0].inbound.error(new StreamError(StreamCode.NotFound, { message: "no such track" })); + expect(await fetch).toBeInstanceOf(Error); + await streams[1].aborted; + + subscriber.close(); +}); + // Coalesced fetches share one FETCH stream. An abort releases only that caller's share; the // stream is cancelled once the last sharer leaves, before its FETCH is sent if it can be. test("one of two fetch sharers aborting leaves the other's fetch", async () => { @@ -1299,10 +1342,12 @@ test.each([ expect(((await b.catch((err: unknown) => err)) as Error).message).toBe("second"); if (stage === "track") { - // The abandoned TRACK exchange is reset promptly; no FETCH is sent. - const err = fromTransport(await streams[0].aborted) as StreamError; - expect(err.code).toBe(StreamCode.Cancel); - expect(streams.length).toBe(1); + // The abandoned TRACK exchange is reset promptly, with the FETCH sent alongside it. + for (const stream of streams) { + const err = fromTransport(await stream.aborted) as StreamError; + expect(err.code).toBe(StreamCode.Cancel); + } + expect(streams.length).toBe(2); } else { const err = fromTransport(await streams[1].aborted) as StreamError; expect(err.code).toBe(StreamCode.Cancel); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 1a009cf4300..5e091eb91cf 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1344,8 +1344,9 @@ export class Subscriber { } } - // Resolve the track's timescale, then open the FETCH stream and wait for it to be accepted. - // Closing the group during that wait resets the stream. + // Open the FETCH stream alongside the TRACK stream that says the track's timescale, and wait + // for both: the response waits unread in its stream until the timescale lands. Closing the + // group during that wait resets the stream, and a failed TRACK_INFO fails the fetch. async #fetchSetup( broadcast: Path.Valid, consumed: Consumed, @@ -1364,29 +1365,41 @@ export class Subscriber { // Only the properties are needed, so the TRACK stream closes however the wait ends. answered.then( ({ stream }) => stream.close(), - () => {}, - ); - const { info } = await untilClosed(group, answered); - return this.#exchange( - { sendOrder: sendOrder({ priority }), version: this.version }, - async (stream) => { - await stream.writer.u53(StreamId.Fetch); - await new FetchMessage({ - broadcast, - epoch, - track, - priority, - maxDelay: hasFetchMaxDelay(this.version) ? maxDelay : undefined, - group: sequence, - }).encode(stream.writer, this.version); - // A byte or an empty-group FIN accepts the fetch; a reset rejects it. - // done() buffers that byte so the response pump can decode it normally. - await untilClosed(group, stream.reader.done()); - return { stream, info }; - }, - signal, - deadline, + (err: unknown) => cancel.abort(error(err)), ); + let stream: Stream; + try { + stream = await this.#exchange( + { sendOrder: sendOrder({ priority }), version: this.version }, + async (stream) => { + await stream.writer.u53(StreamId.Fetch); + await new FetchMessage({ + broadcast, + epoch, + track, + priority, + maxDelay: hasFetchMaxDelay(this.version) ? maxDelay : undefined, + group: sequence, + }).encode(stream.writer, this.version); + // A byte or an empty-group FIN accepts the fetch; a reset rejects it. + // done() buffers that byte so the response pump can decode it normally. + await untilClosed(group, stream.reader.done()); + return stream; + }, + signal, + deadline, + ); + } catch (err: unknown) { + cancel.abort(error(err)); + throw err; + } + try { + const { info } = await untilClosed(group, answered); + return { stream, info }; + } catch (err: unknown) { + stream.abort(error(err)); + throw err; + } } // Read the FETCH response (bare frames, zigzag-delta-timestamped unless `timescale` is 0) diff --git a/js/net/src/request-deadline.test.ts b/js/net/src/request-deadline.test.ts index 61391a7cb10..3ec20072819 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 create gets its own stream: a fetch opens its TRACK stream alongside. + 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(), }; } @@ -125,7 +126,11 @@ for (const operation of ["info", "fetch"] as const) { await transport.cancelled; expect(transport.written).toEqual([]); } - expect(transport.options).toEqual([{ sendOrder: undefined, waitUntilAvailable: false }]); + // A fetch opens its FETCH stream alongside the TRACK stream, under the same deadline. + const opened = transport.options as { sendOrder?: number; waitUntilAvailable: boolean }[]; + expect(opened).toHaveLength(operation === "info" ? 1 : 2); + expect(opened[0]).toEqual({ sendOrder: undefined, waitUntilAvailable: false }); + for (const open of opened) expect(open.waitUntilAvailable).toBe(false); } finally { subscriber.close(); installed.close(); diff --git a/js/net/src/resume.test.ts b/js/net/src/resume.test.ts index 26b459d30f7..05b2cd8583c 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -1451,6 +1451,43 @@ test("aborting a fetch stops it while its route's properties are still being che b.close(); }); +test("a fetch asks its route alongside that route's properties", async () => { + const front = new Front(); + const a = new BroadcastProducer(); + // a serves tracks on demand but never answers, so its properties never arrive. + void wireOf(a).requested(); + const { route, asked } = counted(a); + front.serve(route); + const fetching = front.consumer.track("seq").fetchGroup(0); + void fetching.catch(() => {}); + await settle(); + expect(asked).toEqual([0]); + + front.consumer.close(); + a.close(); +}); + +test("a fetch never hands out a group from a route refused for its properties", async () => { + const front = new Front(); + const a = new BroadcastProducer(); + const b = new BroadcastProducer(); + a.createTrack("seq", { priority: 1 }); + const tb = b.createTrack("seq", { priority: 2 }); + const group = tb.createGroup(0); + group.writeFrame({ payload: enc.encode("b") }); + group.finish(); + front.serve(a.consume()); + await front.consumer.track("seq").info(); + const { route, asked } = counted(b); + front.serve(route); + await expect(front.consumer.track("seq").fetchGroup(0)).rejects.toThrow("changed its properties"); + expect(asked).toEqual([0]); + + front.consumer.close(); + a.close(); + b.close(); +}); + test("a refused route is not served again by a later compatible source", async () => { const front = new Front(); const a = new BroadcastProducer(); diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index c3301ea70ca..d2b1c3a425b 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -1886,10 +1886,24 @@ export class Front { const retired = this.#retire.signal; const signal = options?.signal ? AbortSignal.any([options.signal, retired]) : retired; try { - await until(this.#verify(name, source), signal); + // Asked alongside the check that the source claims the track's recorded properties, + // rather than a round trip after it: only handing the group out waits for the check. const fetchGroup = source.wire.fetchGroup; - if (!fetchGroup) throw new Error("fetch group is not supported by this session's version"); - const group = await fetchGroup(name, sequence, { ...options, signal }); + const ahead = new AbortController(); + const fetching = fetchGroup?.(name, sequence, { ...options, signal: AbortSignal.any([signal, ahead.signal]) }); + fetching?.catch(() => {}); + try { + await until(this.#verify(name, source), signal); + } catch (err) { + ahead.abort(err); + void fetching?.then( + (group) => group.close(), + () => {}, + ); + throw err; + } + if (!fetching) throw new Error("fetch group is not supported by this session's version"); + const group = await fetching; // A copy that already failed continues nothing: that is the route failing again. const failed = group.closed.peek(); if (failed instanceof Error) { diff --git a/js/net/src/util/deadline.ts b/js/net/src/util/deadline.ts index 68c5f37373a..5e14312fd7b 100644 --- a/js/net/src/util/deadline.ts +++ b/js/net/src/util/deadline.ts @@ -18,13 +18,14 @@ function sessionFailure(err: unknown): boolean { /** One fatal deadline for a request, including every stream its setup creates. @internal */ export class Deadline { - #opening = false; + /** Creates still waiting on credit: a fetch opens its TRACK stream alongside. */ + #opening = 0; #streams: Stream[] = []; #failure?: Error; /** Record a create so deadline failures reset its stream even if it opens late. */ async open(opening: Stream | Promise): Promise { - this.#opening = true; + this.#opening++; let stream: Stream; try { stream = await opening; @@ -32,7 +33,7 @@ export class Deadline { if (sessionFailure(cause)) throw cause; throw new NoStreamCredit({ cause }); } finally { - this.#opening = false; + this.#opening--; } if (this.#failure) { stream.abort(this.#failure); @@ -60,7 +61,7 @@ export class Deadline { } catch (cause) { const failure = cause instanceof TimeoutError - ? this.#opening + ? this.#opening > 0 ? new NoStreamCredit({ cause }) : controlTimeout(cause) : error(cause); From b2a603512a53ebe0b7a3f7778a8e72fcbe3a4223 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 04:17:05 -0700 Subject: [PATCH 4/7] style: format Co-Authored-By: Claude Opus 5.5 --- js/net/src/resume.ts | 5 ++++- rs/moq-net/src/ietf/publisher.rs | 3 ++- rs/moq-net/src/ietf/subscriber.rs | 20 +++++++++++++++----- rs/moq-net/src/model/resume.rs | 7 ++++++- rs/moq-net/src/model/track.rs | 4 +++- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index d2b1c3a425b..10bac0baeaa 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -1890,7 +1890,10 @@ export class Front { // rather than a round trip after it: only handing the group out waits for the check. const fetchGroup = source.wire.fetchGroup; const ahead = new AbortController(); - const fetching = fetchGroup?.(name, sequence, { ...options, signal: AbortSignal.any([signal, ahead.signal]) }); + const fetching = fetchGroup?.(name, sequence, { + ...options, + signal: AbortSignal.any([signal, ahead.signal]), + }); fetching?.catch(() => {}); try { await until(this.#verify(name, source), signal); diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index f5b2151a1de..fe110e44583 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -6728,7 +6728,8 @@ mod subscribe_cursor_test { open.write_frame(crate::Timestamp::ZERO, b"a".to_vec()).unwrap(); let consumer = track.consume(); - let read = moq_clock::sim::spawn(async move { read_fetch(&consumer, 0, 0, None, 0, false, None).await.map(|_| ()) }); + let read = + moq_clock::sim::spawn(async move { read_fetch(&consumer, 0, 0, None, 0, false, None).await.map(|_| ()) }); moq_clock::sim::yield_now().await; // The track runs a full second past the group's successor. for ms in [1000, 2000] { diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index daf6ce16b36..e7f4ed7c85e 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -4000,9 +4000,12 @@ impl Subscriber { match step { Step::Fill(fill) => { - let fetch = - self.clone() - .serve_fill(broadcast_path.clone(), track_name.to_owned(), fill, Later::Known(idle.timescale)); + let fetch = self.clone().serve_fill( + broadcast_path.clone(), + track_name.to_owned(), + fill, + Later::Known(idle.timescale), + ); group_fetches.push(fetch); } Step::Subscribe => break true, @@ -5572,7 +5575,8 @@ impl Subscriber { if let Poll::Ready(res) = timescale.poll(waiter) { return Poll::Ready(res); } - fill.poll_unwanted_groups(run.clone(), waiter).map(|_| Err(Error::Cancel)) + fill.poll_unwanted_groups(run.clone(), waiter) + .map(|_| Err(Error::Cancel)) }) .await { @@ -7781,7 +7785,13 @@ mod tests { SetupWait::StatusOpen | SetupWait::StatusWrite | SetupWait::StatusResponse => Box::pin(async { assert!( subscriber - .track_status(&Path::from_static("broadcast"), "video", request, &mut TaskSet::owned(), &mut gate) + .track_status( + &Path::from_static("broadcast"), + "video", + request, + &mut TaskSet::owned(), + &mut gate + ) .await .is_none() ); diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index bac050a3f27..4f3c7e92262 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -2229,7 +2229,12 @@ impl kio::Task for Fetching { loop { let (generation, serving, staged, spliced) = { let route = self.route.state.read(); - (route.generation, route.copy.clone(), route.staged.clone(), route.spliced) + ( + route.generation, + route.copy.clone(), + route.staged.clone(), + route.spliced, + ) }; let staged_id = staged.as_ref().map(|(id, _)| *id); // A fetch sent to a staged copy carries on once that copy is spliced. diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 2cf7d924207..719dca1203f 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -17699,7 +17699,9 @@ mod test { let mut fetched = pending.await.unwrap(); subscriber.assert_no_group(); - let mut live = producer.create_group_at(0u64.into(), 0).expect("the feed takes the slot"); + let mut live = producer + .create_group_at(0u64.into(), 0) + .expect("the feed takes the slot"); live.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"snapshot")) .unwrap(); let mut group = subscriber.assert_group(); From 5fa25ed38c6500ae301e32f3f8530d3e0f665b2b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 04:23:07 -0700 Subject: [PATCH 5/7] test(net): measure the first fetch; finish the pipelined FETCH quest Co-Authored-By: Claude Opus 5.5 --- quest/m2/pipeline-requests/README.md | 1 - quest/m2/pipeline-requests/fetch.md | 61 ------------------- quest/m2/pipeline-requests/subscribe.md | 1 - rs/moq-net/src/ietf/subscriber.rs | 1 - .../tests/integration/fetch_pipeline.rs | 56 +++++++++++++++++ 5 files changed, 56 insertions(+), 64 deletions(-) delete mode 100644 quest/m2/pipeline-requests/fetch.md diff --git a/quest/m2/pipeline-requests/README.md b/quest/m2/pipeline-requests/README.md index ff7f9888c83..2cd01ff1c05 100644 --- a/quest/m2/pipeline-requests/README.md +++ b/quest/m2/pipeline-requests/README.md @@ -25,4 +25,3 @@ 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 deleted file mode 100644 index 9429ef0be46..00000000000 --- a/quest/m2/pipeline-requests/fetch.md +++ /dev/null @@ -1,61 +0,0 @@ -# [L] The first FETCH does not wait for the track's info - -## Goal - -A fetch-only reader's first FETCH goes upstream together with the request for -the track's info, on moq-lite and moq-transport, at every hop, removing a round trip per hop. The fetched group is still handed out only once that -route's info is known and passes the origin's consistency check. Peers that -send them serially keep working unchanged. - -## Plan - -Decided 2026-10-07, while landing #4974 (fetch-only IETF demand), which keeps -a sequential TRACK_STATUS before the first FETCH. Moved from m2 into -[Pipelined requests](/quest/m2/pipeline-requests/README.md) on 2026-10-08: -the maintainer called the extra round trip not worth keeping, for FETCH as -for SUBSCRIBE, without waiting on a latency measurement. - -Why the round trip exists (facts, 2026-10-07): nothing in a FETCH request -needs the info. Two serial gates do: - -- The origin accepts the logical track with the first copy's info (timescale, - max_age, priority) and refuses later copies whose info differs - (`model/front.rs` `track_info`), because a relayed group's raw timestamps go - downstream in the group's own timescale while downstream decodes them with - the logical track's advertised one. A fetch is routed only to a copy already - spliced (`resume::Fetching`, `TrackIo::splice`). -- Both sessions register their fetch handler only after their info exchange: - lite's `TrackServeRun` runs TRACK_INFO first because FETCH frames are - timestamped in its units (only on wires with a track stream; older lite - serves with default info and has no gate to remove); IETF takes the - timescale only from SUBSCRIBE_OK or TRACK_STATUS_OK. - -What needs the info is decoding the response and approving the group, so: - -- Origin: let `resume::Fetching` send the copy-level fetch to the copy whose - info is in flight (a staged generation), resolving only once that copy is - spliced. On a refused or mismatched info, or a detach, drop the pending - fetch and re-issue on the next route, as failover already does. Never hand - out a group before the info check passes. This gate is FETCH's alone: - subscription demand already reaches a copy before it is spliced. -- lite: register the fetch handler up front and run FETCH alongside - TRACK_INFO, leaving the response unread in the QUIC stream until the info - lands (which also covers lite-07 untimed framing). On a failed TRACK_INFO, - reject the fetch and reset the stream. -- moq-transport: send TRACK_STATUS alongside the FETCH and take timestamps' - units from it (or FETCH_OK properties where present). - -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. - -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 -one and the reverse. Measure the first-fetch latency before and after. - -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 index 85cc7d033f9..a32494a4a30 100644 --- a/quest/m2/pipeline-requests/subscribe.md +++ b/quest/m2/pipeline-requests/subscribe.md @@ -89,5 +89,4 @@ 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 diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index e7f4ed7c85e..3beeafff9a1 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -4318,7 +4318,6 @@ impl Subscriber { /// [`Self::track_status`]'s exchange, failing with why the request was refused or /// abandoned. - async fn learn_track_status( &mut self, broadcast_path: &Path, diff --git a/rs/moq-net/tests/integration/fetch_pipeline.rs b/rs/moq-net/tests/integration/fetch_pipeline.rs index d1877d03d36..2af1de47ffc 100644 --- a/rs/moq-net/tests/integration/fetch_pipeline.rs +++ b/rs/moq-net/tests/integration/fetch_pipeline.rs @@ -129,3 +129,59 @@ async fn a_refused_info_fails_the_fetch_sent_with_it() { } } } + +/// How long a fetch-only reader `relays` hops from a publisher waits for its first fetched +/// group over links of `rtt`, counted from the fetch once the broadcast resolved. +async fn first_fetch(version: &str, relays: u64, rtt: Duration) -> Duration { + let version: Version = version.parse().unwrap(); + let publisher = produce_origin(1); + let broadcast = publisher.create_broadcast("bcast").unwrap(); + let mut track = broadcast.create_track("video", timed()).unwrap(); + broadcast.announce(Default::default()).unwrap(); + let mut group = track.append_group().unwrap(); + group.write_frame(Timestamp::ZERO, b"frame".as_ref()).unwrap(); + group.finish().unwrap(); + + let mut links = Vec::new(); + let mut upstream = publisher.clone(); + for hop in 0..=relays { + let next = produce_origin(hop + 2); + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(upstream.consume()); + options.client_subscribe = Some(next.clone()); + options.latency = rtt / 2; + links.push(connect_mock(options).await); + upstream = next; + } + let remote = moq_clock::timeout(TIMEOUT, upstream.consume().follow("bcast", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves"); + let start = moq_clock::now(); + let mut fetched = moq_clock::timeout(TIMEOUT, remote.track("video").unwrap().fetch_group(0, None)) + .await + .expect("fetch timeout") + .unwrap_or_else(|err| panic!("{version}: {err:?}")); + moq_clock::timeout(TIMEOUT, fetched.read_frame()) + .await + .expect("read timeout") + .unwrap() + .expect("a frame"); + let elapsed = moq_clock::now() - start; + drop((links, track, broadcast, publisher)); + elapsed +} + +/// The first fetch costs one round trip per hop, not the two of asking for the track's info +/// first: the info and the fetch travel together. +#[moq_clock::test] +async fn the_first_fetch_takes_one_round_trip_per_hop() { + const RTT: Duration = Duration::from_millis(100); + for version in VERSIONS { + for relays in [0, 1] { + let elapsed = first_fetch(version, relays, RTT).await; + let hops = u32::try_from(relays + 1).unwrap(); + assert!(elapsed <= RTT * hops, "{version}, {relays} relays: {elapsed:?}"); + } + } +} From 495271883c12ca8f8edb92b770ea198818675910 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 04:40:29 -0700 Subject: [PATCH 6/7] test(net): drop a needless mut Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/tests/integration/fetch_pipeline.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-net/tests/integration/fetch_pipeline.rs b/rs/moq-net/tests/integration/fetch_pipeline.rs index 2af1de47ffc..09ea6ea6d8c 100644 --- a/rs/moq-net/tests/integration/fetch_pipeline.rs +++ b/rs/moq-net/tests/integration/fetch_pipeline.rs @@ -136,7 +136,7 @@ async fn first_fetch(version: &str, relays: u64, rtt: Duration) -> Duration { let version: Version = version.parse().unwrap(); let publisher = produce_origin(1); let broadcast = publisher.create_broadcast("bcast").unwrap(); - let mut track = broadcast.create_track("video", timed()).unwrap(); + let track = broadcast.create_track("video", timed()).unwrap(); broadcast.announce(Default::default()).unwrap(); let mut group = track.append_group().unwrap(); group.write_frame(Timestamp::ZERO, b"frame".as_ref()).unwrap(); From 29bfb21307cfa72582d530b66e660fb86d22c5ed Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 23:12:23 -0700 Subject: [PATCH 7/7] fix(net): open TRACK before the fetches sent alongside it TRACK opens its stream with `poll_open_request`, which fails fast without stream credit, while a fetch waits for credit. Polling queued fetches first could spend the last credit and fail the track with every fetch. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 65 +++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 1a9a2e5d41e..05171861b7c 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -1990,7 +1990,7 @@ mod tests { use super::*; use crate::Timescale; use crate::coding::{Decode, Encode}; - use crate::lite::test_transport::SinkSession; + use crate::lite::test_transport::{Sent, SinkSession}; use crate::model::ProduceTest; use futures::FutureExt; @@ -4087,6 +4087,60 @@ mod tests { } } + /// TRACK opens its stream before the fetches sent alongside it: TRACK fails fast without + /// stream credit, where a fetch waits for it, so fetches opened first could leave TRACK + /// none and fail the track with every fetch. + #[moq_clock::test] + async fn track_opens_before_the_fetches_sent_alongside() { + let session = SinkSession::gated_bi(kio::Producer::new(true).consume()); + let log = session.log.clone(); + let subscriber = Subscriber::new(SubscriberConfig { + runtime: crate::time::sim(), + session: crate::driver::Transport::new(session), + origin: origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + recv_bandwidth: None, + version: 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 broadcast = crate::broadcast::Info::new().produce(); + let mut dynamic = broadcast.requests(); + let consumer = broadcast.consume(); + let _waiting = Box::pin(consumer.track("video").unwrap().subscribe(None)); + let request = dynamic.requested_track().await.expect("request"); + let copy = request.consume(); + + let mut run = TrackServeRun::new( + TrackServe { + validity: Default::default(), + route: None, + subscriber, + path: Path::from_static("room"), + epoch: None, + name: "video".to_string(), + }, + request, + ); + // Queued before the run first polls, as several fetches can be. + let mut fetching = Box::pin(copy.fetch_group(0, None)); + assert!(futures::poll!(fetching.as_mut()).is_pending()); + assert!(kio::Task::poll(&mut run, &kio::Waiter::noop()).is_pending()); + + let first = |control: lite::ControlType| { + log.trail().into_iter().find_map(|(stream, sent)| match sent { + Sent::Write(bytes) if bytes.first() == Some(&(u64::from(control) as u8)) => Some(stream), + _ => None, + }) + }; + let track = first(lite::ControlType::Track).expect("TRACK went out"); + let fetch = first(lite::ControlType::Fetch).expect("FETCH went out alongside"); + assert!(track < fetch, "a FETCH opened its stream before TRACK"); + } + /// A lite-07 source of `room` over `session`, a consumer to list it through, and the /// route that keeps it served. fn listed_source( @@ -7186,9 +7240,14 @@ impl kio::Task for TrackServeRun { while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(waiter) { fetches.push(FillServeRun::new(self.serve.clone(), fill, known.later())); } - let _ = fetches.poll(waiter); // Abandonment was checked at the top of this poll, arming its wait. - let res = ready!(info.poll_fetch(&self.serve, waiter)); + let res = info.poll_fetch(&self.serve, waiter); + // TRACK opens first: its stream fails fast without credit, where a fetch's + // waits for it, so fetches opened ahead could starve their own prerequisite. + let Poll::Ready(res) = res else { + let _ = fetches.poll(waiter); + return Poll::Pending; + }; let TrackRunState::Info { request, dynamic,