From df27928815b60318347571c2003b50ec6ea6e520 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 28 Sep 2026 10:40:23 -0700 Subject: [PATCH] fix(net): keep an aborted track's finished groups, expire ended tracks An abort, or a producer dropped without finish(), cleared the whole cache, so a slower reader lost groups that had fully arrived and the track ended short of them. Keep the finished groups and drop only the open ones: readers drain what finished, then see the abort (or the clean end, when the declared end had settled). Nothing pinned by a stale consumer lingers: once no producer remains, the latest group loses its protection, and the pool sweep reaches ended tracks through the new kio::Weak::read, since their closed channel refuses an upgrade. Previously a sealed track's groups were never swept at all. Co-Authored-By: Claude Opus 5.5 --- rs/kio/src/weak.rs | 24 +++ rs/moq-json/src/snapshot/mod.rs | 7 +- rs/moq-net/src/model/cache.rs | 24 ++- rs/moq-net/src/model/track.rs | 274 +++++++++++++++++++++++--------- 4 files changed, 247 insertions(+), 82 deletions(-) diff --git a/rs/kio/src/weak.rs b/rs/kio/src/weak.rs index 13d1beda51e..f24ae59d65d 100644 --- a/rs/kio/src/weak.rs +++ b/rs/kio/src/weak.rs @@ -48,6 +48,14 @@ impl Weak { } .produce() } + + /// Read the state while another handle keeps it allocated, even once the channel + /// closed. Counts as neither a producer nor a consumer. `None` once it was dropped. + pub fn read(&self, f: impl FnOnce(&T) -> R) -> Option { + let state = self.state.upgrade()?; + let state = Ref { state: state.lock() }; + Some(f(&state)) + } } impl Default for Weak { @@ -405,6 +413,22 @@ mod test { assert!(weak.upgrade().is_none()); } + /// A closed channel stays readable through the weak handle while any handle keeps + /// it allocated, without that read reopening it or counting as demand. + #[test] + fn weak_reads_a_closed_channel() { + let producer = Producer::new(7u32); + let weak = producer.downgrade(); + let consumer = producer.consume(); + + drop(producer); + assert_eq!(weak.read(|value| *value), Some(7)); + assert!(weak.upgrade().is_none(), "reading does not reopen it"); + + drop(consumer); + assert_eq!(weak.read(|value| *value), None); + } + /// An upgrade racing the last producer's drop either loses (no handle) or wins /// (a handle that is genuinely open). It must never hand back a producer for a /// channel that the drop is about to close. diff --git a/rs/moq-json/src/snapshot/mod.rs b/rs/moq-json/src/snapshot/mod.rs index 8e2b8032dbf..9b86d81e8d4 100644 --- a/rs/moq-json/src/snapshot/mod.rs +++ b/rs/moq-json/src/snapshot/mod.rs @@ -599,11 +599,16 @@ mod test { *producer.modify().unwrap() = json!({ "big": "x".repeat(moq_net::group::MAX_CACHE_BYTES as usize + 1) }); - // The publisher learns the cause at its next edit, the consumer from the aborted track. + // The publisher learns the cause at its next edit. The consumer drains the snapshot + // that finished, then learns it from the aborted track. assert!(matches!( producer.modify(), Err(crate::Error::Net(moq_net::Error::FrameTooLarge)) )); + assert!(matches!( + subscriber.poll_next_group(&kio::Waiter::noop()), + Poll::Ready(Ok(Some(_))) + )); assert!(matches!( subscriber.poll_next_group(&kio::Waiter::noop()), Poll::Ready(Err(moq_net::Error::FrameTooLarge)) diff --git a/rs/moq-net/src/model/cache.rs b/rs/moq-net/src/model/cache.rs index 6bdb7cfe237..b4f85cea3c7 100644 --- a/rs/moq-net/src/model/cache.rs +++ b/rs/moq-net/src/model/cache.rs @@ -10,7 +10,7 @@ //! global eviction task. //! //! Cross-track ordering comes from one statistic: the mean last-access time of the -//! evictable population (every cached group except each track's protected latest). +//! evictable population (every cached group except each live track's protected latest). //! A group accessed more recently than that mean is never evicted, so freshly read //! or fetched content in one track can't die while another track holds staler //! content, and a track @@ -18,9 +18,10 @@ //! old entries and inserting new ones both advance the mean, so the eviction //! frontier moves with cache turnover on its own. //! -//! The pool also owns the wall-clock LRU window ([`Pool::expiry`]): a non-latest -//! group that nobody has read or written for that long is reclaimed, no matter what -//! retention its track advertises. Track retention +//! The pool also owns the wall-clock LRU window ([`Pool::expiry`]): a group that +//! nobody has read or written for that long is reclaimed, no matter what retention +//! its track advertises. Only a live track's latest group is exempt: once a track +//! ends, a stale consumer can't pin any of it. Track retention //! ([`max_age`](crate::track::Info::max_age)) is measured in media timestamps, so a //! congestion stall can't age content out; the pool's expiry is the orthogonal //! wall-clock bound that keeps unwatched content from pinning RAM. @@ -101,8 +102,8 @@ impl Config { /// Set the wall-clock LRU window. `None` disables idle reclamation. /// - /// A non-latest cached group that nobody reads or writes for this long is - /// reclaimed, surfacing to any remaining reader as + /// A cached group (other than a live track's latest) that nobody reads or writes + /// for this long is reclaimed, surfacing to any remaining reader as /// [`Error::Old`](crate::Error::Old). This is independent of track retention: /// [`max_age`](crate::track::Info::max_age) uses media timestamps, while this /// window keeps idle content from pinning memory. Reclaiming without a write behind @@ -269,7 +270,7 @@ impl Pool { /// Call after polling and at the returned deadline, including when idle. /// Calls before that deadline only advance the pool's sampled clock. A due /// pass visits every cached group, dating accesses since the last pass and - /// reclaiming idle groups except each track's latest. Delayed calls extend + /// reclaiming idle groups except each live track's latest. Delayed calls extend /// retention. Shared pools use the latest supplied instant. /// /// `None` means both cache policies are disabled. After enabling a capacity @@ -564,7 +565,14 @@ impl Track { } // Counts as a producer while it lives, which is why `track::Producer` gates // its teardown on its own clone count rather than the state's. - let Some(state) = self.state.upgrade() else { return }; + let Some(state) = self.state.upgrade() else { + // An ended track's channel is closed, but a stale consumer can still hold its + // groups: the sweep expires them in place, or they would never go. + if full && scan_expiry { + self.state.read(|state| state.expire_closed(state.expiry_scan_drain())); + } + return; + }; let expiry = if scan_expiry { let state = state.read(); let scan = if full { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 9ad9737a221..e15fd8786af 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -207,9 +207,10 @@ pub(crate) struct TrackState { max_sequence: Option, // The sequence of the newest cached group: the live edge, protected from - // eviction by never entering the eviction order. Tracked separately from - // `max_sequence` because datagrams advance that shared counter, and the live - // edge must still demote correctly when the next group lands past one. + // eviction by never entering the eviction order until the track is `closed`. + // Tracked separately from `max_sequence` because datagrams advance that shared + // counter, and the live edge must still demote correctly when the next group + // lands past one. latest_group: Option, // Incarnation counter for `Slot::stamp`. @@ -222,6 +223,11 @@ pub(crate) struct TrackState { // below it will never be produced. sealed: bool, + // No producer remains (aborted, sealed, or dropped), so nothing protects the live + // edge any longer: the pool's idle expiry reclaims it like every other group, and a + // stale consumer cannot pin the cache. + closed: bool, + // The first sequence the live feed serves, once the publisher declared one // (the wire's SUBSCRIBE_START). Lower groups never arrive on their own; a // fetch can still create them. @@ -233,7 +239,7 @@ pub(crate) struct TrackState { // [`Consumer::poll_start`]) wait on it; everything else treats the floor as usual. start_pending: bool, - // Where production stopped, snapshotted when the cached groups are released (an + // Where production stopped, snapshotted when the open groups are released (an // abort, or the last producer dropping). Computed live from the cache otherwise; // see [`Self::resume_position`]. resume: Option, @@ -411,29 +417,26 @@ impl TrackState { next_sequence: u64, end_sequence: Option, ) -> Poll>> { - // An abort before the end settled cut the track off. One after it is a clean - // end (see `is_complete`): the cached groups were everything the end promised, - // so the cursor ends as `poll_recv_group` does, not with the abort. - if let Some(err) = &self.abort - && !self.settled - { - return Poll::Ready(Err(err.clone())); - } - // Nothing more can arrive once the last producer is gone: the track was sealed - // (dropped with its end declared) or aborted with that end settled. Either closed - // the channel, so parking is not an option: the consumer would be handed the - // abort, or `Dropped`, instead of the end it reached. + // Nothing more can arrive once the track was sealed (dropped with its end + // declared) or aborted. Either closed the channel, so parking is not an option: + // the consumer would be handed the abort, or `Dropped`, instead of how it ended. let closed = self.sealed || self.abort.is_some(); + // Once nothing more is in range: an abort before the end settled cut the track + // off. One after it is a clean end (see `is_complete`), as is reaching the end. + let end = || match &self.abort { + Some(err) if !self.settled => Err(err.clone()), + _ => Ok(None), + }; // If the exclusive end is already at or below where we'd resume, no // group can ever satisfy this call until the cap rises. Pending (not // None) so the consumer is parked rather than told the stream is over. // An empty range (`end == 0`) parks even at the first sequence. - if let Some(end) = end_sequence - && end <= next_sequence + if let Some(cap) = end_sequence + && cap <= next_sequence { if closed { - return Poll::Ready(Ok(None)); + return Poll::Ready(end()); } return Poll::Pending; } @@ -458,9 +461,10 @@ impl TrackState { // floor is already at/past it, nothing else can land in range. // A closed track produces nothing more either, so a gap below its // boundary is the end, not a wait. Cached in-range groups were - // returned above. This is the cursor the ordered and spliced readers use. + // returned above, so an aborted track drains what finished first. + // This is the cursor the ordered and spliced readers use. if closed || self.final_sequence.is_some_and(|fin| next_sequence >= fin) { - return Poll::Ready(Ok(None)); + return Poll::Ready(end()); } Poll::Pending } @@ -732,7 +736,7 @@ impl TrackState { continue; } if slot.group.is_aborted() - || (Some(sequence) != self.latest_group + || (!self.protects(sequence) && slot .group .cache_accessed_tick(scan.gc.then_some(scan.now)) @@ -757,6 +761,26 @@ impl TrackState { || self.evict.len() > 2 * self.lookup.len() + EVICT_SLACK } + /// Expire an ended track's idle groups, whose closed channel refuses the write + /// [`Self::evict_expired_scan`] takes: abort them in place, releasing their frames, + /// and leave the slots, which every read path already skips. + pub(super) fn expire_closed(&self, scan: ExpiryScan) { + for (sequence, stamp) in &self.evict { + let Some(slot) = self.lookup.get(sequence) else { + continue; + }; + if slot.stamp == *stamp + && !slot.group.is_aborted() + && slot + .group + .cache_accessed_tick(scan.gc.then_some(scan.now)) + .is_some_and(|tick| scan.now.saturating_sub(tick) > scan.max_ticks) + { + let _ = slot.group.clone().abort(Error::Old); + } + } + } + /// Apply a scan previously selected by [`Self::expiry_scan`] or /// [`Self::expiry_scan_drain`]. pub(super) fn evict_expired_scan(&mut self, scan: ExpiryScan) { @@ -779,7 +803,7 @@ impl TrackState { self.lookup.remove(&sequence); continue; } - if Some(sequence) == self.latest_group + if self.protects(sequence) || slot .group .cache_accessed_tick(scan.gc.then_some(scan.now)) @@ -833,14 +857,32 @@ impl TrackState { self.lookup.get(&sequence).is_some_and(|slot| slot.stamp == stamp) } - /// Drop every cached group and reset the eviction bookkeeping. Each group's - /// access sample lives in its own charge, released when the group itself dies. - fn clear_cache(&mut self) { - self.lookup.clear(); - self.arrival.clear(); - self.evict.clear(); - self.latest_group = None; - self.debt = 0; + /// Whether `sequence` is the live edge, which eviction and expiry never take + /// while a producer remains. + fn protects(&self, sequence: u64) -> bool { + !self.closed && Some(sequence) == self.latest_group + } + + /// No producer remains: stop protecting the live edge, so the pool's idle expiry + /// reclaims every group once readers stop touching it, and a stale consumer can't + /// pin the cache (and its frame buffers) forever. + fn close_cache(&mut self) { + if std::mem::replace(&mut self.closed, true) { + return; + } + if let Some(latest) = self.latest_group + && let Some(slot) = self.lookup.get(&latest) + { + slot.group.cache_demote(); + self.evict.push_back((latest, slot.stamp)); + } + } + + /// Drop the open groups nobody will finish now that the track ended abruptly, and + /// keep the finished ones for readers still draining. A consumer that already + /// pulled an open group keeps its own handle and ends with it. + fn drop_open_groups(&mut self) { + self.lookup.retain(|_, slot| slot.group.is_finished()); } /// Attach `info` to this track, clamping the publisher's window down to the @@ -919,7 +961,8 @@ impl TrackState { self.evict.push_back((latest, prev.stamp)); } self.latest_group = Some(sequence); - } else { + } + if !self.protects(sequence) { group.cache_demote(); self.evict.push_back((sequence, stamp)); } @@ -1032,7 +1075,7 @@ impl TrackState { self.lookup.remove(&sequence); continue; } - if Some(sequence) == self.latest_group { + if self.protects(sequence) { // The live edge is never enqueued, but tolerate finding it anyway. self.evict.push_back((sequence, stamp)); continue; @@ -1118,8 +1161,8 @@ impl TrackState { /// /// `None` while the track has produced nothing, which is an unbounded takeover. fn resume_position(&self) -> Option { - // A snapshot taken when the cache was released wins; the groups it was derived - // from are gone. + // A snapshot taken when the open groups were released wins; the group it was + // derived from may be gone. if self.resume.is_some() { return self.resume; } @@ -1199,20 +1242,17 @@ impl TrackState { /// Record `err` and close the track: the shared tail of [`Producer::abort`] and /// [`Producer::abort_unused`]. fn commit_abort(mut state: kio::Mut<'_, TrackState>, err: Error) { - // Snapshot the frame boundary before the cache it's derived from goes away: an + // Snapshot the frame boundary before the open group it may sit in goes away: an // abort is exactly when a replacement route asks where to resume. state.resume = state.resume_position(); - // Decided before the cache goes: the groups below the end are the evidence. + // Decided before the open groups go: the groups below the end are the evidence. state.settled = state.is_settled(); state.abort = Some(err); - // A settled end is a clean end: the track holds every group it promised, so keep - // them for consumers still draining, as `finish()` does. Clearing here would abort - // the finished groups a slower reader has not pulled yet, and it would then see the - // track end short of them with no error. - if !state.settled { - state.clear_cache(); - state.datagrams.clear(); - } + // Keep what finished for consumers still draining: they get it, then the abort (or + // the clean end, when the end settled). Clearing here would abort finished groups a + // slower reader has not pulled yet. The pool's idle expiry bounds how long they stay. + state.drop_open_groups(); + state.close_cache(); state.close(); } @@ -1516,16 +1556,15 @@ impl Producer { /// Abort the track with the given error. /// - /// Consumes the handle, since nothing can be written to an aborted track. Drops the - /// cached groups so a stale [`Consumer`] can't pin them (and their frame buffers) in - /// memory forever. Consumers that haven't drained yet surface the abort error instead - /// of the leftover cache. Child groups are independent: a consumer that already pulled - /// a [`group::Consumer`] keeps its own handle and can finish reading it. + /// Consumes the handle, since nothing can be written to an aborted track. Consumers + /// still draining get the finished groups, then the abort error. Open groups leave the + /// cache; a consumer that already pulled one keeps its own handle and ends with it. + /// The pool's idle expiry (see [`cache::Config::with_expiry`]) reclaims the rest, the + /// latest group included, so a stale [`Consumer`] can't pin them in memory forever. /// - /// Unless the declared end had settled: the final sequence from - /// [`finish_at`](Self::finish_at) was reached and every group below it finished. The - /// track then already holds everything it promised, so it ends cleanly and keeps the - /// cached groups for consumers still draining, as [`finish`](Self::finish) does. + /// If the declared end had settled (the final sequence from + /// [`finish_at`](Self::finish_at) was reached and every group below it finished), + /// consumers get a clean end instead of the error, as after [`finish`](Self::finish). /// /// [`finish`](Self::finish) is deliberately not terminal: it declares the final /// sequence, and lower-numbered groups may still be written afterwards. @@ -1944,10 +1983,10 @@ impl Drop for Alive { if !self.published.load(Ordering::Relaxed) { return; } - // The last producer going away without finishing is an abrupt teardown: - // release the cached groups so a stale consumer can't pin them (and their - // frame buffers) forever, the same as an explicit abort. A cleanly - // finished track keeps its cache so consumers can still drain it. + // The last producer going away ends the track: nothing protects its live edge + // anymore, so the pool's idle expiry reclaims what a stale consumer would pin. + // Without a finish it is an abrupt teardown, the same as an explicit abort: + // the open groups go and the finished ones stay for readers still draining. // `abort()` closes the channel, so `write()` returns `Err(Ref)`. `finish()` // leaves it open with `final_sequence` set, so inspect both outcomes. match self.state.write() { @@ -1956,6 +1995,7 @@ impl Drop for Alive { // Groups still missing below the boundary can no longer arrive, so a // reader waiting on one ends cleanly instead of with `Dropped`. state.sealed = true; + state.close_cache(); return; } if state.abort.is_some() { @@ -1965,10 +2005,10 @@ impl Drop for Alive { track = %self.name, "track::Producer dropped without finish() or abort()" ); - // See `abort`: keep the frame boundary once its groups go away. + // See `abort`: keep the frame boundary once its open group goes away. state.resume = state.resume_position(); - state.clear_cache(); - state.datagrams.clear(); + state.drop_open_groups(); + state.close_cache(); } Err(state) => { if state.final_sequence.is_some() || state.abort.is_some() { @@ -6693,25 +6733,21 @@ mod test { } #[tokio::test] - async fn abort_clears_cached_groups() { + async fn abort_drops_open_groups() { let producer = track_producer("test", None); producer.append_group().unwrap(); producer.append_group().unwrap(); - // A stale consumer that never drains must not pin the cached groups. let mut consumer = producer.subscribe(None); assert_eq!(live_groups(&producer.state.read()), 2); producer.clone().abort(Error::Cancel).unwrap(); - { - let state = producer.state.read(); - assert!(state.lookup.is_empty(), "cached groups should be dropped on abort"); - assert!(state.arrival.is_empty()); - assert!(state.evict.is_empty()); - } - - // The consumer now surfaces the abort error rather than the leftover cache. + // Nobody will finish them, so they leave the cache rather than park a reader. + assert!( + producer.state.read().lookup.is_empty(), + "open groups are dropped on abort" + ); let result = consumer.recv_group().now_or_never().expect("should not block"); assert!(matches!(result, Err(Error::Cancel))); } @@ -6956,6 +6992,93 @@ mod test { assert!(matches!(res, Err(Error::Timeout))); } + /// An abort short of the end keeps the groups that finished for a reader that has not + /// pulled them yet: it gets them, then the abort. The open group nobody will finish + /// is gone, on both cursors. + #[tokio::test] + async fn abort_keeps_finished_groups_for_a_slow_reader() { + let producer = track_producer("test", None); + let mut arrival = producer.subscribe(None); + let mut ordered = producer.subscribe(None).ordered(); + + for sequence in 0..2 { + producer + .create_group(group::Info { sequence }) + .unwrap() + .finish() + .unwrap(); + } + let _open = producer.create_group(group::Info { sequence: 2 }).unwrap(); + producer.abort(Error::Timeout).unwrap(); + + assert_eq!(arrival.assert_group().sequence, 0); + assert_eq!(arrival.assert_group().sequence, 1); + let res = arrival.recv_group().now_or_never().expect("should not block"); + assert!(matches!(res, Err(Error::Timeout)), "expected the abort"); + + assert_eq!(drain_ordered(&mut ordered), [0, 1]); + let res = ordered + .next_group() + .now_or_never() + .expect("should not block") + .map(|group| group.map(|group| group.sequence)); + assert!(matches!(res, Err(Error::Timeout)), "expected the abort, got {res:?}"); + } + + /// The last producer dropping without a finish keeps the finished groups the same way. + #[tokio::test] + async fn dropped_producer_keeps_finished_groups_for_a_slow_reader() { + let producer = track_producer("test", None); + let mut consumer = producer.subscribe(None); + producer + .create_group(group::Info { sequence: 0 }) + .unwrap() + .finish() + .unwrap(); + drop(producer); + + assert_eq!(consumer.assert_group().sequence, 0); + let res = consumer.recv_group().now_or_never().expect("should not block"); + assert!(matches!(res, Err(Error::Dropped)), "expected the drop"); + } + + /// A closed track's groups, its latest included, expire once idle, so a stale + /// consumer cannot pin them. A live track's latest stays protected. + #[tokio::test] + async fn closed_track_expires_its_latest_group() { + let pool = cache::Pool::new(cache::Config::default().with_expiry(Duration::from_secs(1))); + + let live = track_producer_pooled("live", pool.clone()); + live.append_group().unwrap().finish().unwrap(); + + let aborted = track_producer_pooled("aborted", pool.clone()); + aborted.append_group().unwrap().finish().unwrap(); + let stale_aborted = aborted.consume(); + aborted.abort(Error::Timeout).unwrap(); + + let finished = track_producer_pooled("finished", pool.clone()); + finished.append_group().unwrap().finish().unwrap(); + finished.finish().unwrap(); + let stale_finished = finished.consume(); + drop(finished); + + // The first pass dates the activity it has not seen yet; the next one expires it. + for _ in 0..2 { + crate::model::clock::advance(Duration::from_secs(2)); + pool.sweep(); + } + + assert!(live.consume().peek_group(0).is_some(), "a live track keeps its latest"); + assert!( + stale_aborted.peek_group(0).is_none(), + "an aborted track's latest expired" + ); + assert!( + stale_finished.peek_group(0).is_none(), + "a sealed track's latest expired" + ); + } + /// An abort after every group below the declared end finished leaves the end standing. #[tokio::test] async fn abort_after_the_end_settles_ends_clean() { @@ -8310,8 +8433,13 @@ mod test { /// surviving publisher, or an abrupt drop silently behaves like a clean finish. #[tokio::test] async fn teardown_ignores_a_settling_group() { - let (mut producer, pool) = pooled_producer(1 << 40); - finished_group(&mut producer, 100); + let (producer, pool) = pooled_producer(1 << 40); + // Open, so only the abrupt teardown releases it. + let mut group = producer.append_group().unwrap(); + group + .write_frame(Timestamp::ZERO, bytes::Bytes::from(vec![0u8; 100])) + .unwrap(); + drop(group); // Stand in for a concurrent `cache::Track::settle`, mid-upgrade. let settling = producer.state.downgrade().upgrade().expect("open");