diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 2a28ef5afe6..201522fd9a8 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -1575,6 +1575,9 @@ struct Alive { // the group lock is released and before the track is told: a track scan reading a stale // false judges the group frameless until that wake re-judges it. started: AtomicBool, + // Set once a final drop of this group waits on this copy, a fetched head still filling: + // an abort before it finishes makes the drop the group's cut, so cut watchers hear it. + carries_drop: AtomicBool, // The cache stamp `GroupState::charge` maintains, held here as well so the // eviction and expiry walks can weigh a candidate without taking the group lock // they already hold the track lock over. @@ -1712,6 +1715,7 @@ impl Producer { cut: OnceLock::new(), checked: AtomicBool::new(false), started: AtomicBool::new(false), + carries_drop: AtomicBool::new(false), access, log, pace, @@ -2804,6 +2808,7 @@ impl Handle { if cut { let _ = self.alive.cut.set(err.clone()); } + let finished = guard.outcome.frames().is_some(); guard.outcome = match guard.outcome { Outcome::Finished(frames) => Outcome::Released(frames, err), _ => Outcome::Aborted(err), @@ -2813,7 +2818,23 @@ impl Handle { self.alive.fall.store(fall as u8, Ordering::Release); guard.release(salvage); guard.close(); - self.cache.aborted(self.sequence, cut); + // Pairs with the fence after the track's `give_up` ended the live copy: either it sees + // this abort, or this sees its flag and that copy ended, so the drop riding on this + // copy is announced once it is the cut. Cache policy announces nothing, as it cuts + // nothing: it runs under the track guard, which a woken cursor would take. The fence + // runs only on an abort that could carry a drop, so a released group pays nothing. + let carried = !finished && !policy && { + std::sync::atomic::fence(Ordering::SeqCst); + self.alive.carries_drop.load(Ordering::Relaxed) + }; + self.cache.aborted(self.sequence, cut || carried); + } + + /// Mark that a final drop of this group waits on this copy, so a failure before it + /// finishes is announced to cut watchers as a cut would be. An abort racing this is the + /// caller's to announce; see the fence in [`Self::commit_abort`]. + pub(super) fn carry_drop(&self) { + self.alive.carries_drop.store(true, Ordering::Relaxed); } /// Whether the group has been aborted (including pool eviction). The track's diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index cdc483b1c05..a93dce1acb1 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -811,6 +811,11 @@ struct Slot { // so an abandoned fetch leaves the group servable rather than gone. Boxed: rare. head: Option>, + // A final drop of this group while a fetched head still filling served it: the head wins + // if it finishes, and the drop is why the group is gone if it dies first. Kept here rather + // than in the track's drops, so no drop sits beside a group readers are handed. + dropped: Option, + // A cut on an ended track that outlived its expiry: readers no longer get it, as they // no longer get the group it was. Atomic because an ended track's sweep holds only a // read guard, so it cannot reclaim the slot. @@ -846,11 +851,24 @@ impl Slot { /// serves its end: a reader is handed the group to see that error, never a missing group. fn cut(&self) -> Option<&Error> { match self.visible && !self.serves_end() && !self.retired.load(Ordering::Relaxed) { - true => self.group.cut_error(), + true => self.verdict(), false => None, } } + /// Why the publisher gave this group up, whichever copy serves it: the live copy's cut, + /// or a final drop that a fetched head outlived only while it filled, once the head + /// failed. A copy that finished won over the drop, even once freed, and a head the cache + /// let go ages out like any group, as the slot it goes with does. + fn verdict(&self) -> Option<&Error> { + self.group.cut_error().or_else(|| { + self.dropped.as_ref().filter(|_| { + !self.copies().any(group::Handle::is_settled) + && self.head.as_deref().is_some_and(|head| head.fate().failed()) + }) + }) + } + /// Whether a cursor hands this slot out: walked, or cut. fn is_read(&self) -> bool { self.is_walked() || self.cut().is_some() @@ -1585,7 +1603,7 @@ impl TrackState { fn reclaim_aborted(&mut self, sequence: u64) { if let Some(slot) = self.uncache(sequence) && slot.visible - && let Some(err) = slot.group.cut_error() + && let Some(err) = slot.verdict() && let Some(end) = sequence.checked_add(1) { self.drop_groups(sequence..end, Hole::drop(StreamError::from(err))); @@ -2731,6 +2749,7 @@ impl TrackState { visible, arrived: now, head: None, + dropped: None, retired: AtomicBool::new(false), }, ); @@ -3698,6 +3717,10 @@ impl TrackState { if !protected { group.handle().cache_demote(); } + // A replacing head inherits the drop the slot holds, announcing it if it dies. + if slot.dropped.is_some() { + group.handle().carry_drop(); + } if let Some(head) = slot.head.replace(Box::new(group.handle())) { self.detach(&head); } @@ -4450,7 +4473,8 @@ impl Producer { /// leaves a skipped sequence skipped, and a group a copy still serves whole, since a group /// stream wins over its drop: a stale drop leaves it still arriving or not, and a final one /// ends the live copy still arriving but leaves a group finished here or served by a - /// fetched head. + /// fetched head. A head still filling holds that final drop until it finishes, which + /// wins, or dies, which leaves the drop as the slot's cut. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn give_up(&self, groups: ops::Range, hole: Hole) -> RangeSet { let error = match &hole { @@ -4467,13 +4491,14 @@ impl Producer { if groups.is_empty() { return RangeSet::new(); } - let (runs, open): (_, Vec) = match self.modify() { + let (runs, open, carried, cache): (_, Vec, Vec, _) = match self.modify() { Ok(mut state) => { let stale = hole.stale_budget().is_some(); let mut left = RangeSet::new(); left.insert(groups.clone()); let mut open = Vec::new(); - for (&sequence, slot) in state.lookup.range(groups) { + let mut carried = Vec::new(); + for (&sequence, slot) in state.lookup.range_mut(groups) { if !slot.visible { continue; } @@ -4493,6 +4518,18 @@ impl Producer { && let Some(end) = sequence.checked_add(1) { left.remove(sequence..end); + // A fetched head still filling may yet die, leaving the live copy this + // drop ended: the slot keeps the drop to surface as its cut then, and + // the head announces its death to cut watchers. A copy that finished + // already won. + if !stale + && !slot.copies().any(group::Handle::is_settled) + && let Some(head) = slot.head.as_deref() + { + slot.dropped = Some(Error::Stream(error.clone())); + head.carry_drop(); + carried.push(sequence); + } } } let mut runs = RangeSet::new(); @@ -4500,7 +4537,8 @@ impl Producer { runs.extend(state.drop_groups(run.clone(), hole.clone()).iter().cloned()); } state.charge_debt(); - (runs, open) + let cache = state.cache.clone(); + (runs, open, carried, cache) } Err(_) => return RangeSet::new(), }; @@ -4509,6 +4547,23 @@ impl Producer { for group in open { group.abort_open(Error::Stream(error.clone())); } + // A head, or one replacing it, that died before the live copy above ended announced its + // drop while that copy still served, so a cursor may have passed it by: announce every + // drop the slot now shows as its cut. Pairs with the fence in a head's abort: a head + // this misses announces after it, with the live copy ended. + if !carried.is_empty() { + std::sync::atomic::fence(std::sync::atomic::Ordering::SeqCst); + let cut: Vec = { + let state = self.state.read(); + carried + .into_iter() + .filter(|sequence| state.lookup.get(sequence).and_then(Slot::cut).is_some()) + .collect() + }; + for sequence in cut { + cache.aborted(sequence, true); + } + } runs } @@ -7818,12 +7873,13 @@ impl DropCursor { } } - /// Record `slot`, cached at `sequence`, if it holds a cut no fetched copy covers: a - /// copy still serving the group's end delivers it, so it is never also dropped. + /// Record `slot`, cached at `sequence`, if it holds a cut no fetched copy covers, or a + /// final drop a fetched head died before winning over: a copy still serving the group's + /// end delivers it, so it is never also dropped. fn cut(record: &mut Record, sequence: u64, slot: &Slot) { if slot.visible && !slot.serves_end() - && let Some(err) = slot.group.cut_error() + && let Some(err) = slot.verdict() && let Some(end) = sequence.checked_add(1) { record @@ -10999,6 +11055,114 @@ mod test { } } + /// A live group 0 fed from frame 1, plus a fetched head still arriving that serves it + /// whole. + #[cfg(feature = "async")] + async fn arriving_head(producer: &Producer) -> (group::Producer, group::Producer) { + let dynamic = producer.requests(); + let consumer = producer.consume(); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(1).unwrap(); + live.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"delta")) + .unwrap(); + let _fetch = consumer.fetch_group(0, None); + let request = dynamic.requested_fill_group().await.unwrap(); + let mut head = request.accept(Info::timed()).unwrap(); + head.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"snapshot")) + .unwrap(); + (live, head) + } + + /// A final drop of a group a fetched head is still filling waits on the head: once the + /// head dies unfinished, the drop is why the group is gone, so a reader starting there + /// sees it rather than stall, even for a drop that abandons the live copy without a cut. + #[kio::test] + #[cfg(feature = "async")] + async fn a_final_drop_outlives_a_head_that_dies() { + let producer = track_producer("test", Info::timed()); + let (live, head) = arriving_head(&producer).await; + + producer.drop_groups(0..1, StreamError::Old); + assert!(live.is_aborted(), "the live copy still arriving ends"); + { + let state = producer.state.read(); + assert!(state.drops.iter().next().is_none(), "the head still serves the group"); + assert!(state.given_up(0).is_none()); + } + + head.abort(Error::Cancel).unwrap(); + let state = producer.state.read(); + assert!(state.lookup[&0].is_read(), "a reader is handed the drop"); + assert!(matches!(state.given_up(0), Some(Error::Stream(StreamError::Old)))); + } + + /// A drop cursor that scanned while the head filled hears the drop once the head dies: + /// the head's abort is announced as the cut it leaves. + #[kio::test] + #[cfg(feature = "async")] + async fn a_drop_cursor_hears_a_final_drop_a_dying_head_leaves() { + let producer = track_producer("test", Info::timed()); + let (_live, head) = arriving_head(&producer).await; + let mut sub = producer.subscribe(None); + + producer.drop_groups(0..1, StreamError::Old); + assert!(sub.poll_drop(&kio::Waiter::noop()).is_pending()); + + head.abort(Error::Cancel).unwrap(); + match sub.poll_drop(&kio::Waiter::noop()) { + Poll::Ready((run, hole)) => { + assert_eq!(run, 0..1); + assert!(matches!(hole.error(), StreamError::Old)); + } + Poll::Pending => panic!("the drop the head left is reported"), + } + } + + /// A live copy that finished wins over a final drop for good, even while a fetched head + /// still fills, and even once both copies are gone. + #[kio::test] + #[cfg(feature = "async")] + async fn a_finished_live_copy_wins_over_a_final_drop_a_head_waits_on() { + let producer = track_producer("test", Info::timed()); + let (live, head) = arriving_head(&producer).await; + let live = live.finish().unwrap(); + + producer.drop_groups(0..1, StreamError::Old); + head.abort(Error::Cancel).unwrap(); + live.abort(Error::Evicted).unwrap(); + let state = producer.state.read(); + assert!(state.drops.iter().next().is_none()); + assert!(state.given_up(0).is_none()); + } + + /// A fetched head that finishes after a final drop of its group wins over the drop, even + /// once it is later freed: no drop is ever recorded beside it. + #[kio::test] + #[cfg(feature = "async")] + async fn a_head_that_finishes_wins_over_a_final_drop() { + let producer = track_producer("test", Info::timed()); + let (_live, mut head) = arriving_head(&producer).await; + + producer.drop_groups(0..1, StreamError::Old); + head.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"delta")) + .unwrap(); + let head = head.finish().unwrap(); + { + let state = producer.state.read(); + assert!(state.drops.iter().next().is_none()); + assert!(state.given_up(0).is_none()); + } + let mut sub = producer.subscribe(None); + assert!(sub.poll_drop(&kio::Waiter::noop()).is_pending()); + assert_eq!(sub.assert_group().sequence, 0); + + head.abort(Error::Evicted).unwrap(); + assert!( + producer.state.read().given_up(0).is_none(), + "a freed head was still whole" + ); + } + /// Let `duration` pass on the cache pool `producer`'s groups charge into. fn elapse(producer: &Producer, duration: Duration) { producer.broadcast.pool.step(duration); diff --git a/rs/moq-net/verify/subscribe/SubscribeAccounting.tla b/rs/moq-net/verify/subscribe/SubscribeAccounting.tla index f57caf80bd2..58e328d5fc0 100644 --- a/rs/moq-net/verify/subscribe/SubscribeAccounting.tla +++ b/rs/moq-net/verify/subscribe/SubscribeAccounting.tla @@ -344,7 +344,9 @@ TailFold(g) == \* copy unless its stream finished under this update or the copy holds it \* (`give_up` keeps an open group), and a final drop records it and ends its readers \* unless the copy holds it whole (`give_up` keeps a group a finished copy serves, -\* the live one or a fetched head). +\* the live one or a fetched head). A fetched head still filling, which this model +\* leaves out, holds the drop in its slot until it finishes ("whole" here) or dies, +\* when the drop is the slot's cut (`Slot::verdict`): never lost, never beside a group. \* A stale drop of a group inside the span a fold forgot that the ledger already \* accounted (`Tail::overtaken`) may be one a finished stream overtook, so the next \* update owes it nothing again.