diff --git a/js/net/src/lite/fills.test.ts b/js/net/src/lite/fills.test.ts index 1d701a48e68..31d961047b1 100644 --- a/js/net/src/lite/fills.test.ts +++ b/js/net/src/lite/fills.test.ts @@ -117,10 +117,14 @@ test("range fills: a stale drop of asked groups is recovered once, on a subscrip fills.refuse(1n, 3n, new StreamError(StreamCode.Old)); fills.poll(tail, true); expect(opened.map((entry) => entry.groups)).toEqual([{ start: 1n, end: 3n }]); - // With the fill's priority. The run already asked stays asked whole while the fill owes any of - // it, so the subscription does not change under the recovery. + // With the fill's priority. The recovered run leaves the subscription, though the rest of the + // fill's run stays asked: left in, any later update would reopen its stale drop there, and the + // publisher would serve it on both. expect(opened[0]?.priority).toBe(peer.fill.priority); - expect(fills.ask.groups).toEqual([{ start: 0n, end: 4n }]); + expect(fills.ask.groups).toEqual([ + { start: 0n, end: 1n }, + { start: 3n, end: 4n }, + ]); // The recovery delivered nothing, and the next stale drop of the same groups stands. opened[0]?.finish([]); diff --git a/js/net/src/lite/fills.ts b/js/net/src/lite/fills.ts index fe05f6d1a43..0848c06e1ae 100644 --- a/js/net/src/lite/fills.ts +++ b/js/net/src/lite/fills.ts @@ -331,9 +331,12 @@ export class RangeFills { for (const sequence of this.#reading.keys()) if (!draining.some((run) => run.start <= sequence && sequence < run.end)) this.#reading.delete(sequence); this.#draining = draining; + // A run being recovered leaves the subscription, even out of a run kept whole: any later update + // reopens its stale drop there, and the publisher would serve it on both. + const remaining = this.#recovering.reduce((runs, run) => removeRun(runs, run.start, run.end), groups); // What finishes a stream still asks with what its fills asked. - if (this.#held.length === 0 && groups.length > 0) ask.priority = this.ask.priority; - ask.groups = groups; + if (this.#held.length === 0 && remaining.length > 0) ask.priority = this.ask.priority; + ask.groups = remaining; if (ask.priority === this.ask.priority && sameRuns(ask.groups, this.ask.groups)) return this.isEmpty() !== empty; this.#retireSkipped(ask.groups); diff --git a/quest/m2/subscribe-ranges/recovery-duplicate.md b/quest/m2/subscribe-ranges/recovery-duplicate.md deleted file mode 100644 index fdb56dbea9e..00000000000 --- a/quest/m2/subscribe-ranges/recovery-duplicate.md +++ /dev/null @@ -1,30 +0,0 @@ -# [S] A recovered run is served once - -## Goal - -A lite-07 relay's copy never receives the same group on both a recovery and -its demand subscription. Today, in Rust and JS, a recovery's stale groups can -race a SUBSCRIBE_UPDATE that reopens the same stale runs on the copy's demand -subscription. The publisher then serves the group on both, and the relay -resets the duplicate stream (code 0) for a group it already holds. No reader -is affected, but each occurrence costs a stream and puts a reset on the wire. - -## Plan - -Found by [moq-dev/moq#8616](https://github.com/moq-dev/moq/pull/8616), which -ported the Rust `Recovery` (`rs/moq-net/src/lite/subscriber/fills.rs`) to JS -(`js/net/src/lite/fills.ts`, `#recover` in `js/net/src/lite/subscriber.ts`). - -Settled on #8616 (maintainer, 2026-11-12), which a fix keeps: - -- ✅ A recovery asks with no max delay, in Rust and JS (rejected: asking with - the most tolerant budget among the fills owing the run). A narrow reader - therefore takes a run's stale history once every hop forgot it. -- ✅ A narrow reader may take stale groups of a recovered run (rejected: - judging a recovery against the whole track, which needs a wire signal or a - change to how a ranged subscription resolves its start). - -Reproduce the race in a two-hop test first, in both languages, then fix it at -the root: either the recovery's run stays out of what the update reopens, or -the copy settles which subscription delivers a group before both send it. -Keep Rust and JS in parity. Wire: none expected. diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 5c3418b711f..f081a6f26db 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -6387,6 +6387,59 @@ mod tests { (serve, serving, tail, broadcast) } + /// A stale drop of part of a fill's run recovers that part on its own subscription, which + /// takes it out of the copy's ranges at once, though the rest of the run stays asked: left + /// in, any later update would reopen the stale drop and the publisher would serve the + /// recovered groups on both. + #[kio::test] + #[expect(clippy::single_range_in_vec_init, reason = "the asked runs are a list, here one")] + async fn a_recovered_run_leaves_the_subscription() { + let (serve, _serving, _tail, _broadcast) = lite07_serving(Duration::from_millis(500)).await; + let waiter = kio::Waiter::noop(); + let copy = crate::track::Producer::new( + std::sync::Arc::new(crate::broadcast::Info::default()), + "video", + crate::track::Info::default() + .with_timescale(crate::Timescale::MILLI) + .with_max_age(None), + ); + let requests = copy.requests(); + for sequence in 0..6 { + let mut group = copy.create_group(group::Info { sequence }).unwrap(); + group + .write_frame(crate::Timestamp::from_millis(sequence * 1000).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + } + for sequence in 1..5 { + copy.evict(sequence); + } + let mut reader = copy + .subscribe( + crate::track::Subscription::default() + .with_delay(crate::track::Delay::default().with_max(None)) + .with_range(1..5), + ) + .ordered(); + assert!(reader.poll_next_group(&waiter).is_pending(), "groups 1..5 are owed"); + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { + panic!("groups 1..5 are a miss"); + }; + let mut fills = fills::RangeFills::default(); + fills.push(fill); + assert!(fills.poll(&serve, ©, None, true, &waiter).unwrap()); + assert_eq!(fills.ask.groups.iter().cloned().collect::>(), [1..5]); + + fills.refuse(1..3, crate::StreamError::Old); + assert!(fills.poll(&serve, ©, None, true, &waiter).unwrap()); + assert!(fills.recovering(), "1..3 recovered on its own subscription"); + assert_eq!( + fills.ask.groups.iter().cloned().collect::>(), + [3..5], + "the recovered run is no longer asked" + ); + } + /// A SUBSCRIBE_DROP can only echo an update the subscriber sent: one naming a later update /// is a protocol violation, and accounts for nothing. #[kio::test] diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index 5dd4f73b382..885b74c4d84 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -416,6 +416,11 @@ impl RangeFills { } self.reading.retain(|sequence, _| draining.contains(sequence)); self.draining = draining; + // A run being recovered leaves the subscription, even out of a run kept whole: any later + // update reopens its stale drop there, and the publisher would serve it on both. + for run in self.recovering.iter() { + groups.remove(run.clone()); + } // What finishes a stream still asks with what its fills asked. if self.held.is_empty() && !groups.is_empty() { ask.priority = self.ask.priority;