From dbcffd322225197e5559e0e699141ab64f576f68 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 20 Oct 2026 17:55:49 -0700 Subject: [PATCH 1/5] quest: claim m1/subscribe-ranges/frame-bounds From 1df60848d97ac1bee89eec802bbde4d63c26af59 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 20 Oct 2026 18:27:15 -0700 Subject: [PATCH 2/5] fix(moq-net): a subscriber reads only the frames its ranges cover The cursor filtered whole groups, so a range starting or ending inside a cached group handed out every frame of it. Each handed-out group is now positioned to the frames its covering ranges name, on the arrival, re-offer, and ordered paths, and the datagram path judges object IDs against the same per-group bounds. Co-Authored-By: Claude Opus 5.5 --- quest/m1/subscribe-ranges/README.md | 1 - quest/m1/subscribe-ranges/frame-bounds.md | 37 ------ rs/moq-net/src/model/group.rs | 6 + rs/moq-net/src/model/subscription.rs | 5 +- rs/moq-net/src/model/subscription/window.rs | 68 ++++++++++ rs/moq-net/src/model/track.rs | 132 ++++++++++++++++++-- 6 files changed, 198 insertions(+), 51 deletions(-) delete mode 100644 quest/m1/subscribe-ranges/frame-bounds.md diff --git a/quest/m1/subscribe-ranges/README.md b/quest/m1/subscribe-ranges/README.md index b67786518ed..b5cdf7cec2c 100644 --- a/quest/m1/subscribe-ranges/README.md +++ b/quest/m1/subscribe-ranges/README.md @@ -91,7 +91,6 @@ every sequence delivered or dropped. - [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the lite-07 wire, Rust publisher and subscriber, and the draft - [moq-transport ranges](/quest/m1/subscribe-ranges/ietf.md) - range FETCH upstream per missing run, and non-blocking FETCH served downstream - [JS ranges](/quest/m1/subscribe-ranges/js.md) - `@moq/net` model and lite-07 parity -- [Frame bounds](/quest/m1/subscribe-ranges/frame-bounds.md) - a local subscriber reads only the frames its ranges cover - [Time anchors](/quest/m1/subscribe-ranges/time-anchors.md) - a range start or end may name a presentation time ## Related diff --git a/quest/m1/subscribe-ranges/frame-bounds.md b/quest/m1/subscribe-ranges/frame-bounds.md deleted file mode 100644 index 23c940af714..00000000000 --- a/quest/m1/subscribe-ranges/frame-bounds.md +++ /dev/null @@ -1,37 +0,0 @@ -# [M] A local subscriber reads only its frame bounds - -## Goal - -A `track::Subscriber` whose ranges start or end partway through a group reads only the -frames they cover: a start at group 4 frame 5 hands out group 4 from frame 5, and an -end mid-group stops there, whatever another subscriber widened the aggregate to. Today -the model filters whole groups and hands out every cached frame, so local readers -(moq-ffi, moq-mux, in-process apps) see frames they never asked for. - -## Plan - -Found by Codex on #5929 and reproduced on `main` (824179a3a) as well as on that PR: a -subscription starting at frame 5 of group 0 is handed the group from frame 0. #5929's -`Window` collapses each range to group sequences, but the leak predates it: `main`'s -cursor never positioned a delivered group either. Only the datagram path checks the -start frame (`mid_group` in `Cursor::poll_recv_datagram`). - -The wire is not affected: the lite publisher positions each served group per -subscription (`position_group` in `rs/moq-net/src/lite/publisher.rs`, and the same -shape in `js/net/src/lite/publisher.ts`). - -- Keep each range's absolute frame bounds in the `Window` beside its group runs. -- Position every group the cursor hands out (`group::Consumer::start_at`/`end_at`), on - the arrival, ordered, and re-offer paths, using the bounds of the range that covers it. - A group whose head is gone and that was asked for from frame 0 stays a miss, as - `position_group` does today. -- Then let the lite publisher rely on the model's positioning instead of its own, if - that is simpler. -- Check `js/net`'s `track` subscriber for the same shape and mirror the fix. - -Regression test: a start at frame 5 and an end at frame 3 of a cached group, each read -through `recv_group` and `ordered()`. - -## Related - -- [Dynamic range requests](/quest/m1/subscribe-ranges/dynamic.md) - the per-subscriber ranges this extends to frames diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 6498967b55d..da36b85f6c9 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -2017,6 +2017,12 @@ impl Consumer { self.cursor.state.read().wire().cloned() } + /// Whether the group was relayed from moq-transport, whose Object IDs may not be its frame + /// ordinals. + pub(super) fn relayed(&self) -> bool { + self.cursor.state.read().wire().is_some() + } + /// The index of the first frame the group holds (see [`Producer::start_at`]). // private: passthrough pub(crate) fn first_frame(&self) -> u64 { diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index 81f53a38461..683d15ad243 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -73,8 +73,8 @@ pub struct Subscription { /// for nothing. /// /// A request to the publisher, aggregated across every live subscriber as the union of - /// their ranges, and this subscriber's read filter: it reads only the groups its own - /// ranges cover, whatever another subscriber widens the union to. + /// their ranges, and this subscriber's read filter: it reads only the groups and frames + /// its own ranges cover, whatever another subscriber widens the union to. /// [`crate::track::Subscriber::set_groups`] narrows the reads further. See [Local cursor /// vs wire preference](crate::track::Subscriber#local-cursor-vs-wire-preference). pub ranges: Vec, @@ -287,6 +287,7 @@ impl Subscription { /// from the latest group (or none is asked for). /// /// The form a wire without ranges carries: a floor below which nothing is requested. + #[cfg(any(feature = "async", test))] // pending: /quest/m1/public-model/serve.md pub(crate) fn start(&self) -> Option { let mut floor: Option = None; diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 8fd46645bf8..2e31495a08a 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -41,6 +41,17 @@ struct Span { floored: bool, } +impl Span { + /// Whether group `sequence` is in this span. + fn covers(&self, sequence: u64) -> bool { + sequence >= self.start + && match self.end { + End::Before(end) => sequence < end, + End::Open => true, + } + } +} + #[derive(Clone, Copy, Debug)] enum End { /// Before this group. @@ -138,6 +149,36 @@ impl Window { all.into_iter().chain(runs.into_iter().flatten()) } + /// The frames of group `sequence` in range: from a start frame up to an exclusive end, + /// `None` through the group's end. + /// + /// Only an absolute anchor names a frame, so a group a range starts or ends inside reads + /// part of itself and every other group reads whole. Ranges that cover a group + /// differently read the hull of their frames. Everything while a relative anchor waits. + pub fn frames(&self, sequence: u64) -> (u64, Option) { + if self.pending { + return (0, None); + } + let mut hull = None; + for span in self.spans.iter().filter(|span| span.covers(sequence)) { + let range = &self.ranges[span.index]; + let start = match range.start { + Anchor::Absolute(start) if start.group == sequence => start.frame, + _ => 0, + }; + // A head-of-group end covers nothing of its group, so it never gets here. + let end = match range.end { + Some(Anchor::Absolute(end)) if end.group == sequence => Some(end.frame), + _ => None, + }; + hull = Some(match hull { + None => (start, end), + Some((low, high)) => (start.min(low), end.zip(high).map(|(end, high)| end.max(high))), + }); + } + hull.unwrap_or((0, None)) + } + /// The first run of `range` that is in range. pub fn clip(&self, range: ops::Range) -> Option> { let last = range.end.checked_sub(1).filter(|last| *last >= range.start)?; @@ -253,6 +294,33 @@ mod tests { assert_eq!((window.floor(), window.end()), (Some(0), Some(0))); } + /// Only a group an absolute anchor falls inside reads part of itself, and overlapping + /// ranges read the hull of their frames there. + #[test] + fn frames_bound_only_the_groups_an_anchor_falls_inside() { + let at = |group, frame| Anchor::Absolute(Position { group, frame }); + let window = Window::new(&[range(at(4, 5), at(6, 3))], None); + assert_eq!(window.frames(4), (5, None)); + assert_eq!(window.frames(5), (0, None)); + assert_eq!(window.frames(6), (0, Some(3))); + + // Within one group, both ends bound it. + let window = Window::new(&[range(at(4, 2), at(4, 7))], None); + assert_eq!(window.frames(4), (2, Some(7))); + + // A second range reaching further either way widens the group to cover both. + let window = Window::new(&[range(at(4, 5), at(4, 7)), range(at(4, 2), at(4, 6))], None); + assert_eq!(window.frames(4), (2, Some(7))); + let window = Window::new(&[range(at(4, 5), None), range(at(2, 0), at(4, 3))], None); + assert_eq!(window.frames(4), (0, None)); + + // Relative anchors cover whole groups, and a pending one bounds nothing yet. + let window = Window::new(&[range(Anchor::Relative(1), at(9, 2))], Some(9)); + assert_eq!((window.frames(8), window.frames(9)), ((0, None), (0, Some(2)))); + let window = Window::new(&[range(at(4, 5), None), Range::live()], None); + assert_eq!(window.frames(4), (0, None)); + } + /// Relative anchors count back from the latest group, both ends whole groups, and wait /// for a first group when there is none. #[test] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 5a7d2f66c1b..9cf62d433b1 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5417,7 +5417,21 @@ impl Cursor { } } - fn with_expiry(&self, group: group::Consumer) -> group::Consumer { + /// Hand `group` out: bounded to the frames this cursor's ranges cover in it, and expiring + /// against its budget. + /// + /// Positioned without clamping, so a group missing the head asked for reads it as + /// [`Error::Lagged`], which a route copy recovers from another. A group relayed from + /// moq-transport is handed out whole, since its ranges may name Object IDs rather than + /// frame ordinals, so its reader filters by ID. + fn deliver(&self, mut group: group::Consumer) -> group::Consumer { + if !group.relayed() { + let (start, end) = self.filter.window.frames(group.sequence); + group.skip_to(start); + if let Some(end) = end { + group.end_at(Bound::Excluded(end)); + } + } let sequence = group.sequence; group.with_expiry(Arc::new(GroupExpiry { state: self.state.weak(), @@ -5431,7 +5445,7 @@ impl Cursor { if let Some(peeked) = self.peeked.take().filter(|peeked| take && !peeked.ordered) { self.advance_arrival(peeked.group.sequence, peeked.index); peeked.group.cache_refresh(); - return Poll::Ready(Ok(Some(self.with_expiry(peeked.group)))); + return Poll::Ready(Ok(Some(self.deliver(peeked.group)))); } self.sync(); self.admit(); @@ -5521,7 +5535,7 @@ impl Cursor { continue; } return Poll::Ready(Ok(Some(if take { - self.with_expiry(consumer) + self.deliver(consumer) } else { self.peeked = Some(Box::new(Peeked { group: consumer.clone(), @@ -5544,7 +5558,8 @@ impl Cursor { /// The next datagram inside this subscriber's range, dropping the ones outside it. /// - /// The range is the one groups get: the floor, the cap, and the start Object ID. + /// The range is the one groups get: the floor, the cap, and the frames its group's + /// ranges cover, matched by Object ID. /// Plain datagrams have Object ID 0. A datagram is judged when the cursor reaches /// it, against the range in force then, and one outside it is gone for good: unlike a /// group past the cap, nothing holds it for a later raise. @@ -5559,11 +5574,10 @@ impl Cursor { self.sync(); let sequence = datagram.sequence; - let mid_group = self.subscription.read().start().is_some_and(|start| { - start.group == sequence - && start.frame > datagram.wire.as_ref().and_then(|wire| wire.object_id).unwrap_or(0) - }); - if sequence >= self.floor() && self.admits(sequence) && !mid_group { + let (start, end) = self.filter.window.frames(sequence); + let object = datagram.wire.as_ref().and_then(|wire| wire.object_id).unwrap_or(0); + let framed = object >= start && end.is_none_or(|end| object < end); + if sequence >= self.floor() && self.admits(sequence) && framed { return Poll::Ready(Ok(Some(datagram))); } } @@ -5655,7 +5669,7 @@ impl Cursor { // The delivery commits everything the seek stepped over to reach this group. self.commit_seek_stale(self.next_sequence.unwrap_or(u64::MAX)); group.cache_refresh(); - self.with_expiry(group) + self.deliver(group) } /// Count the sequence path's convictions below `committed` into [`Self::stale`], @@ -5825,6 +5839,10 @@ impl Subscriber { /// that the cache still holds, lowest sequence first. Once the track finishes, a /// reader with nothing left in range ends. /// + /// 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(Err(e))` when the track has been aborted, or @@ -7119,6 +7137,90 @@ mod test { group } + /// Every group a reader hands out with the frame indices it reads, until one would block. + fn read_groups( + mut next: impl FnMut(&kio::Waiter) -> Poll>>, + ) -> Vec<(u64, Vec)> { + let noop = kio::Waiter::noop(); + let mut groups = Vec::new(); + while let Poll::Ready(group) = next(&noop) { + let Some(mut group) = group.unwrap() else { break }; + let mut frames = Vec::new(); + loop { + let index = group.index(); + match group.poll_read_frame(&noop) { + Poll::Ready(Ok(Some(_))) => frames.push(index), + Poll::Ready(Ok(None)) => break, + res => panic!("group {} read {res:?}", group.sequence), + } + } + groups.push((group.sequence, frames)); + } + groups + } + + /// A range starting and ending inside cached groups reads only the frames it covers, + /// in arrival and sequence order alike, however wide another subscriber made the + /// aggregate. + #[test] + fn a_range_reads_only_its_frames() { + let producer = track_producer("test", Info::default()); + let _groups: Vec<_> = (3..8) + .map(|sequence| { + let mut group = framed(&producer, sequence, 8); + group.finish().unwrap(); + group + }) + .collect(); + let _wide = producer.subscribe(replay()); + let range = Range { + start: Anchor::Absolute(Position { group: 4, frame: 5 }), + end: Some(Anchor::Absolute(Position { group: 6, frame: 3 })), + }; + let bounded = || producer.subscribe(replay().with_ranges([range])); + + let want = vec![(4, vec![5, 6, 7]), (5, (0..8).collect()), (6, vec![0, 1, 2])]; + let mut arrival = bounded(); + assert_eq!(read_groups(|waiter| arrival.poll_recv_group(waiter)), want); + let mut ordered = bounded().ordered(); + assert_eq!(read_groups(|waiter| ordered.poll_next_group(waiter)), want); + } + + /// A group missing the frames a range asks for is never served short: reading them is + /// [`Error::Lagged`], which a route copy recovers. One holding them from where the range + /// starts is served from there. + #[test] + fn a_group_missing_its_asked_head_lags() { + let producer = track_producer("test", Info::default()); + let mut headless = producer.create_group(group::Info { sequence: 4 }).unwrap(); + headless.start_at(2).unwrap(); + for _ in 0..3 { + headless.write_frame(None, &b"x"[..]).unwrap(); + } + headless.finish().unwrap(); + + let noop = kio::Waiter::noop(); + for frame in [0, 1] { + let start = Anchor::Absolute(Position { group: 4, frame }); + let mut sub = producer.subscribe(replay().with_ranges([Range { start, end: None }])); + let Poll::Ready(Ok(Some(mut group))) = sub.poll_recv_group(&noop) else { + panic!("from frame {frame}: no group"); + }; + assert_eq!(group.index(), frame); + assert!( + matches!(group.poll_read_frame(&noop), Poll::Ready(Err(Error::Lagged))), + "from frame {frame}" + ); + } + + let start = Anchor::Absolute(Position { group: 4, frame: 2 }); + let mut sub = producer.subscribe(replay().with_ranges([Range { start, end: None }])); + assert_eq!( + read_groups(|waiter| sub.poll_recv_group(waiter)), + vec![(4, vec![2, 3, 4])] + ); + } + /// The largest reports only while it is within the bounds: a cap below it or a floor /// above it reports nothing, rather than a cached group standing in for it. #[test] @@ -7801,12 +7903,19 @@ mod test { } /// A start past the start group's first frame excludes that group's datagram, its only - /// frame; a start at frame 0 keeps it. + /// frame, even behind an earlier range; a start at frame 0 keeps it. #[test] #[cfg(feature = "async")] fn a_mid_group_start_drops_the_start_groups_datagram() { let mut producer = track_producer("test", Info::timed()); let mut mid = producer.subscribe(Subscription::default().with_start(Position { group: 5, frame: 1 })); + let mut split = producer.subscribe(Subscription::default().with_ranges([ + Range::groups(2..4), + Range { + start: Anchor::Absolute(Position { group: 5, frame: 1 }), + end: None, + }, + ])); let mut whole = producer.subscribe(Subscription::default().with_start(Position::group(5))); for sequence in [5, 6] { producer @@ -7815,6 +7924,7 @@ mod test { } assert_eq!(recv_datagram(&mut mid).sequence, 6); + assert_eq!(recv_datagram(&mut split).sequence, 6); assert_eq!(recv_datagram(&mut whole).sequence, 5); assert_eq!(recv_datagram(&mut whole).sequence, 6); } From 33c5e0062dbf20b32d635e9a67c9f36ea5f4631e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 20 Oct 2026 19:43:26 -0700 Subject: [PATCH 3/5] style(moq-net): format the time-end frames case Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/subscription/window.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 4aa29022496..7667e6df3f0 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -791,12 +791,13 @@ mod tests { assert_eq!(window.frames(4), (0, None)); // A time end covers whole groups, through its cap while it waits to settle. - let groups = Groups::new([ - Groups::started(4, 4000), - Groups::started(5, 5000), - (6, Held::Pending), - ]); - let window = Window::new(&[range(at(6, 2), None), range(time(4500), time(5000))], Some(6), &groups).unwrap(); + let groups = Groups::new([Groups::started(4, 4000), Groups::started(5, 5000), (6, Held::Pending)]); + let window = Window::new( + &[range(at(6, 2), None), range(time(4500), time(5000))], + Some(6), + &groups, + ) + .unwrap(); assert!(!window.is_settled()); assert_eq!(window.frames(6), (0, None)); } From ea6eb50a2aba5771af934ca8a94b55449331d622 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 20 Oct 2026 19:56:37 -0700 Subject: [PATCH 4/5] fix(moq-net): judge a datagram against one range at a time Sequence, timestamp, and Object ID were checked against the window as a whole, so two ranges could each pass one condition and admit a datagram neither covers. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/subscription/window.rs | 91 +++++++++++++++------ rs/moq-net/src/model/track.rs | 8 +- 2 files changed, 67 insertions(+), 32 deletions(-) diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 7667e6df3f0..0867c7e561e 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -389,16 +389,25 @@ impl Window { } /// Whether a datagram is in range, judged on its own and never through a time end's - /// settling: its `timestamp` against each range's times, and its `sequence` against the - /// range's absolute groups and the relative ones resolved so far. - pub fn admits_datagram(&self, sequence: u64, timestamp: Option) -> bool { + /// settling: its `timestamp` against each range's times, its `sequence` against the + /// range's absolute groups and the relative ones resolved so far, and its `object` ID + /// against the frames that same range covers in its group. + pub fn admits_datagram(&self, sequence: u64, timestamp: Option, object: u64) -> bool { + let framed = |range: &Range| { + let (start, end) = frames(range, sequence); + object >= start && end.is_none_or(|end| object < end) + }; let Some(timestamp) = timestamp else { - return self.contains(sequence); + return self.pending + || self + .spans + .iter() + .any(|span| span.covers(sequence) && framed(&self.ranges[span.index])); }; self.ranges .iter() .enumerate() - .filter(|(_, range)| !range.is_empty()) + .filter(|(_, range)| !range.is_empty() && framed(range)) .any(|(index, range)| { // A relative anchor still waiting on a latest group bounds nothing yet. let span = self.spans.iter().find(|span| span.index == index); @@ -481,23 +490,15 @@ impl Window { /// /// Only an absolute anchor names a frame, so a group a range starts or ends inside reads /// part of itself and every other group reads whole. Ranges that cover a group - /// differently read the hull of their frames. Everything while a relative anchor waits. + /// differently read the hull of their frames, since a group is handed out once, as one + /// run of frames. Everything while a relative anchor waits. pub fn frames(&self, sequence: u64) -> (u64, Option) { if self.pending { return (0, None); } let mut hull = None; for span in self.spans.iter().filter(|span| span.covers(sequence)) { - let range = &self.ranges[span.index]; - let start = match range.start { - Anchor::Absolute(start) if start.group == sequence => start.frame, - _ => 0, - }; - // A head-of-group end covers nothing of its group, so it never gets here. - let end = match range.end { - Some(Anchor::Absolute(end)) if end.group == sequence => Some(end.frame), - _ => None, - }; + let (start, end) = frames(&self.ranges[span.index], sequence); hull = Some(match hull { None => (start, end), Some((low, high)) => (start.min(low), end.zip(high).map(|(end, high)| end.max(high))), @@ -572,6 +573,21 @@ impl Window { } } +/// The frames of group `sequence` that `range` names, if it covers that group: from a start +/// frame up to an exclusive end, `None` through the group's end. Only an absolute anchor +/// names a frame; a head-of-group end covers nothing of its group, so it never gets here. +fn frames(range: &Range, sequence: u64) -> (u64, Option) { + let start = match range.start { + Anchor::Absolute(start) if start.group == sequence => start.frame, + _ => 0, + }; + let end = match range.end { + Some(Anchor::Absolute(end)) if end.group == sequence => Some(end.frame), + _ => None, + }; + (start, end) +} + /// The first group a range starts at, and whether it names that group rather than /// reaching back to the oldest one. `None` while it waits on a latest group. fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Option<(u64, bool)> { @@ -975,8 +991,8 @@ mod tests { &Groups::default(), ) .unwrap(); - assert!(window.admits_datagram(9, Some(at(3500)))); - assert!(!window.admits_datagram(9, Some(at(3600)))); + assert!(window.admits_datagram(9, Some(at(3500)), 0)); + assert!(!window.admits_datagram(9, Some(at(3600)), 0)); } /// Groups whose `racing` one starts past the end after the first time it is asked @@ -1080,18 +1096,18 @@ mod tests { }; let window = Window::new(&[range(time(0), time(5000))], None, &groups).unwrap(); assert_eq!(window.resolved(), vec![Some(Resolved { start: 0, end: Some(2) })]); - assert!(window.admits_datagram(1, Some(at(4000)))); - assert!(!window.admits_datagram(1, Some(at(7000)))); + assert!(window.admits_datagram(1, Some(at(4000)), 0)); + assert!(!window.admits_datagram(1, Some(at(7000)), 0)); } #[test] fn a_waiting_window_judges_a_datagram_against_both_times() { let window = Window::new(&[range(time(3000), time(3500))], None, &Groups::default()).unwrap(); assert!(window.is_pending(), "no group has started"); - assert!(!window.admits_datagram(0, Some(at(2900)))); - assert!(window.admits_datagram(1, Some(at(3000)))); - assert!(window.admits_datagram(2, Some(at(3500)))); - assert!(!window.admits_datagram(3, Some(at(3600)))); + assert!(!window.admits_datagram(0, Some(at(2900)), 0)); + assert!(window.admits_datagram(1, Some(at(3000)), 0)); + assert!(window.admits_datagram(2, Some(at(3500)), 0)); + assert!(!window.admits_datagram(3, Some(at(3600)), 0)); } #[test] @@ -1103,9 +1119,30 @@ mod tests { ) .unwrap(); assert!(window.is_pending(), "no group has started"); - assert!(window.admits_datagram(9, Some(at(3500)))); - assert!(!window.admits_datagram(10, Some(at(3500)))); - assert!(!window.admits_datagram(9, Some(at(2900)))); + assert!(window.admits_datagram(9, Some(at(3500)), 0)); + assert!(!window.admits_datagram(10, Some(at(3500)), 0)); + assert!(!window.admits_datagram(9, Some(at(2900)), 0)); + } + + /// A datagram is in range only when one range admits its sequence, time, and Object ID + /// together, not when each passes a different range. + #[test] + fn a_datagram_is_judged_against_one_range_at_a_time() { + let pos = |group, frame| Anchor::Absolute(Position { group, frame }); + let groups = Groups::new([Groups::started(5, 5000)]); + // The time range covers group 5 whole, but only from 5500 on. + let window = Window::new(&[range(pos(5, 1), None), range(time(5500), None)], Some(5), &groups).unwrap(); + assert!(!window.admits_datagram(5, Some(at(5000)), 0)); + assert!(window.admits_datagram(5, Some(at(5000)), 1)); + assert!(window.admits_datagram(5, Some(at(5500)), 0)); + + // Untimed, two ranges splitting a group admit only the Object IDs each names. + let window = Window::new(&[range(pos(4, 1), pos(4, 2)), range(pos(4, 5), None)], None, &Untimed).unwrap(); + assert_eq!(window.frames(4), (1, None), "a group stream reads the hull"); + let admitted: Vec = (0..7) + .filter(|object| window.admits_datagram(4, None, *object)) + .collect(); + assert_eq!(admitted, vec![1, 5, 6]); } #[test] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 682b783ccf9..f0f7b6bea9c 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5777,12 +5777,10 @@ impl Cursor { let sequence = datagram.sequence; // A datagram is a whole group, so its own timestamp places it against a time end. - let admits = super::subscription::before_end(sequence, self.end_sequence) - && self.filter.window.admits_datagram(sequence, datagram.timestamp); - let (start, end) = self.filter.window.frames(sequence); let object = datagram.wire.as_ref().and_then(|wire| wire.object_id).unwrap_or(0); - let framed = object >= start && end.is_none_or(|end| object < end); - if sequence >= self.floor() && admits && framed { + let admits = super::subscription::before_end(sequence, self.end_sequence) + && self.filter.window.admits_datagram(sequence, datagram.timestamp, object); + if sequence >= self.floor() && admits { return Poll::Ready(Ok(Some(datagram))); } } From 64e789fb0032402014875762248a071810eaa513 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 20 Oct 2026 20:27:45 -0700 Subject: [PATCH 5/5] fix(moq-net): bound a peeked group by the ranges at handout A Control update between a peek and the take left the cursor's window stale, so the reserved group was positioned by the old ranges. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index f0f7b6bea9c..c380adf683d 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5632,6 +5632,9 @@ impl Cursor { fn poll_recv_group(&mut self, take: bool, waiter: &kio::Waiter) -> Poll>> { if let Some(peeked) = self.peeked.take().filter(|peeked| take && !peeked.ordered) { + // The reserved group is still the one handed out, but bounded by the ranges in + // force now, which a `Control` may have replaced since the peek. + self.sync(); self.advance_arrival(peeked.group.sequence, peeked.index); peeked.group.cache_refresh(); return Poll::Ready(Ok(Some(self.deliver(peeked.group)))); @@ -5790,6 +5793,8 @@ impl Cursor { /// drift budget has convicted on the way. fn poll_next_group(&mut self, take: bool, waiter: &kio::Waiter) -> Poll>> { if let Some(peeked) = self.peeked.take().filter(|peeked| take && peeked.ordered) { + // Bounded by the ranges in force now, as on the arrival path. + self.sync(); return Poll::Ready(Ok(Some(self.advance_sequence(peeked.group)))); } let Some(next) = self.next_sequence else { @@ -7477,6 +7482,31 @@ mod test { ); } + /// A range replaced between a peek and the take re-bounds the reserved group: it is + /// still the group handed out, positioned by the ranges in force when it is taken. + #[test] + fn a_peeked_group_is_bounded_by_the_ranges_at_handout() { + for ordered in [false, true] { + let producer = track_producer("test", Info::default()); + framed(&producer, 4, 8).finish().unwrap(); + let mut subscriber = producer.subscribe(replay()); + let waiter = kio::Waiter::noop(); + assert_eq!( + subscriber.poll_peek_group(ordered, &waiter).map(Result::unwrap), + Poll::Ready(Some(4)) + ); + let start = Anchor::Absolute(Position { group: 4, frame: 5 }); + subscriber + .control() + .update(replay().with_ranges([Range { start, end: None }])) + .unwrap(); + let Poll::Ready(Ok(Some(group))) = subscriber.poll_group(ordered, &waiter) else { + panic!("ordered={ordered}: the reserved group must be returned"); + }; + assert_eq!((group.sequence, group.index()), (4, 5), "ordered={ordered}"); + } + } + /// The largest reports only while it is within the bounds: a cap below it or a floor /// above it reports nothing, rather than a cached group standing in for it. #[test]