diff --git a/rs/moq-net/src/model/idle.rs b/rs/moq-net/src/model/idle.rs index b3d008ea6c8..8ad171a3425 100644 --- a/rs/moq-net/src/model/idle.rs +++ b/rs/moq-net/src/model/idle.rs @@ -12,7 +12,9 @@ //! //! Gates nest: a read through a gate that polls another gated reader passes it the outer //! gate's waiter, whose waker stays the same across reads, so the inner gate keeps its arm -//! and its wake reaches the outer gate on the way to the task. +//! and its wake reaches the outer gate on the way to the task. A change made outside the +//! read, such as a drop poll hearing a fill's answer, reopens the gate instead of disarming +//! it, so that wake reaches the outer gates too. pub(super) use crate::util::Idle; diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 4a8f327b4cb..b769a437450 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -8434,6 +8434,8 @@ impl Cursor { /// The next run given up, judging the live edge first only when `probe`. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] fn poll_drop(&mut self, probe: bool, waiter: &kio::Waiter) -> Poll<(ops::Range, Hole)> { + // What the group read walks, which this poll may move outside that read. + let walks = (self.filter.revision, self.fill_view()); self.sync(); if self.drops.is_none() { self.drops = Some(Box::new(DropCursor::new(&self.state.read()))); @@ -8453,6 +8455,12 @@ impl Cursor { self.probe(from, &drift, waiter); judged = Some(drift); } + // The window settled, or a fill was asked or answered, on this poll's waiter rather + // than the one the group read parked on: that read, and every gate it nests in, reads + // again. + if (self.filter.revision, self.fill_view()) != walks { + idle::Idle::reopen(&mut self.idle); + } // A cap at or below the floor leaves nothing to report, not a reversed span. let floor = self.floor(); let within = Within { @@ -9205,6 +9213,15 @@ impl Cursor { group } + /// What a group read consults of the fills: the lowest group still owed, whether a filled + /// one waits to be taken, and whether any is owed at all. + fn fill_view(&self) -> (Option, bool, bool) { + let Some(misses) = &self.misses else { + return (None, false, false); + }; + (misses.lowest_wanted(), misses.has_filled(), misses.is_waiting()) + } + /// Whether a fill still owes this cursor a group, so the end of the feed is not its end. fn awaits_fill(&self) -> bool { self.misses.as_ref().is_some_and(|misses| misses.is_waiting()) diff --git a/rs/moq-net/src/util/idle.rs b/rs/moq-net/src/util/idle.rs index 531c495c3f5..3db5f228b5e 100644 --- a/rs/moq-net/src/util/idle.rs +++ b/rs/moq-net/src/util/idle.rs @@ -6,7 +6,8 @@ //! can answer `Pending` again without walking, and locking, everything it read before. //! //! Only what notifies can be trusted this way. A change the owner makes itself, which -//! notifies nothing, has to [`Idle::disarm`] the gate. +//! notifies nothing, has to [`Idle::disarm`] the gate, or [`Idle::reopen`] it when made +//! outside the read, so the gates it nests in and the task read again. //! //! An owner with more than one read keys each by `K`, since one parking says nothing //! about another. @@ -109,6 +110,17 @@ impl Idle { idle.parked = None; } } + + /// [`Self::disarm`] for a change made outside the read, which a gate the read nests in + /// never hears: it parked on this one and answers `Pending` again until woken. So this + /// wakes through the gate's own waiter, and every gate it nests in reads again, as does + /// the task. + pub(crate) fn reopen(this: &mut Option>) { + if let Some(idle) = this { + idle.parked = None; + idle.wake.wake_by_ref(); + } + } } #[cfg(test)] @@ -155,6 +167,44 @@ mod test { assert!(!Idle::parked(&slot, (), &other), "another task reads anew"); } + /// A reopen reaches the gate the read nests in, which parked on this one and would + /// otherwise answer `Pending` again without reading through it. A disarm does not. + #[test] + fn a_reopen_reaches_the_gates_the_read_nests_in() { + for reopen in [false, true] { + let task = Arc::new(Woken { + woken: AtomicBool::new(false), + task: Waker::noop().clone(), + }); + let waiter = kio::Waiter::new(Waker::from(task.clone())); + let mut outer_slot: Option> = None; + let mut inner_slot: Option> = None; + + // The outer read parks through the inner one, as an ordered read parks on its cursor. + let mut outer = Idle::start(&mut outer_slot, &waiter); + let inner = Idle::start(&mut inner_slot, outer.waiter()); + inner.finish(&mut inner_slot, (), true); + outer.finish(&mut outer_slot, (), true); + assert!(Idle::parked(&outer_slot, (), &waiter)); + + match reopen { + true => Idle::reopen(&mut inner_slot), + false => Idle::disarm(&mut inner_slot), + } + assert!(!Idle::parked(&inner_slot, (), &kio::Waiter::noop())); + assert_eq!( + Idle::parked(&outer_slot, (), &waiter), + !reopen, + "reopen {reopen}: the outer gate reads through" + ); + assert_eq!( + task.woken.load(Ordering::Acquire), + reopen, + "reopen {reopen}: the task wakes" + ); + } + } + /// A gate keyed by read answers only for the read that parked. #[test] fn a_parked_read_says_nothing_about_another() {