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
4 changes: 4 additions & 0 deletions rs/moq-net/src/model/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@ impl Consumer {
if self.expired {
return res;
}
// Read to its end: no route is needed for it any more.
if matches!(res, Poll::Ready(Ok(_))) && self.read_idx >= self.size() {
self.recover = None;
}
let Some(recover) = self.recover.as_mut() else {
return res;
};
Expand Down
67 changes: 56 additions & 11 deletions rs/moq-net/src/model/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,19 @@ impl Consumer {
Subscriber { reader, subscription }
}

/// Fetch one group from whichever route serves the track.
pub(super) fn fetch_group(&self, sequence: u64, options: group::Fetch) -> Fetching {
/// Fetch one group from whichever route serves the track. `logical` is the logical
/// track's state, which the fetched group holds while it is read.
pub(super) fn fetch_group(
&self,
sequence: u64,
options: group::Fetch,
logical: kio::Consumer<track::TrackState>,
) -> Fetching {
Fetching {
route: self.clone(),
sequence,
options,
logical,
pending: None,
}
}
Expand Down Expand Up @@ -590,6 +597,7 @@ impl Reader {
generation: copy.generation,
copy: copy.track.clone(),
lease: Some(copy.lease.clone()),
logical: None,
fetch: None,
})
}
Expand Down Expand Up @@ -878,6 +886,10 @@ pub(super) struct Recover {
copy: track::Consumer,
/// Keeps a replaced copy subscribed while this group is read from it.
lease: Option<Arc<()>>,
/// A fetch's logical track, held while the group is read. A fetch is no subscription,
/// so without it the last subscriber leaving parks the track, dropping the copy that
/// still feeds an open group, and the rest of the group never comes.
logical: Option<kio::Consumer<track::TrackState>>,
/// A fetch of the rest of the group from the serving route, by its generation.
fetch: Option<(u64, Fetch)>,
}
Expand All @@ -898,6 +910,7 @@ impl Clone for Recover {
generation: self.generation,
copy: self.copy.clone(),
lease: self.lease.clone(),
logical: self.logical.clone(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Release logical demand after a cloned frame finishes

When a fetched group is consumed through poll_next_frame, this clone is installed on the returned frame::Consumer; however, frame::Consumer::poll_resumed returns on every successful read_all or terminal read_chunk without clearing its recovery state. If a caller retains that fully consumed frame handle, the cloned logical consumer therefore keeps TrackWeak used indefinitely, preventing the front from parking and retaining the upstream live subscription even though no unread payload remains. Release this logical demand when the frame payload completes while preserving it during partial-frame recovery.

AGENTS.md reference: rs/AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, reproduced, and fixed in bd61bb5. frame::Consumer::poll_resumed now drops its recovery state once the payload is read (read_idx >= size), matching how group::Consumer drops it on Ok(None); partial-frame recovery is unchanged. This also stops a retained frame from holding a replaced copy's lease. Regression: resume::test::a_read_frame_releases_the_track, which fails without the fix.

(Written by Claude Opus 5.5)

// A clone reads in parallel, so it asks for itself.
fetch: None,
}
Expand Down Expand Up @@ -1104,6 +1117,7 @@ pub(super) struct Fetching {
route: Consumer,
sequence: u64,
options: group::Fetch,
logical: kio::Consumer<track::TrackState>,
/// The fetch in flight, and the route generation it went to.
pending: Option<(u64, kio::Pending<track::Fetching>)>,
}
Expand Down Expand Up @@ -1152,6 +1166,7 @@ impl kio::Task for Fetching {
generation,
copy: serving.clone(),
lease: None,
logical: Some(self.logical.clone()),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Release logical demand after fetch expiry

When a routed fetch has a max_delay and the caller retains its returned group or frame after the open source stalls, the terminal expiry path leaves this cloned logical inside Recover: group::Consumer::poll_resumed returns early when expired/ended, and frame::Consumer::poll_resumed returns early when expired, before clearing recovery. Demand therefore remains used and keeps the upstream subscription alive indefinitely even though the read has already returned None or Error::Old. The successful-frame cleanup added here does not cover these expiry paths; clear the logical recovery state when expiry terminates the read.

AGENTS.md reference: rs/AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

fetch: None,
}))),
// The route failed: ask the next one, unless none will come.
Expand Down Expand Up @@ -2190,10 +2205,11 @@ mod test {
let a = copy();
let dynamic = a.dynamic();
routes.serve(a.consume());
let mut fetching = routes.consume().fetch_group(0, group::Fetch::default());
let logical = logical(&routes);
let mut fetching = logical.consume().fetch_group(0, None);
let wake = Arc::new(Wake::default());
let waiter = kio::Waiter::new(wake.clone().into());
assert!(kio::Task::poll(&mut fetching, &waiter).is_pending());
assert!(kio::Task::poll(&mut *fetching, &waiter).is_pending());
let _request = dynamic.requested_group().now_or_never().unwrap().unwrap();
wake.0.store(false, std::sync::atomic::Ordering::Relaxed);
let b = copy();
Expand All @@ -2202,7 +2218,7 @@ mod test {
group.finish().unwrap();
routes.serve(b.consume());
assert!(wake.0.load(std::sync::atomic::Ordering::Relaxed));
assert!(matches!(kio::Task::poll(&mut fetching, &waiter), Poll::Ready(Ok(_))));
assert!(matches!(kio::Task::poll(&mut *fetching, &waiter), Poll::Ready(Ok(_))));
}

#[test]
Expand Down Expand Up @@ -2425,7 +2441,9 @@ mod test {
let a = copy();
let _open = a.create_group(group::Info { sequence: 0 }).unwrap();
routes.serve(a.consume());
let mut reading = kio::Pending::new(routes.consume().fetch_group(0, group::Fetch::default()))
let mut reading = logical(&routes)
.consume()
.fetch_group(0, None)
.now_or_never()
.unwrap()
.unwrap();
Expand All @@ -2448,7 +2466,9 @@ mod test {

let mut arriving = a.create_group(group::Info { sequence: 0 }).unwrap();
arriving.write_frame(ts(0), b"a".as_ref()).unwrap();
let mut fetched = kio::Pending::new(routes.consume().fetch_group(0, group::Fetch::default()))
let mut fetched = logical(&routes)
.consume()
.fetch_group(0, None)
.now_or_never()
.unwrap()
.unwrap();
Expand All @@ -2467,6 +2487,28 @@ mod test {
assert_eq!(read(&mut fetched), None);
}

/// A fetched frame read to its end no longer holds the logical track, so a caller
/// keeping the handle does not stop the track from parking.
#[test]
#[cfg(feature = "async")]
fn a_read_frame_releases_the_track() {
let routes = Producer::new();
let a = copy();
routes.serve(a.consume());
let mut open = a.create_group(group::Info { sequence: 0 }).unwrap();
open.write_frame(ts(0), b"a".as_ref()).unwrap();

let logical = logical(&routes);
let demand = logical.demand();
let mut fetched = logical.consume().fetch_group(0, None).now_or_never().unwrap().unwrap();
assert!(demand.is_used(), "an open fetched group holds the track");
let mut frame = fetched.next_frame().now_or_never().unwrap().unwrap().unwrap();
drop(fetched);
assert!(demand.is_used(), "an unread frame holds the track");
assert_eq!(frame.read_all().now_or_never().unwrap().unwrap(), b"a".as_ref());
assert!(!demand.is_used(), "a read frame does not");
}

/// A rerouted fetch keeps its own age budget: the new route is asked with it, and a
/// read waiting on that route gives up once the group falls past it there.
#[test]
Expand All @@ -2480,7 +2522,9 @@ mod test {

let budget = Duration::from_millis(500);
let options = group::Fetch::default().with_max_delay(budget);
let mut fetched = kio::Pending::new(routes.consume().fetch_group(0, options))
let mut fetched = logical(&routes)
.consume()
.fetch_group(0, options)
.now_or_never()
.unwrap()
.unwrap();
Expand Down Expand Up @@ -2674,12 +2718,13 @@ mod test {
let a = copy();
let _dynamic = a.dynamic();
routes.serve(a.consume());
let mut fetching = routes.consume().fetch_group(0, group::Fetch::default());
let logical = logical(&routes);
let mut fetching = logical.consume().fetch_group(0, None);
let waiter = kio::Waiter::noop();
assert!(kio::Task::poll(&mut fetching, &waiter).is_pending());
assert!(kio::Task::poll(&mut *fetching, &waiter).is_pending());
routes.end(Err(Error::Unroutable));
assert!(matches!(
kio::Task::poll(&mut fetching, &waiter),
kio::Task::poll(&mut *fetching, &waiter),
Poll::Ready(Err(Error::Unroutable))
));
}
Expand Down
4 changes: 2 additions & 2 deletions rs/moq-net/src/model/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3761,7 +3761,7 @@ impl Consumer {
result: None,
hit: None,
stats: self.stats.clone(),
resume: Some(Box::new(resume.fetch_group(sequence, options))),
resume: Some(Box::new(resume.fetch_group(sequence, options, state.clone()))),
});
}

Expand Down Expand Up @@ -4248,7 +4248,7 @@ impl kio::Task for Fetching {
max_delay: self.max_delay,
frame_start: self.frame_start,
};
self.resume = Some(Box::new(routes.fetch_group(self.sequence, options)));
self.resume = Some(Box::new(routes.fetch_group(self.sequence, options, self.state.clone())));
}
if let Some(resume) = &mut self.resume {
let group = ready!(kio::Task::poll(&mut **resume, waiter))?;
Expand Down
Loading
Loading