From 9a50b9f273326de28bddcb1821f552a2629c144e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 13:23:06 -0700 Subject: [PATCH] fix(net): a FETCH never stalls on a parked open group When the last reader leaves mid-group, the front parks a warm copy that rebuilds the open live-edge group, and nothing writes to it again. A FETCH answered from that copy got the cached frames and then waited forever. An unfinished group in a warm segment is now a FETCH miss, and a fetch never latches onto a warm segment, so its own demand re-splices the track and the FETCH goes upstream on the new copy. Release only: main deleted the warm copy in #4741. Closes #5348 Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/resume.rs | 17 +++-- rs/moq-net/src/model/track.rs | 7 +- rs/moq-net/tests/idle_cancel_refetch.rs | 93 +++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 rs/moq-net/tests/idle_cancel_refetch.rs diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index b5b352ca9af..c8b06fae5b5 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -627,12 +627,16 @@ impl Consumer { /// A segment can answer a FETCH only when it owns the requested head and the /// rest of the group. A frame boundary inside the group therefore falls /// through to another segment or to the live route's upstream FETCH. + /// + /// A warm cache's open group is a miss: its upstream subscription is gone, so + /// nothing writes the rest of it, and a FETCH answered from it stalls after + /// the cached frames. Only a re-splice can continue it. pub(crate) fn cached_group(&self, sequence: u64, frame_start: u64) -> Option { let segments = self.state.read().segments.clone(); segments.iter().rev().find_map(|segment| { let (start, end) = frames(segment.start, segment.end, sequence)?; (start <= frame_start && end.is_none()) - .then(|| segment.track.cached_group(sequence, frame_start)) + .then(|| segment.track.cached_group(sequence, frame_start, segment.warm)) .flatten() }) } @@ -822,10 +826,15 @@ impl Fetching { /// than `latched` when given. `Err` when the logical track aborted; /// `Ok(None)` when the producer is gone with no qualifying segment, so /// nothing will ever answer. + /// + /// A warm cache never qualifies. Its complete groups already answered through + /// [`Consumer::cached_group`], and its open ones must miss, so the fetch waits + /// for the re-splice its own demand triggers and asks that copy instead. fn poll_latch(&self, waiter: &kio::Waiter, latched: Option) -> Poll>> { let newest = move |segments: &[Segment]| { segments .last() + .filter(|segment| !segment.warm) .filter(|segment| latched.is_none_or(|latched| segment.id > latched)) .map(|segment| (segment.id, segment.track.clone())) }; @@ -879,9 +888,9 @@ impl kio::Pollable for Fetching { Poll::Ready(Err(err)) => err, Poll::Ready(Ok(group)) => return Poll::Ready(Ok(group)), Poll::Pending => { - // A warm cache deliberately parks misses while demand re-splices - // upstream. Follow that newer segment without waiting for the warm - // copy to reject its queued request. + // A takeover may splice a newer copy while this one still holds the + // request queued. Follow it without waiting for the old copy to + // reject. let latched = *latched; match self.poll_latch(waiter, Some(latched)) { Poll::Ready(Ok(Some((id, track)))) => { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 38d53444677..5b9284a6e92 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -2503,11 +2503,16 @@ impl Consumer { /// /// Unlike [`Self::peek_group`], this is suitable for satisfying a FETCH: a /// partial cached copy is a miss so the caller can ask upstream for its head. - pub(crate) fn cached_group(&self, sequence: u64, frame_start: u64) -> Option { + /// With `finished`, an unfinished copy is a miss too, for a cache nothing + /// writes to anymore. + pub(crate) fn cached_group(&self, sequence: u64, frame_start: u64, finished: bool) -> Option { match &self.inner { ConsumerKind::Plain(state) => { let state = state.read(); let group = state.covering_group(sequence, frame_start)?; + if finished && !group.is_finished() { + return None; + } group.cache_refresh(); Some(group.consume()) } diff --git a/rs/moq-net/tests/idle_cancel_refetch.rs b/rs/moq-net/tests/idle_cancel_refetch.rs new file mode 100644 index 00000000000..9fa7d218759 --- /dev/null +++ b/rs/moq-net/tests/idle_cancel_refetch.rs @@ -0,0 +1,93 @@ +//! A relay whose last reader leaves mid-group cancels the idle upstream subscription and +//! parks what it cached. A later fetch of that group still gets every frame (#5348). + +mod support; + +use std::time::Duration; + +use moq_net::{Hop, Timestamp, Version}; +use support::harness::{MockConnectOptions, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(5); + +fn produce_origin(hop: u64) -> moq_net::origin::Producer { + let (producer, driver) = moq_net::origin::Producer::new(moq_net::origin::Config::new(Hop::new(hop).unwrap())); + tokio::spawn(support::harness::run(driver)); + producer +} + +#[tokio::test(start_paused = true)] +async fn a_group_cut_by_an_idle_cancel_is_refetched_whole() { + let publisher = produce_origin(1); + let broadcast = publisher.create_broadcast("bcast").unwrap(); + let track = broadcast.create_track("audio", None).unwrap(); + broadcast.announce(Default::default()).unwrap(); + + let mut g0 = track.create_group(moq_net::group::Info { sequence: 0 }).unwrap(); + g0.write_frame(Timestamp::ZERO, &b"0a"[..]).unwrap(); + g0.finish().unwrap(); + let mut g1 = track.create_group(moq_net::group::Info { sequence: 1 }).unwrap(); + g1.write_frame(Timestamp::ZERO, &b"1a"[..]).unwrap(); + + let client = produce_origin(2); + let mut options = MockConnectOptions::new("moq-lite-06".parse::().unwrap()); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(client.clone()); + let _pair = connect_mock(options).await; + + let consumer = client.consume(); + tokio::time::timeout(TIMEOUT, consumer.routed("bcast")) + .await + .unwrap() + .unwrap(); + let remote = tokio::time::timeout(TIMEOUT, consumer.request_broadcast("bcast")) + .await + .unwrap() + .unwrap(); + + { + let mut subscriber = tokio::time::timeout(TIMEOUT, remote.track("audio").unwrap().subscribe(None)) + .await + .unwrap() + .unwrap(); + let mut group = tokio::time::timeout(TIMEOUT, subscriber.recv_group()) + .await + .unwrap() + .unwrap() + .unwrap(); + assert_eq!(group.sequence, 1); + let frame = tokio::time::timeout(TIMEOUT, group.read_frame()) + .await + .unwrap() + .unwrap() + .unwrap(); + assert_eq!(&frame.payload[..], b"1a"); + } + + // The relay cancels the idle upstream subscription. + tokio::time::timeout(TIMEOUT, track.unused()).await.unwrap().unwrap(); + + g1.write_frame(Timestamp::ZERO, &b"1b"[..]).unwrap(); + g1.finish().unwrap(); + let mut g2 = track.create_group(moq_net::group::Info { sequence: 2 }).unwrap(); + g2.write_frame(Timestamp::ZERO, &b"2a"[..]).unwrap(); + + let track = remote.track("audio").unwrap(); + let _feed = track.subscribe(None); + let mut group = tokio::time::timeout(TIMEOUT, track.fetch_group(1, None)) + .await + .unwrap() + .unwrap(); + let mut frames = Vec::new(); + loop { + match tokio::time::timeout(TIMEOUT, group.read_frame()) + .await + .expect("group 1 hangs") + { + Ok(Some(frame)) => frames.push(frame.payload), + Ok(None) => break, + Err(err) => panic!("group 1 failed: {err}"), + } + } + assert_eq!(frames, vec![&b"1a"[..], &b"1b"[..]]); +}