From a45014161b9f420b69a871f51ef51920ce1e3785 Mon Sep 17 00:00:00 2001 From: Michael Stuurman Date: Fri, 6 Nov 2026 18:05:35 +0100 Subject: [PATCH 1/8] fix(net): fail an idle copy whose route answers behind its cache Co-Authored-By: Claude Opus 5.5 --- quest/m1/README.md | 1 - quest/m1/answer-behind-cache.md | 64 ------ rs/moq-net/src/ietf/subscriber.rs | 9 +- rs/moq-net/src/lite/subscriber.rs | 8 +- rs/moq-net/src/model/track.rs | 201 +++++++++++++----- .../tests/integration/answer_behind_cache.rs | 108 ++++++++++ rs/moq-net/tests/integration/main.rs | 1 + 7 files changed, 269 insertions(+), 123 deletions(-) delete mode 100644 quest/m1/answer-behind-cache.md create mode 100644 rs/moq-net/tests/integration/answer_behind_cache.rs diff --git a/quest/m1/README.md b/quest/m1/README.md index e96abb676a5..268d2bd2aad 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -39,7 +39,6 @@ the same reason, gates the cut through the - [Routing guide](/quest/m1/routing-guide.md) - concepts and practical outbound federation setup - [A reader on a failed copy continues on the held re-offer](/quest/m1/copy-recover.md) - a live reader keeps a group whose copy fails by continuing on the same track's re-offer at the same frame - [Group errors are cached by kind](/quest/m1/group-error-cache.md) - permanent group errors bind every reader, retryable ones are kept only for a bounded time, and a live SUBSCRIBE_DROP or FETCH answered `Old` no longer caches it for good -- [A route answer behind its idle copy's cache fails the copy](/quest/m1/answer-behind-cache.md) - a resuming copy whose route answers behind its cache fails loud instead of refusing the feed's groups as duplicates, so readers resubscribe onto a fresh copy - [The overcommitted receive-window cells run again](/quest/m1/receive-windows-evicted.md) - `benchmark_receive_windows` measures its 256-stream and 4 MiB-frame cells again, reporting delivered frames and evictions instead of stalling - [Datagram tracks](/quest/m1/datagram-tracks/README.md) - a track is datagrams or streams; datagram tracks have their own request and sequence, no ranges, no cache, and map to object ID 0 on moq-transport - [Stream credit by priority](/quest/m1/stream-credit/README.md) - every stream opens through one priority entry point, and a higher-priority group takes over or resets a lower one when credit runs out diff --git a/quest/m1/answer-behind-cache.md b/quest/m1/answer-behind-cache.md deleted file mode 100644 index d10e95cb658..00000000000 --- a/quest/m1/answer-behind-cache.md +++ /dev/null @@ -1,64 +0,0 @@ -# [S] A route answer behind its idle copy's cache fails the copy - -## Goal - -When an idle track copy resumes and its route's answer (lite SUBSCRIBE_START -or moq-transport SUBSCRIBE_OK) is behind what the copy already cached from -that path, the copy fails its track instead of guessing. Behind means no -Largest while the copy holds shown groups, or a Largest position before the -newest position the copy holds. Readers get the error, the lingering copy goes -away, and the next subscribe builds a fresh copy that accepts the feed from -group 0. Today the no-Largest case hides the cache behind a live floor while -`claim_sequence` still refuses the feed's re-sent groups as `Duplicate`, so -live readers stall until the feed passes the old newest group, forever for a -one-group catalog. - -Non-goals: detecting a genuine publisher restart on the same path. Restarted -numbering needs a new broadcast name -([moq-dev/moq#4741](https://github.com/moq-dev/moq/pull/4741)). Rust only: -`js/net` has no idle-copy reuse. - -## Plan - -Planned 2026-11-06 (maintainer, /quest-import of -[moq-dev/moq#7973](https://github.com/moq-dev/moq/issues/7973)). Reproduced on -main with the issue's test: the first re-sent group fails with -`Err(Duplicate)`. `release` predates the disowning arm, so nothing to -backport. - -Decisions: - -- Direction: ✅ fix in moq-net / won't fix / fix in moqxr only. -- Shape: max edge (delete the disowning arm and trust the cache) / ✅ error - on any disagreement / error only on a missing Largest. The maintainer - first picked the max edge, then chose the error: a cache that disagrees - with its route is likely the wrong track, and failing loud makes a - non-compliant upstream (moqxr omitting Largest while it has content) fix - it rather than the relay serving stale groups. The max edge would also be - inconsistent, since reusing a broadcast does not always answer below the - cache. -- Milestone: ✅ m1 / m0. Additive, no public API or wire change. - -Recommendations: - -- `answered` (`rs/moq-net/src/model/track.rs`) judges the disagreement - against the same shown-groups view it uses now, which already discounts - the group the idling cancel reset. The `(None, Some(cached))` live-floor - arm goes away; keep the gap arm (a start past the cache), which is not a - disagreement. -- Fail with the existing `Error::MalformedTrack`: it maps to the - stream-scoped `MALFORMED_TRACK` code, so the upstream session survives. - `Duplicate` would map to a session-level protocol violation. -- Make `set_live`/`set_live_ahead` report the failure to their callers in - `lite/subscriber.rs` and `ietf/subscriber.rs` rather than aborting inside - the model, if that keeps the copy's teardown in one place. -- Tests on the model: both disagreements fail readers; the issue's restart - sequence succeeds on the fresh copy; - `a_relative_start_on_an_idle_copy_skips_a_cache_the_route_disowns` is - replaced. One session-level test that a no-Largest SUBSCRIBE_OK over a - lingering copy fails the downstream subscription and a resubscribe reads - group 0. - -## Closes - -- [#7973](https://github.com/moq-dev/moq/issues/7973) - a lingering track refuses a restarted publisher's groups as duplicates diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 71a51e3b941..4c0f10ad687 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -4487,15 +4487,20 @@ impl SubscribeOnce { // An absolute feed starts where it asked, whatever the answer's Largest; a // live join's starts there. let from = live.sent_bounds.0.filter(|_| !self.live); - match largest { + let answered = match largest { // A feed ending before the answer's Largest never shows it, so the // answer alone makes the copy current. A live join's feed is open. Some(largest) if !self.live && live.sent_bounds.1.is_some_and(|end| end <= largest.group) => { track.set_live_ahead(largest, from) } largest => track.set_live(largest, from), - } + }; live.resuming = None; + // An answer behind the copy's cache fails it. + if let Err(err) = answered { + tracing::warn!(%err, "subscribe ok behind the cache"); + return Poll::Ready(ServeEnd::Invalid(err)); + } } // The last subscriber left: the upstream subscription goes with it, as on // lite, and the copy lingers for fetches and a returning subscriber. diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 0ac030e46ae..a8e6f93f535 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -198,7 +198,7 @@ impl TrackEntry { self.start = self.start.or(start); self.first = self.first.or(first); if let (Some(start), Some(first), Some(copy)) = (self.start, self.first, &mut self.copy) { - copy.producer.set_live(Some(first), Some(start)); + copy.producer.set_live_from(first, Some(start)); } } } @@ -382,11 +382,11 @@ impl TrackEntry { frame: hdr.frame_start, }; if !version.has_track_stream() { - producer.set_live(Some(first), None); + producer.set_live_from(first, None); } else if !version.has_largest() { self.first = self.first.or(Some(first)); if let (Some(start), Some(first)) = (self.start, self.first) { - producer.set_live(Some(first), Some(start)); + producer.set_live_from(first, Some(start)); } } } @@ -10641,10 +10641,12 @@ impl ServeLoop { } else if serve.subscriber.version.has_largest() { // A bounded feed that never delivers Largest goes live at the // answer alone; one that does waits for it to show. + // An answer behind the copy's cache fails it. match active.never_serves(start.largest) { Some(largest) => self.serving.set_live_ahead(largest, served), None => self.serving.set_live(start.largest, served), } + .inspect_err(|err| tracing::warn!(track = %serve.name, %err, "subscribe start behind the cache"))?; if let Some(largest) = start.largest { self.serving.report_largest(largest); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 3a3c31c8c04..8894bf7605a 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -2862,44 +2862,91 @@ impl TrackState { /// /// Readers wait until the cache shows that position: before then its newest group would /// read as the live edge, though the route already holds a newer one. + /// + /// [`Error::MalformedTrack`] when the answer is behind the cache; see [`Self::judge`]. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - fn set_live(&mut self, largest: Option, start: Option) { + fn set_live(&mut self, largest: Option, start: Option) -> Result<()> { + self.judge(largest)?; self.answered(largest, start, true); + Ok(()) } /// [`Self::set_live`] for a feed that serves no group from `largest`'s on, such as a /// bounded subscription of history: the cache can never show that group, so the answer /// alone makes it current, and relative starts count back from that group. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - fn set_live_ahead(&mut self, largest: Position, start: Option) { + fn set_live_ahead(&mut self, largest: Position, start: Option) -> Result<()> { + self.judge(Some(largest))?; self.answered(Some(largest), start, false); + Ok(()) + } + + /// [`Self::set_live`] for an answer with no Largest, where the first group the feed + /// sent, `first`, stands in for it. A group says nothing of how far the route's feed + /// reaches, so the cache is not judged against it. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + fn set_live_from(&mut self, first: Position, start: Option) { + self.answered(Some(first), start, true); + } + + /// An idle copy's route answered behind what the copy cached from it: no Largest while + /// the copy shows groups, or a Largest before the newest position it holds. The cache + /// is likely another track, so it is dropped rather than served while the copy fails. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + fn judge(&mut self, largest: Option) -> Result<()> { + let Some(cached) = self.idle_cached() else { + return Ok(()); + }; + let frames = self + .lookup + .get(&cached) + .map_or(0, |slot| slot.group.frame_count() as u64); + let held = Position { + group: cached, + frame: frames.saturating_sub(1), + }; + if largest.is_some_and(|largest| largest >= held) { + return Ok(()); + } + let err = Error::MalformedTrack; + let sequences: Vec = self.lookup.keys().copied().collect(); + for sequence in sequences { + self.evict_group(sequence, err.clone()); + } + Err(err) + } + + /// The newest group an idle copy still shows from its ended feed. Judged by what + /// survived: the cancel that idled the copy resets the group in flight. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + fn idle_cached(&self) -> Option { + let Feed::Idle { newest } = self.feed else { + return None; + }; + self.lookup + .range(..=newest?) + .rev() + .find(|(_, slot)| slot.is_shown()) + .map(|(sequence, _)| *sequence) } /// The route answered with `largest`. With `reaches`, its feed reaches that group, so /// readers wait until the cache shows it. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] fn answered(&mut self, largest: Option, start: Option, reaches: bool) { - let Feed::Idle { newest } = self.feed else { + if !matches!(self.feed, Feed::Idle { .. }) { return; - }; - // Judged by what survived: the cancel that idled the copy resets the group in flight, - // and an answer past it leaves a gap after the newest group still cached. - let cached = newest.and_then(|newest| { - self.lookup - .range(..=newest) - .rev() - .find(|(_, slot)| slot.is_shown()) - .map(|(sequence, _)| *sequence) - }); + } + // An answer past the newest group still cached leaves a gap after it. + let cached = self.idle_cached(); // A feed asked from the cache's head resends what follows it, newest first on a // descending track, so its largest alone would read as a gap. let start = start.or(largest.map(|largest| largest.group)); - self.live_floor = match (largest, cached) { - (Some(_), Some(cached)) if start.is_some_and(|start| start > cached.saturating_add(1)) => Some(cached + 1), - // The route has nothing, so whatever is cached is not its feed. - (None, Some(cached)) => Some(cached.saturating_add(1)), - _ => self.live_floor, - }; + if let (Some(start), Some(cached)) = (start, cached) + && start > cached.saturating_add(1) + { + self.live_floor = Some(cached + 1); + } match largest.map(|largest| largest.group) { Some(from) if reaches && !self.shows(from) => self.feed = Feed::Answered { from }, edge => self.go_live(edge), @@ -3732,10 +3779,15 @@ impl Producer { /// The route answered with its largest position, `None` for nothing yet, and the first /// group its feed serves when the answer names it; see [`Self::set_idle`]. Readers /// resume once the cache shows the largest position. A no-op unless idle. + /// + /// [`Error::MalformedTrack`] when the answer is behind the cache (no Largest while the + /// copy shows groups, or a Largest before its newest position): the cache is likely + /// another track, so the caller fails the copy and the next request builds a fresh one. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn set_live(&mut self, largest: Option, start: Option) { - if let Ok(mut state) = self.modify() { - state.set_live(largest, start); + pub(crate) fn set_live(&mut self, largest: Option, start: Option) -> Result<()> { + match self.modify() { + Ok(mut state) => state.set_live(largest, start), + Err(_) => Ok(()), } } @@ -3743,9 +3795,19 @@ impl Producer { /// bounded subscription of history: readers resume at once, counting relative starts /// back from `largest`'s group. A no-op unless idle. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn set_live_ahead(&mut self, largest: Position, start: Option) { + pub(crate) fn set_live_ahead(&mut self, largest: Position, start: Option) -> Result<()> { + match self.modify() { + Ok(mut state) => state.set_live_ahead(largest, start), + Err(_) => Ok(()), + } + } + + /// [`Self::set_live`] for an answer with no Largest, where `first`, the first group the + /// feed sent, stands in for it. The cache is not judged against it. A no-op unless idle. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn set_live_from(&mut self, first: Position, start: Option) { if let Ok(mut state) = self.modify() { - state.set_live_ahead(largest, start); + state.set_live_from(first, start); } } @@ -10157,7 +10219,7 @@ mod test { reader.set_range(..); assert!(reader.poll_largest(&noop).is_pending(), "the copy is idle"); - producer.set_live(Some(Position::group(2)), None); + producer.set_live(Some(Position::group(2)), None).unwrap(); assert!( reader.poll_largest(&noop).is_pending(), "the same largest is not reported again" @@ -10178,7 +10240,7 @@ mod test { let mut reader = producer.subscribe(replay()); assert!(reader.poll_largest(&noop).is_pending(), "the copy is idle"); - producer.set_live(Some(Position::group(5)), None); + producer.set_live(Some(Position::group(5)), None).unwrap(); let _answer = framed(&producer, 5, 1); assert_eq!(reader.poll_largest(&noop), Poll::Ready(Some(Position::group(5)))); @@ -10203,7 +10265,7 @@ mod test { let (flag, waiter) = Flag::waiter(); assert!(reader.poll_largest(&waiter).is_pending(), "the copy is idle"); - producer.set_live(Some(Position::group(5)), None); + producer.set_live(Some(Position::group(5)), None).unwrap(); producer.report_largest(Position::group(5)); assert!( reader.poll_largest(&waiter).is_pending(), @@ -10250,7 +10312,7 @@ mod test { assert_eq!(ready_groups(&mut reader), Vec::::new(), "the copy is idle"); assert_eq!(reader.floor(), None, "the start waits for the live feed"); - producer.set_live(Some(Position::group(4)), None); + producer.set_live(Some(Position::group(4)), None).unwrap(); framed(&producer, 4, 1).finish().unwrap(); assert_eq!(ready_groups(&mut reader), expected, "{ranges:?}"); } @@ -10262,7 +10324,7 @@ mod test { fn a_relative_start_on_an_idle_copy_waits_for_the_answer_to_show() { let mut producer = idle_copy(); let mut reader = producer.subscribe(replay().with_ranges([Range::live()])); - producer.set_live(Some(Position::group(5)), None); + producer.set_live(Some(Position::group(5)), None).unwrap(); framed(&producer, 4, 1).finish().unwrap(); assert_eq!(ready_groups(&mut reader), Vec::::new(), "the answer has not shown"); @@ -10270,19 +10332,52 @@ mod test { assert_eq!(ready_groups(&mut reader), vec![5]); } - /// A route that answers with nothing leaves the cache unrelated to its feed, so a - /// relative start resolves at the first group the feed then produces. + /// A route answer behind what an idle copy cached, with no Largest or one before the + /// newest position the copy holds, is malformed, and the copy's cache is dropped. #[test] - fn a_relative_start_on_an_idle_copy_skips_a_cache_the_route_disowns() { + fn an_answer_behind_an_idle_copys_cache_is_malformed() { + let behind = [None, Some(Position::group(2)), Some(Position { group: 3, frame: 0 })]; + let current = [Some(Position { group: 3, frame: 1 }), Some(Position::group(4))]; + for (largest, ok) in behind + .map(|largest| (largest, false)) + .into_iter() + .chain(current.map(|largest| (largest, true))) + { + let mut producer = track_producer("test", None); + framed(&producer, 2, 1).finish().unwrap(); + framed(&producer, 3, 2).finish().unwrap(); + producer.set_idle(); + let mut reader = producer.subscribe(replay()); + + let answered = producer.set_live(largest, None); + assert_eq!(answered.is_ok(), ok, "{largest:?}"); + if let Err(err) = answered { + assert!(matches!(err, Error::MalformedTrack), "{largest:?}"); + // The session fails the copy, and its readers get the error, not the cache. + producer.abort(err).unwrap(); + let noop = kio::Waiter::noop(); + assert!( + matches!(reader.poll_recv_group(&noop), Poll::Ready(Err(Error::MalformedTrack))), + "{largest:?}" + ); + } + } + let mut producer = idle_copy(); - let mut reader = producer.subscribe(replay().with_ranges([Range::live()])); - producer.set_live(None, None); - assert_eq!(ready_groups(&mut reader), Vec::::new()); - assert_eq!(reader.floor(), None, "no cached group anchors the start"); + assert!(matches!( + producer.set_live_ahead(Position::group(2), None), + Err(Error::MalformedTrack) + )); + } - framed(&producer, 7, 1).finish().unwrap(); - assert_eq!(ready_groups(&mut reader), vec![7]); - assert_eq!(reader.floor(), Some(7)); + /// A group standing in for a missing Largest says nothing of how far the route's feed + /// reaches, so one below the cache does not judge it. + #[test] + fn a_first_group_below_an_idle_copys_cache_is_not_judged() { + let mut producer = idle_copy(); + let mut reader = producer.subscribe(replay()); + producer.set_live_from(Position::group(0), None); + assert_eq!(ready_groups(&mut reader), vec![0, 1, 2, 3]); } /// An end the idle copy has not reached does not make its cache the whole track: until @@ -10297,7 +10392,7 @@ mod test { assert_eq!(live.floor(), None, "the start waits for the live feed"); assert_eq!(ready_groups(&mut oldest), Vec::::new(), "the copy is idle"); - producer.set_live(Some(Position::group(4)), None); + producer.set_live(Some(Position::group(4)), None).unwrap(); framed(&producer, 4, 1).finish().unwrap(); assert_eq!(ready_groups(&mut live), vec![4]); assert_eq!(ready_groups(&mut oldest), vec![0, 1, 2, 3, 4]); @@ -11508,7 +11603,7 @@ mod test { open.abort(Error::Cancel).unwrap(); let mut answer = producer.create_group(group::Info { sequence: 2 }).unwrap(); - producer.set_live(Some(Position::group(2)), None); + producer.set_live(Some(Position::group(2)), None).unwrap(); answer .write_frame(Timestamp::from_millis(533).unwrap(), bytes::Bytes::from_static(b"")) .unwrap(); @@ -11543,7 +11638,7 @@ mod test { .with_delay(Delay::default().with_max(Duration::from_millis(400))) .with_range(0..), ); - producer.set_live(Some(Position::group(8)), None); + producer.set_live(Some(Position::group(8)), None).unwrap(); for sequence in [6, 8] { framed(&producer, sequence, 1).finish().unwrap(); } @@ -11568,7 +11663,7 @@ mod test { } producer.set_idle(); producer.start_at(6).unwrap(); - producer.set_live(Some(Position::group(6)), None); + producer.set_live(Some(Position::group(6)), None).unwrap(); framed(&producer, 6, 1).finish().unwrap(); if !cached { for sequence in 0..3 { @@ -11624,7 +11719,7 @@ mod test { } producer.set_idle(); producer.start_at(6).unwrap(); - producer.set_live(Some(Position::group(6)), None); + producer.set_live(Some(Position::group(6)), None).unwrap(); framed(&producer, 6, 1).finish().unwrap(); for sequence in 0..3 { evict(&producer, sequence); @@ -11677,7 +11772,7 @@ mod test { framed(&producer, sequence, 1).finish().unwrap(); } producer.set_idle(); - producer.set_live(Some(Position::group(6)), None); + producer.set_live(Some(Position::group(6)), None).unwrap(); framed(&producer, 6, 1).finish().unwrap(); let delivered = Delivered::default(); let unread = Unread { @@ -11704,7 +11799,7 @@ mod test { producer.received(); framed(&producer, 0, 1).finish().unwrap(); producer.set_idle(); - producer.set_live(Some(Position::group(6)), None); + producer.set_live(Some(Position::group(6)), None).unwrap(); framed(&producer, 6, 1).finish().unwrap(); evict(&producer, 0); let state = producer.state.read(); @@ -11741,7 +11836,7 @@ mod test { assert_eq!(read_now(&mut reader, ordered), None, "the copy is idle"); producer.start_at(3).unwrap(); - producer.set_live(Some(Position::group(3)), None); + producer.set_live(Some(Position::group(3)), None).unwrap(); framed(&producer, 3, 1).finish().unwrap(); assert_eq!(read_now(&mut reader, ordered), Some(Some(0)), "cached history"); // An arrival-order reader may take the live group before its history. @@ -11793,7 +11888,7 @@ mod test { assert!(subscriber.poll_drop(&waiter).is_pending(), "nothing was given up"); let mut again = producer.create_group(group::Info { sequence: 1 }).unwrap(); - producer.set_live(Some(Position::group(1)), None); + producer.set_live(Some(Position::group(1)), None).unwrap(); again .write_frame(Timestamp::from_millis(1000).unwrap(), bytes::Bytes::from_static(b"x")) .unwrap(); @@ -13383,7 +13478,7 @@ mod test { let consumer = request.consume(); let mut producer = request.idle().accept(None); assert!(!consumer.is_live(), "idle from the accept"); - producer.set_live(None, None); + producer.set_live(None, None).unwrap(); assert!(consumer.is_live()); } @@ -13415,7 +13510,7 @@ mod test { assert!(consumer.is_live(), "accepting the track starts its live feed"); producer.set_idle(); assert!(!consumer.is_live(), "an idle accepted copy is not live"); - producer.set_live(None, None); + producer.set_live(None, None).unwrap(); assert!(consumer.is_live(), "the answered copy is live again"); // The fetch handler holds the same producer lifetime, so both leave. @@ -20432,7 +20527,7 @@ mod test { } producer.set_idle(); // The feed resumes at 8, above the cached 0..=4: its live floor is 5. - producer.set_live(Some(Position::group(8)), None); + producer.set_live(Some(Position::group(8)), None).unwrap(); framed(&producer, 8, 1).finish().unwrap(); let mut reader = producer.subscribe( Subscription::default() @@ -22252,7 +22347,7 @@ mod test { let mut producer = track_producer("test", Info::timed()); if copy { producer.set_idle(); - producer.set_live(Some(Position::group(9)), Some(2)); + producer.set_live(Some(Position::group(9)), Some(2)).unwrap(); } let mut groups: Vec<_> = (2..9) .map(|sequence| producer.create_group(group::Info { sequence }).unwrap()) @@ -23706,7 +23801,7 @@ mod test { for _ in 0..2 { assert_eq!(read_now(&mut reader, ordered), end, "the source is idle"); } - source.set_live(Some(Position::group(5)), None); + source.set_live(Some(Position::group(5)), None).unwrap(); reader.update(subscription.clone()).unwrap(); assert_eq!(read_now(&mut reader, ordered), end); reader.update(subscription.clone().with_range(3..4)).unwrap(); diff --git a/rs/moq-net/tests/integration/answer_behind_cache.rs b/rs/moq-net/tests/integration/answer_behind_cache.rs new file mode 100644 index 00000000000..22d871fbc60 --- /dev/null +++ b/rs/moq-net/tests/integration/answer_behind_cache.rs @@ -0,0 +1,108 @@ +#![cfg(feature = "async")] + +//! A relay copy lingers after its last reader leaves and resumes on the next subscribe. +//! A route answering behind that copy's cache fails the subscription rather than leave it +//! waiting, and the next subscribe builds a fresh copy (#7973). + +use crate::support; + +use std::time::Duration; + +use moq_net::{Error, Hop, Version, origin, track}; +use support::harness::peer; + +const TIMEOUT: Duration = Duration::from_secs(5); + +fn produce_origin(hop: u64) -> origin::Producer { + let (producer, driver) = origin::Producer::new(origin::Config::new(Hop::new(hop).unwrap())); + support::harness::spawn(driver); + producer +} + +fn write_group(track: &track::Producer, payload: &'static [u8]) { + let mut group = track.create_group(moq_net::group::Info { sequence: 0 }).unwrap(); + group.write_frame(None, payload).unwrap(); + group.finish().unwrap(); +} + +/// The first group `subscriber` receives, with its first frame. +async fn first_group(subscriber: &mut track::Subscriber) -> moq_net::Result<(u64, Vec)> { + let mut group = kio::clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .expect("the subscription waits forever")? + .expect("the subscription ended"); + let frame = kio::clock::timeout(TIMEOUT, group.read_frame()) + .await + .unwrap()? + .unwrap(); + Ok((group.sequence, frame.payload.to_vec())) +} + +/// The publisher replaces its track with an empty one of the same name, so the lingering +/// copy's resubscription is answered with no Largest while the copy holds group 0. A lite +/// publisher holds its answer to that resubscription until the group exists, so only +/// moq-transport answers this way. +async fn an_empty_answer_fails_the_lingering_copy(version: &str) { + let version: Version = version.parse().unwrap(); + let publisher = produce_origin(1); + let relay = produce_origin(2); + let _pair = peer(version, &publisher, &relay).await; + + let route = publisher.route("room", origin::RouteOptions::hidden()).unwrap(); + let info = track::Info::default().with_timescale(None); + let old = route.broadcast().create_track("catalog", info.clone()).unwrap(); + route.announce().unwrap(); + write_group(&old, b"old"); + + let consumer = relay.consume().request("room", None).await.unwrap(); + { + let mut subscriber = kio::clock::timeout(TIMEOUT, consumer.track("catalog").unwrap().subscribe(None)) + .await + .unwrap() + .unwrap(); + assert_eq!(first_group(&mut subscriber).await.unwrap(), (0, b"old".to_vec())); + } + // The relay cancels its idle upstream subscription; the copy lingers. + kio::clock::timeout(TIMEOUT, support::unwanted(old.demand())) + .await + .unwrap() + .unwrap(); + + old.abort(Error::Cancel).unwrap(); + let new = route.broadcast().create_track("catalog", info).unwrap(); + + let resumed = kio::clock::timeout(TIMEOUT, consumer.track("catalog").unwrap().subscribe(None)) + .await + .expect("the resubscribe waits forever"); + let failed = match resumed { + Ok(mut subscriber) => first_group(&mut subscriber).await.map(|_| ()), + Err(err) => Err(err), + }; + assert!(matches!(failed, Err(Error::MalformedTrack)), "{version}: {failed:?}"); + // The failed copy leaves once the relay drops its upstream subscription. + kio::clock::timeout(TIMEOUT, support::unwanted(new.demand())) + .await + .unwrap() + .unwrap(); + + write_group(&new, b"new"); + let mut subscriber = kio::clock::timeout(TIMEOUT, consumer.track("catalog").unwrap().subscribe(None)) + .await + .unwrap() + .unwrap(); + assert_eq!( + first_group(&mut subscriber).await.unwrap(), + (0, b"new".to_vec()), + "{version}: a fresh copy accepts the feed from group 0" + ); +} + +#[kio::test] +async fn an_empty_answer_fails_the_lingering_copy_ietf18() { + an_empty_answer_fails_the_lingering_copy("moq-transport-18").await; +} + +#[kio::test] +async fn an_empty_answer_fails_the_lingering_copy_ietf22() { + an_empty_answer_fails_the_lingering_copy("moq-transport-22").await; +} diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 78b2f3a2b22..05422ad1f96 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -10,6 +10,7 @@ mod announce_covering_route; mod announce_takeover; mod announce_to_serve; +mod answer_behind_cache; mod auth; mod broadcast_close; mod catalog_acquisition; From b911760b9301fe67e49db384a3c76a1490c9c7bf Mon Sep 17 00:00:00 2001 From: Michael Stuurman Date: Fri, 6 Nov 2026 18:45:01 +0100 Subject: [PATCH 2/8] fix(net): judge a resuming copy by its newest frame, not an empty group Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 35 ++++++++++++++----- .../tests/integration/answer_behind_cache.rs | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 8894bf7605a..76898a58da9 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -2890,20 +2890,28 @@ impl TrackState { } /// An idle copy's route answered behind what the copy cached from it: no Largest while - /// the copy shows groups, or a Largest before the newest position it holds. The cache - /// is likely another track, so it is dropped rather than served while the copy fails. + /// the copy holds frames, or a Largest before the newest frame it holds. Largest names + /// an object, so an empty group says nothing. The cache is likely another track, so it + /// is dropped rather than served while the copy fails. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] fn judge(&mut self, largest: Option) -> Result<()> { - let Some(cached) = self.idle_cached() else { + let Feed::Idle { newest: Some(newest) } = self.feed else { return Ok(()); }; - let frames = self + let held = self .lookup - .get(&cached) - .map_or(0, |slot| slot.group.frame_count() as u64); - let held = Position { - group: cached, - frame: frames.saturating_sub(1), + .range(..=newest) + .rev() + .filter(|(_, slot)| slot.is_shown()) + .find_map(|(&group, slot)| { + let frames = slot.group.frame_count() as u64; + Some(Position { + group, + frame: frames.checked_sub(1)?, + }) + }); + let Some(held) = held else { + return Ok(()); }; if largest.is_some_and(|largest| largest >= held) { return Ok(()); @@ -10368,6 +10376,15 @@ mod test { producer.set_live_ahead(Position::group(2), None), Err(Error::MalformedTrack) )); + + // Largest names the newest object, which an empty newest group does not hold. + for (largest, ok) in [(Some(Position::group(2)), true), (None, false)] { + let mut producer = track_producer("test", None); + framed(&producer, 2, 1).finish().unwrap(); + framed(&producer, 3, 0).finish().unwrap(); + producer.set_idle(); + assert_eq!(producer.set_live(largest, None).is_ok(), ok, "{largest:?}"); + } } /// A group standing in for a missing Largest says nothing of how far the route's feed diff --git a/rs/moq-net/tests/integration/answer_behind_cache.rs b/rs/moq-net/tests/integration/answer_behind_cache.rs index 22d871fbc60..ce2b1d37c8b 100644 --- a/rs/moq-net/tests/integration/answer_behind_cache.rs +++ b/rs/moq-net/tests/integration/answer_behind_cache.rs @@ -2,7 +2,7 @@ //! A relay copy lingers after its last reader leaves and resumes on the next subscribe. //! A route answering behind that copy's cache fails the subscription rather than leave it -//! waiting, and the next subscribe builds a fresh copy (#7973). +//! waiting, and the next subscribe builds a fresh copy. use crate::support; From 518cf3bab9d304f9d9faac741b527c44400c1021 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 15:17:38 -0800 Subject: [PATCH 3/8] fix(net): answer the last held object when the newest group is unreadable A moq-transport publisher whose newest group cannot be read, such as one its upstream reset, answered with no Largest while it still held earlier groups. With the behind-the-cache check, that failed a downstream relay's lingering copy with MALFORMED_TRACK. Report the last held object instead, keeping the subscription past the newest group. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/ietf/publisher.rs | 47 ++++++-- .../tests/integration/answer_behind_cache.rs | 113 +++++++++++++++++- 2 files changed, 147 insertions(+), 13 deletions(-) diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 1a727e60b44..b37adba03dc 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -20286,13 +20286,14 @@ mod tests { struct LiveEdge { /// The newest group sequence, `None` before any group exists. latest: Option, - /// The precise Largest Object. `None` when the track is empty, or when the newest - /// group's frames cannot be read right now (none written yet), in which case nothing - /// is advertised and no fill is servable. + /// The precise Largest Object: the newest object held, skipping a newest group whose + /// frames cannot be read. `None` when no object is held, in which case nothing is + /// advertised and no fill is servable. largest: Option, - /// One past the Largest Object, which is where a Next Object subscription begins. - /// When the edge is imprecise this falls back to the next group boundary: never below - /// the true Next Object, at worst under-delivering the current group's tail. + /// Where a Next Object subscription begins: one past the Largest Object, or past a + /// newest group that cannot be read. When the edge is imprecise this falls back to the + /// next group boundary: never below the true Next Object, at worst under-delivering the + /// current group's tail. next: Option, } @@ -20356,9 +20357,12 @@ fn cached_edge(track: &track::Consumer) -> LiveEdge { next: Some(next), } } + // The newest group cannot be read, such as one aborted when this copy went idle, so + // the largest is the last object held before it, and nothing of the newest group + // can be served, so a subscription starts past it. _ => LiveEdge { latest: Some(latest), - largest: None, + largest: largest_before(track, latest), next: Some(Location { group: latest.saturating_add(1), object: 0, @@ -20912,6 +20916,35 @@ mod range_tests { assert_eq!(edge.next, Some(Location { group: 0, object: 1 })); } + /// A newest group that cannot be read, such as one aborted when an idle copy cancelled + /// it, leaves the last object held before it as the largest, while a subscription still + /// starts past it so nothing is stranded behind its floor. + #[test] + fn an_aborted_newest_group_leaves_the_earlier_largest() { + let track = track::Producer::new( + std::sync::Arc::new(crate::broadcast::Info::default()), + "video", + crate::track::Info::timed(), + ); + let mut first = track.create_group(group::Info { sequence: 0 }).unwrap(); + for _ in 0..2 { + first + .write_frame(crate::Timestamp::from_millis(0).unwrap(), b"frame".as_slice()) + .unwrap(); + } + first.finish().unwrap(); + let mut newest = track.create_group(group::Info { sequence: 1 }).unwrap(); + newest + .write_frame(crate::Timestamp::from_millis(0).unwrap(), b"frame".as_slice()) + .unwrap(); + newest.abort(Error::Cancel).unwrap(); + + let edge = live_edge(&track.consume()); + assert_eq!(edge.latest, Some(1)); + assert_eq!(edge.largest, Some(Location { group: 0, object: 1 })); + assert_eq!(edge.next, Some(Location { group: 2, object: 0 })); + } + /// Both ends carry through, object bounds included, so the boundary groups can be /// trimmed rather than widened. #[test] diff --git a/rs/moq-net/tests/integration/answer_behind_cache.rs b/rs/moq-net/tests/integration/answer_behind_cache.rs index ce2b1d37c8b..df900dcd7f8 100644 --- a/rs/moq-net/tests/integration/answer_behind_cache.rs +++ b/rs/moq-net/tests/integration/answer_behind_cache.rs @@ -9,7 +9,7 @@ use crate::support; use std::time::Duration; use moq_net::{Error, Hop, Version, origin, track}; -use support::harness::peer; +use support::harness::{MockConnectOptions, connect_mock, peer}; const TIMEOUT: Duration = Duration::from_secs(5); @@ -25,8 +25,8 @@ fn write_group(track: &track::Producer, payload: &'static [u8]) { group.finish().unwrap(); } -/// The first group `subscriber` receives, with its first frame. -async fn first_group(subscriber: &mut track::Subscriber) -> moq_net::Result<(u64, Vec)> { +/// The next group `subscriber` receives, with its first frame. +async fn next_group(subscriber: &mut track::Subscriber) -> moq_net::Result<(u64, Vec)> { let mut group = kio::clock::timeout(TIMEOUT, subscriber.recv_group()) .await .expect("the subscription waits forever")? @@ -60,7 +60,7 @@ async fn an_empty_answer_fails_the_lingering_copy(version: &str) { .await .unwrap() .unwrap(); - assert_eq!(first_group(&mut subscriber).await.unwrap(), (0, b"old".to_vec())); + assert_eq!(next_group(&mut subscriber).await.unwrap(), (0, b"old".to_vec())); } // The relay cancels its idle upstream subscription; the copy lingers. kio::clock::timeout(TIMEOUT, support::unwanted(old.demand())) @@ -75,7 +75,7 @@ async fn an_empty_answer_fails_the_lingering_copy(version: &str) { .await .expect("the resubscribe waits forever"); let failed = match resumed { - Ok(mut subscriber) => first_group(&mut subscriber).await.map(|_| ()), + Ok(mut subscriber) => next_group(&mut subscriber).await.map(|_| ()), Err(err) => Err(err), }; assert!(matches!(failed, Err(Error::MalformedTrack)), "{version}: {failed:?}"); @@ -91,7 +91,7 @@ async fn an_empty_answer_fails_the_lingering_copy(version: &str) { .unwrap() .unwrap(); assert_eq!( - first_group(&mut subscriber).await.unwrap(), + next_group(&mut subscriber).await.unwrap(), (0, b"new".to_vec()), "{version}: a fresh copy accepts the feed from group 0" ); @@ -106,3 +106,104 @@ async fn an_empty_answer_fails_the_lingering_copy_ietf18() { async fn an_empty_answer_fails_the_lingering_copy_ietf22() { an_empty_answer_fails_the_lingering_copy("moq-transport-22").await; } + +/// A relay whose upstream reset its newest group while it stayed live still holds the +/// groups before it, so its moq-transport answer names the last of them as Largest. A +/// downstream copy that lingered over the same groups is current with that answer and +/// resumes rather than failing. +async fn a_reset_newest_group_answers_the_group_before(version: &str) { + let version: Version = version.parse().unwrap(); + let publisher = produce_origin(1); + let upper = produce_origin(2); + let lower = produce_origin(3); + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(upper.clone()); + let _upper = connect_mock(options).await; + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(upper.consume()); + options.client_subscribe = Some(lower.clone()); + let _lower = connect_mock(options).await; + + let route = publisher.route("room", origin::RouteOptions::hidden()).unwrap(); + let track = route + .broadcast() + .create_track("audio", track::Info::default().with_timescale(None)) + .unwrap(); + route.announce().unwrap(); + let mut g0 = track.create_group(moq_net::group::Info { sequence: 0 }).unwrap(); + g0.write_frame(None, &b"0a"[..]).unwrap(); + + // A reader on the upper relay keeps its copy live after the lower relay leaves. + let local = kio::clock::timeout(TIMEOUT, upper.consume().follow("room", None).unwrap().available()) + .await + .unwrap() + .unwrap() + .unwrap(); + let mut keeper = kio::clock::timeout(TIMEOUT, local.track("audio").unwrap().subscribe(None)) + .await + .unwrap() + .unwrap(); + assert_eq!(next_group(&mut keeper).await.unwrap(), (0, b"0a".to_vec())); + + let remote = kio::clock::timeout(TIMEOUT, lower.consume().follow("room", None).unwrap().available()) + .await + .unwrap() + .unwrap() + .unwrap(); + { + let mut subscriber = kio::clock::timeout(TIMEOUT, remote.track("audio").unwrap().subscribe(None)) + .await + .unwrap() + .unwrap(); + assert_eq!(next_group(&mut subscriber).await.unwrap(), (0, b"0a".to_vec())); + g0.finish().unwrap(); + let mut g1 = track.create_group(moq_net::group::Info { sequence: 1 }).unwrap(); + g1.write_frame(None, &b"1a"[..]).unwrap(); + assert_eq!(next_group(&mut subscriber).await.unwrap(), (1, b"1a".to_vec())); + let mut kept = kio::clock::timeout(TIMEOUT, keeper.recv_group()) + .await + .unwrap() + .unwrap() + .unwrap(); + assert_eq!(kept.sequence, 1); + g1.abort(Error::Cancel).unwrap(); + // The upper relay's copy stays live with group 1 reset and group 0 held. + loop { + match kio::clock::timeout(TIMEOUT, kept.read_frame()).await.unwrap() { + Ok(Some(_)) => {} + Ok(None) => panic!("group 1 finished rather than reset"), + Err(_) => break, + } + } + } + // On the simulated clock: the lower relay cancels its upstream subscription and its copy + // lingers over group 0. + kio::clock::sleep(Duration::from_secs(1)).await; + + let mut subscriber = kio::clock::timeout(TIMEOUT, remote.track("audio").unwrap().subscribe(None)) + .await + .expect("the resubscribe waits forever") + .unwrap(); + // The upper relay answers the resubscription before the feed moves on. + kio::clock::sleep(Duration::from_secs(1)).await; + let mut g2 = track.create_group(moq_net::group::Info { sequence: 2 }).unwrap(); + g2.write_frame(None, &b"2a"[..]).unwrap(); + loop { + let (sequence, payload) = next_group(&mut subscriber).await.unwrap(); + if sequence == 2 { + assert_eq!(payload, b"2a".to_vec(), "{version}"); + break; + } + } +} + +#[kio::test] +async fn a_reset_newest_group_answers_the_group_before_ietf18() { + a_reset_newest_group_answers_the_group_before("moq-transport-18").await; +} + +#[kio::test] +async fn a_reset_newest_group_answers_the_group_before_ietf22() { + a_reset_newest_group_answers_the_group_before("moq-transport-22").await; +} From 874bc8a9722417c60af51797d5fb49e1b3006d90 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 15:30:06 -0800 Subject: [PATCH 4/8] test(net): a draft-22 fetch past an aborted group's held Largest is refused Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/ietf/publisher.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index b37adba03dc..1cd874a0706 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -14398,7 +14398,7 @@ mod serve_tests { IdleEnded, /// An idle copy whose track ends after its newest group: that group's last object. IdleFinished, - /// A live feed whose newest group was aborted mid-write: unknown. + /// A live feed whose newest group was aborted mid-write: the last object before it. Aborted, } @@ -14464,7 +14464,7 @@ mod serve_tests { ) .await; match largest { - Largest::Finished | Largest::Live | Largest::IdleFinished => assert_eq!( + Largest::Finished | Largest::Live | Largest::IdleFinished | Largest::Aborted => assert_eq!( fetch_refusal(buf, version), INVALID_RANGE, "{version} {largest:?} start={start}" From 4b508bb65728b1804f1f98b0ac960a170a19b1bb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 15:38:58 -0800 Subject: [PATCH 5/8] fix(net): judge a resumed copy against what its ended feed left, by object ID The answer was judged against the cache at the time it arrived, which on moq-transport already holds the next feed's resent groups, and by frame count, which is not a sparse group's newest object ID. Snapshot the held position at idle, using the wire's object ID where the group has one. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/group.rs | 10 ++++ rs/moq-net/src/model/track.rs | 101 ++++++++++++++++++++++++++-------- 2 files changed, 88 insertions(+), 23 deletions(-) diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 68203c6de0a..0254699f8b9 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -2577,6 +2577,16 @@ impl Handle { self.state.read().next_index } + /// The newest object's ID, by its moq-transport object ID when the group carries one, + /// else its frame index; `None` before the first frame. + pub(crate) fn last_object(&self) -> Option { + let state = self.state.read(); + match state.wire() { + Some(wire) => wire.last, + None => (state.next_index as u64).checked_sub(1), + } + } + /// Cap the group at `frames`, for a track end inside it, or lift the cap with `None`: /// later writes past the cap are [`Error::Closed`]. A frame already at or past the cap is /// [`Error::ProtocolViolation`]. An aborted group gets no more frames, so it takes no cap. diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 385b12a51d9..a46a1329ec4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -312,6 +312,9 @@ enum Feed { /// The newest group cached when the track went idle: what the route's answer is /// judged against, whatever lands before it. newest: Option, + /// The newest object the ended feed left in the cache. Snapshotted, since the next + /// feed's groups can land before its answer is judged. + held: Option, }, /// The route answered that its feed reaches group `from`, which the cache cannot show /// yet: until it can, the newest group cached would read as the live edge. @@ -2838,7 +2841,6 @@ impl TrackState { .rev() .find(|(_, slot)| slot.is_shown()) .map(|(sequence, _)| *sequence); - self.feed = Feed::Idle { newest }; // An open group stops because this copy dropped its feed, not because the publisher // gave it up: its stream resets as a cut would, so it leaves the cache first, and the // next feed sends it again. @@ -2851,6 +2853,19 @@ impl TrackState { for sequence in open { self.displace(sequence); } + // Largest names an object, so an empty group holds nothing to judge. + let held = self + .lookup + .iter() + .rev() + .filter(|(_, slot)| slot.is_shown()) + .find_map(|(&group, slot)| { + Some(Position { + group, + frame: slot.group.last_object()?, + }) + }); + self.feed = Feed::Idle { newest, held }; } /// The route answered with its largest position (`None` for nothing yet): the cache @@ -2889,28 +2904,13 @@ impl TrackState { self.answered(Some(first), start, true); } - /// An idle copy's route answered behind what the copy cached from it: no Largest while - /// the copy holds frames, or a Largest before the newest frame it holds. Largest names - /// an object, so an empty group says nothing. The cache is likely another track, so it - /// is dropped rather than served while the copy fails. + /// An idle copy's route answered behind what its ended feed left in the cache: no + /// Largest while the copy held an object, or a Largest before the newest one it held. + /// The cache is likely another track, so it is dropped rather than served while the + /// copy fails. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] fn judge(&mut self, largest: Option) -> Result<()> { - let Feed::Idle { newest: Some(newest) } = self.feed else { - return Ok(()); - }; - let held = self - .lookup - .range(..=newest) - .rev() - .filter(|(_, slot)| slot.is_shown()) - .find_map(|(&group, slot)| { - let frames = slot.group.frame_count() as u64; - Some(Position { - group, - frame: frames.checked_sub(1)?, - }) - }); - let Some(held) = held else { + let Feed::Idle { held: Some(held), .. } = self.feed else { return Ok(()); }; if largest.is_some_and(|largest| largest >= held) { @@ -2928,7 +2928,7 @@ impl TrackState { /// survived: the cancel that idled the copy resets the group in flight. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] fn idle_cached(&self) -> Option { - let Feed::Idle { newest } = self.feed else { + let Feed::Idle { newest, .. } = self.feed else { return None; }; self.lookup @@ -3926,7 +3926,7 @@ impl Producer { pub(crate) fn idle_newest(&self) -> Option { let state = self.state.read(); match state.feed { - Feed::Idle { newest } => newest, + Feed::Idle { newest, .. } => newest, _ => None, } } @@ -10387,6 +10387,61 @@ mod test { } } + /// The answer is judged against what the ended feed left, not the next feed's groups + /// that land before it: a group in flight at idle is sent again, past the answer. + #[test] + fn an_idle_copy_judges_its_answer_against_the_ended_feed() { + let mut producer = track_producer("test", None); + framed(&producer, 2, 1).finish().unwrap(); + let _open = framed(&producer, 3, 1); + producer.set_idle(); + + let resent = framed(&producer, 3, 3); + producer.set_live(Some(Position { group: 3, frame: 0 }), None).unwrap(); + drop(resent); + } + + /// Largest names a moq-transport object by its ID, which a sparse group's frame count + /// does not give. + #[test] + fn an_idle_copy_judges_a_sparse_group_by_object_id() { + let wire = group::Wire { + sparse: true, + first_object: true, + first_id: 0, + extensions: false, + has_end: false, + fetchable: true, + last: None, + }; + for (object, ok) in [(50, false), (100, true)] { + let mut producer = track_producer("test", None); + let mut group = producer.create_group(group::Info { sequence: 2 }).unwrap(); + group.set_wire(wire.clone()).unwrap(); + for id in [0, 100] { + let mut frame = group + .create_object(frame::Header { + info: frame::Info { + size: 1, + timestamp: None, + }, + fields: Some(Arc::new(super::super::object::Fields { + id, + properties: Default::default(), + })), + inflated: None, + }) + .unwrap(); + frame.write(&b"x"[..]).unwrap(); + group = frame.finish().unwrap(); + } + group.finish().unwrap(); + producer.set_idle(); + let answered = producer.set_live(Some(Position { group: 2, frame: object }), None); + assert_eq!(answered.is_ok(), ok, "object {object}"); + } + } + /// A group standing in for a missing Largest says nothing of how far the route's feed /// reaches, so one below the cache does not judge it. #[test] From f342773c2148fed32f0afcd6f54606e6c0cbaa09 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 15:43:47 -0800 Subject: [PATCH 6/8] style: rustfmt Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index a46a1329ec4..2f47d8a32c4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -10437,7 +10437,13 @@ mod test { } group.finish().unwrap(); producer.set_idle(); - let answered = producer.set_live(Some(Position { group: 2, frame: object }), None); + let answered = producer.set_live( + Some(Position { + group: 2, + frame: object, + }), + None, + ); assert_eq!(answered.is_ok(), ok, "object {object}"); } } From c178fcdd3d5f691236149f9a2c65a300be90ea50 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 16:13:37 -0800 Subject: [PATCH 7/8] test(net): the sparse-group idle test needs async, like set_wire Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 2f47d8a32c4..6f9ee992a4f 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -10403,6 +10403,7 @@ mod test { /// Largest names a moq-transport object by its ID, which a sparse group's frame count /// does not give. + #[cfg(feature = "async")] #[test] fn an_idle_copy_judges_a_sparse_group_by_object_id() { let wire = group::Wire { From 33ca70ff0c447faeb4d623e678e648ca7797d312 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 16:41:58 -0800 Subject: [PATCH 8/8] test(moq-net): build the origin config from a NonZeroU64 main made Hop crate-private, so the answer_behind_cache test takes the public origin::Config::new(NonZeroU64) like the other integration tests. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/tests/integration/answer_behind_cache.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/tests/integration/answer_behind_cache.rs b/rs/moq-net/tests/integration/answer_behind_cache.rs index df900dcd7f8..efd4596ad5d 100644 --- a/rs/moq-net/tests/integration/answer_behind_cache.rs +++ b/rs/moq-net/tests/integration/answer_behind_cache.rs @@ -8,13 +8,13 @@ use crate::support; use std::time::Duration; -use moq_net::{Error, Hop, Version, origin, track}; +use moq_net::{Error, Version, origin, track}; use support::harness::{MockConnectOptions, connect_mock, peer}; const TIMEOUT: Duration = Duration::from_secs(5); fn produce_origin(hop: u64) -> origin::Producer { - let (producer, driver) = origin::Producer::new(origin::Config::new(Hop::new(hop).unwrap())); + let (producer, driver) = origin::Producer::new(origin::Config::new(std::num::NonZeroU64::new(hop).unwrap())); support::harness::spawn(driver); producer }