Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions rs/moq-net/src/model/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<group::Consumer> {
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()
})
}
Expand Down Expand Up @@ -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<u64>) -> Poll<Result<Option<(u64, track::Consumer)>>> {
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()))
};
Expand Down Expand Up @@ -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)))) => {
Expand Down
7 changes: 6 additions & 1 deletion rs/moq-net/src/model/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<group::Consumer> {
/// 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<group::Consumer> {
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())
}
Expand Down
93 changes: 93 additions & 0 deletions rs/moq-net/tests/idle_cancel_refetch.rs
Original file line number Diff line number Diff line change
@@ -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).

Comment thread
kixelated marked this conversation as resolved.
Resolved
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::<Version>().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"[..]]);
}
Loading