From 08fbc9c4bcab2738f5428ef8b790acf62473d27b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 03:13:42 -0800 Subject: [PATCH 1/9] chore(quest): claim quest/m1/subscribe-ranges/fetch-range.md Quest-Claim: 0686e6ba42fbd16e From 43a96fe6e5cebc873c1a551dd19c80ed2718359c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 03:32:25 -0800 Subject: [PATCH 2/9] feat(net)!: end a bounded read once its ranges are accounted for A subscription whose ranges all end now returns None once every group in them was handed out, dropped, skipped as stale, or given up as unfillable, instead of parking for an update to widen it. Wire publishers keep parking, since their peer reads the end of a stream as the track's. A group another reader's fill cached is now owed to every subscriber whose window holds it, so a warm cache serves what a cold one fills. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/MIGRATE.md | 6 + rs/moq-net/src/ietf/publisher.rs | 3 +- rs/moq-net/src/lite/publisher.rs | 3 +- rs/moq-net/src/model/resume.rs | 11 +- rs/moq-net/src/model/track.rs | 242 +++++++++++++++++++++++++++-- rs/moq-net/src/model/track/miss.rs | 13 +- 6 files changed, 250 insertions(+), 28 deletions(-) diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index b1d9c4efbc3..809c6a70e38 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -4,6 +4,12 @@ How to update your code for each breaking change, newest first. ## Unreleased +### A bounded subscription ends once its ranges are read + +When every one of a subscription's `ranges` has an end, `track::Subscriber::recv_group` and `track::Ordered::next_group` return `None` once each group in them was handed out, dropped, skipped as stale, or given up as unfillable, instead of waiting for an update to widen the ranges. +A reader that wants to widen its ranges later keeps one open, such as `Range::groups(n..)`, and narrows its reads with `with_range`, whose cap never ends a read. +A group another reader's fetch or fill cached is now handed to every subscriber whose ranges hold it, not only fetches. + ### `broadcast::Demand` reads like `track::Demand` Replace `is_used()` with `wanted()`, and `used()`, `unused()`, `poll_used`, and `poll_unused` with a loop over `wanted_changed()` (or `poll_wanted_changed`), which returns the new value each time it changes and errors once the broadcast closes. diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index a934a151fdc..7f663c63d5d 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -1984,7 +1984,8 @@ impl Publisher { .until_withdrawn(stream, |waiter| subscribing.poll_ok(waiter)) .await? { - Ok(subscribed) => subscribed, + // A peer reads the end of the subscription as the track's. + Ok(subscribed) => subscribed.parked(), Err(err) => return Some(Err(err)), }; drop(subscribing); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 59c4e16b368..0414f6a41e2 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -3991,7 +3991,8 @@ impl TrackRun { // Apply the initial cap from the original Subscribe. Subsequent updates // flow through the SUBSCRIBE_UPDATE arm below. let cap = bounds.groups().end().map_or(Bound::Unbounded, Bound::Excluded); - let track = track.with_range((Bound::Included(floor), cap)); + // The FIN of a covered subscription is the wire's to send, below. + let track = track.with_range((Bound::Included(floor), cap)).parked(); let emit_range = ctx.version.has_track_stream(); let datagrams = ctx.version.has_datagrams() && ctx.session.max_datagram_size() > 0; diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 5fbc5137807..fcd1b24a32d 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -541,10 +541,13 @@ impl Copy { if let Sub::Pending(pending) = &self.sub { let sub = ready!(pending.poll_ok(waiter))?; // An anchor the wire kept may resolve below where the reader got to. - let sub = sub.with_range(( - Bound::Included(self.start(groups.0)), - groups.1.map_or(Bound::Unbounded, Bound::Excluded), - )); + // The routes judge where the reader ends, not each copy. + let sub = sub + .with_range(( + Bound::Included(self.start(groups.0)), + groups.1.map_or(Bound::Unbounded, Bound::Excluded), + )) + .parked(); self.sub = Sub::Ready(Box::new(sub)); } let Sub::Ready(sub) = &mut self.sub else { unreachable!() }; diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 24c93496831..ad81795861c 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3891,6 +3891,7 @@ impl Producer { stats: stats::Scope::default(), _stats_sub: stats::Subscription::default(), largest: Reported::default(), + parks: false, } } @@ -5367,6 +5368,7 @@ impl Subscribing { stats: self.stats.clone(), _stats_sub: self.stats.subscribe(), largest: Reported::default(), + parks: false, })) } @@ -5684,6 +5686,9 @@ pub struct Subscriber { // while held, closing them on drop. Empty (no-op) for an untagged track. _stats_sub: stats::Subscription, largest: Reported, + /// Whether a read past a bounded window's end parks rather than ending: a wire + /// publisher's, whose peer would read the end of its stream as the track's. + parks: bool, } /// What a [`Subscriber`]'s `largest` stream last reported. @@ -6164,6 +6169,9 @@ struct Cursor { drops: Option>, /// What this cursor misses and asks fills for, once it first reads. misses: Option>, + /// Groups of a bounded window skipped as stale, which account for it as handed out + /// would, until a wider budget owes them again. + written_off: RangeSet, } /// A group a cursor found but has not moved past; see [`Cursor::take`]. @@ -6378,6 +6386,7 @@ impl Cursor { skipped: Skipped::default(), drops: None, misses: None, + written_off: RangeSet::new(), } } @@ -6517,6 +6526,48 @@ impl Cursor { self.min_sequence.max(self.filter.window.floor().unwrap_or(0)) } + /// Whether a bounded window has nothing left to deliver: every group in its ranges was + /// handed out (`taken`), dropped, skipped as stale, or given up as unfillable. An open + /// window, or one with a fill still out, always has more, and so does one its cap + /// keeps a group out of, since a reader may raise a cap again. + fn is_exhausted(&self, taken: &Delivered) -> bool { + let Some(end) = self.filter.window.end() else { + return false; + }; + if self.filter.window.is_pending() || self.awaits_fill() { + return false; + } + let given_up = self.misses.as_ref().map(|misses| misses.given_up()); + let state = self.state.read(); + let accounted = |gap: ops::Range| { + let mut left = RangeSet::new(); + left.insert(gap.clone()); + for run in self.written_off.overlapping(&gap) { + left.remove(run.clone()); + } + if let Some(given_up) = given_up { + for run in given_up.overlapping(&gap) { + left.remove(run.clone()); + } + } + for (run, _) in state.drops.overlapping(&gap) { + left.remove(run.clone()); + } + for sequence in self.seek_pending.range(gap).map(|(sequence, _)| *sequence) { + left.remove(sequence..sequence + 1); + } + left.is_empty() + }; + self.filter + .window + .runs(self.filter.window.floor().unwrap_or(0)) + .take_while(|run| *run.start() < end) + .all(|run| { + let run = *run.start()..run.end().checked_add(1).map_or(end, |next| next.min(end)); + taken.gaps(run).into_iter().all(&accounted) + }) + } + /// Whether group `sequence`, at or above [`Self::floor`], is one this cursor reads now: /// in its window and below its cap. One that isn't may be later. fn admits(&self, sequence: u64) -> bool { @@ -6631,6 +6682,7 @@ impl Cursor { /// the cache, and nothing when the budget did not widen. fn reoffer(&mut self, budget: Duration) { for run in self.skipped.widened(budget) { + self.written_off.remove(*run.start()..run.end().saturating_add(1)); // Judged again, so a drop not reported yet is withdrawn; a reported one stands. if let Some(drops) = &mut self.drops { let span = *run.start()..run.end().saturating_add(1); @@ -6991,6 +7043,11 @@ impl Cursor { true => self.skipped.remove(consumer.sequence), false => self.skip(consumer.sequence, drift.budget), } + if self.filter.window.end().is_some() + && let Some(end) = consumer.sequence.checked_add(1) + { + self.written_off.insert(consumer.sequence..end); + } continue; } return Poll::Ready(Ok(Some(Peeked { @@ -7393,12 +7450,19 @@ impl Subscriber { /// budget offers the cached groups it skipped as stale and now covers the same way. /// Once the track finishes, a reader with nothing left in range ends. /// + /// A reader whose [`Subscription::ranges`] all end ends too, once every group in them + /// was handed out, dropped, skipped as stale, or given up as unfillable, rather than + /// waiting for an update to widen them: a reader that wants more keeps a range open. A + /// [`Self::with_range`] cap never ends a read, since it may rise again, and a group it + /// keeps out stays owed. + /// A track an origin serves from whichever of its routes is up still parks there for now. + /// /// A group one of [`Subscription::ranges`] starts or ends inside reads only the frames /// they cover there. One missing the frames asked for reads [`Error::Lagged`] rather /// than starting short. /// /// Returns `Poll::Ready(Ok(Some(group)))` when a group is available, - /// `Poll::Ready(Ok(None))` when the track is finished, + /// `Poll::Ready(Ok(None))` when the track is finished or the ranges are read, /// `Poll::Ready(Err(e))` when the track has been aborted, or /// `Poll::Pending` when no group is available yet. pub fn poll_recv_group(&mut self, waiter: &kio::Waiter) -> Poll>> { @@ -7426,7 +7490,13 @@ impl Subscriber { pub(super) fn poll_peek_group(&mut self, ordered: bool, waiter: &kio::Waiter) -> Poll>> { match &mut self.inner { Inner::Plain(cursor) => loop { - let Some(peeked) = ready!(cursor.poll_peek(ordered, &self.delivered, waiter))? else { + let peeked = match cursor.poll_peek(ordered, &self.delivered, waiter) { + Poll::Pending if !self.parks && cursor.is_exhausted(&self.delivered) => { + return Poll::Ready(Ok(None)); + } + polled => ready!(polled)?, + }; + let Some(peeked) = peeked else { return Poll::Ready(Ok(None)); }; if !self.delivered.contains(peeked.group.sequence) { @@ -7435,6 +7505,7 @@ impl Subscriber { // A replay advances only this copy's cursor, as an ordinary read would. cursor.take(peeked); }, + // A front's bounded read parks past its end for now, as each copy's reader does. Inner::Resume(resume, _) => resume .poll_peek_group(ordered, &self.delivered, waiter) .map(|res| res.map(|candidate| candidate.map(|candidate| Reserved::Resume(Box::new(candidate))))), @@ -7900,6 +7971,15 @@ impl Subscriber { } /// [`Self::with_range`] on a reader held in place. + /// Park past the end of a bounded window rather than end the read there, as a wire + /// publisher does: its peer reads the end of a subscription's stream as the track's, + /// unless the wire ends a covered subscription itself. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn parked(mut self) -> Self { + self.parks = true; + self + } + pub(crate) fn set_range(&mut self, groups: impl RangeBounds) { let (start, end) = super::subscription::sequence_bounds(groups); match &mut self.inner { @@ -8113,11 +8193,12 @@ impl Ordered { /// /// Honors the group range set by [`Self::with_range`]: /// a group past the cap stays in the producer's cache and becomes eligible again if - /// the cap rises or is removed. + /// the cap rises or is removed. Ranges that all end end the read once they are + /// accounted for, as for [`Subscriber::poll_recv_group`]. /// /// Returns `Poll::Ready(Ok(Some(group)))` when a group is available, /// `Poll::Ready(Ok(None))` when the track is finished (including an abort after its - /// declared end settled, see [`Producer::abort`]), + /// declared end settled, see [`Producer::abort`]) or the ranges are read, /// `Poll::Ready(Err(e))` when the track has been aborted short of its end, or /// `Poll::Pending` when no group is available yet. pub fn poll_next_group(&mut self, waiter: &kio::Waiter) -> Poll>> { @@ -18199,11 +18280,12 @@ mod test { .unwrap(); } - /// Lowering the floor re-offers only what the arrival walk could have read: a fetched - /// group stays out of arrival order, while a live one is offered before its first frame. + /// Lowering the floor re-offers what the arrival walk could have read, a live group + /// before its first frame, and owes a fetched one as cached backfill. Only the walked + /// groups count as unread for a copy handing over. #[kio::test] #[cfg(feature = "async")] - async fn a_lowered_floor_reoffers_live_groups_but_not_fetched_ones() { + async fn a_lowered_floor_reoffers_live_groups_and_owes_fetched_ones() { let (producer, _pool) = pooled_producer(1 << 40); let dynamic = producer.requests(); let consumer = producer.consume(); @@ -18227,6 +18309,7 @@ mod test { subscriber.update(replay().with_range(0..)).unwrap(); assert_eq!(subscriber.assert_group().sequence, 3, "offered before its first frame"); + assert_eq!(subscriber.assert_group().sequence, 2, "cached backfill is owed"); subscriber.assert_no_group(); // A cursor floor lowered before the next read counts only walked groups as unread. @@ -18243,11 +18326,12 @@ mod test { assert!(!lowered.has_unread_group(&unread), "the fetched group 2 is not unread"); } - /// A fetched (backfill) group is served by sequence but never replayed to - /// arrival-order subscribers. + /// A group another reader's fill cached is owed to a subscriber whose window holds it, + /// as its own fill's answer would be, though the arrival walk never reaches it: a warm + /// cache serves what a cold one fills. #[kio::test] #[cfg(feature = "async")] - async fn fetched_backfill_not_subscribed() { + async fn cached_backfill_is_owed_to_a_subscriber() { let (producer, _pool) = pooled_producer(1 << 40); let dynamic = producer.requests(); let consumer = producer.consume(); @@ -18272,11 +18356,21 @@ mod test { assert_eq!(&fetched.read_frame().await.unwrap().unwrap().payload[..], b"backfill"); assert!(consumer.peek_group(2).is_some(), "backfill is cached for later fetches"); - // ...but an arrival-order subscriber only sees the live groups. + // ...and a subscriber reads it beside the live groups, asking no fill of its own. let mut subscriber = producer.subscribe(replay()); - assert_eq!(subscriber.assert_group().sequence, 5); - assert_eq!(subscriber.assert_group().sequence, 6); + let mut read: Vec<_> = (0..3).map(|_| subscriber.assert_group().sequence).collect(); + read.sort_unstable(); + assert_eq!(read, [2, 5, 6]); subscriber.assert_no_group(); + + // A bounded read of it alone ends once it took it. + let mut bounded = producer.subscribe(replay().with_range(2..3)); + assert_eq!(read_now(&mut bounded, false), Some(Some(2))); + assert_eq!(read_now(&mut bounded, false), Some(None)); + assert!( + dynamic.requested_fill_group().now_or_never().is_none(), + "the cache answered both reads" + ); } /// The feed's copy of a group a fetch cached first takes the slot, so a subscriber reads @@ -22149,4 +22243,126 @@ mod test { let _joined = producer.subscribe(None); assert_eq!(producer.demand().wanted(), Some(1), "a join asks for the same group"); } + + /// What a read returns right now: `Some(Some(sequence))` for a group, `Some(None)` for + /// its end, `None` while it parks. + fn read_now(subscriber: &mut Subscriber, ordered: bool) -> Option> { + match subscriber.poll_group(ordered, &kio::Waiter::noop()) { + Poll::Ready(res) => Some(res.expect("would have errored").map(|group| group.sequence)), + Poll::Pending => None, + } + } + + /// A read whose ranges all end returns `None` once it handed out every group in them, + /// in either order, rather than parking for an update to widen it. + #[test] + fn a_bounded_read_ends_once_its_window_is_read() { + for ordered in [false, true] { + let producer = track_producer("test", Info::timed()); + for sequence in 0..4u64 { + producer.create_group(sequence.into()).unwrap().finish().unwrap(); + } + let mut reader = producer.subscribe( + replay() + .with_order(Order::Ascending) + .with_ranges([Range::groups(1..2), Range::groups(3..4)]), + ); + assert_eq!(read_now(&mut reader, ordered), Some(Some(1)), "ordered: {ordered}"); + assert_eq!(read_now(&mut reader, ordered), Some(Some(3)), "ordered: {ordered}"); + assert_eq!(read_now(&mut reader, ordered), Some(None), "ordered: {ordered}"); + } + } + + /// An open range, or a bounded one still owed a group, parks; a local cap alone bounds + /// nothing, since its reader may raise it again. + #[test] + fn a_read_with_more_to_come_parks() { + let producer = track_producer("test", Info::timed()); + producer.create_group(0u64.into()).unwrap().finish().unwrap(); + + let mut open = producer.subscribe(replay().with_range(0..)); + assert_eq!(read_now(&mut open, false), Some(Some(0))); + assert_eq!(read_now(&mut open, false), None); + + let mut capped = producer.subscribe(replay().with_range(0..)).with_range(..1); + assert_eq!(read_now(&mut capped, false), Some(Some(0))); + assert_eq!(read_now(&mut capped, false), None); + + let mut owed = producer.subscribe(replay().with_range(0..2)); + assert_eq!(read_now(&mut owed, false), Some(Some(0))); + assert_eq!(read_now(&mut owed, false), None, "group 1 may still arrive"); + producer.create_group(1u64.into()).unwrap().finish().unwrap(); + assert_eq!(read_now(&mut owed, false), Some(Some(1))); + assert_eq!(read_now(&mut owed, false), Some(None)); + } + + /// A dropped group, or one skipped as stale, accounts for its place in a bounded window + /// as a group handed out does. + #[test] + fn a_bounded_read_ends_past_dropped_and_stale_groups() { + let producer = track_producer("test", Info::timed()); + let mut dropped = producer.subscribe(replay().with_range(0..2)); + producer.create_group(0u64.into()).unwrap().finish().unwrap(); + assert_eq!(read_now(&mut dropped, false), Some(Some(0))); + assert_eq!(read_now(&mut dropped, false), None); + producer.drop_groups(1..2, StreamError::NotFound); + assert_eq!(read_now(&mut dropped, false), Some(None)); + + let producer = track_producer("test", Info::timed()); + // Group 1 runs on past where group 0 could still present. + for (sequence, frames) in [(0u64, &[0u64][..]), (1, &[1_000, 1_100])] { + let mut group = producer.create_group(sequence.into()).unwrap(); + for at in frames { + group + .write_frame(Timestamp::from_millis(*at).unwrap(), bytes::Bytes::from_static(b"f")) + .unwrap(); + } + group.finish().unwrap(); + } + let mut stale = producer.subscribe( + Subscription::default() + .with_max_delay(Duration::from_millis(10)) + .with_order(Order::Ascending) + .with_range(0..2), + ); + assert_eq!(read_now(&mut stale, false), Some(Some(1)), "group 0 is stale"); + assert_eq!(read_now(&mut stale, false), Some(None)); + } + + /// A wire publisher's read parks past its window's end, since its peer would read the + /// end of the subscription as the track's. + #[test] + fn a_parked_bounded_read_waits_past_its_end() { + let producer = track_producer("test", Info::timed()); + producer.create_group(0u64.into()).unwrap().finish().unwrap(); + let mut reader = producer.subscribe(replay().with_range(0..1)).parked(); + assert_eq!(read_now(&mut reader, false), Some(Some(0))); + assert_eq!(read_now(&mut reader, false), None); + } + + /// A bounded read waits on the fill its miss asked, and ends once it took the answer. + #[kio::test] + #[cfg(feature = "async")] + async fn a_bounded_read_ends_after_its_fill() { + let (producer, _pool) = pooled_producer(1 << 40); + let dynamic = producer.requests(); + producer.create_group(0u64.into()).unwrap().finish().unwrap(); + producer.create_group(2u64.into()).unwrap().finish().unwrap(); + producer.finish().unwrap(); + let mut reader = producer.subscribe(replay().with_order(Order::Ascending).with_range(1..2)); + assert_eq!(read_now(&mut reader, false), None, "the fill is out"); + let fill = dynamic + .requested_fill_group() + .now_or_never() + .expect("the miss asks a fill") + .unwrap(); + assert_eq!(fill.sequence(), 1); + let mut group = fill.accept(Info::timed()).unwrap(); + group + .write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"filled")) + .unwrap(); + group.finish().unwrap(); + assert_eq!(read_now(&mut reader, false), Some(Some(1))); + assert_eq!(read_now(&mut reader, false), Some(None)); + } } diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index 0359daeb224..8dedbd883a4 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -682,16 +682,11 @@ impl Misses { } for run in candidates.iter() { // Backfill another reader's fill cached is no hole, yet the arrival walk never - // reaches it: this reader takes it as filled where it would ask for a hole. + // reaches it: this reader takes it as filled, as it would its own fill's answer, + // even while the live feed may still deliver the group, so a warm cache serves + // what a cold one fills. A live copy landing later is the same group handed out. for held in backfill(state, run.clone()).iter() { - let (unfed, awaited) = state.unfed(held.clone(), now); - for run in unfed.iter() { - self.filled.extend(run.clone()); - } - for (run, folds) in awaited { - self.filled.extend(run.start..run.end.min(start)); - self.deferred.insert(run.start.max(start)..run.end, folds); - } + self.filled.extend(held.clone()); } for hole in holes(state, run.clone()) { for hole in self.unclaimed(state, hole) { From e07f95ccc2d17507a4036d3dfa5b8926547cff62 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 03:37:55 -0800 Subject: [PATCH 3/9] refactor(net): keep a bounded window's write-offs with its filter Moves the stale write-offs into the boxed filter so the plain cursor stays small, names the refill test for what it checks, and rewrites the quest to the work and decisions that remain. Co-Authored-By: Claude Opus 5.5 --- quest/m1/subscribe-ranges/fetch-range.md | 77 ++++++++++++++---------- rs/moq-net/src/model/track.rs | 22 ++++--- 2 files changed, 58 insertions(+), 41 deletions(-) diff --git a/quest/m1/subscribe-ranges/fetch-range.md b/quest/m1/subscribe-ranges/fetch-range.md index 54ad84e5a59..537450884d5 100644 --- a/quest/m1/subscribe-ranges/fetch-range.md +++ b/quest/m1/subscribe-ranges/fetch-range.md @@ -15,20 +15,27 @@ Split out of dynamic range requests, which replaced `requested_group` with breaks one API once. Resized from [M] to [L] on 2026-10-24, when the first attempt ([moq-dev/moq#6722](https://github.com/moq-dev/moq/pull/6722)) found the questions decided below and landed only the `track_range_filling` benchmark. +The second attempt ([moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)) +landed the bounded-read end on a plain cursor, the parking wire publishers, and +the cached-backfill rule; what remains is below. - **Ending a bounded read** (decided 2026-10-20). `recv_group` returns `None` once every sequence in a bounded window is delivered, dropped, stale, or - unfillable. Today it parks past the window's end on purpose, so an update - can still widen it; a reader that wants more keeps an open range instead. - `Ok(None)` before any group arrives is a miss, which the moq-ffi fetch - wrapper reports as `NotFound`; after one, it is the range's end. A one-range - read surfaces no `NotFetchable` or `Old` result, and a peer's reset with - either names the group dropped. Every fetch boundary already treats - `NotFetchable` as not found (relay HTTP 404, hls `is_cache_miss`, moq-ffi - `map_fetch_error`, moq-c `NOT_FOUND`); a fetched `Old` becomes a miss too, - where relay HTTP answers 500 and moq-c reports `OLD` today. Both stay as wire - stream errors, and `Old` still aborts a cut group mid-read. - The Impact bullet says how to migrate each affected package. + unfillable. Landed for a track's own cache: a `with_range` cap never ends a + read, since it may rise again. A front's logical track (a routed track, + `model/resume.rs`) still parks, because each copy's reader parks and the + routes judge the end. Its `Delivered` counts what is no longer owed as + handed out (`track::Subscriber::skip_ordered`), so it cannot account for the + window alone, and `resume_model` asserts a reader never ends before a route + declares an end, which ending there has to change. `Ok(None)` before any group arrives is a miss, which the + moq-ffi fetch wrapper reports as `NotFound`; after one, it is the range's + end. A one-range read surfaces no `NotFetchable` or `Old` result, and a + peer's reset with either names the group dropped. Every fetch boundary + already treats `NotFetchable` as not found (relay HTTP 404, hls + `is_cache_miss`, moq-ffi `map_fetch_error`, moq-c `NOT_FOUND`); a fetched + `Old` becomes a miss too, where relay HTTP answers 500 and moq-c reports + `OLD` today. Both stay as wire stream errors, and `Old` still aborts a cut + group mid-read. The Impact bullet says how to migrate each affected package. - A one-range subscription for a cached live group missing the frames its range starts at needs a head fill. Frame bounds ([moq-dev/moq#6129](https://github.com/moq-dev/moq/pull/6129)) hand such a @@ -38,14 +45,12 @@ the questions decided below and landed only the `track_range_filling` benchmark. read has to request it instead of letting `Lagged` reach the caller. `fill::Ask` already carries a start frame, so the miss judge can count a headless slot as a hole for a bounded window. -- **Wire publishers keep parking.** The lite subscriber reads an upstream - FIN as the track's end on a published version. Once `recv_group` ends a - bounded read, the IETF publisher and lite publishers before lite-07 keep - parking on a bounded downstream SUBSCRIBE rather than FIN it, or a peer - reads the range's end as the track's. The lite-07 half already landed in - [moq-dev/moq#6807](https://github.com/moq-dev/moq/pull/6807): a lite-07 - publisher FINs a covered bounded subscription without SUBSCRIBE_END, which - ends only the subscription. +- **Wire publishers keep parking** (landed). Every wire publisher reads its + subscription through `track::Subscriber::parked`: the IETF publisher and + lite publishers before lite-07 keep parking on a bounded downstream + SUBSCRIBE rather than FIN it, and a lite-07 publisher FINs a covered bounded + subscription without SUBSCRIBE_END + ([moq-dev/moq#6807](https://github.com/moq-dev/moq/pull/6807)). - **Route recovery keeps a frame start.** `resume::Recover` fetches the rest of a group from frame N on the new route (`group::Fetch::frame_start`), which a whole-group range cannot say, so a crate-private fetch stays for it. @@ -58,17 +63,25 @@ the questions decided below and landed only the `track_range_filling` benchmark. its end. This asks nothing of a live feed; the rejected options were treating every bounded range's holes as misses, which asks fills for groups a live feed has yet to produce, and keeping a by-sequence read beside - subscriptions. -- **Cached backfill is owed** (maintainer, 2026-10-25). A range reader never - reads a group another reader's fill cached today: the arrival walk skips - backfill and the miss judge counts it held, so a second `0..1` read after a - first reader's fill cached group 0 parks with no fill asked. A cached - backfill group inside a reader's window is owed to it like its own fill's - answer, so a relay behaves the same warm or cold. This retires - `fetched_backfill_not_subscribed` and - `refilled_sequence_stays_out_of_subscriptions`, which assert that a - subscriber never sees backfill. Re-filling a cached group was rejected: it - costs an upstream FETCH for a group already in the cache. + subscriptions. The model has no signal for "no live feed" yet: no group + produced (`newest_produced` unset) also holds for a live subscription + before its first group, where it asks fills for groups the feed has yet to + produce (`subscribe_ranges::sparse_ranges_reach_the_publisher_on_lite07` + reads `[0]` on lite-06 when it does), and a moq-archive track is accepted + and live by every flag the model keeps. Open on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). +- **Holes below where the feed started.** The arrival ledger never folds the + gap below its first arrival (`Ledger::expire`), so on a live track with no + declared start a bounded read below the feed's first group parks forever + where `fetch_group` asks at once, and one with no budget waits out an + unbounded grace (the `rejoin` failures on + [moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487)). Open on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). +- **Cached backfill is owed** (maintainer, 2026-10-25; landed). A cached + backfill group inside a reader's window is taken as filled, whether or not + the live feed may still deliver it, so a relay behaves the same warm or + cold. Re-filling a cached group was rejected: it costs an upstream FETCH for + a group already in the cache. - **Relay demand before lite-07** (maintainer, 2026-10-25). A one-range read's ranges join the track's aggregate, so a relay would put a bounded SUBSCRIBE upstream where `fetch_group` puts a FETCH. `fetch_only.rs` asserts that a @@ -80,7 +93,9 @@ the questions decided below and landed only the `track_range_filling` benchmark. is one range FETCH on moq-transport, and per-group FETCH is lite-only, until [lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) land. A bounded upstream SUBSCRIBE accounted by SUBSCRIBE_START and SUBSCRIBE_DROP was - rejected: it works on lite-05 and later only. + rejected: it works on lite-05 and later only. A subscription to a copy no + SUBSCRIBE accepts waits for the track's info today, so this also needs a + one-range read to resolve from the info its fill installs. - **The fill path goes linear first** (maintainer, 2026-10-25). One-range reads put every past read on the fill path, which `track_range_filling` measured as superlinear in range count and span. It is flat since diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index ad81795861c..04bc1232ec3 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6169,9 +6169,6 @@ struct Cursor { drops: Option>, /// What this cursor misses and asks fills for, once it first reads. misses: Option>, - /// Groups of a bounded window skipped as stale, which account for it as handed out - /// would, until a wider budget owes them again. - written_off: RangeSet, } /// A group a cursor found but has not moved past; see [`Cursor::take`]. @@ -6322,6 +6319,9 @@ struct Filter { /// budget, scanned in the cache in the subscription's order and handed out ahead of the /// walk: each cached group in them is visited once. owed: RangeInclusiveSet, + /// Groups of a bounded window skipped as stale, which account for it as handed out + /// would, until a wider budget owes them again. + written_off: RangeSet, } impl Cursor { @@ -6364,6 +6364,7 @@ impl Cursor { admitted: RangeInclusiveSet::new(), admitted_at: None, owed: RangeInclusiveSet::new(), + written_off: RangeSet::new(), }; (filter, state.datagram_offset + start, state.cache.clone()) }; @@ -6386,7 +6387,6 @@ impl Cursor { skipped: Skipped::default(), drops: None, misses: None, - written_off: RangeSet::new(), } } @@ -6542,7 +6542,7 @@ impl Cursor { let accounted = |gap: ops::Range| { let mut left = RangeSet::new(); left.insert(gap.clone()); - for run in self.written_off.overlapping(&gap) { + for run in self.filter.written_off.overlapping(&gap) { left.remove(run.clone()); } if let Some(given_up) = given_up { @@ -6682,7 +6682,9 @@ impl Cursor { /// the cache, and nothing when the budget did not widen. fn reoffer(&mut self, budget: Duration) { for run in self.skipped.widened(budget) { - self.written_off.remove(*run.start()..run.end().saturating_add(1)); + self.filter + .written_off + .remove(*run.start()..run.end().saturating_add(1)); // Judged again, so a drop not reported yet is withdrawn; a reported one stands. if let Some(drops) = &mut self.drops { let span = *run.start()..run.end().saturating_add(1); @@ -7046,7 +7048,7 @@ impl Cursor { if self.filter.window.end().is_some() && let Some(end) = consumer.sequence.checked_add(1) { - self.written_off.insert(consumer.sequence..end); + self.filter.written_off.insert(consumer.sequence..end); } continue; } @@ -18063,11 +18065,11 @@ mod test { assert!(pool.used() <= 3_000, "growth is reclaimed: {}", pool.used()); } - /// A stale arrival entry whose sequence was later re-served by fetched backfill - /// must not leak the replacement into arrival-order subscriptions. + /// A stale arrival entry whose sequence was later re-served by fetched backfill must + /// not put the replacement in the arrival walk, which a subscriber reads as backfill. #[kio::test] #[cfg(feature = "async")] - async fn refilled_sequence_stays_out_of_subscriptions() { + async fn a_refilled_sequence_stays_out_of_the_arrival_walk() { let (producer, _pool) = pooled_producer(1 << 40); let dynamic = producer.requests(); let consumer = producer.consume(); From b3913b5e932e95fe9fec0e12ec2c0afddbd1dd16 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 10:23:26 -0800 Subject: [PATCH 4/9] quest(fetch-range): record the explicit-end answer and restate the open frontier decisions The maintainer answered on 2026-11-01 that an end should be an explicit signal rather than a stream FIN, which closes the subscription to later updates. Record the answer and restate the frontier and start decisions against it. Co-Authored-By: Claude Opus 5.5 --- quest/m1/subscribe-ranges/fetch-range.md | 60 +++++++++++++++--------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/quest/m1/subscribe-ranges/fetch-range.md b/quest/m1/subscribe-ranges/fetch-range.md index 537450884d5..0f657d8d05b 100644 --- a/quest/m1/subscribe-ranges/fetch-range.md +++ b/quest/m1/subscribe-ranges/fetch-range.md @@ -50,32 +50,48 @@ the cached-backfill rule; what remains is below. lite publishers before lite-07 keep parking on a bounded downstream SUBSCRIBE rather than FIN it, and a lite-07 publisher FINs a covered bounded subscription without SUBSCRIBE_END - ([moq-dev/moq#6807](https://github.com/moq-dev/moq/pull/6807)). + ([moq-dev/moq#6807](https://github.com/moq-dev/moq/pull/6807)) until the + explicit end below replaces that FIN. - **Route recovery keeps a frame start.** `resume::Recover` fetches the rest of a group from frame N on the new route (`group::Fetch::frame_start`), which a whole-group range cannot say, so a crate-private fetch stays for it. -- **A miss with no feed frontier** (maintainer, 2026-10-25). A reader asks - fills only below the newest group the live feed produced (decision 2 on - [moq-dev/moq#6123](https://github.com/moq-dev/moq/pull/6123)), so on a track - nothing feeds live, such as the moq-archive reader's or a relay copy no - subscription feeds, `Range::groups(5..6)` asks no fill and parks forever. - While the track has no live feed, holes in a bounded range are misses up to - its end. This asks nothing of a live feed; the rejected options were - treating every bounded range's holes as misses, which asks fills for groups - a live feed has yet to produce, and keeping a by-sequence read beside - subscriptions. The model has no signal for "no live feed" yet: no group - produced (`newest_produced` unset) also holds for a live subscription - before its first group, where it asks fills for groups the feed has yet to - produce (`subscribe_ranges::sparse_ranges_reach_the_publisher_on_lite07` - reads `[0]` on lite-06 when it does), and a moq-archive track is accepted - and live by every flag the model keeps. Open on +- **Ends are signaled, not inferred** (maintainer, 2026-11-01, on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)): "we should + have a SUBSCRIBE_END message or something to signal the last group of a + track? we were using QUIC FIN to signal that but that prevents updates in the + future, so we should have a signal instead". Two FINs end a lite + subscription today: the one after SUBSCRIBE_END on lite-05 and later, and + lite-07's covered bounded FIN without SUBSCRIBE_END. Either way, a later + SUBSCRIBE_UPDATE can no longer widen the subscription. The direction is an + explicit signal that names the end and leaves the stream open. It does not + yet say which message carries a covered subscription's end, or whether + SUBSCRIBE_END stops closing the stream. Both are open on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545), and the wire + half belongs to [lite-07 ranges](/quest/m1/subscribe-ranges/lite.md). +- **A miss with no frontier** (maintainer, 2026-10-25). A reader asks fills + only below the newest group the live feed produced (decision 2 on + [moq-dev/moq#6123](https://github.com/moq-dev/moq/pull/6123)), and + `Misses::judge` asks nothing while `newest_produced` is unset. So on a relay + copy that no subscription feeds, `Range::groups(5..6)` asks no fill and + parks forever. While the track has no live feed, holes in a bounded range + are misses up to its end. The rejected options were treating every bounded + range's holes as misses, which asks fills for groups a live feed has yet to + produce, and keeping a by-sequence read beside subscriptions. Treating "no + group produced" as "no feed" asks too early: on lite-06, + `subscribe_ranges::sparse_ranges_reach_the_publisher_on_lite07` then reads + `[0]`. moq-archive needs no new signal. Its consumer creates a track in the + same refresh that pushes the newest stored sequence and declares it with + `start_at`, so only an empty index has no frontier there. Which explicit + signal stands in for the frontier is open on [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). -- **Holes below where the feed started.** The arrival ledger never folds the - gap below its first arrival (`Ledger::expire`), so on a live track with no - declared start a bounded read below the feed's first group parks forever - where `fetch_group` asks at once, and one with no budget waits out an - unbounded grace (the `rejoin` failures on - [moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487)). Open on +- **Holes below where the feed started.** On a live track with no declared + start, a hole below the feed's first arrival is waited for over the arrival + grace (`Ledger::folds_at`) before it is asked, where `fetch_group` asks at + once. With no budget and no publisher max age the grace is unbounded, so the + read parks for good (the `rejoin` failures on + [moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487)). A hole under a + declared start is asked at once, but a refusal there is final only once the + ledger gives up on it. Open on [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). - **Cached backfill is owed** (maintainer, 2026-10-25; landed). A cached backfill group inside a reader's window is taken as filled, whether or not From ed7afb53d75e91f3280d281bdd1fa9a0a540dca1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 10:55:18 -0800 Subject: [PATCH 5/9] fix(net): bound the wait before a hole is a miss, and record the 2026-11-01 answers A hole the live feed may still deliver waited out the arrival grace before it was asked, and a refusal under a declared start was held for the same grace. With no budget and no publisher max age that grace is unbounded, so a bounded read of such a hole parked for good. The miss judge now waits ledger::GRACE when nothing bounds the grace (TrackState::miss_grace), and a refusal held in Misses::resolve wakes its reader at its deadline, which nothing did before. The quest records the maintainer's answers: no SUBSCRIBE_COVERED, the Subscribe Stream stays open after SUBSCRIBE_END, the OK blocks rather than naming a cache-derived edge, and every hole ends. The wire half is recorded for lite-07 ranges. Co-Authored-By: Claude Opus 5.5 --- quest/m1/subscribe-ranges/fetch-range.md | 106 +++++++++++++++-------- rs/moq-net/MIGRATE.md | 1 + rs/moq-net/src/model/track.rs | 66 ++++++++++++++ rs/moq-net/src/model/track/miss.rs | 30 +++++-- 4 files changed, 161 insertions(+), 42 deletions(-) diff --git a/quest/m1/subscribe-ranges/fetch-range.md b/quest/m1/subscribe-ranges/fetch-range.md index 0f657d8d05b..76c8bfe21b7 100644 --- a/quest/m1/subscribe-ranges/fetch-range.md +++ b/quest/m1/subscribe-ranges/fetch-range.md @@ -16,8 +16,8 @@ breaks one API once. Resized from [M] to [L] on 2026-10-24, when the first attempt ([moq-dev/moq#6722](https://github.com/moq-dev/moq/pull/6722)) found the questions decided below and landed only the `track_range_filling` benchmark. The second attempt ([moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)) -landed the bounded-read end on a plain cursor, the parking wire publishers, and -the cached-backfill rule; what remains is below. +landed the bounded-read end on a plain cursor, the parking wire publishers, +the cached-backfill rule, and the bounded miss grace; what remains is below. - **Ending a bounded read** (decided 2026-10-20). `recv_group` returns `None` once every sequence in a bounded window is delivered, dropped, stale, or @@ -51,7 +51,7 @@ the cached-backfill rule; what remains is below. SUBSCRIBE rather than FIN it, and a lite-07 publisher FINs a covered bounded subscription without SUBSCRIBE_END ([moq-dev/moq#6807](https://github.com/moq-dev/moq/pull/6807)) until the - explicit end below replaces that FIN. + explicit end below removes that FIN. - **Route recovery keeps a frame start.** `resume::Recover` fetches the rest of a group from frame N on the new route (`group::Fetch::frame_start`), which a whole-group range cannot say, so a crate-private fetch stays for it. @@ -59,40 +59,72 @@ the cached-backfill rule; what remains is below. [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)): "we should have a SUBSCRIBE_END message or something to signal the last group of a track? we were using QUIC FIN to signal that but that prevents updates in the - future, so we should have a signal instead". Two FINs end a lite - subscription today: the one after SUBSCRIBE_END on lite-05 and later, and - lite-07's covered bounded FIN without SUBSCRIBE_END. Either way, a later - SUBSCRIBE_UPDATE can no longer widen the subscription. The direction is an - explicit signal that names the end and leaves the stream open. It does not - yet say which message carries a covered subscription's end, or whether - SUBSCRIBE_END stops closing the stream. Both are open on - [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545), and the wire - half belongs to [lite-07 ranges](/quest/m1/subscribe-ranges/lite.md). -- **A miss with no frontier** (maintainer, 2026-10-25). A reader asks fills - only below the newest group the live feed produced (decision 2 on - [moq-dev/moq#6123](https://github.com/moq-dev/moq/pull/6123)), and - `Misses::judge` asks nothing while `newest_produced` is unset. So on a relay - copy that no subscription feeds, `Range::groups(5..6)` asks no fill and - parks forever. While the track has no live feed, holes in a bounded range - are misses up to its end. The rejected options were treating every bounded - range's holes as misses, which asks fills for groups a live feed has yet to - produce, and keeping a by-sequence read beside subscriptions. Treating "no - group produced" as "no feed" asks too early: on lite-06, - `subscribe_ranges::sparse_ranges_reach_the_publisher_on_lite07` then reads - `[0]`. moq-archive needs no new signal. Its consumer creates a track in the - same refresh that pushes the newest stored sequence and declares it with - `start_at`, so only an empty index has no frontier there. Which explicit - signal stands in for the frontier is open on - [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). -- **Holes below where the feed started.** On a live track with no declared - start, a hole below the feed's first arrival is waited for over the arrival - grace (`Ledger::folds_at`) before it is asked, where `fetch_group` asks at - once. With no budget and no publisher max age the grace is unbounded, so the - read parks for good (the `rejoin` failures on - [moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487)). A hole under a - declared start is asked at once, but a refusal there is final only once the - ledger gives up on it. Open on - [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). + future, so we should have a signal instead". The follow-up answers, the same + day: + - "do we need a SUBSCRIBE_COVERED? We have SUBSCRIBE_DROP so the peer can + determine that the bounded range has been received? SUBSCRIBE_END (rename + to FINISH?) would signal the final group of the track maybe, but would be + independent of the bounded range?" No new message: a subscriber judges a + bounded range complete from the groups it received, SUBSCRIBE_DROP, the + Range Starts in SUBSCRIBE_OK, and SUBSCRIBE_END. Drops and groups name + absolute sequences, so the subscriber judges against its latest ranges and + an update in flight changes nothing it has counted; the FIN's race with an + unread update goes away. One gap: before SUBSCRIBE_OK a lite-07 publisher + holds back what it gave up (`TrackRun::skipped`) and answers only once a + group stream resolves the start, so a bounded subscription none of whose + groups it can serve gets neither, and only the covered FIN ends it today. + Fix: once such a subscription is covered, the publisher sends SUBSCRIBE_OK + and then the drops it held. Datagrams are left out: they become their own + track kind, which takes no ranges (maintainer, 2026-11-01). + - "Keep stream open": SUBSCRIBE_END (or FINISH) names the track's final group + and no longer FINs the Subscribe Stream, so an update can still reach an + ended track's history. The subscriber FINs once it wants nothing more, and + the publisher FINs in reply. The covered FIN goes too. + - Whether SUBSCRIBE_END is renamed SUBSCRIBE_FINISH is open on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). + + This wire half belongs to [lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) + (Rust and the draft) and [JS ranges](/quest/m1/subscribe-ranges/js.md). It is + recorded here while [moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487) + rewrites that quest, and moves there once it lands. +- **The live edge is told, never inferred** (maintainer, 2026-11-01, on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)): "If the relay + doesn't know the live edge, it shouldn't say anything. It should be a + separate message if it's async or the OK should block. NEVER use the cache + as the live edge." The OK blocks, since the wire already works that way: + lite-07 SUBSCRIBE_OK carries Largest Group and waits until each Range's + start resolves, a relay's `TrackRun` waits on its source's start + (`track::Subscriber::poll_start`) before answering, and an idle copy reads + nothing until its route answers. The draft's exception for a start past the + publisher's largest position should say that a relay knows that position + only from its upstream's answer (lite-07 ranges' draft change). A reader + asks fills only below the newest group the live feed produced (decision 2 + on [moq-dev/moq#6123](https://github.com/moq-dev/moq/pull/6123)), so with + nothing produced it waits for the answer that its own demand's upstream + subscription brings. Earlier (maintainer, 2026-10-25): while a track has no + live feed, holes in a bounded range are misses up to its end, and treating + "no group produced" as "no feed" asks too early + (`subscribe_ranges::sparse_ranges_reach_the_publisher_on_lite07` reads + `[0]` on lite-06). How a copy that no subscription feeds, such as a + moq-transport fetch's, learns that it has no feed is open on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). moq-archive + needs nothing new: its consumer declares the newest stored sequence with + `start_at`. +- **Every hole ends** (maintainer, 2026-11-01, on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)): "uhh the + publisher is supposed to emit the start/end bounds if the subscriber didn't + ask for one? I don't understand, but parking forever is terrible?" A hole is + asked as a miss, refused, or covered by the publisher's bounds, and a + bounded read never parks for good. A hole under the start the publisher + resolved is asked at once, and a refusal there is held while the feed may + still deliver the group late (maintainer, 2026-10-21, on + [moq-dev/moq#6171](https://github.com/moq-dev/moq/pull/6171)). Nothing is + inferred from the first arrival, since a local producer may still write a + lower group (`late_lower_group`). Landed: that hold, and the wait before a + hole with no start under it is asked, last the arrival grace, or + `ledger::GRACE` when neither a budget nor a max age bounds it + (`TrackState::miss_grace`), and a held refusal wakes its reader at its + deadline. - **Cached backfill is owed** (maintainer, 2026-10-25; landed). A cached backfill group inside a reader's window is taken as filled, whether or not the live feed may still deliver it, so a relay behaves the same warm or diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 809c6a70e38..376bed35ebe 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -9,6 +9,7 @@ How to update your code for each breaking change, newest first. When every one of a subscription's `ranges` has an end, `track::Subscriber::recv_group` and `track::Ordered::next_group` return `None` once each group in them was handed out, dropped, skipped as stale, or given up as unfillable, instead of waiting for an update to widen the ranges. A reader that wants to widen its ranges later keeps one open, such as `Range::groups(n..)`, and narrows its reads with `with_range`, whose cap never ends a read. A group another reader's fetch or fill cached is now handed to every subscriber whose ranges hold it, not only fetches. +A missing group the live feed may still deliver is waited for at most one second before it is asked of a fill or named dropped, when neither the readers' `max_delay` nor the track's max age bounds the wait. Before, a reader with `max_delay: None` on a track with no max age waited for it until the track ended. ### `broadcast::Demand` reads like `track::Demand` diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index d8f8acc4e28..f32c36f9c62 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -22482,4 +22482,70 @@ mod test { assert_eq!(read_now(&mut reader, false), Some(Some(1))); assert_eq!(read_now(&mut reader, false), Some(None)); } + + /// A hole the live feed may still deliver is waited for over the arrival grace before + /// it is asked as a miss. With no budget and no publisher max age nothing bounds that + /// grace, so the wait is [`ledger::GRACE`]: a bounded read of a hole below where a live + /// feed started, with no start declared, ends rather than parking for good. + #[kio::test] + #[cfg(feature = "async")] + async fn a_hole_below_an_undeclared_start_is_asked_after_a_bounded_grace() { + let (producer, _pool) = pooled_producer(1 << 40); + let dynamic = producer.requests(); + producer.create_group(10u64.into()).unwrap().finish().unwrap(); + let mut reader = producer.subscribe(Subscription::default().with_max_delay(None).with_range(5..6)); + assert_eq!(read_now(&mut reader, false), None, "group 5 may still arrive"); + assert!(dynamic.requested_fill_group().now_or_never().is_none()); + elapse(&producer, ledger::GRACE); + assert_eq!(read_now(&mut reader, false), None, "the fill is out"); + let fill = dynamic + .requested_fill_group() + .now_or_never() + .expect("the hole is asked once the grace passed") + .unwrap(); + assert_eq!(fill.sequence(), 5); + fill.reject(Error::NotFound); + assert_eq!(read_now(&mut reader, false), Some(None), "the refusal is final"); + + // With no fill handler, the hole is given up once the grace passed. + let (producer, _pool) = pooled_producer(1 << 40); + producer.create_group(10u64.into()).unwrap().finish().unwrap(); + let mut reader = producer.subscribe(Subscription::default().with_max_delay(None).with_range(5..6)); + assert_eq!(read_now(&mut reader, false), None); + elapse(&producer, ledger::GRACE); + assert_eq!(read_now(&mut reader, false), Some(None)); + } + + /// A hole below a declared start is asked at once, and a refusal there is held while the + /// live feed may still deliver the group late. With nothing bounding the arrival grace the + /// hold lasts [`ledger::GRACE`], on a received copy as on a local track, so the read ends. + #[kio::test] + #[cfg(feature = "async")] + async fn a_refusal_below_the_start_is_held_for_a_bounded_grace() { + for received in [false, true] { + let (mut producer, _pool) = pooled_producer(1 << 40); + if received { + producer.received(); + } + let dynamic = producer.requests(); + producer.start_at(10).unwrap(); + producer.create_group(10u64.into()).unwrap().finish().unwrap(); + let mut reader = producer.subscribe(Subscription::default().with_max_delay(None).with_range(5..6)); + assert_eq!(read_now(&mut reader, false), None, "received: {received}"); + let fill = dynamic + .requested_fill_group() + .now_or_never() + .expect("a hole below the start is asked at once") + .unwrap(); + assert_eq!(fill.sequence(), 5, "received: {received}"); + fill.reject(Error::NotFound); + assert_eq!( + read_now(&mut reader, false), + None, + "received: {received}: may still arrive" + ); + elapse(&producer, ledger::GRACE); + assert_eq!(read_now(&mut reader, false), Some(None), "received: {received}"); + } + } } diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index 0fdeea34f55..bb44bb17b09 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -5,7 +5,8 @@ //! fill is bounded by construction. Below it, a hole is a miss once the feed is past it: //! under where the feed starts, or accounted for by the arrival ledger without being cached //! (evicted, or a gap the feed skipped). A hole the ledger still waits for is deferred and -//! judged again as the feed moves. +//! judged again as the feed moves, or once its wait ends: the arrival grace, bounded by +//! [`ledger::GRACE`] when nothing else bounds it, so every hole is asked in finite time. //! //! Each sequence is judged once, except a group evicted after the reader judged it cached //! and before it took it: judged again as a miss, once. A refill evicted again before it is @@ -30,7 +31,7 @@ use super::super::delivered::Delivered; pub(super) use super::fill::Asking; use super::fill::{Ask, FillState, Join}; use super::{Drift, Order, Position, TrackState}; -use crate::Error; +use crate::{Error, ledger}; /// What a probe judges against: the span read, the feed's newest group, where the feed /// and readers start, and the drops revision. @@ -350,6 +351,7 @@ impl Misses { waiter: &kio::Waiter, ) -> RangeSet { let mut retry = RangeSet::new(); + let deadline = self.deferred.deadline(); // Only the joins holding something unanswered: one holding only filled groups until // they are taken has nothing to answer. let keys: BTreeSet = self @@ -433,6 +435,13 @@ impl Misses { self.release(&taken); // Judged again: the same join may have more to answer, or park the waiter now. } + // A refusal held while the feed may still deliver its groups is judged again once + // the feed gives up on them, which nothing else wakes this reader for. + if let Some(at) = self.deferred.deadline() + && Some(at) != deadline + { + state.read().cache.watch_wall(at, waiter); + } retry } @@ -489,7 +498,7 @@ impl Misses { if unchanged && self.deferred.runs.is_empty() { return asks; } - let grace = state.arrival_grace(); + let grace = state.miss_grace(); let unchanged = unchanged && self.grace == Some(grace); let deadline = self.deferred.deadline(); if unchanged && deadline.is_none() { @@ -848,7 +857,7 @@ impl Misses { for run in left.iter() { self.refused.insert(run.clone()); self.deferred - .insert(run.clone(), state.arrivals.folds_at(run.start, state.arrival_grace())); + .insert(run.clone(), state.arrivals.folds_at(run.start, state.miss_grace())); } } } @@ -953,6 +962,17 @@ impl Inboxes { } impl TrackState { + /// How long a hole the live feed may still deliver is waited for before it is a miss: + /// the arrival grace, or [`ledger::GRACE`] when nothing bounds that, so every hole is + /// asked or given up in finite time. A reader with no budget on a track with no max age + /// would otherwise wait out an unbounded grace, and a read of the hole would never end. + pub(super) fn miss_grace(&self) -> Duration { + match self.arrival_grace() { + Duration::MAX => ledger::GRACE, + grace => grace, + } + } + /// Split `hole`, which the cache holds nothing for, into what the live feed will never /// deliver and the gaps it still waits for, with when each stops being waited for. /// @@ -977,7 +997,7 @@ impl TrackState { return (unfed, awaited); } let mut from = hole.start; - let grace = self.arrival_grace(); + let grace = self.miss_grace(); let mut gap = |groups: ops::Range, unfed: &mut RangeSet| match self.arrivals.folds_at(groups.start, grace) { Some(at) if at <= now => unfed.insert(groups), From e6ff6aa402ab9eacd96b45a3167c3cf557d96a48 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 11:22:20 -0800 Subject: [PATCH 6/9] feat(net): the session marks a fetch-only copy unfed, and record the 2026-11-01 answers A copy the session holds no upstream subscription for is unfed (track::Producer::set_unfed), from the session's own state. A bounded read's holes there are misses up to its end at once, a refusal is final, and the read takes its window from the cache even while the copy is idle. The moq-transport subscriber marks a lingering copy unfed, and serves a subscription wholly below the reported Largest Object by FETCH rather than subscribing upstream. Record the SUBSCRIBE_FINISH rename and the settled lite-07 wire design in the quest until moq-dev/moq#7487 lands. Co-Authored-By: Claude Opus 5.5 --- quest/m1/subscribe-ranges/fetch-range.md | 60 ++++++++--- rs/moq-net/MIGRATE.md | 1 + rs/moq-net/src/ietf/subscriber.rs | 29 +++++- rs/moq-net/src/model/track.rs | 116 +++++++++++++++++++-- rs/moq-net/src/model/track/miss.rs | 34 ++++-- rs/moq-net/tests/integration/fetch_only.rs | 116 +++++++++++++++++++++ 6 files changed, 324 insertions(+), 32 deletions(-) diff --git a/quest/m1/subscribe-ranges/fetch-range.md b/quest/m1/subscribe-ranges/fetch-range.md index 76c8bfe21b7..36fb438a660 100644 --- a/quest/m1/subscribe-ranges/fetch-range.md +++ b/quest/m1/subscribe-ranges/fetch-range.md @@ -17,7 +17,8 @@ attempt ([moq-dev/moq#6722](https://github.com/moq-dev/moq/pull/6722)) found the questions decided below and landed only the `track_range_filling` benchmark. The second attempt ([moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)) landed the bounded-read end on a plain cursor, the parking wire publishers, -the cached-backfill rule, and the bounded miss grace; what remains is below. +the cached-backfill rule, the bounded miss grace, and the unfed moq-transport +copy; what remains is below. - **Ending a bounded read** (decided 2026-10-20). `recv_group` returns `None` once every sequence in a bounded window is delivered, dropped, stale, or @@ -80,13 +81,30 @@ the cached-backfill rule, and the bounded miss grace; what remains is below. and no longer FINs the Subscribe Stream, so an update can still reach an ended track's history. The subscriber FINs once it wants nothing more, and the publisher FINs in reply. The covered FIN goes too. - - Whether SUBSCRIBE_END is renamed SUBSCRIBE_FINISH is open on - [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). + - ✅ Rename SUBSCRIBE_END to SUBSCRIBE_FINISH in lite-07 (maintainer, + 2026-11-01). It matches TRACKS_FINISH and `track::Producer::finish`, and + "END" would read as closing the stream, which it no longer does. + + The settled lite-07 wire design, together: + - SUBSCRIBE_FINISH (formerly SUBSCRIBE_END) names the track's final group and + no longer FINs the Subscribe Stream. + - A covered bounded subscription is not FINed either: the subscriber judges + it complete from the groups it received, SUBSCRIBE_DROP, the Range Starts in + SUBSCRIBE_OK, and SUBSCRIBE_FINISH. + - A publisher that held back SUBSCRIBE_OK (`TrackRun::skipped`) sends it, then + the drops it held, once such a subscription is covered. + - The subscriber FINs the stream once it wants nothing more, and the + publisher FINs in reply. + - Datagrams take no ranges, since they become their own track kind. The + datagram-tracks plan + ([moq-dev/moq#7575](https://github.com/moq-dev/moq/pull/7575)) touches + `lite.md` too, and should take this design and the rename as given. This wire half belongs to [lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - (Rust and the draft) and [JS ranges](/quest/m1/subscribe-ranges/js.md). It is - recorded here while [moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487) - rewrites that quest, and moves there once it lands. + (Rust and the draft) and [JS ranges](/quest/m1/subscribe-ranges/js.md), not + to this quest. It is recorded here while + [moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487) rewrites that + quest, and moves there once it lands. - **The live edge is told, never inferred** (maintainer, 2026-11-01, on [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)): "If the relay doesn't know the live edge, it shouldn't say anything. It should be a @@ -105,11 +123,20 @@ the cached-backfill rule, and the bounded miss grace; what remains is below. live feed, holes in a bounded range are misses up to its end, and treating "no group produced" as "no feed" asks too early (`subscribe_ranges::sparse_ranges_reach_the_publisher_on_lite07` reads - `[0]` on lite-06). How a copy that no subscription feeds, such as a - moq-transport fetch's, learns that it has no feed is open on - [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545). moq-archive - needs nothing new: its consumer declares the newest stored sequence with - `start_at`. + `[0]` on lite-06). moq-archive needs nothing new: its consumer declares the + newest stored sequence with `start_at`. +- **The session marks a copy unfed** (maintainer, 2026-11-01, on + [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545); landed). ✅ + The session marks a copy it holds no upstream subscription for unfed + (`track::Producer::set_unfed`), from its own state, never from the cache. + A bounded read's holes there are misses up to its end at once, per the + 2026-10-25 rule, a refusal is final, and the read takes what the cache holds + in its window even while the copy is idle; an open read still waits for a + feed. The moq-transport subscriber marks a lingering copy unfed, such as one + learned from TRACK_STATUS for a fetch, and keeps a subscription whose ranges + all end below the publisher's reported Largest Object out of the upstream + SUBSCRIBE (`fetch_only.rs`). The rejected alternative, a one-range read always + subscribing so its OK names the edge, broke the fetch-only rule below. - **Every hole ends** (maintainer, 2026-11-01, on [moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545)): "uhh the publisher is supposed to emit the start/end bounds if the subscriber didn't @@ -141,9 +168,14 @@ the cached-backfill rule, and the bounded miss grace; what remains is below. is one range FETCH on moq-transport, and per-group FETCH is lite-only, until [lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) land. A bounded upstream SUBSCRIBE accounted by SUBSCRIBE_START and SUBSCRIBE_DROP was - rejected: it works on lite-05 and later only. A subscription to a copy no - SUBSCRIBE accepts waits for the track's info today, so this also needs a - one-range read to resolve from the info its fill installs. + rejected: it works on lite-05 and later only. Landed for a lingering + moq-transport copy whose subscription lies wholly below the reported Largest + Object (the unfed copy above). Still to do: a first request with a bounded + subscription, which SUBSCRIBEs before any TRACK_STATUS names an edge; a + subscription only partly below the edge; and lite before lite-07. A + subscription to a copy no SUBSCRIBE accepts waits for the track's info + today, so this also needs a one-range read to resolve from the info its fill + installs. - **The fill path goes linear first** (maintainer, 2026-10-25). One-range reads put every past read on the fill path, which `track_range_filling` measured as superlinear in range count and span. It is flat since diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 376bed35ebe..43787da6395 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -10,6 +10,7 @@ When every one of a subscription's `ranges` has an end, `track::Subscriber::recv A reader that wants to widen its ranges later keeps one open, such as `Range::groups(n..)`, and narrows its reads with `with_range`, whose cap never ends a read. A group another reader's fetch or fill cached is now handed to every subscriber whose ranges hold it, not only fetches. A missing group the live feed may still deliver is waited for at most one second before it is asked of a fill or named dropped, when neither the readers' `max_delay` nor the track's max age bounds the wait. Before, a reader with `max_delay: None` on a track with no max age waited for it until the track ended. +Over moq-transport, a subscription to a copy the session holds no upstream subscription for, such as one only fetched so far, whose ranges all end below the publisher's reported Largest Object, is served by FETCH without subscribing upstream. Each missing group is asked at once and a refusal is final, since nothing else would deliver it. ### `broadcast::Demand` reads like `track::Demand` diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 4f9e02231c5..5d928d470bb 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -751,6 +751,18 @@ fn largest_group(track: &track::Producer, answer: Option) -> Opt answer.map(|largest| largest.group).max(track.latest_sequence()) } +/// Whether `subscription` needs a live feed for a copy whose publisher reported its Largest +/// Object in group `edge`: one of its ranges is open, counts back from the live edge, or +/// reaches the group the publisher may still be writing. Ranges wholly below it read only +/// history, which fills serve. +fn wants_feed(subscription: &track::Subscription, edge: Option) -> bool { + if subscription.ranges.is_empty() { + return false; + } + let history = subscription.start().is_some() && subscription.end().zip(edge).is_some_and(|(end, edge)| end <= edge); + !history +} + /// A copy with no subscription upstream, kept for fetches and a returning subscriber. struct Idle { track: track::Producer, @@ -3963,6 +3975,12 @@ impl Subscriber { /// Keep a copy nobody subscribes to, cache and all, for fetches and a subscriber /// returning soon: `Some` once one does, to subscribe again, `None` once nobody held /// it through the linger, or the session ended. A finished copy never subscribes again. + /// + /// The session holds no upstream subscription for the copy meanwhile, so it marks the + /// copy unfed: a bounded read's holes are misses at once, which the fills answer. A + /// subscription whose ranges all lie below the Largest Object the publisher reported + /// reads only history, so it is served that way too, as a FETCH would be, rather than + /// subscribing. async fn linger( &mut self, broadcast_path: &Path, @@ -3980,10 +3998,11 @@ impl Subscriber { id }; let mut linger = crate::time::Deadline::new(&self.runtime); + idle.track.set_unfed(true); let mut subscribed = idle .track .subscription() - .is_some_and(|subscription| !subscription.ranges.is_empty()); + .is_some_and(|subscription| wants_feed(&subscription, idle.track.reported_group())); enum Step { Fill(track::Fill), Subscribe, @@ -4006,7 +4025,7 @@ impl Subscriber { while let Poll::Ready(Ok(subscription)) = idle.track.poll_subscription_changed(waiter) { subscribed = subscription .as_ref() - .is_some_and(|subscription| !subscription.ranges.is_empty()); + .is_some_and(|subscription| wants_feed(subscription, idle.track.reported_group())); } if subscribed && !idle.finished { return Poll::Ready(Step::Subscribe); @@ -4038,7 +4057,11 @@ impl Subscriber { ); group_fetches.push(fetch); } - Step::Subscribe => break true, + Step::Subscribe => { + // Marked before the SUBSCRIBE goes out: a hole may arrive live from here. + idle.track.set_unfed(false); + break true; + } Step::Expired => match idle.track.abort_unwanted(Error::Cancel) { Ok(()) => { tracing::debug!(broadcast = %crate::path::Rooted(self.origin.root(), broadcast_path), track = %track_name, "track released (idle)"); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index f32c36f9c62..c37ef5af15a 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -516,6 +516,11 @@ pub(crate) struct TrackState { // creation, and only a session's copy goes idle: when its upstream subscription ends // with the copy still held, until the route's answer shows in the cache. feed: Feed, + // No subscription feeds this copy: its session holds none upstream for it, so nothing + // reaches it but what fills answer. Set and cleared only by the session, from its own + // state, never judged from the cache. A bounded read's holes are then misses up to its + // end at once, with nothing to wait for (see [`Self::nothing_arrives`]). + unfed: bool, // Readers start at this group: the route's live feed starts past a gap after // everything cached, so nothing bounds how old the cache below it is. live_floor: Option, @@ -1156,14 +1161,20 @@ impl TrackState { }; } - /// Find the next live group at or after `index` in arrival order. + /// Find the next live group at or after `index` in arrival order, for a reader reading + /// `window`. /// /// Returns the group and its absolute index so the consumer can advance past it. /// The cached producer rather than a consumer: `consume` reads the clock and can /// take the group's own lock, which the caller does once the track guard is gone. - fn poll_recv_group(&self, index: usize, min_sequence: u64) -> Poll>> { + fn poll_recv_group( + &self, + index: usize, + min_sequence: u64, + window: &Window, + ) -> Poll>> { let start = index.saturating_sub(self.offset); - let readable = self.readable(); + let readable = self.serves_bounded(window); let min_sequence = min_sequence.max(self.live_floor.unwrap_or(0)); for (i, (sequence, stamp)) in self.arrival.iter().enumerate().skip(start).filter(|_| readable) { // A cut group is handed out too, so its reader sees why it ended. @@ -1293,7 +1304,7 @@ impl TrackState { let best = window .runs(next_sequence.max(self.live_floor.unwrap_or(0))) - .filter(|_| self.readable()) + .filter(|_| self.serves_bounded(window)) .find_map(|run| { // A cut group is handed out too, so its reader sees why it ended. self.lookup @@ -2594,6 +2605,19 @@ impl TrackState { reached && (self.abort.is_none() || self.settled.load(Ordering::Relaxed)) } + /// Whether nothing more reaches the cache but what fills answer: the track was aborted, + /// reached its end, or its session holds no subscription that feeds it. + pub(super) fn nothing_arrives(&self) -> bool { + self.abort.is_some() || self.is_complete() || self.unfed + } + + /// Whether a bounded window reads from the cache: whenever readers may, and on a copy no + /// subscription feeds, whose groups in the window are the groups themselves however old + /// its live edge is. An open window still waits for the feed. + fn serves_bounded(&self, window: &Window) -> bool { + self.readable() || self.unfed && window.end().is_some() + } + /// Whether the live edge reached `fin`, or a received copy's tail decides it instead: /// what it still misses up to `fin` is waited for only as long as the grace. fn reaches(&self, fin: u64) -> bool { @@ -3601,6 +3625,28 @@ impl Producer { } } + /// Whether a subscription feeds this copy: its session holds one upstream for it, or + /// none, so nothing reaches it but what fills answer. Only the session knows, from its + /// own state; the cache never tells. While unfed, a bounded read's holes are misses up + /// to its end at once, and it reads the cache whether or not the copy is idle. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn set_unfed(&self, unfed: bool) { + // Unchanged wakes nobody: every write wakes the readers. + if self.state.read().unfed == unfed { + return; + } + if let Ok(mut state) = self.modify() { + state.unfed = unfed; + } + } + + /// The group of the newest Largest Object the upstream publisher's answers reported, + /// kept however far the cache has moved past it: every group below it is history there. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn reported_group(&self) -> Option { + self.state.read().object_edge.map(|((group, _), _)| group) + } + /// The live feed's route answered that it reaches `group`: a relative start counts back /// from it, rather than from whichever group lands first. A no-op unless live. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] @@ -6657,7 +6703,7 @@ impl Cursor { let ascending = self.subscription.read().order == Order::Ascending; let state = self.state.read(); // Nothing is read from the cache now, so leave what is owed until it is. - if !state.readable() { + if !state.serves_bounded(&self.filter.window) { return None; } let low = state.live_floor.unwrap_or(0); @@ -6921,6 +6967,7 @@ impl Cursor { &state, (from, revision, cap), window.named(), + window.end(), |span| { let end = cap.map_or(*span.end(), |cap| (*span.end()).min(cap.saturating_sub(1))); if cap == Some(0) || *span.start() > end { @@ -7034,7 +7081,9 @@ impl Cursor { Some((producer, taken)) => (producer.consume(), taken), None => { let floor = self.floor(); - let polled = self.poll(waiter, |state| state.poll_recv_group(self.index, floor)); + let polled = self.poll(waiter, |state| { + state.poll_recv_group(self.index, floor, &self.filter.window) + }); // A moved window moved the servable edge `drift` was judged against. if polled.is_pending() && self.watch_time(waiter) { self.admit(); @@ -18216,7 +18265,11 @@ mod test { assert!(consumer.peek_group(1).is_some()); let mut walked = Vec::new(); let mut index = 0; - while let Poll::Ready(Ok(Some((group, next)))) = producer.state.read().poll_recv_group(index, 0) { + let window = { + let state = producer.state.read(); + Window::new(&replay().ranges, None, &*state).unwrap() + }; + while let Poll::Ready(Ok(Some((group, next)))) = producer.state.read().poll_recv_group(index, 0, &window) { walked.push(group.sequence); index = next + 1; } @@ -22548,4 +22601,53 @@ mod test { assert_eq!(read_now(&mut reader, false), Some(None), "received: {received}"); } } + + /// A copy its session holds no subscription for asks a bounded read's holes as misses + /// at once, though it is idle and its feed produced nothing, and the read ends once the + /// fill answered or refused each, since nothing else will deliver them. + #[kio::test] + #[cfg(feature = "async")] + async fn an_unfed_copy_asks_a_bounded_read_s_holes_at_once() { + let (mut producer, _pool) = pooled_producer(1 << 40); + let dynamic = producer.requests(); + producer.set_idle(); + let mut reader = producer.subscribe(replay().with_order(Order::Ascending).with_range(1..3)); + assert_eq!(read_now(&mut reader, false), None); + assert!( + dynamic.requested_fill().now_or_never().is_none(), + "an idle copy a feed may still answer asks nothing" + ); + + producer.set_unfed(true); + assert_eq!(read_now(&mut reader, false), None, "the fill is out"); + let fill = dynamic + .requested_fill() + .now_or_never() + .expect("the holes are asked at once") + .unwrap(); + assert_eq!((fill.start(), fill.end()), (Position::group(1), 3)); + let mut group = fill.accept(1, Info::timed()).unwrap(); + group + .write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"filled")) + .unwrap(); + group.finish().unwrap(); + fill.refuse_groups(2..3, Error::Unsupported); + assert_eq!(read_now(&mut reader, false), Some(Some(1))); + assert_eq!(read_now(&mut reader, false), Some(None), "the refusal is final"); + } + + /// On a copy no subscription feeds, a bounded read takes what the cache holds in its + /// window, idle or not, while an open read still waits for a feed to answer. + #[test] + fn an_unfed_copy_serves_a_bounded_read_from_its_cache() { + let producer = idle_copy(); + producer.set_unfed(true); + let mut bounded = producer.subscribe(replay().with_order(Order::Ascending).with_range(1..3)); + assert_eq!(read_now(&mut bounded, false), Some(Some(1))); + assert_eq!(read_now(&mut bounded, false), Some(Some(2))); + assert_eq!(read_now(&mut bounded, false), Some(None)); + + let mut open = producer.subscribe(replay().with_range(1..)); + assert_eq!(read_now(&mut open, false), None, "the cache is not the live edge"); + } } diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index bb44bb17b09..d9a217a511b 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -447,8 +447,9 @@ impl Misses { /// Judge the holes in `target` the last probe has not, returning the runs to ask a /// fill for. Gives up a run already stale under this reader's budget. `named` is the - /// lowest group the reader's ranges name, which may reach below the feed's start, and - /// `taken` what the reader handed out, which an eviction never makes a miss again. + /// lowest group the reader's ranges name, which may reach below the feed's start, + /// `bound` the exclusive end of a window whose ranges all end, and `taken` what the + /// reader handed out, which an eviction never makes a miss again. #[expect( clippy::too_many_arguments, reason = "one probe's inputs, borrowed apart from its cursor" @@ -458,6 +459,7 @@ impl Misses { state: &TrackState, key: (u64, u64, Option), named: Option, + bound: Option, mut target: impl FnMut(RangeInclusive) -> RangeInclusiveSet, taken: &Delivered, drift: &Drift, @@ -465,12 +467,15 @@ impl Misses { waiter: &kio::Waiter, ) -> RangeSet { let mut asks = RangeSet::new(); + // On a copy no subscription feeds, a bounded window's holes are misses up to its + // end, since no feed will deliver them nor produce anything to judge them below. + let bound = bound.filter(|_| state.unfed).and_then(|end| end.checked_sub(1)); // The feed's start is unknown until the serving session resolves it, and readers // take nothing from a cache that does not reflect the feed. - if state.start_pending || !state.readable() { + if state.start_pending || !state.readable() && bound.is_none() { return asks; } - let Some(newest) = state.newest_produced else { + let Some(newest) = state.newest_produced.max(bound) else { return asks; }; let at = ( @@ -482,7 +487,7 @@ impl Misses { state.start_sequence, state.drops_revision, ); - let ended = state.abort.is_some() || state.is_complete(); + let ended = state.nothing_arrives(); let ledger = state.arrivals.changes(); let drops = &state.drops_changes; // Every reader probes on every poll, mostly with nothing changed since the last, so @@ -990,9 +995,10 @@ impl TrackState { ) -> (RangeSet, Vec<(ops::Range, Option)>) { let mut unfed = RangeSet::new(); let mut awaited = Vec::new(); - // Nothing more arrives after an abort, a seal, or once the live edge reached a - // declared end with no tail owed: until then, a group below the end may still arrive. - if self.abort.is_some() || self.is_complete() { + // Nothing more arrives after an abort, a seal, once the live edge reached a declared + // end with no tail owed, or while no subscription feeds the copy: until then, a group + // below the end may still arrive. + if self.nothing_arrives() { unfed.insert(hole); return (unfed, awaited); } @@ -1103,6 +1109,7 @@ mod tests { &state, (0, 0, None), None, + None, |span| [span].into_iter().collect(), &Delivered::default(), &drift, @@ -1143,6 +1150,7 @@ mod tests { &state, (0, 0, None), None, + None, |_| panic!("the edge has not moved"), &Delivered::default(), &drift, @@ -1168,6 +1176,7 @@ mod tests { &state, (0, 0, None), None, + None, |_| panic!("recreation extends no span"), &Delivered::default(), &drift, @@ -1239,6 +1248,7 @@ mod tests { &state, (0, 0, None), None, + None, |span| [span].into_iter().collect(), &Delivered::default(), &drift, @@ -1273,6 +1283,7 @@ mod tests { &state, (0, 0, None), None, + None, |_| panic!("the edge has not moved"), &Delivered::default(), &drift, @@ -1310,6 +1321,7 @@ mod tests { &state, (0, 0, None), None, + None, |span| [span].into_iter().collect(), &Delivered::default(), &drift, @@ -1326,6 +1338,7 @@ mod tests { &state, (0, 0, None), None, + None, |_| panic!("an unchanged probe rebuilds no span"), &Delivered::default(), &drift, @@ -1347,6 +1360,7 @@ mod tests { &state, (0, 0, None), None, + None, |_| panic!("a late arrival extends no span"), &Delivered::default(), &drift, @@ -1364,6 +1378,7 @@ mod tests { &state, (0, 0, None), None, + None, |_| panic!("a due gap extends no span"), &Delivered::default(), &drift, @@ -1406,6 +1421,7 @@ mod tests { &state, (0, 0, None), None, + None, full, &Delivered::default(), &drift, @@ -1427,6 +1443,7 @@ mod tests { &state, (0, 0, None), None, + None, |_| panic!("nothing deferred: the new grace waits for a change"), &Delivered::default(), &drift, @@ -1450,6 +1467,7 @@ mod tests { &state, (0, 0, None), None, + None, full, &Delivered::default(), &drift, diff --git a/rs/moq-net/tests/integration/fetch_only.rs b/rs/moq-net/tests/integration/fetch_only.rs index 7f5b0f6715b..89a735d04e3 100644 --- a/rs/moq-net/tests/integration/fetch_only.rs +++ b/rs/moq-net/tests/integration/fetch_only.rs @@ -204,3 +204,119 @@ async fn a_relay_fetches_without_subscribing_upstream() { } assert!(failures.is_empty(), "failed: {failures:?}"); } + +/// Subscribe to group 1 alone: history below the Largest Object a publisher of +/// [`publish_finished`] reports. Our moq-transport publishers answer only a one-group FETCH. +async fn subscribe_history(track: &moq_net::track::Consumer) -> moq_net::track::Subscriber { + let subscription = moq_net::track::Subscription::default() + .with_order(moq_net::track::Order::Ascending) + .with_range(1..2); + kio::clock::timeout(TIMEOUT, track.subscribe(subscription)) + .await + .expect("subscribe timed out") + .expect("subscribe") +} + +/// The next group `sub` hands out, read whole. +async fn next_frames(sub: &mut moq_net::track::Subscriber) -> Option>> { + let mut group = kio::clock::timeout(TIMEOUT, sub.recv_group()) + .await + .expect("recv timed out") + .expect("recv group")?; + let mut frames = Vec::new(); + while let Some(frame) = kio::clock::timeout(TIMEOUT, group.read_frame()) + .await + .expect("read timed out") + .expect("read frame") + { + frames.push(frame.payload.to_vec()); + } + Some(frames) +} + +async fn bounded_direct(version: &str) { + let version: Version = version.parse().unwrap(); + let publisher = produce_origin(1); + let (broadcast, subscribed) = publish_finished(&publisher); + + let client = produce_origin(2); + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(client.clone()); + let pair = connect_mock(options).await; + + let remote = resolve(&client).await; + let copy = remote.track("video").unwrap(); + copy.info().await.expect("TRACK_STATUS resolves the track"); + // A front's bounded read still parks past its end, as each copy's reader does, so this + // reads the group and stops there. + let mut sub = subscribe_history(©).await; + assert_eq!(next_frames(&mut sub).await, Some(frames(1)), "{version}"); + assert!( + !subscribed.load(Ordering::SeqCst), + "{version}: the bounded read subscribed" + ); + + drop((sub, copy, remote, pair, broadcast, client, publisher)); +} + +/// A copy the session holds no subscription for is unfed: a bounded read of the history +/// below the publisher's Largest Object fetches its holes at once, never subscribing. +#[kio::test] +async fn a_bounded_read_of_history_fetches_without_subscribing() { + let mut failures = Vec::new(); + for version in VERSIONS { + if kio::clock::sim::spawn(bounded_direct(version)).await.is_err() { + failures.push(*version); + } + } + assert!(failures.is_empty(), "failed: {failures:?}"); +} + +async fn bounded_relayed(version: &str) { + let version: Version = version.parse().unwrap(); + let publisher = produce_origin(1); + let (broadcast, subscribed) = publish_finished(&publisher); + + let relay = produce_origin(2); + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(relay.clone()); + let upstream = connect_mock(options).await; + + let client = produce_origin(3); + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(relay.consume()); + options.client_subscribe = Some(client.clone()); + let downstream = connect_mock(options).await; + + // A fetch leaves the relay holding a copy no subscription feeds. + let remote = resolve(&client).await; + assert_eq!(fetch(&remote, 2).await.expect("fetch"), frames(2), "{version}"); + + let copy = remote.track("video").unwrap(); + let mut sub = subscribe_history(©).await; + assert_eq!(next_frames(&mut sub).await, Some(frames(1)), "{version}"); + assert_eq!(next_frames(&mut sub).await, None, "{version}: the read ends"); + assert!( + !subscribed.load(Ordering::SeqCst), + "{version}: the relay subscribed upstream" + ); + + drop(( + sub, copy, remote, downstream, upstream, broadcast, client, relay, publisher, + )); +} + +/// A relay serves a bounded subscription to its fetch-only copy from the fills it asks +/// upstream, never subscribing upstream. +#[kio::test] +async fn a_relay_serves_a_bounded_read_of_history_without_subscribing_upstream() { + let mut failures = Vec::new(); + for version in VERSIONS { + if kio::clock::sim::spawn(bounded_relayed(version)).await.is_err() { + failures.push(*version); + } + } + assert!(failures.is_empty(), "failed: {failures:?}"); +} From 398c5cfbd8daaec3063fb53cad1d71e9360b35eb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 11:31:55 -0800 Subject: [PATCH 7/9] fix(net): settle a bounded read's stale write-offs once nothing can offer them again A group written off as stale that left the cache, or was judged at the bound, can never be offered again, so it moves into the reader's delivered set, which compacts below its floor. Only write-offs a wider budget may still reverse stay, so they never outnumber the stale skips the cache holds. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/skipped.rs | 7 +++ rs/moq-net/src/model/track.rs | 78 ++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/src/model/skipped.rs b/rs/moq-net/src/model/skipped.rs index 30b7634a898..ad369204a99 100644 --- a/rs/moq-net/src/model/skipped.rs +++ b/rs/moq-net/src/model/skipped.rs @@ -72,6 +72,13 @@ impl Skipped { self.0.as_ref().is_none_or(|skipped| skipped.runs.is_empty()) } + /// Whether `sequence` is skipped, so a wider budget may offer it again. + pub fn contains(&self, sequence: u64) -> bool { + self.0 + .as_ref() + .is_some_and(|skipped| skipped.runs.contains_key(&sequence)) + } + /// The lowest sequence skipped. pub fn lowest(&self) -> Option { let skipped = self.0.as_ref()?; diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index c37ef5af15a..e6778c5f1c8 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6692,6 +6692,28 @@ impl Cursor { self.filter.admitted_at = Some(at); } + /// Move each group written off as stale that nothing can offer again, since its group + /// left the cache or was judged at the bound, into `taken`, which compacts what it + /// accounts for below its floor. Only a group a wider budget may still offer stays + /// written off, so the write-offs never outnumber the stale skips the cache holds. + fn retire_written_off(&mut self, taken: &mut Delivered) { + // Most cursors never write a group off, and this runs on every hand-out. + if self.filter.written_off.is_empty() { + return; + } + let settled: Vec = self + .filter + .written_off + .iter() + .flat_map(|run| run.clone()) + .filter(|&sequence| !self.skipped.contains(sequence)) + .collect(); + for sequence in settled { + self.filter.written_off.remove(sequence..sequence + 1); + taken.insert(sequence); + } + } + /// The next cached group still owed in the subscription's order, newest first unless /// ascending, consuming the spans scanned on the way: only one the walk could have read, /// never fetched backfill or a withheld group. The group itself stays owed until @@ -7598,8 +7620,13 @@ impl Subscriber { match &mut self.inner { Inner::Plain(cursor) => loop { let peeked = match cursor.poll_peek(ordered, &self.delivered, waiter) { - Poll::Pending if !self.parks && cursor.is_exhausted(&self.delivered) => { - return Poll::Ready(Ok(None)); + Poll::Pending => { + // A read that only skipped since its last hand-out settles its write-offs too. + cursor.retire_written_off(&mut self.delivered); + if !self.parks && cursor.is_exhausted(&self.delivered) { + return Poll::Ready(Ok(None)); + } + return Poll::Pending; } polled => ready!(polled)?, }; @@ -7699,6 +7726,9 @@ impl Subscriber { let mut delivered = std::mem::take(&mut self.delivered); self.forget_past_budget(&mut delivered); self.delivered = delivered; + if let Inner::Plain(cursor) = &mut self.inner { + cursor.retire_written_off(&mut self.delivered); + } } /// Forget the gaps in `delivered` below each group past this subscriber's budget now: @@ -7716,6 +7746,15 @@ impl Subscriber { self.delivered.runs() } + /// How many runs of groups written off as stale this subscriber remembers. + #[cfg(test)] + fn written_off_runs(&self) -> usize { + match &self.inner { + Inner::Plain(cursor) => cursor.filter.written_off.iter().count(), + Inner::Resume(..) => 0, + } + } + /// Whether an unread cached group needs this subscription, without moving its cursor. pub(super) fn has_unread_group(&self, unread: &Unread<'_>) -> bool { let cursor = match &self.inner { @@ -22650,4 +22689,39 @@ mod test { let mut open = producer.subscribe(replay().with_range(1..)); assert_eq!(read_now(&mut open, false), None, "the cache is not the live edge"); } + + /// A long bounded read that writes a group off as stale between each it hands out + /// remembers a write-off only while its group is cached, since only then can a wider + /// budget offer it again, and still ends. + #[test] + fn a_bounded_read_retires_its_write_offs_as_it_advances() { + // Small enough that each round evicts the groups before it. + let (producer, _pool) = pooled_producer(1); + let mut reader = producer.subscribe( + Subscription::default() + .with_max_delay(Duration::from_millis(10)) + .with_order(Order::Ascending) + .with_range(0..16), + ); + for round in 0..8u64 { + // Group 2k+1 runs on past where group 2k could still present, so 2k is stale. + let at = 2_000 * round; + for (sequence, frames) in [(2 * round, vec![at]), (2 * round + 1, vec![at + 1_000, at + 1_100])] { + let mut group = producer.create_group(sequence.into()).unwrap(); + for at in frames { + group + .write_frame(Timestamp::from_millis(at).unwrap(), bytes::Bytes::from_static(b"f")) + .unwrap(); + } + group.finish().unwrap(); + } + assert_eq!(read_now(&mut reader, false), Some(Some(2 * round + 1)), "round {round}"); + assert!( + reader.written_off_runs() <= 1, + "round {round}: {}", + reader.written_off_runs() + ); + } + assert_eq!(read_now(&mut reader, false), Some(None)); + } } From 25c872bbbc75a0ade92a9cdfe01f8227803ed163 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 11:37:22 -0800 Subject: [PATCH 8/9] perf(net): settle stale write-offs by run, not by group Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/delivered.rs | 45 +++++++++++++++++++++++++++++++ rs/moq-net/src/model/skipped.rs | 11 ++++---- rs/moq-net/src/model/track.rs | 13 ++++----- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/rs/moq-net/src/model/delivered.rs b/rs/moq-net/src/model/delivered.rs index 42bc13ee3da..ec631e54c53 100644 --- a/rs/moq-net/src/model/delivered.rs +++ b/rs/moq-net/src/model/delivered.rs @@ -55,6 +55,33 @@ impl Delivered { true } + /// Record every sequence in `groups` as handed out, joining the runs it touches. + pub fn insert_run(&mut self, groups: std::ops::Range) { + let Some(mut last) = groups.end.checked_sub(1).filter(|_| !groups.is_empty()) else { + return; + }; + let mut first = groups.start; + // A run ending at or just below the first sequence joins it. + if let Some((&start, &end)) = self.runs.range(..=first).next_back() + && end.checked_add(1).is_none_or(|after| after >= first) + { + first = start; + last = last.max(end); + } + // So does every run starting inside it or just past it. + let joined: Vec = self + .runs + .range(first..=last.saturating_add(1)) + .map(|(&start, _)| start) + .collect(); + for start in joined { + if let Some(end) = self.runs.remove(&start) { + last = last.max(end); + } + } + self.runs.insert(first, last); + } + /// The parts of `groups` not handed out, in order. pub fn gaps(&self, groups: std::ops::Range) -> Vec> { let mut gaps = Vec::new(); @@ -160,6 +187,24 @@ mod test { assert!(!delivered.covers(3..5) && !delivered.covers(8..10)); } + #[test] + fn insert_run_joins_every_run_it_touches() { + let mut delivered = Delivered::default(); + for sequence in [2, 5, 6, 9, 14] { + delivered.insert(sequence); + } + delivered.insert_run(3..5); + assert_eq!(runs(&delivered), [(2, 6), (9, 9), (14, 14)], "adjacent on both sides"); + delivered.insert_run(8..13); + assert_eq!(runs(&delivered), [(2, 6), (8, 12), (14, 14)], "a run inside it"); + delivered.insert_run(4..15); + assert_eq!(runs(&delivered), [(2, 14)], "overlapping several"); + delivered.insert_run(7..7); + assert_eq!(runs(&delivered), [(2, 14)], "an empty run"); + delivered.insert_run(u64::MAX - 1..u64::MAX); + assert!(delivered.contains(u64::MAX - 1) && !delivered.contains(u64::MAX)); + } + #[test] fn the_extremes_are_sequences_too() { let mut delivered = Delivered::default(); diff --git a/rs/moq-net/src/model/skipped.rs b/rs/moq-net/src/model/skipped.rs index ad369204a99..b39073a1bd2 100644 --- a/rs/moq-net/src/model/skipped.rs +++ b/rs/moq-net/src/model/skipped.rs @@ -72,11 +72,12 @@ impl Skipped { self.0.as_ref().is_none_or(|skipped| skipped.runs.is_empty()) } - /// Whether `sequence` is skipped, so a wider budget may offer it again. - pub fn contains(&self, sequence: u64) -> bool { - self.0 - .as_ref() - .is_some_and(|skipped| skipped.runs.contains_key(&sequence)) + /// The parts of `groups` not skipped, which no wider budget offers again. + pub fn gaps(&self, groups: RangeInclusive) -> Vec> { + match &self.0 { + Some(skipped) => skipped.runs.gaps(&groups).collect(), + None => vec![groups], + } } /// The lowest sequence skipped. diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index d6925da5b00..64ab5710c86 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6701,16 +6701,17 @@ impl Cursor { if self.filter.written_off.is_empty() { return; } - let settled: Vec = self + // By run, so a long stale run costs one lookup rather than one per group. + let settled: Vec> = self .filter .written_off .iter() - .flat_map(|run| run.clone()) - .filter(|&sequence| !self.skipped.contains(sequence)) + .flat_map(|run| self.skipped.gaps(run.start..=run.end - 1)) + .map(|gap| *gap.start()..gap.end() + 1) .collect(); - for sequence in settled { - self.filter.written_off.remove(sequence..sequence + 1); - taken.insert(sequence); + for run in settled { + self.filter.written_off.remove(run.clone()); + taken.insert_run(run); } } From 8229e5c7fb23be8b1f70daec1c73fa8f5ff692a6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 12:27:25 -0800 Subject: [PATCH 9/9] fix(net): account stale skips under an open window, and wake on a settled refusal A group skipped as stale while a reader's window was open is now written off too, so a reader that narrows to a bounded window over it still ends, cached or evicted. `Misses::resolve` registers the deferred deadline every time: a refusal `ask` settled after the judge registered its deadline never woke the reader. MIGRATE.md says a routed track still waits past its ranges. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/MIGRATE.md | 1 + rs/moq-net/src/model/track.rs | 62 +++++++++++++++++++++++++++--- rs/moq-net/src/model/track/miss.rs | 9 ++--- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 43787da6395..7bc82a99c30 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -7,6 +7,7 @@ How to update your code for each breaking change, newest first. ### A bounded subscription ends once its ranges are read When every one of a subscription's `ranges` has an end, `track::Subscriber::recv_group` and `track::Ordered::next_group` return `None` once each group in them was handed out, dropped, skipped as stale, or given up as unfillable, instead of waiting for an update to widen the ranges. +This holds for a track read from its own producer's cache. A track read through an origin's routes, such as a remote broadcast's, still waits past its ranges for now, so a reader there stops once it has the groups it asked for rather than waiting for `None`. A reader that wants to widen its ranges later keeps one open, such as `Range::groups(n..)`, and narrows its reads with `with_range`, whose cap never ends a read. A group another reader's fetch or fill cached is now handed to every subscriber whose ranges hold it, not only fetches. A missing group the live feed may still deliver is waited for at most one second before it is asked of a fill or named dropped, when neither the readers' `max_delay` nor the track's max age bounds the wait. Before, a reader with `max_delay: None` on a track with no max age waited for it until the track ended. diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 64ab5710c86..14d0a2971f3 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6396,8 +6396,9 @@ struct Filter { /// budget, scanned in the cache in the subscription's order and handed out ahead of the /// walk: each cached group in them is visited once. owed: RangeInclusiveSet, - /// Groups of a bounded window skipped as stale, which account for it as handed out - /// would, until a wider budget owes them again. + /// Groups skipped as stale, which account for a bounded window as handed out would, + /// until a wider budget owes them again. Kept under an open window too, which a reader + /// may narrow to a bounded one. written_off: RangeSet, } @@ -7163,9 +7164,9 @@ impl Cursor { true => self.skipped.remove(consumer.sequence), false => self.skip(consumer.sequence, drift.budget), } - if self.filter.window.end().is_some() - && let Some(end) = consumer.sequence.checked_add(1) - { + // Whatever the window is now: narrowed to a bounded one later, it accounts for + // this group by it, even once the group left the cache. + if let Some(end) = consumer.sequence.checked_add(1) { self.filter.written_off.insert(consumer.sequence..end); } continue; @@ -22746,4 +22747,55 @@ mod test { } assert_eq!(read_now(&mut reader, false), Some(None)); } + + /// A read that skipped a group as stale while its window was open, then narrows to a + /// bounded window holding that group, still ends, whether the group is cached or evicted. + #[test] + fn a_read_narrowed_past_a_stale_skip_ends() { + for evict in [false, true] { + let (producer, _pool) = pooled_producer(if evict { 1 } else { 1 << 40 }); + let mut reader = producer.subscribe( + Subscription::default() + .with_max_delay(Duration::from_millis(10)) + .with_order(Order::Ascending) + .with_range(0..), + ); + for (sequence, frames) in [(0u64, &[0u64][..]), (1, &[1_000, 1_100])] { + let mut group = producer.create_group(sequence.into()).unwrap(); + for at in frames { + group + .write_frame(Timestamp::from_millis(*at).unwrap(), bytes::Bytes::from_static(b"f")) + .unwrap(); + } + group.finish().unwrap(); + } + assert_eq!(read_now(&mut reader, false), Some(Some(1)), "evict: {evict}"); + reader + .update( + Subscription::default() + .with_max_delay(Duration::from_millis(10)) + .with_order(Order::Ascending) + .with_range(0..2), + ) + .unwrap(); + assert_eq!(read_now(&mut reader, false), Some(None), "evict: {evict}"); + } + } + + /// A hole below the start that no fill handler can take is held as refused while the feed + /// may still deliver it, and the hold ending wakes the parked reader. + #[kio::test] + #[cfg(feature = "async")] + async fn an_unfillable_hole_below_the_start_wakes_its_reader() { + let (mut producer, _pool) = pooled_producer(1 << 40); + producer.start_at(10).unwrap(); + producer.create_group(10u64.into()).unwrap().finish().unwrap(); + let mut reader = producer.subscribe(Subscription::default().with_max_delay(None).with_range(5..6)); + let (flag, waiter) = Flag::waiter(); + assert!(reader.poll_group(false, &waiter).is_pending()); + flag.take(); + elapse(&producer, ledger::GRACE); + assert!(flag.take(), "the hold's end woke the reader"); + assert_eq!(read_now(&mut reader, false), Some(None)); + } } diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index d9a217a511b..a9d0390b62f 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -351,7 +351,6 @@ impl Misses { waiter: &kio::Waiter, ) -> RangeSet { let mut retry = RangeSet::new(); - let deadline = self.deferred.deadline(); // Only the joins holding something unanswered: one holding only filled groups until // they are taken has nothing to answer. let keys: BTreeSet = self @@ -436,10 +435,10 @@ impl Misses { // Judged again: the same join may have more to answer, or park the waiter now. } // A refusal held while the feed may still deliver its groups is judged again once - // the feed gives up on them, which nothing else wakes this reader for. - if let Some(at) = self.deferred.deadline() - && Some(at) != deadline - { + // the feed gives up on them, which nothing else wakes this reader for. Registered + // whether or not the deadline moved here: `ask` settles what no handler can take + // before this runs, and the judge registered only the deadline it saw. + if let Some(at) = self.deferred.deadline() { state.read().cache.watch_wall(at, waiter); } retry