From 0559864ac1651e9d6ff68f1aa622bcf218488da0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 03:28:39 -0800 Subject: [PATCH 01/17] chore(quest): claim quest/m1/waiting-time-start-reads.md Quest-Claim: 8136ab78df2b931e From 0b0521d3bd104072452da91d9fc7d38a0c44ab2e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 05:51:03 -0800 Subject: [PATCH 02/17] fix(moq-net): a waiting time start reads its owed group first, and a fill past the live edge reaches subscriptions An ordered read of a lite-07 copy whose time start still waits on the publisher's answer hands out no group past that time, so the group the start resolves to comes first instead of landing as an Old gap. A whole group a track's own publisher accepts from a fill past every group it produced is its live feed: subscriptions deliver it, as js/net does. Below the live edge it stays history, and a relay's copy still waits for its upstream's subscription. Co-Authored-By: Claude Opus 5.5 --- js/net/src/fill.ts | 4 +- js/net/src/ordered.test.ts | 13 ++ quest/m1/waiting-time-start-reads.md | 31 --- rs/moq-net/src/model/subscription/window.rs | 8 + rs/moq-net/src/model/track.rs | 118 ++++++++++- rs/moq-net/src/model/track/fill.rs | 4 + rs/moq-net/tests/integration/main.rs | 1 + .../tests/integration/waiting_time_start.rs | 186 ++++++++++++++++++ 8 files changed, 325 insertions(+), 40 deletions(-) delete mode 100644 quest/m1/waiting-time-start-reads.md create mode 100644 rs/moq-net/tests/integration/waiting_time_start.rs diff --git a/js/net/src/fill.ts b/js/net/src/fill.ts index 7d80230e3dc..c2fefb3222c 100644 --- a/js/net/src/fill.ts +++ b/js/net/src/fill.ts @@ -692,8 +692,8 @@ export class FillGroups { /** * Insert group `key` into the track's cache, handing it to the readers waiting on it, - * and return the producer to write its frames into. `info` commits the track's {@link Info} - * if it has none yet. A cached copy missing its first frames, which a reader asked whole, is + * and return the producer to write its frames into. A live subscription covering it receives + * it too. `info` commits the track's {@link Info} if it has none yet. A cached copy missing its first frames, which a reader asked whole, is * replaced. Throws a `RangeError` outside the fill, and an error if the group was already * answered or cached whole, or the track is closed. */ diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index b29bdcf5b2f..899448b538d 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -405,3 +405,16 @@ test("a read returning only gaps keeps its handed-out record compact", () => { } expect(hooks.deliveredRuns(sub)).toBe(1); }); + +test("an ordered read hands out nothing past a time start still waiting to be placed", () => { + const producer = receivedTrack(); + put(producer, 2000n, false); + const ordered = producer.subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }).ordered(); + expect(items(ordered)).toEqual([]); + // A session's tail holds the read at the answered start's group until it lands, and says + // nothing lies between it and 2000. + hooks.timeStarts(producer, [[1500n, 1000n]]); + put(producer, 1000n, false); + producer.skipGroups(1001n, 2000n); + expect(items(ordered)).toEqual(["group 1000", "group 2000"]); +}); diff --git a/quest/m1/waiting-time-start-reads.md b/quest/m1/waiting-time-start-reads.md deleted file mode 100644 index 2879aca4dd3..00000000000 --- a/quest/m1/waiting-time-start-reads.md +++ /dev/null @@ -1,31 +0,0 @@ -# [M] A waiting time start reads its owed group, and a fill reaches subscriptions - -## Goal - -A Rust reader whose time start is still waiting on the groups held never hands -out a later group ahead of the one the start resolves to, and a group a -publisher accepts from a fill is delivered on its live subscriptions the way -`@moq/net` delivers it, or the difference is documented as intended. - -## Plan - -Both were seen while chasing the interop hang that -[moq-dev/moq#9127](https://github.com/moq-dev/moq/pull/9127) fixed, through a -lite-07 publisher, a relay, and two viewers, all in one simulated test: - -- A viewer that fetched group 67, then subscribed from `Time(66)` before 66 - was written, read 67 from an `Ordered` reader at once, then reported 66 as an - `Old` gap once 66 landed, though its budget was unlimited. Its own copy held - 67 and placed nothing for the time, so the waiting span read from 66, and the - ordered read did not wait for the group the start would resolve to. -- With the fetch on the other viewer instead, the subscribing viewer got 66 and - never 67: neither the Rust publisher nor the relay served the fill-accepted - group on a subscription, while the `@moq/net` publisher serves one there - (the interop relay received 69 over its subscription in every passing run). - -Start from a test in `rs/moq-net/tests/integration` with that topology -(`MockConnectOptions` on `moq-lite-07-wip`, the publisher's `Requests` serving -67 only). Decide the second from the draft's Subscribe section: a fill below -the live edge is history, but one at or past it is the live feed. - -Public API: none expected. Wire: none. diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 6231a37afc9..d4ccf0f86a1 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -255,6 +255,14 @@ impl Window { !self.waiting.is_empty() } + /// Whether a time start still waiting on the groups held may resolve below `key`: the + /// group containing its time starts at or before it, so it may still land below `key`. + pub fn waits_below(&self, key: u64) -> bool { + self.waiting + .iter() + .any(|&position| matches!(self.spans[position].anchor, Anchor::Time(time) if time < key)) + } + /// Resolve what the groups now held settle: relative anchors once `latest` exists, and /// time starts once a group arrives. Returns whether the groups in range moved. pub fn settle(&mut self, latest: Option, holdings: &impl Holdings) -> bool { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 550c1b2c8de..496d6f62d3f 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3948,9 +3948,20 @@ impl TrackState { // A head holding frame 0 gives a group received past it a start. self.fetched += 1; } - // Invisible to arrival-order subscribers: fetched on demand, not produced - // live by the publisher. - None => self.insert_group(&group.handle(), false), + // Below the live edge, history: invisible to arrival-order subscribers, served by + // key to whoever misses it. A whole group past every one the live publisher + // produced is the live feed, which a subscription delivers like any group it + // produces. A received copy's feed is its upstream's subscription, which delivers + // the group itself, and a track nobody accepted has no live feed yet. + None => { + let live = !self.anchors.received() + && self.minted() + && self.feed.is_live() + && frames.start == 0 + && frames.end.is_none() + && self.newest_produced.is_none_or(|newest| key > newest); + self.insert_group(&group.handle(), live) + } } self.evict_expired(); Ok(group) @@ -9880,6 +9891,20 @@ impl Cursor { floor = floor.max(self.floor()).max(group.start.saturating_add(1)); continue; } + // In order means the group a waiting time start resolves to comes first: it + // starts at or before that time, below this group, and may still land. Its + // landing, or the answer placing the start, wakes us. Registered before syncing + // again, so a change placing it meanwhile is not missed; a closed track places it. + if self.filter.window.waits_below(group.start) { + let closed = self.state.poll(waiter, |_| Poll::<()>::Pending).is_ready(); + self.sync(); + if !self.filter.window.waits_below(group.start) { + continue; + } + if !closed { + return Poll::Pending; + } + } } // Skip a group the budget has given up on and keep scanning, so one poll @@ -15410,6 +15435,42 @@ mod test { assert_eq!(drain(&mut past), vec![1000, 3000]); } + /// An ordered read of a lite-07 copy hands out no group past a time start still waiting to + /// be placed, since the group containing the time may still land below it: it reads that + /// group first, with no gap, once the publisher's answer and the group arrive. + #[test] + #[cfg(feature = "async")] + fn an_ordered_read_waits_for_its_time_start_to_be_placed() { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + let mut next = || match ordered.next_group().now_or_never() { + None => None, + Some(Ok(Some(Item::Group(group)))) => Some(Ok(group.start)), + Some(Ok(Some(Item::Gap { range, .. }))) => Some(Err(range)), + Some(res) => panic!("ended: {:?}", res.map(|_| ())), + }; + assert_eq!(next(), None, "2000 waits on the start"); + // A session's tail holds the read at the answered start's group until it lands, and + // says nothing lies between it and 2000. + producer.time_starts([(1500, 1000)]); + write_group(&producer, 1000); + producer.skip_groups(1001..2000); + assert_eq!(next(), Some(Ok(1000))); + assert_eq!(next(), Some(Ok(2000))); + } + /// A lite-07 copy holds only part of the track, so a relative start counts back as its /// publisher reported, not over the groups the copy holds, until a group past the latest /// the publisher counted from moves the live edge. @@ -16852,12 +16913,14 @@ mod test { assert!(!consumer.is_live(), "a track whose producer left is not live"); } - /// A one-shot fetch populates the shared cache but never the live arrival cursor, - /// so it cannot make content available to a subscription look stale. + /// A received copy's one-shot fetch populates the shared cache but never the live arrival + /// cursor, which only its upstream's subscription feeds, so it cannot make content available + /// to a subscription look stale. #[kio::test] #[cfg(feature = "async")] async fn fetched_group_is_not_a_live_drift_edge() { let mut producer = track_producer("test", Info::timed()); + producer.received(); let dynamic = producer.requests(); let consumer = producer.consume(); append_at(&mut producer, 0); @@ -16883,6 +16946,45 @@ mod test { groups.assert_no_group(); } + /// A publisher's fill answer past every group it produced is its live feed: a subscription + /// delivers it in arrival order, as a group the publisher produces. One below the live edge + /// stays history, served by key to whoever misses it. + #[kio::test] + #[cfg(feature = "async")] + async fn a_fill_past_the_live_edge_is_the_live_feed() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + let mut subscriber = producer.subscribe(replay()); + for key in [0, 1] { + producer + .create_group(group::Info { start: key }) + .unwrap() + .finish() + .unwrap(); + } + assert_eq!(drain(&mut subscriber), vec![0, 1]); + + let fill = |key: u64| { + let pending = consumer.fetch_group(key, None); + let fill = dynamic + .requested_fill_group() + .now_or_never() + .expect("the fetch asks at once") + .unwrap(); + let mut group = fill.accept(None).unwrap(); + group + .write_frame(Timestamp::from_millis(key).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + pending + }; + fill(3).await.unwrap(); + assert_eq!(drain(&mut subscriber), vec![3], "past the edge, live"); + let mut live = producer.subscribe(replay().with_ranges([Range::live()])); + assert_eq!(drain(&mut live), vec![3], "the live edge"); + } + /// A failed fill answer is no verdict on a gap the feed owes: an ordered reader neither takes /// the failed copy nor moves past it, and gets the feed's group once it lands. Mirrors JS /// `fill: a failed answer to a gap the feed owes holds the end and the ordered reader`. @@ -23954,6 +24056,7 @@ mod test { #[cfg(feature = "async")] async fn the_feed_takes_a_group_a_fetch_cached_first() { let (producer, _pool) = pooled_producer(1 << 40); + producer.received(); let dynamic = producer.requests(); let consumer = producer.consume(); let mut subscriber = producer.subscribe(replay()); @@ -26727,12 +26830,13 @@ mod test { assert_eq!(drain(&mut live), vec![0, 1]); } - /// Fetched backfill past the newest produced group moves no relative start, whether the - /// track had a produced group at subscribe or not. + /// A received copy's fetched backfill past the newest group its feed delivered moves no + /// relative start, whether the copy had a group at subscribe or not. #[kio::test] #[cfg(feature = "async")] async fn a_live_range_ignores_a_fetched_group_past_the_newest_group() { let producer = track_producer("test", Info::timed()); + producer.received(); let dynamic = producer.requests(); let consumer = producer.consume(); let mut early = producer.subscribe(replay().with_ranges([Range::live()])); diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index fab777aefc5..38c50d9979e 100644 --- a/rs/moq-net/src/model/track/fill.rs +++ b/rs/moq-net/src/model/track/fill.rs @@ -1009,6 +1009,10 @@ impl FillGroups { /// Insert group `key` into the track's cache, handing it to the readers waiting on /// it, and return a [`group::Producer`] to fill. /// + /// On a track this publisher accepted, a group past every one it produced is its live feed, + /// which its subscriptions deliver too; one below is history, served only to whoever misses + /// it. A relay's copy leaves that to its upstream's subscription. + /// /// The group starts at [`start`](Self::start)'s frame when it is the first group, so the /// frames written keep the indices they have in the group. `info` sets the track's /// [`Info`] if it hasn't been accepted yet (a fetch with no live subscription), and is diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 48b3485359e..adf869c1797 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -104,6 +104,7 @@ mod tracks_ietf; mod unannounce_release; mod untimed; mod viewer_target; +mod waiting_time_start; mod warm_backfill; mod wide_key; mod widened_max_age; diff --git a/rs/moq-net/tests/integration/waiting_time_start.rs b/rs/moq-net/tests/integration/waiting_time_start.rs new file mode 100644 index 00000000000..5466a6e0810 --- /dev/null +++ b/rs/moq-net/tests/integration/waiting_time_start.rs @@ -0,0 +1,186 @@ +#![cfg(feature = "async")] + +//! A lite-07 publisher, a relay, and two viewers, the way the interop replacement round +//! drives them: the publisher writes group 66, a viewer fetches group 67, which the publisher +//! serves only from a fill, and a viewer then subscribes from time 66. + +use crate::support; + +use std::time::Duration; + +use moq_net::track::{Anchor, Delay, Item, Order, Range, Subscription}; +use moq_net::{Timestamp, Version}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(5); + +/// The group only a fill serves. +const FILLED: u64 = 67; +/// The group the time start resolves to, written live. +const LIVE: u64 = 66; + +fn origin(hop: u64) -> moq_net::origin::Producer { + let (producer, driver) = + moq_net::origin::Producer::new(moq_net::origin::Config::new(std::num::NonZeroU64::new(hop).unwrap())); + support::harness::spawn(driver); + producer.with_proxy(true) +} + +async fn link(from: &moq_net::origin::Producer, to: &moq_net::origin::Producer) -> MockPair { + let mut options = MockConnectOptions::new("moq-lite-07-wip".parse::().unwrap()); + options.server_publish = Some(from.consume()); + options.client_subscribe = Some(to.clone()); + connect_mock(options).await +} + +fn write(mut group: moq_net::group::Producer) { + let key = group.start; + group + .write_frame(Timestamp::unscaled(key), key.to_string().into_bytes()) + .unwrap(); + group.finish().unwrap(); +} + +struct Fixture { + track: moq_net::track::Producer, + viewers: [moq_net::broadcast::Consumer; 2], + serving: kio::clock::sim::JoinHandle<()>, + _broadcast: moq_net::origin::Route, + _links: Vec, + _origins: Vec, +} + +impl Fixture { + /// A publisher whose scale-less track holds nothing, serving [`FILLED`] from a fill and + /// dropping every other fill and lookup, a relay, and two viewers of it. + async fn new() -> Self { + let publisher = origin(1); + let broadcast = publisher + .route("bcast", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track("value", moq_net::track::Info::default().with_max_age(None)) + .unwrap(); + broadcast.announce().unwrap(); + let requests = track.requests(); + let serving = kio::clock::sim::spawn(async move { + while let Ok(fill) = requests.requested_fill().await { + let moq_net::track::Fill::Groups(fill) = fill else { + continue; + }; + if (fill.start().group..fill.end()).contains(&FILLED) + && let Ok(group) = fill.accept(FILLED, None) + { + write(group); + } + } + }); + + let relay = origin(2); + let mut links = vec![link(&publisher, &relay).await]; + let mut viewers = Vec::new(); + let mut origins = vec![publisher, relay.clone()]; + for hop in 3..5 { + let viewer = origin(hop); + links.push(link(&relay, &viewer).await); + let remote = kio::clock::timeout(TIMEOUT, viewer.consume().follow("bcast", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + viewers.push(remote); + origins.push(viewer); + } + Self { + track, + viewers: viewers.try_into().map_err(drop).unwrap(), + serving, + _broadcast: broadcast, + _links: links, + _origins: origins, + } + } + + /// Fetch [`FILLED`] on viewer `index` and read it whole. + async fn fetch(&self, index: usize) { + let mut fetched = kio::clock::timeout( + TIMEOUT, + self.viewers[index].track("value").unwrap().fetch_group(FILLED, None), + ) + .await + .expect("fetch timeout") + .unwrap(); + let frame = kio::clock::timeout(TIMEOUT, fetched.read_frame()) + .await + .expect("read timeout") + .unwrap() + .expect("a frame"); + assert_eq!(&frame.payload[..], FILLED.to_string().as_bytes()); + } + + /// An ordered reader on viewer `index` from time [`LIVE`], with no budget. + async fn subscribe(&self, index: usize) -> moq_net::track::Ordered { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_order(Order::Ascending) + .with_ranges([Range { + start: Anchor::Time(LIVE), + end: None, + }]); + kio::clock::timeout( + TIMEOUT, + self.viewers[index].track("value").unwrap().subscribe(subscription), + ) + .await + .expect("subscribe timeout") + .unwrap() + .ordered() + } + + /// Write [`LIVE`] at the publisher. + fn write_live(&self) { + write(self.track.create_group(moq_net::group::Info { start: LIVE }).unwrap()); + } +} + +/// The keys of the next `count` items `reader` returns, a gap as its range. +async fn read(reader: &mut moq_net::track::Ordered, count: usize) -> Vec>> { + let mut items = Vec::new(); + while items.len() < count { + let item = kio::clock::timeout(TIMEOUT, reader.next_group()) + .await + .unwrap_or_else(|_| panic!("timed out after {items:?}")) + .unwrap() + .expect("an item"); + items.push(match item { + Item::Group(group) => Ok(group.start), + Item::Gap { range, .. } => Err(range), + }); + } + items +} + +/// A viewer that holds [`FILLED`] from its own fetch reads the group its time start resolves +/// to first, though its copy can't place the start until the publisher answers. +#[kio::test] +async fn a_waiting_time_start_reads_its_group_before_a_held_later_one() { + let fixture = Fixture::new().await; + fixture.write_live(); + fixture.fetch(0).await; + let mut reader = fixture.subscribe(0).await; + assert_eq!(read(&mut reader, 2).await, [Ok(LIVE), Ok(FILLED)]); + fixture.serving.abort(); +} + +/// A group the publisher accepted from another viewer's fetch, past the live edge, reaches a +/// subscription covering it. +#[kio::test] +async fn a_filled_group_past_the_live_edge_reaches_a_subscription() { + let fixture = Fixture::new().await; + fixture.write_live(); + fixture.fetch(1).await; + let mut reader = fixture.subscribe(0).await; + assert_eq!(read(&mut reader, 2).await, [Ok(LIVE), Ok(FILLED)]); + fixture.serving.abort(); +} From 64fd64b508fc53dcfe3fdf4e7f7c3fd8166b9adf Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 06:40:09 -0800 Subject: [PATCH 03/17] fix(moq-net): a time start folded into an earlier range places from that range A lite-07 copy whose time start the upstream folded into an earlier open range (an Oldest range, or a time range with no answer of its own) now places it from the covering range's reported start, and counts a group as the start once the feed's accounting has landed every key from there through the time. The session reports Oldest ranges as time 0 and each range's reach. Mirrored in js/net. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 2 +- js/net/src/lite/subscriber.ts | 26 +++-- js/net/src/ordered.test.ts | 15 +++ js/net/src/track.ts | 56 +++++++++- rs/moq-net/src/lite/subscriber.rs | 21 +++- rs/moq-net/src/model/track.rs | 166 +++++++++++++++++++++++++++--- 6 files changed, 259 insertions(+), 27 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 193870c6ce6..0e23bb9f5ea 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -744,7 +744,7 @@ export const hooks: { * start the copy cannot place among its own groups resolves to it. Mirrors the Rust * `track::Producer::time_starts`; assigned by `track.ts`. */ - timeStarts: (producer: Producer, starts: Iterable) => void; + timeStarts: (producer: Producer, starts: Iterable) => void; /** * Tell a lite-07 copy where its publisher started each live range counting back, as * `[back, start, latest]` triples, replacing the last set: the copy holds only part of the diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index dddc9e11339..8df4a8a6f2c 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -89,6 +89,7 @@ import { Subscribe, SubscribeUpdate, toWireRanges, + type WireAnchor, type WireRange, } from "./subscribe.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; @@ -2871,10 +2872,10 @@ export class Subscriber { // requested floor, pending, while SUBSCRIBE_START or a range's SUBSCRIBE_STARTED has yet to // say where one starts. Mirrors the Rust `SubStream::declare`. #declare(entry: SubscribeEntry): void { - // Where the publisher started each live range asked from a group's start, which a reader's - // time start there resolves to when the copy holds no group at it. Mirrors the Rust - // `report_time_starts`. - const times: [bigint, bigint][] = []; + // Where the publisher started each live range asked from a group's start or the oldest group + // (time 0), with the last time it reaches, which a reader's time start there or folded into it + // resolves to when the copy holds no group at it. Mirrors the Rust `SubStream::report_starts`. + const times: [bigint, bigint, bigint | undefined][] = []; // And each one counting back, which the copy, holding only part of the track, can't count, // with the latest group it counted back from. const relative: [bigint, bigint, bigint | undefined][] = []; @@ -2884,8 +2885,11 @@ export class Subscriber { const back = range?.start?.relative; const at = entry.starts.get(id); if (at === undefined) continue; - if (time !== undefined) times.push([time, at]); - else if (back !== undefined) relative.push([back, at, entry.edges.get(id)]); + if (back !== undefined) relative.push([back, at, entry.edges.get(id)]); + else if (time !== undefined || range?.start === undefined) { + const asked = time ?? 0n; + times.push([asked, at, through(range?.end, asked)]); + } } hooks.timeStarts(entry.track, times); hooks.relativeStarts(entry.track, relative); @@ -3427,3 +3431,13 @@ class ConsumeBroadcast extends broadcast.Consumer { return new ConsumeBroadcast(this.#subscriber, this.#path, this.#consumed, this.shareState()); } } + +// The last time a lite-07 range ending at `end` reaches, undefined while it follows the live edge: +// an exclusive group end stops at the time before it, and a relative end, unknown here, at the +// range's own time `asked`. Mirrors the Rust `SubStream::report_starts`. +function through(end: WireAnchor | undefined, asked: bigint): bigint | undefined { + if (end === undefined) return undefined; + if (end.time !== undefined) return end.time; + if (end.absolute !== undefined) return end.absolute > asked ? end.absolute - 1n : asked; + return asked; +} diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index 899448b538d..a96e8ff96ac 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -418,3 +418,18 @@ test("an ordered read hands out nothing past a time start still waiting to be pl producer.skipGroups(1001n, 2000n); expect(items(ordered)).toEqual(["group 1000", "group 2000"]); }); + +test("a time start folded upstream into an earlier range places from the range covering it", () => { + const producer = receivedTrack(); + put(producer, 2000n, false); + const subscriber = producer.subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }); + // A range asked from 1000 that ends at 1200 doesn't reach the time. + hooks.timeStarts(producer, [[1000n, 1000n, 1200n]]); + expect(hooks.servingResolved(subscriber)).toEqual([undefined]); + // The oldest group's range, answered at 2000, served nothing at or before it. + hooks.timeStarts(producer, [ + [1000n, 1000n, 1200n], + [0n, 2000n, undefined], + ]); + expect(hooks.servingResolved(subscriber)).toEqual([{ start: 0n, end: undefined, floored: false }]); +}); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 14b7d6c202c..2f2ffadf870 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1052,10 +1052,22 @@ let keysOf: (producer: Producer) => readonly bigint[]; // producer's static block. See `hooks.startsAtKeys` and `hooks.timeStarts`. let timeAnchors: (producer: Producer) => { atKeys: boolean; - starts: ReadonlyMap; + starts: ReadonlyMap; relative: ReadonlyMap; }; +/** Where a received copy's publisher started a range asked from a time, and the last time it reaches. */ +interface TimeStart { + start: bigint; + /** Undefined while the range follows the live edge. */ + through?: bigint; +} + +// Whether a received copy's live feed may land nothing more at or below `time` that could contain +// it, counting from `from` or its first arrival at or below `time`; assigned in the producer's +// static block. Mirrors the Rust `TrackState::landed_through`. +let landedThrough: (producer: Producer, time: bigint, from: bigint | undefined) => boolean; + /** The groups a reader reads, and the exclusive end it stops at; undefined before its ranges resolve. */ type Coverage = { runs: readonly Run[]; cap?: bigint } | undefined; @@ -1315,7 +1327,7 @@ export class Producer { #startsAtKeys = false; // Where the upstream publisher started each of a lite-07 copy's live wire ranges, by the time // it asked from. Mirrors the Rust `TrackState::time_starts`. - #timeStarts = new Map(); + #timeStarts = new Map(); // On a lite-07 copy, where the publisher started each live range counting back, by its count. #relativeStarts = new Map(); // The highest-key checked start evicted. Eviction is not a key prefix, so a @@ -1553,7 +1565,31 @@ export class Producer { producer.#startsAtKeys = true; }; hooks.timeStarts = (producer, starts) => { - producer.#timeStarts = new Map(starts); + const merged = new Map(); + for (const [time, start, through] of starts) { + const held = merged.get(time); + // Two ranges asked from one time: the lower start and the farther reach hold for both. + merged.set( + time, + held === undefined + ? { start, through } + : { + start: min(held.start, start), + through: + held.through === undefined || through === undefined + ? undefined + : max(held.through, through), + }, + ); + } + producer.#timeStarts = merged; + }; + landedThrough = (producer, time, from) => { + const first = producer.#arrivals.first(); + const start = from ?? (first !== undefined && first <= time ? first : undefined); + if (start === undefined) return false; + const landing = producer.#landing({ start, end: time + 1n, floor: true, grace: producer.#grace() }); + return landing.next().done === true; }; hooks.relativeStarts = (producer, starts) => { producer.#relativeStarts = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); @@ -3614,13 +3650,25 @@ export class Subscriber { // `TrackState::containing_received`. if (receivedCopy(producer) && !state.dropsAnchors.peek()) { if (below === time) return { group: time }; - const start = anchors.starts.get(time); + const start = anchors.starts.get(time)?.start; // The publisher serves nothing at or before it, as for a time older than every group. if (start !== undefined) return start <= time ? { group: start } : "before"; + // A range asked from an earlier time that reaches this one, which upstream folded this + // reader's range into, so it reports nothing for this time itself. + let covering: [bigint, bigint] | undefined; + for (const [asked, reported] of anchors.starts) { + if (asked >= time || (reported.through !== undefined && reported.through < time)) continue; + if (covering === undefined || asked > covering[0]) covering = [asked, reported.start]; + } if (below !== undefined && (covered(below + 1n, time + 1n, () => true) || index < keys.length)) { return { group: below }; } if (below === undefined && covered(0n, time + 1n, () => true)) return "before"; + // The live feed accounted for every key it could land through the time, so the group + // containing it came already: the newest one held, or one since evicted. + if (producer && landedThrough(producer, time, covering?.[1])) { + return below !== undefined ? { group: below } : "before"; + } return "unknown"; } if (below !== undefined) return { group: below }; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 4abb3ca4f1f..6caa6e6a6a3 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10142,9 +10142,10 @@ impl SubStream { } /// Tell the copy where the publisher started each live range: one asked from a group's - /// start, which a reader's time start there resolves to when the copy holds no group at - /// it, and one counting back, which the copy, holding only part of the track, can't count. - /// Lite-07 only, where each range's start is reported. + /// start or the oldest group, with how far it reaches, which a reader's time start there or + /// folded into it resolves to when the copy holds no group at it, and one counting back, + /// which the copy, holding only part of the track, can't count. Lite-07 only, where each + /// range's start is reported. fn report_starts(&self, serving: &track::Producer) { if !self.live.reports_starts() { return; @@ -10155,8 +10156,20 @@ impl SubStream { let Some(Some(start)) = self.starts.get(&id) else { continue; }; + // The last time the range reaches: an exclusive group end stops at the time before + // it, and a relative end, unknown here, at the range's own time. + let through = |asked: u64| match range.end { + None => None, + Some(lite::Anchor::Absolute(end)) if end.frame == 0 => Some(end.group.saturating_sub(1).max(asked)), + Some(lite::Anchor::Absolute(end)) => Some(end.group), + Some(lite::Anchor::Time(end)) => Some(end), + Some(lite::Anchor::Relative(_)) => Some(asked), + }; match range.start { - Some(lite::Anchor::Absolute(time)) if time.frame == 0 => times.push((time.group, *start)), + None => times.push((0, *start, through(0))), + Some(lite::Anchor::Absolute(time)) if time.frame == 0 => { + times.push((time.group, *start, through(time.group))) + } Some(lite::Anchor::Relative(back)) => { if let Some(Some(latest)) = self.edges.get(&id) { relative.push((back, *start, *latest)); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 496d6f62d3f..e412ef23a29 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -606,6 +606,14 @@ impl Anchors { } } +/// Where a received copy's publisher started a range asked from a time, and the last time +/// the range reaches, `None` while it follows the live edge. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct TimeStart { + start: u64, + through: Option, +} + /// Where a received copy's publisher started a range counting back, and the latest group it /// counted from. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -749,10 +757,11 @@ pub(crate) struct TrackState { // of the track cannot tell which earlier group contains the time. starts_at_keys: bool, // Where the upstream publisher started each of a lite-07 copy's live wire ranges, by the - // time it asked from: a copy holding part of the track cannot tell which group contains - // a time it holds no group at, but its publisher reports where it resolved one. Replaced - // whole as the ranges change, so it holds at most one entry per live range. - time_starts: BTreeMap, + // time it asked from (0 for the oldest group): a copy holding part of the track cannot tell + // which group contains a time it holds no group at, but its publisher reports where it + // resolved one. Replaced whole as the ranges change, so it holds at most one entry per + // time asked from. + time_starts: BTreeMap, // Where the upstream publisher started each of a lite-07 copy's live wire ranges counting // back, by how many groups each counted, with the latest group it counted from: the copy // holds only part of the track, so it can't count back over its own groups. Replaced whole @@ -1231,12 +1240,20 @@ impl TrackState { return Containing::Group(time); } match self.time_starts.get(&time) { - Some(&start) if start <= time => return Containing::Group(start), + Some(reported) if reported.start <= time => return Containing::Group(reported.start), // The publisher serves nothing at or before it, as it does for a time older than // every group it holds. Some(_) => return Containing::Before, None => {} } + // A range asked from an earlier time that reaches this one, which upstream folded this + // reader's range into, so it reports nothing for this time itself. + let covering = self + .time_starts + .range(..time) + .rev() + .find(|(_, reported)| reported.through.is_none_or(|through| through >= time)) + .map(|(_, reported)| reported.start); match self.lookup.range(..=time).next_back() { Some((&key, _)) if self.dropped_cover(key.saturating_add(1)..time.saturating_add(1)) @@ -1245,10 +1262,39 @@ impl TrackState { Containing::Group(key) } None if self.dropped_cover(0..time.saturating_add(1)) => Containing::Before, + // The live feed accounted for every key it could land through the time, so the + // group containing it came already: the newest one held, or one since evicted, which + // reaches back to the oldest group as on the publisher's own track. + held if self.landed_through(time, covering) => { + held.map_or(Containing::Before, |(&key, _)| Containing::Group(key)) + } _ => Containing::Unknown, } } + /// Whether the live feed may land nothing more at or below `time` that could contain it: + /// every key from `from`, where a range covering the time started, or else from the feed's + /// first arrival at or below `time`, through `time` arrived, was dropped, or is no longer + /// waited for. A covering range that started past the time served nothing at or before + /// it. Either names where the feed's groups begin, so a range added below changes nothing + /// of the group containing the time. Arrival order is not key order, so with neither, a + /// lower key may still land. + fn landed_through(&self, time: u64, from: Option) -> bool { + if !self.readable() { + return false; + } + let Some(from) = from.or_else(|| self.arrivals.first().filter(|&first| first <= time)) else { + return false; + }; + self.find_landable( + from..time.saturating_add(1), + true, + self.arrival_grace(), + |_, _| Some(()), + ) + .is_none() + } + /// Whether drops, for whatever reason, cover every key in `keys`. An empty run is covered. fn dropped_cover(&self, keys: ops::Range) -> bool { keys.is_empty() || self.dropped.get(&keys.start).is_some_and(|run| run.end >= keys.end) @@ -4789,11 +4835,24 @@ impl Producer { } /// Record where the upstream publisher started each of this lite-07 copy's live wire - /// ranges, as `(time, start)` pairs by the time each asked from, replacing the last set. - /// A reader's time start this copy cannot place among its own groups resolves to it. + /// ranges, as `(time, start, through)` by the time each asked from (0 for the oldest group) + /// and the last time it reaches (`None` while open), replacing the last set. A reader's + /// time start this copy cannot place among its own groups resolves to the range asked from + /// it, or to one reaching it that upstream folded it into. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn time_starts(&self, starts: impl IntoIterator) { - let starts: BTreeMap = starts.into_iter().collect(); + pub(crate) fn time_starts(&self, starts: impl IntoIterator)>) { + let mut merged: BTreeMap = BTreeMap::new(); + for (time, start, through) in starts { + // Two ranges asked from one time: the lower start and the farther reach hold for both. + merged + .entry(time) + .and_modify(|held| { + held.start = held.start.min(start); + held.through = held.through.zip(through).map(|(a, b)| a.max(b)); + }) + .or_insert(TimeStart { start, through }); + } + let starts = merged; if let Ok(mut state) = self.modify() && state.time_starts != starts { @@ -9896,7 +9955,7 @@ impl Cursor { // landing, or the answer placing the start, wakes us. Registered before syncing // again, so a change placing it meanwhile is not missed; a closed track places it. if self.filter.window.waits_below(group.start) { - let closed = self.state.poll(waiter, |_| Poll::<()>::Pending).is_ready(); + let closed = self.poll(waiter, |_| Poll::>::Pending).is_ready(); self.sync(); if !self.filter.window.waits_below(group.start) { continue; @@ -15417,7 +15476,7 @@ mod test { // The publisher started the range asked from 1700 at 1000, and the one from 500 at 1000 // too, past it: nothing at or before 500 is served. - producer.time_starts([(1700, 1000), (500, 1000)]); + producer.time_starts([(1700, 1000, None), (500, 1000, None)]); assert_eq!(placed(&mut answered), Some((1000, true))); assert_eq!(placed(&mut before), Some((0, false))); assert_eq!(placed(&mut later), None); @@ -15464,13 +15523,96 @@ mod test { assert_eq!(next(), None, "2000 waits on the start"); // A session's tail holds the read at the answered start's group until it lands, and // says nothing lies between it and 2000. - producer.time_starts([(1500, 1000)]); + producer.time_starts([(1500, 1000, None)]); write_group(&producer, 1000); producer.skip_groups(1001..2000); assert_eq!(next(), Some(Ok(1000))); assert_eq!(next(), Some(Ok(2000))); } + /// A lite-07 copy places a time start that upstream folded into an earlier range by that + /// range's answer, which reports nothing for the time itself: one that started past the + /// time served nothing at or before it, so an ordered read goes on. A range ending before + /// the time says nothing of it. + #[test] + #[cfg(feature = "async")] + fn a_folded_time_start_places_from_the_range_covering_it() { + let ordered = |producer: &Producer| { + producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered() + }; + let next = |ordered: &mut Ordered| match ordered.next_group().now_or_never() { + None => None, + Some(Ok(Some(Item::Group(group)))) => Some(group.start), + Some(res) => panic!("not a group: {:?}", res.map(|item| item.is_some())), + }; + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + + // A range asked from 1000 that ends at 1200 doesn't reach the time. + producer.time_starts([(1000, 1000, Some(1200))]); + let mut bounded = ordered(&producer); + assert_eq!(next(&mut bounded), None, "the start still waits"); + + // The oldest group's range, answered at 2000, reaches it. + producer.time_starts([(1000, 1000, Some(1200)), (0, 2000, None)]); + assert_eq!(next(&mut bounded), Some(2000)); + assert_eq!(next(&mut ordered(&producer)), Some(2000)); + } + + /// A lite-07 copy whose live feed accounted for every key through a time start's time + /// places it though the group containing it was since evicted, as when its range was folded + /// into a wider one upstream, which answers nothing for the time: an ordered read goes on + /// rather than wait for a group that came and went. + #[test] + #[cfg(feature = "async")] + fn an_evicted_start_group_still_places_a_time_start() { + let (mut producer, _pool) = pooled_producer(2_000); + producer.received(); + producer.anchored(); + producer.start_resolved(1000).unwrap(); + let mut big = producer.create_group(group::Info { start: 1000 }).unwrap(); + big.write_frame( + Timestamp::from_millis(1000).unwrap(), + bytes::Bytes::from(vec![0u8; 10_000]), + ) + .unwrap(); + big.finish().unwrap(); + for (key, below) in [(2000, 1001), (3000, 2001)] { + producer.skip_groups(below..key); + write_group(&producer, key); + } + assert!(producer.consume().peek_group(1000).is_none(), "1000 is evicted"); + + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + let mut groups = Vec::new(); + while let Some(item) = ordered.next_group().now_or_never() { + if let Item::Group(group) = item.unwrap().expect("an item") { + groups.push(group.start); + } + } + assert_eq!(groups.last(), Some(&3000), "the read goes on past the evicted group"); + } + /// A lite-07 copy holds only part of the track, so a relative start counts back as its /// publisher reported, not over the groups the copy holds, until a group past the latest /// the publisher counted from moves the live edge. From 28df1d8c97b8b8d114f3e8e00307d4b3ee2d7dbd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 06:58:29 -0800 Subject: [PATCH 04/17] fix(moq-net): an ordered read parked at a waiting time start wakes when a gap's grace folds A read held at a time start that still waits on a gap below its time now watches when that gap's arrival grace folds, which places the start with no change to the track. js/net gets the same barrier: an ordered read (and a front's peek in key order) holds at a waiting time start once its copy's feed start is known, and a time start answer wakes it. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ordered.test.ts | 40 +++++++ js/net/src/resume.test.ts | 2 + js/net/src/track.ts | 65 +++++++++--- js/net/src/window.ts | 14 +++ rs/moq-net/src/model/subscription/window.rs | 10 +- rs/moq-net/src/model/track.rs | 110 ++++++++++++++++---- 6 files changed, 206 insertions(+), 35 deletions(-) diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index a96e8ff96ac..f8974c631e1 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -433,3 +433,43 @@ test("a time start folded upstream into an earlier range places from the range c ]); expect(hooks.servingResolved(subscriber)).toEqual([{ start: 0n, end: undefined, floored: false }]); }); + +test("a folded time start wakes its read when the grace for a gap below it passes", async () => { + const clock = new Manual(); + const installed = clock.install(); + try { + const producer = receivedTrack(); + // An oldest-group reader holds the feed open from its answered start, 1000. + const oldest = producer.subscribe({ delay: { max: Duration.millis(50) } }); + hooks.startTrack(producer, { start: 1000n }); + hooks.timeStarts(producer, [[0n, 1000n]]); + // Group 1000's stream reset before its header: only 2000 and the skips below it land. + producer.skipGroups(1001n, 2000n); + put(producer, 2000n, false); + const ordered = producer + .subscribe({ delay: { max: Duration.millis(50) }, ranges: [{ start: { time: 1500n } }] }) + .ordered(); + expect(items(ordered)).toEqual([]); + let read: bigint | undefined; + const next = ordered.nextGroup().then((group) => { + read = group?.start; + }); + // Let the read settle into its wait, so only a timer can wake it. + await flush(); + clock.jump(Duration.millis(49)); + await flush(); + expect(read).toBeUndefined(); + clock.jump(Duration.millis(1)); + await flush(); + expect(read).toBe(2000n); + await next; + oldest.close(); + } finally { + installed.close(); + } +}); + +// Run every pending microtask and the ones they queue, as a timer firing would. +async function flush() { + for (let i = 0; i < 20; i++) await Promise.resolve(); +} diff --git a/js/net/src/resume.test.ts b/js/net/src/resume.test.ts index 5fc669e32ff..180f0777f34 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -2075,6 +2075,8 @@ for (const resolution of ["arrival", "drop", "other copy", "budget"] as const) { expect((await ordered.nextGroup())?.start).toBe(4n); ta.write(6n); tb.write(7n); + // The session reports where the range the front asked from time 4 started, as with its START. + hooks.timeStarts(tb.producer, [[4n, 7n]]); hooks.startTrack(tb.producer, { start: 7n, requested: 4n }); front.serve(b.consume()); await settle(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 2f2ffadf870..61175c18eaf 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1068,6 +1068,22 @@ interface TimeStart { // static block. Mirrors the Rust `TrackState::landed_through`. let landedThrough: (producer: Producer, time: bigint, from: bigint | undefined) => boolean; +// When a received copy's time start at `time`, waiting to be placed, next may place itself with no +// change to wake its readers: the grace of the lowest gap {@link landedThrough} still waits on +// folds. Mirrors the Rust `TrackState::time_start_folds`. +let timeStartFolds: (producer: Producer, time: bigint) => Instant | undefined; + +// Where a range asked from an earlier time that reaches `time` started, which upstream folded a +// range asked from `time` into, so it reports nothing for this time itself. +function coveringStart(starts: ReadonlyMap, time: bigint): bigint | undefined { + let covering: [bigint, bigint] | undefined; + for (const [asked, reported] of starts) { + if (asked >= time || (reported.through !== undefined && reported.through < time)) continue; + if (covering === undefined || asked > covering[0]) covering = [asked, reported.start]; + } + return covering?.[1]; +} + /** The groups a reader reads, and the exclusive end it stops at; undefined before its ranges resolve. */ type Coverage = { runs: readonly Run[]; cap?: bigint } | undefined; @@ -1583,13 +1599,13 @@ export class Producer { ); } producer.#timeStarts = merged; + // The answer may place a time start an ordered read waits at. + for (const state of [producer.#state, ...producer.#sinks]) state.timelineChanged.update((rev) => rev + 1); }; - landedThrough = (producer, time, from) => { - const first = producer.#arrivals.first(); - const start = from ?? (first !== undefined && first <= time ? first : undefined); - if (start === undefined) return false; - const landing = producer.#landing({ start, end: time + 1n, floor: true, grace: producer.#grace() }); - return landing.next().done === true; + landedThrough = (producer, time, from) => producer.#landingThrough(time, from) === undefined; + timeStartFolds = (producer, time) => { + if (!producer.#wire || producer.#timeStarts.has(time)) return undefined; + return producer.#landingThrough(time, coveringStart(producer.#timeStarts, time))?.folds; }; hooks.relativeStarts = (producer, starts) => { producer.#relativeStarts = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); @@ -1697,6 +1713,16 @@ export class Producer { }; } + // Undefined once {@link landedThrough} holds, else the lowest gap it still waits on, with when + // its grace folds, if it ever does. + #landingThrough(time: bigint, from: bigint | undefined): { folds?: Instant } | undefined { + const first = this.#arrivals.first(); + const start = from ?? (first !== undefined && first <= time ? first : undefined); + if (start === undefined) return {}; + const landing = this.#landing({ start, end: time + 1n, floor: true, grace: this.#grace() }).next(); + return landing.done ? undefined : { folds: landing.value.folds }; + } + // The first group that may still land from the live feed, by the rule a pending tail is judged // by; see {@link Land}. Mirrors the Rust `TrackState::may_land`. #landable(land: Land): { key: bigint; folds?: Instant } | undefined { @@ -3653,20 +3679,14 @@ export class Subscriber { const start = anchors.starts.get(time)?.start; // The publisher serves nothing at or before it, as for a time older than every group. if (start !== undefined) return start <= time ? { group: start } : "before"; - // A range asked from an earlier time that reaches this one, which upstream folded this - // reader's range into, so it reports nothing for this time itself. - let covering: [bigint, bigint] | undefined; - for (const [asked, reported] of anchors.starts) { - if (asked >= time || (reported.through !== undefined && reported.through < time)) continue; - if (covering === undefined || asked > covering[0]) covering = [asked, reported.start]; - } + const covering = coveringStart(anchors.starts, time); if (below !== undefined && (covered(below + 1n, time + 1n, () => true) || index < keys.length)) { return { group: below }; } if (below === undefined && covered(0n, time + 1n, () => true)) return "before"; // The live feed accounted for every key it could land through the time, so the group // containing it came already: the newest one held, or one since evicted. - if (producer && landedThrough(producer, time, covering?.[1])) { + if (producer && landedThrough(producer, time, covering)) { return below !== undefined ? { group: below } : "before"; } return "unknown"; @@ -5381,6 +5401,21 @@ export class Subscriber { if (this.#dropsSeen) this.#pending = insertDrop(this.#pending, run.start, run.end, hole); } + // In order, the group a waiting time start resolves to comes first: it starts at or before that + // time, below `key`, and may still land. Its landing, the answer placing the start, or the grace + // for a gap below it folding wakes the read; a closed track places the start. + #waitTimeStart(key: bigint): boolean { + const waiting = this.#window.waitingBelow(key); + if (waiting.length === 0) return false; + const producer = this.#state.producer; + for (const time of waiting) { + const folds = producer && timeStartFolds(producer, time); + if (folds !== undefined) this.#wakeAt(folds.since(Instant.ZERO).asNanos()); + } + this.#state.waitingOrdered = true; + return true; + } + // A missing group the subscription still owes holds sequence order until it lands, // is accounted for, or the same drift budget as a buffered group gives up on it. #waitOwed(start: bigint, end: bigint, drift: Drift): boolean { @@ -5415,6 +5450,7 @@ export class Subscriber { for (const group of this.#state.groups.peek()) { if (group.start < start || !this.#window.contains(group.start)) continue; if (cursor.end !== undefined && group.start >= cursor.end) return undefined; + if (ordered && this.#waitTimeStart(group.start)) return undefined; if (!this.#delivered.has(group.start) && !this.#staleAt(group.start, drift)) { return group.start; } @@ -5461,6 +5497,7 @@ export class Subscriber { index++; continue; } + if (this.#waitTimeStart(group.start)) return { kind: "idle" }; // Every frame this group could still hold is past the budget, so keep // scanning: one pass walks a whole backlog off rather than replaying it. if (this.#staleAt(group.start, drift)) { diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 03596fa4498..f2f9a98c5b4 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -518,6 +518,20 @@ export class Window { return started; } + /** + * The times of the time starts still waiting on the groups held that may resolve below `key`: + * the group containing each starts at or before its time, so it may still land below `key`. + * Mirrors the Rust `Window::waiting_below`. + */ + waitingBelow(key: bigint): bigint[] { + const times: bigint[] = []; + for (const position of this.#waiting) { + const at = time(this.#spans[position].anchor); + if (at !== undefined && at < key) times.push(at); + } + return times; + } + /** Whether group `key` is in range. Everything is while a relative anchor waits. */ contains(key: bigint): boolean { if (this.#pending) return true; diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index d4ccf0f86a1..c8da1cd962c 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -258,9 +258,17 @@ impl Window { /// Whether a time start still waiting on the groups held may resolve below `key`: the /// group containing its time starts at or before it, so it may still land below `key`. pub fn waits_below(&self, key: u64) -> bool { + self.waiting_below(key).next().is_some() + } + + /// The times of the waiting time starts before `key`, as [`Self::waits_below`] finds them. + pub fn waiting_below(&self, key: u64) -> impl Iterator + '_ { self.waiting .iter() - .any(|&position| matches!(self.spans[position].anchor, Anchor::Time(time) if time < key)) + .filter_map(move |&position| match self.spans[position].anchor { + Anchor::Time(time) if time < key => Some(time), + _ => None, + }) } /// Resolve what the groups now held settle: relative anchors once `latest` exists, and diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index e412ef23a29..f0ae4b84b22 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1246,14 +1246,7 @@ impl TrackState { Some(_) => return Containing::Before, None => {} } - // A range asked from an earlier time that reaches this one, which upstream folded this - // reader's range into, so it reports nothing for this time itself. - let covering = self - .time_starts - .range(..time) - .rev() - .find(|(_, reported)| reported.through.is_none_or(|through| through >= time)) - .map(|(_, reported)| reported.start); + let covering = self.covering_start(time); match self.lookup.range(..=time).next_back() { Some((&key, _)) if self.dropped_cover(key.saturating_add(1)..time.saturating_add(1)) @@ -1272,6 +1265,26 @@ impl TrackState { } } + /// Where a range asked from an earlier time that reaches `time` started, which upstream + /// folded a range asked from `time` into, so it reports nothing for this time itself. + fn covering_start(&self, time: u64) -> Option { + self.time_starts + .range(..time) + .rev() + .find(|(_, reported)| reported.through.is_none_or(|through| through >= time)) + .map(|(_, reported)| reported.start) + } + + /// When a received copy's time start at `time`, waiting to be placed, next may place + /// itself with no change to wake its readers: the arrival grace of the lowest gap + /// [`Self::landed_through`] still waits on folds. + fn time_start_folds(&self, time: u64) -> Option { + if !self.anchors.received() || self.time_starts.contains_key(&time) { + return None; + } + self.landing_through(time, self.covering_start(time)).flatten() + } + /// Whether the live feed may land nothing more at or below `time` that could contain it: /// every key from `from`, where a range covering the time started, or else from the feed's /// first arrival at or below `time`, through `time` arrived, was dropped, or is no longer @@ -1280,19 +1293,21 @@ impl TrackState { /// of the group containing the time. Arrival order is not key order, so with neither, a /// lower key may still land. fn landed_through(&self, time: u64, from: Option) -> bool { + self.landing_through(time, from).is_none() + } + + /// `None` once [`Self::landed_through`] holds, else `Some` of when the arrival grace of the + /// lowest gap it still waits on folds, if it ever does. + fn landing_through(&self, time: u64, from: Option) -> Option> { if !self.readable() { - return false; + return Some(None); } let Some(from) = from.or_else(|| self.arrivals.first().filter(|&first| first <= time)) else { - return false; + return Some(None); }; - self.find_landable( - from..time.saturating_add(1), - true, - self.arrival_grace(), - |_, _| Some(()), - ) - .is_none() + self.find_landable(from..time.saturating_add(1), true, self.arrival_grace(), |_, folds| { + Some(folds) + }) } /// Whether drops, for whatever reason, cover every key in `keys`. An empty run is covered. @@ -9952,10 +9967,21 @@ impl Cursor { } // In order means the group a waiting time start resolves to comes first: it // starts at or before that time, below this group, and may still land. Its - // landing, or the answer placing the start, wakes us. Registered before syncing - // again, so a change placing it meanwhile is not missed; a closed track places it. + // landing, the answer placing the start, or the grace for a gap below it folding + // wakes us. Registered before syncing again, so a change placing it meanwhile is + // not missed; a closed track places it. if self.filter.window.waits_below(group.start) { - let closed = self.poll(waiter, |_| Poll::>::Pending).is_ready(); + let window = &self.filter.window; + let closed = self + .poll(waiter, |state| { + for time in window.waiting_below(group.start) { + if let Some(folds) = state.time_start_folds(time) { + state.cache.watch_wall(folds, waiter); + } + } + Poll::>::Pending + }) + .is_ready(); self.sync(); if !self.filter.window.waits_below(group.start) { continue; @@ -15613,6 +15639,50 @@ mod test { assert_eq!(groups.last(), Some(&3000), "the read goes on past the evicted group"); } + /// A time start folded into an open range waits out the grace of a gap below its time, + /// where the group containing it may still land, and that grace folding alone wakes an + /// ordered read parked on it, with no change to the track. + #[kio::test] + #[cfg(feature = "async")] + async fn a_folded_time_start_wakes_its_read_when_a_gap_below_folds() { + let grace = replay().budget(); + let mut producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + elapse(&producer, Duration::ZERO); + producer.start_resolved(1000).unwrap(); + producer.time_starts([(0, 1000, None)]); + // Group 1000's stream reset before its header: only 2000 and the skips below it land. + producer.skip_groups(1001..2000); + write_group(&producer, 2000); + let mut ordered = producer + .subscribe(replay().with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }])) + .ordered(); + + let woken = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let waiter = kio::Waiter::new(futures::task::waker(Arc::new(FlagWake(woken.clone())))); + assert!(ordered.poll_next_group(&waiter).is_pending(), "1000 may still land"); + elapse(&producer, grace - Duration::from_millis(1)); + assert!(!woken.load(Ordering::SeqCst), "woken before the grace"); + assert!(ordered.poll_next_group(&waiter).is_pending()); + elapse(&producer, Duration::from_millis(1)); + assert!(woken.load(Ordering::SeqCst), "the grace never woke the read"); + let mut read = Vec::new(); + while let Poll::Ready(item) = ordered.poll_next_group(&waiter) { + match item.unwrap().expect("an item") { + Item::Group(group) => { + read.push(group.start); + break; + } + Item::Gap { .. } => {} + } + } + assert_eq!(read, [2000]); + } + /// A lite-07 copy holds only part of the track, so a relative start counts back as its /// publisher reported, not over the groups the copy holds, until a group past the latest /// the publisher counted from moves the live edge. From f39227d46cfc6f678d837c48b7994ec5b7b93c8a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 07:43:17 -0800 Subject: [PATCH 05/17] perf(moq-net): bound a copy's landing span from its highest place, not every reported range The first key the feed may land from a place only rises with it, so the span the feed's accounting may place is bounded by one walk from the highest place it counts from. The highest reported start is kept as the starts are set, so a drop no longer costs a walk per reported range. Mirrored in js/net. Co-Authored-By: Claude Opus 5.5 --- js/net/src/track.ts | 30 +++++++++++++++++------------ rs/moq-net/src/model/track.rs | 36 +++++++++++++++-------------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/js/net/src/track.ts b/js/net/src/track.ts index d09838c23ae..997489f4605 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1361,6 +1361,9 @@ export class Producer { // Where the upstream publisher started each of a lite-07 copy's live wire ranges, by the time // it asked from. Mirrors the Rust `TrackState::time_starts`. #timeStarts = new Map(); + // The lowest time and highest start in `#timeStarts`, so the times its ranges may place are + // bounded without a walk of them all. + #timeStartReach: { lowest: bigint; highest: bigint } | undefined; // On a lite-07 copy, where the publisher started each live range counting back, by its count. #relativeStarts = new Map(); // Counts changes to how a time start is placed other than by groups arriving or drops: the wire, @@ -1623,6 +1626,13 @@ export class Producer { ); } producer.#timeStarts = merged; + let reach: { lowest: bigint; highest: bigint } | undefined; + for (const [asked, reported] of merged) { + reach = reach + ? { lowest: min(reach.lowest, asked), highest: max(reach.highest, reported.start) } + : { lowest: asked, highest: reported.start }; + } + producer.#timeStartReach = reach; producer.#placing++; // The answer may place a time start an ordered read waits at. for (const state of [producer.#state, ...producer.#sinks]) state.timelineChanged.update((rev) => rev + 1); @@ -1630,19 +1640,15 @@ export class Producer { landedThrough = (producer, time, from) => producer.#landingThrough(time, from) === undefined; landingSpan = (producer) => { const first = producer.#arrivals.first(); - const froms = first === undefined ? [] : [first]; - let start = first; - for (const [asked, reported] of producer.#timeStarts) { - froms.push(reported.start); - start = start === undefined ? asked + 1n : min(start, asked + 1n); - } + const reach = producer.#timeStartReach; + const asked = reach && reach.lowest + 1n; + const start = first === undefined ? asked : asked === undefined ? first : min(first, asked); if (start === undefined) return undefined; - let end = start; - const grace = producer.#grace(); - for (const from of froms) { - const landable = producer.#landing({ start: from, end: UNBOUNDED, floor: true, grace }).next(); - end = max(end, landable.done ? UNBOUNDED : landable.value.start); - } + // The first key landable from a place only rises with it, so the highest bounds them all. + const from = first === undefined ? (reach?.highest ?? start) : max(first, reach?.highest ?? first); + const landable = producer.#landing({ start: from, end: UNBOUNDED, floor: true, grace: producer.#grace() }); + const next = landable.next(); + const end = next.done ? UNBOUNDED : next.value.start; return end > start ? { start, end } : undefined; }; timeStartFolds = (producer, time) => { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index c1a316c3c62..89ef1fba848 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -763,6 +763,9 @@ pub(crate) struct TrackState { // resolved one. Replaced whole as the ranges change, so it holds at most one entry per // time asked from. time_starts: BTreeMap, + // The highest start in `time_starts`, so the times its ranges may place are bounded + // without a walk of them all. + highest_time_start: Option, // Where the upstream publisher started each of a lite-07 copy's live wire ranges counting // back, by how many groups each counted, with the latest group it counted from: the copy // holds only part of the track, so it can't count back over its own groups. Replaced whole @@ -1330,35 +1333,25 @@ impl TrackState { /// The times [`Self::landed_through`] may place, wherever the groups and drops leave them: /// from the lowest time a first arrival or a reported range reaches, through the key below - /// the farthest one either may still land. Empty when it places none. + /// the first one the highest place it counts from may still land. The first key landable + /// from a place only rises with it, so that one bounds them all. Empty when it places none. fn landing_span(&self) -> ops::RangeInclusive { if !self.readable() { return Unplaced::NONE; } let first = self.arrivals.first(); - let reported = || { - self.time_starts - .iter() - .map(|(&asked, reported)| (asked, reported.start)) - }; - let lowest = first - .into_iter() - .chain(reported().map(|(asked, _)| asked.saturating_add(1))) - .min(); - let Some(lowest) = lowest else { + let asked = self.time_starts.keys().next().map(|asked| asked.saturating_add(1)); + let Some(lowest) = first.into_iter().chain(asked).min() else { return Unplaced::NONE; }; + let from = first.into_iter().chain(self.highest_time_start).max().unwrap_or(lowest); let grace = self.arrival_grace(); - let mut landable = lowest; - for from in first.into_iter().chain(reported().map(|(_, start)| start)) { - match self.find_landable(from..u64::MAX, true, grace, |open, _| Some(open.start)) { - Some(key) => landable = landable.max(key), - None => return lowest..=u64::MAX, - } - } - match landable.checked_sub(1) { - Some(last) => lowest..=last, - None => Unplaced::NONE, + match self.find_landable(from..u64::MAX, true, grace, |open, _| Some(open.start)) { + Some(key) => match key.checked_sub(1) { + Some(last) => lowest..=last, + None => Unplaced::NONE, + }, + None => lowest..=u64::MAX, } } @@ -4914,6 +4907,7 @@ impl Producer { if let Ok(mut state) = self.modify() && state.time_starts != starts { + state.highest_time_start = starts.values().map(|reported| reported.start).max(); state.time_starts = starts; state.placing += 1; } From 59bbe4d5b6923da2fc5cb55afa48d06ddd71cf5c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 09:55:44 -0800 Subject: [PATCH 06/17] fix(moq-net): a waiting time start holds an ordered read within its floor and budget, and counts from a real group Review fixes for the waiting time start park: - The park runs after the stale check and only on times at or past the read's floor (and a finite reader's cache floor), and the reader's own drift budget releases it as it does an owed group (`poll_drifted` / `#staleAt`). - `landed_through` counts from the feed's lowest real arrival (`first_arrival`), never a skip or forgotten drop the arrivals ledger accounted. - A placement never lands below where the counting started: a held group below it is not the group, and `from` itself only when nothing after it through the time was a group. - `Subscriber::poll_start` arms the grace wake an ordered read does. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ordered.test.ts | 74 ++++- js/net/src/track.ts | 94 ++++-- js/net/src/window.ts | 12 +- rs/moq-net/src/model/subscription/window.rs | 22 +- rs/moq-net/src/model/track.rs | 326 +++++++++++++++++--- 5 files changed, 424 insertions(+), 104 deletions(-) diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index f8974c631e1..fa507d18a2c 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -440,14 +440,18 @@ test("a folded time start wakes its read when the grace for a gap below it passe try { const producer = receivedTrack(); // An oldest-group reader holds the feed open from its answered start, 1000. - const oldest = producer.subscribe({ delay: { max: Duration.millis(50) } }); + const oldest = producer.subscribe({ delay: { max: Duration.secs(1) } }); hooks.startTrack(producer, { start: 1000n }); hooks.timeStarts(producer, [[0n, 1000n]]); - // Group 1000's stream reset before its header: only 2000 and the skips below it land. - producer.skipGroups(1001n, 2000n); + // Group 1000's stream reset before its header: only 1800, 2000, and the skips around them + // land, 1800 last, so the read's own budget for the time runs out after the gap's grace. + producer.skipGroups(1001n, 1800n); + producer.skipGroups(1801n, 2000n); put(producer, 2000n, false); + clock.jump(Duration.millis(10)); + put(producer, 1800n, false); const ordered = producer - .subscribe({ delay: { max: Duration.millis(50) }, ranges: [{ start: { time: 1500n } }] }) + .subscribe({ delay: { max: Duration.secs(1) }, ranges: [{ start: { time: 1500n } }] }) .ordered(); expect(items(ordered)).toEqual([]); let read: bigint | undefined; @@ -456,11 +460,12 @@ test("a folded time start wakes its read when the grace for a gap below it passe }); // Let the read settle into its wait, so only a timer can wake it. await flush(); - clock.jump(Duration.millis(49)); + clock.jump(Duration.millis(989)); await flush(); expect(read).toBeUndefined(); clock.jump(Duration.millis(1)); await flush(); + // 1800 came a full budget after 2000 superseded it. expect(read).toBe(2000n); await next; oldest.close(); @@ -469,6 +474,65 @@ test("a folded time start wakes its read when the grace for a gap below it passe } }); +test("a waiting time start holds an ordered read only within its budget", async () => { + const clock = new Manual(); + const installed = clock.install(); + try { + const producer = receivedTrack(); + put(producer, 2000n, false); + const ordered = producer + .subscribe({ delay: { max: Duration.millis(100) }, ranges: [{ start: { time: 1500n } }] }) + .ordered(); + expect(items(ordered)).toEqual([]); + let read: bigint | undefined; + const next = ordered.nextGroup().then((group) => { + read = group?.start; + }); + await flush(); + clock.jump(Duration.millis(99)); + await flush(); + expect(read).toBeUndefined(); + clock.jump(Duration.millis(1)); + await flush(); + expect(read).toBe(2000n); + await next; + } finally { + installed.close(); + } +}); + +test("a waiting time start below an ordered read's own floor holds nothing", () => { + const producer = receivedTrack(); + put(producer, 2000n, false); + const ordered = producer + .subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }) + .withRange({ start: { included: 2000n } }) + .ordered(); + expect(items(ordered)).toEqual(["group 2000"]); +}); + +test("a skip is no group to place a waiting time start from", () => { + const producer = receivedTrack(); + // GROUP 2000 says the group before it is 1000, which is still on its way. + producer.skipGroups(1001n, 2000n); + put(producer, 2000n, false); + const ordered = producer.subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }).ordered(); + expect(items(ordered)).toEqual([]); + put(producer, 1000n, false); + expect(items(ordered)).toEqual(["group 1000", "group 2000"]); +}); + +test("a folded time start never places below the range covering it", () => { + const producer = receivedTrack(); + put(producer, 500n, false); + hooks.timeStarts(producer, [[1000n, 1200n, 1500n]]); + // The covering range's first group was given up, and nothing lies between it and the time. + drop(producer, 1200n, 1201n); + producer.skipGroups(1201n, 1600n); + const subscriber = producer.subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }); + expect(hooks.servingResolved(subscriber)).toEqual([{ start: 1200n, end: undefined, floored: true }]); +}); + // Run every pending microtask and the ones they queue, as a timer firing would. async function flush() { for (let i = 0; i < 20; i++) await Promise.resolve(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 997489f4605..a0d7d8bd7c9 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1065,17 +1065,19 @@ interface TimeStart { through?: bigint; } -// Whether a received copy's live feed may land nothing more at or below `time` that could contain -// it, counting from `from` or its first arrival at or below `time`; assigned in the producer's -// static block. Mirrors the Rust `TrackState::landed_through`. -let landedThrough: (producer: Producer, time: bigint, from: bigint | undefined) => boolean; +// Where a received copy's live feed landed every key it could through `time`, counting from +// `covering`, where a range covering the time started, or else its first arrival at or below +// `time`: the group containing the time starts there or past it. Undefined while a key may still +// land, or with neither to count from. Assigned in the producer's static block. Mirrors the Rust +// `TrackState::landing_from` and `TrackState::landed_through`. +let landedFrom: (producer: Producer, time: bigint, covering: bigint | undefined) => bigint | undefined; // When a received copy's time start at `time`, waiting to be placed, next may place itself with no -// change to wake its readers: the grace of the lowest gap {@link landedThrough} still waits on +// change to wake its readers: the grace of the lowest gap {@link landedFrom} still waits on // folds. Mirrors the Rust `TrackState::time_start_folds`. let timeStartFolds: (producer: Producer, time: bigint) => Instant | undefined; -// The times {@link landedThrough} may place on a received copy, wherever its groups and drops leave +// The times {@link landedFrom} may place on a received copy, wherever its groups and drops leave // them: from the lowest time a first arrival or a reported range reaches, up to the farthest key // one of them may still land. Mirrors the Rust `TrackState::landing_span`. let landingSpan: (producer: Producer) => Run | undefined; @@ -1416,6 +1418,10 @@ export class Producer { // subscription's grace after a later arrival opened it, as the session's tail does, so this // holds only the gaps still worth waiting for. #arrivals = new Ledger(); + // The lowest key the live feed delivered a group at. Unlike the arrivals' first key, never one + // a drop, a skip, or a forgotten run accounted, so a group starts there. Mirrors the Rust + // `first_arrival`. + #firstArrival: bigint | undefined; // What this track's own publisher produced since its last append, which skips every // key below it that never arrived and was not given up: the producer moved on. The // arrivals cannot say so, since a gap folds into them once nobody waits for it. Only @@ -1637,9 +1643,13 @@ export class Producer { // The answer may place a time start an ordered read waits at. for (const state of [producer.#state, ...producer.#sinks]) state.timelineChanged.update((rev) => rev + 1); }; - landedThrough = (producer, time, from) => producer.#landingThrough(time, from) === undefined; + landedFrom = (producer, time, covering) => { + const from = producer.#landingFrom(time, covering); + if (from === undefined || producer.#landingThrough(time, from) !== undefined) return undefined; + return from; + }; landingSpan = (producer) => { - const first = producer.#arrivals.first(); + const first = producer.#firstArrival; const reach = producer.#timeStartReach; const asked = reach && reach.lowest + 1n; const start = first === undefined ? asked : asked === undefined ? first : min(first, asked); @@ -1653,7 +1663,8 @@ export class Producer { }; timeStartFolds = (producer, time) => { if (!producer.#wire || producer.#timeStarts.has(time)) return undefined; - return producer.#landingThrough(time, coveringStart(producer.#timeStarts, time))?.folds; + const from = producer.#landingFrom(time, coveringStart(producer.#timeStarts, time)); + return from === undefined ? undefined : producer.#landingThrough(time, from)?.folds; }; hooks.relativeStarts = (producer, starts) => { producer.#relativeStarts = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); @@ -1761,13 +1772,18 @@ export class Producer { }; } - // Undefined once {@link landedThrough} holds, else the lowest gap it still waits on, with when - // its grace folds, if it ever does. - #landingThrough(time: bigint, from: bigint | undefined): { folds?: Instant } | undefined { - const first = this.#arrivals.first(); - const start = from ?? (first !== undefined && first <= time ? first : undefined); - if (start === undefined) return {}; - const landing = this.#landing({ start, end: time + 1n, floor: true, grace: this.#grace() }).next(); + // Where the group containing `time` starts at the earliest: `covering`, or else the feed's first + // arrival at or below `time`. Each is a group; a skip or a forgotten run names none. Mirrors the + // Rust `TrackState::landing_from`. + #landingFrom(time: bigint, covering: bigint | undefined): bigint | undefined { + const first = this.#firstArrival; + return covering ?? (first !== undefined && first <= time ? first : undefined); + } + + // Undefined once the live feed landed every key it could from `from` through `time`, else the + // lowest gap it still waits on, with when its grace folds, if it ever does. + #landingThrough(time: bigint, from: bigint): { folds?: Instant } | undefined { + const landing = this.#landing({ start: from, end: time + 1n, floor: true, grace: this.#grace() }).next(); return landing.done ? undefined : { folds: landing.value.folds }; } @@ -2418,7 +2434,10 @@ export class Producer { insertKey(this.#keys, group.start); this.#fills.arrived(group.start); this.#advance(group.start, !fetched); - if (!fetched) this.#arrive(group.start); + if (!fetched) { + this.#firstArrival = this.#firstArrival === undefined ? group.start : min(this.#firstArrival, group.start); + this.#arrive(group.start); + } this.#produce(group.start); // A newer group demotes the live edge to an evictable one. const edge = this.#edgeEntry(); @@ -3731,12 +3750,16 @@ export class Subscriber { return { group: below }; } if (below === undefined && drops.covers(0n, time + 1n)) return "before"; - // The live feed accounted for every key it could land through the time, so the group - // containing it came already: the newest one held, or one since evicted. - if (producer && landedThrough(producer, time, covering)) { - return below !== undefined ? { group: below } : "before"; - } - return "unknown"; + // The live feed accounted for every key it could land from a group at `from` through the + // time, so the group containing it came already, at `from` or past it: the newest one held + // there, or `from` itself when nothing after it through the time was a group. A group held + // below it, such as one fetched, is not it. Else it was evicted, and reaching back to the + // oldest group looks it up. + const from = producer && landedFrom(producer, time, covering); + if (from === undefined) return "unknown"; + if (below !== undefined && below >= from) return { group: below }; + if (from <= time && drops.covers(from + 1n, time + 1n)) return { group: from }; + return "before"; } if (below !== undefined) return { group: below }; // Every key at or before it is gone, so whichever group held it will never arrive. @@ -5487,18 +5510,23 @@ export class Subscriber { } // In order, the group a waiting time start resolves to comes first: it starts at or before that - // time, below `key`, and may still land. Its landing, the answer placing the start, or the grace - // for a gap below it folding wakes the read; a closed track places the start. - #waitTimeStart(key: bigint): boolean { - const waiting = this.#window.waitingBelow(key); - if (waiting.length === 0) return false; + // time, below `key`, and may still land at or past the read's floor `start`, until the budget + // gives up on it as on an owed group. Its landing, the answer placing the start, the grace for a + // gap below it folding, or that verdict wakes the read; a closed track places the start. + // Mirrors the Rust `Cursor::poll_time_start`. + #waitTimeStart(start: bigint, key: bigint, drift: Drift): boolean { const producer = this.#state.producer; - for (const time of waiting) { + let waits = false; + for (const time of this.#window.waitingIn(start, key)) { + // Its successor is this group's or one below: the group containing the time is judged as an + // owed group at it would be. + if (this.#staleAt(time, drift, true)) continue; const folds = producer && timeStartFolds(producer, time); if (folds !== undefined) this.#wakeAt(folds.since(Instant.ZERO).asNanos()); + waits = true; } - this.#state.waitingOrdered = true; - return true; + if (waits) this.#state.waitingOrdered = true; + return waits; } // A missing group the subscription still owes holds sequence order until it lands, @@ -5535,8 +5563,8 @@ export class Subscriber { for (const group of this.#state.groups.peek()) { if (group.start < start || !this.#window.contains(group.start)) continue; if (cursor.end !== undefined && group.start >= cursor.end) return undefined; - if (ordered && this.#waitTimeStart(group.start)) return undefined; if (!this.#delivered.has(group.start) && !this.#staleAt(group.start, drift)) { + if (ordered && this.#waitTimeStart(start, group.start, drift)) return undefined; return group.start; } } @@ -5582,7 +5610,6 @@ export class Subscriber { index++; continue; } - if (this.#waitTimeStart(group.start)) return { kind: "idle" }; // Every frame this group could still hold is past the budget, so keep // scanning: one pass walks a whole backlog off rather than replaying it. if (this.#staleAt(group.start, drift)) { @@ -5600,6 +5627,7 @@ export class Subscriber { group.close(); continue; } + if (this.#waitTimeStart(start, group.start, drift)) return { kind: "idle" }; if (this.#waitOwed(start, group.start, drift)) return { kind: "idle" }; // A group missing its head comes from the fill asked for it, in its turn. if (this.#lacksHead(group)) { diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 5627f3be557..e3e022fa416 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -582,15 +582,15 @@ export class Window { } /** - * The times of the time starts still waiting on the groups held that may resolve below `key`: - * the group containing each starts at or before its time, so it may still land below `key`. - * Mirrors the Rust `Window::waiting_below`. + * The times in `[start, end)` of the time starts still waiting on the groups held. The group + * containing each starts at or before its time, so one at or past a read's floor and below a + * group may still land between them. Mirrors the Rust `Window::waiting_in`. */ - waitingBelow(key: bigint): bigint[] { + waitingIn(start: bigint, end: bigint): bigint[] { const times: bigint[] = []; for (const entry of this.#waiting.all) { - if (entry.time >= key) break; - times.push(entry.time); + if (entry.time >= end) break; + if (entry.time >= start) times.push(entry.time); } return times; } diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 61de9f773c7..864122e056b 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -354,15 +354,19 @@ impl Window { !self.waiting.is_empty() } - /// Whether a time start still waiting on the groups held may resolve below `key`: the - /// group containing its time starts at or before it, so it may still land below `key`. - pub fn waits_below(&self, key: u64) -> bool { - self.waiting_below(key).next().is_some() - } - - /// The times of the waiting time starts before `key`, as [`Self::waits_below`] finds them. - pub fn waiting_below(&self, key: u64) -> impl Iterator + '_ { - self.waiting.range(..(key, 0)).map(|&(time, _)| time) + /// The times in `times` of the time starts still waiting on the groups held. The group + /// containing each starts at or before its time, so one at or past a read's floor and + /// below a group may still land between them. + pub fn waiting_in + 'static>(&self, times: R) -> impl Iterator + '_ { + let from = match times.start_bound() { + ops::Bound::Included(&start) => start, + ops::Bound::Excluded(&start) => start.saturating_add(1), + ops::Bound::Unbounded => 0, + }; + self.waiting + .range((from, 0)..) + .map(|&(time, _)| time) + .take_while(move |time| times.contains(time)) } /// Resolve what the groups now held settle: relative anchors once `latest` exists, and diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 89ef1fba848..055f45a5c82 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -736,6 +736,9 @@ pub(crate) struct TrackState { // away a subscription's grace after a later arrival opened it, as the session's tail // does, so this holds only the gaps still worth waiting for. arrivals: Ledger, + // The lowest key the live feed delivered a group at. Unlike the arrivals' first key, never + // one a forgotten drop or skip accounted, so a group starts there. + first_arrival: Option, // What `arrivals` bills to the pool, [`ledger::RUN_OVERHEAD`] per gap. Paying eviction // debt folds its oldest gaps early, so an unbounded grace holds no more gaps than the // pool would. @@ -1311,13 +1314,22 @@ impl TrackState { Containing::Group(key) } None if self.dropped_cover(0..time.saturating_add(1)) => Containing::Before, - // The live feed accounted for every key it could land through the time, so the - // group containing it came already: the newest one held, or one since evicted, which - // reaches back to the oldest group as on the publisher's own track. - held if self.landed_through(time, covering) => { - held.map_or(Containing::Before, |(&key, _)| Containing::Group(key)) - } - _ => Containing::Unknown, + // The live feed accounted for every key it could land from a group at `from` + // through the time, so the group containing it came already, at `from` or past it: + // the newest one held there, or `from` itself when nothing after it through the + // time was a group. A group held below it, such as one fetched, is not it. Else it + // was evicted, and reaching back to the oldest group, as on the publisher's own + // track, looks it up. + held => match self.landing_from(time, covering) { + Some(from) if self.landed_through(time, from) => match held { + Some((&key, _)) if key >= from => Containing::Group(key), + _ if from <= time && self.dropped_cover(from.saturating_add(1)..time.saturating_add(1)) => { + Containing::Group(from) + } + _ => Containing::Before, + }, + _ => Containing::Unknown, + }, } } @@ -1339,7 +1351,7 @@ impl TrackState { if !self.readable() { return Unplaced::NONE; } - let first = self.arrivals.first(); + let first = self.first_arrival; let asked = self.time_starts.keys().next().map(|asked| asked.saturating_add(1)); let Some(lowest) = first.into_iter().chain(asked).min() else { return Unplaced::NONE; @@ -1362,29 +1374,34 @@ impl TrackState { if !self.anchors.received() || self.time_starts.contains_key(&time) { return None; } - self.landing_through(time, self.covering_start(time)).flatten() + let from = self.landing_from(time, self.covering_start(time))?; + self.landing_through(time, from).flatten() + } + + /// Where the group containing `time` starts at the earliest, which [`Self::landed_through`] + /// counts from: `covering`, where a range covering the time started, or else the feed's + /// first arrival at or below `time`. Each is a group, so the one containing the time is + /// there or past it, and a range added below changes nothing of it. A skip or a forgotten + /// drop names no group. Arrival order is not key order, so with neither, a lower key may + /// still land. + fn landing_from(&self, time: u64, covering: Option) -> Option { + covering.or_else(|| self.first_arrival.filter(|&first| first <= time)) } /// Whether the live feed may land nothing more at or below `time` that could contain it: - /// every key from `from`, where a range covering the time started, or else from the feed's - /// first arrival at or below `time`, through `time` arrived, was dropped, or is no longer - /// waited for. A covering range that started past the time served nothing at or before - /// it. Either names where the feed's groups begin, so a range added below changes nothing - /// of the group containing the time. Arrival order is not key order, so with neither, a - /// lower key may still land. - fn landed_through(&self, time: u64, from: Option) -> bool { + /// every key from `from` (see [`Self::landing_from`]) through `time` arrived, was dropped, + /// or is no longer waited for. A covering range that started past the time served nothing + /// at or before it. + fn landed_through(&self, time: u64, from: u64) -> bool { self.landing_through(time, from).is_none() } /// `None` once [`Self::landed_through`] holds, else `Some` of when the arrival grace of the /// lowest gap it still waits on folds, if it ever does. - fn landing_through(&self, time: u64, from: Option) -> Option> { + fn landing_through(&self, time: u64, from: u64) -> Option> { if !self.readable() { return Some(None); } - let Some(from) = from.or_else(|| self.arrivals.first().filter(|&first| first <= time)) else { - return Some(None); - }; self.find_landable(from..time.saturating_add(1), true, self.arrival_grace(), |_, folds| { Some(folds) }) @@ -2836,6 +2853,7 @@ impl TrackState { /// Record that the live feed delivered `key`, then fold the gaps that aged out. fn record_arrival(&mut self, key: u64, now: crate::time::Instant) { self.arrivals.arrive(key, now); + self.first_arrival = Some(self.first_arrival.map_or(key, |first| first.min(key))); if !self.anchors.received() && self.appended.is_none_or(|appended| key >= appended) && let Some(end) = key.checked_add(1) @@ -9486,6 +9504,53 @@ impl Cursor { }) } + /// Pending while a time start in `times` waits to be placed and the group containing it, + /// at or before its time and so below `times.end`, may still land at or past the read's + /// floor `times.start` within the drift budget. Its landing, the answer placing the start, + /// the grace for a gap below it folding, or the budget giving up on it wakes us; a closed + /// track places it. `Ready(true)` when a change placed one meanwhile, so the caller looks + /// again. + fn poll_time_start(&mut self, times: ops::Range, drift: &Drift, waiter: &kio::Waiter) -> Poll { + if self.filter.window.is_settled() || self.filter.window.waiting_in(times.clone()).next().is_none() { + return Poll::Ready(false); + } + let window = &self.filter.window; + let waited = std::cell::RefCell::new(Vec::new()); + let closed = self + .poll(waiter, |state| { + waited.borrow_mut().clear(); + // An unjudgeable old gap cannot hold a finite read, as for an owed group. + let floor = times.start.max(state.cache_floor(drift.budget)); + for time in window.waiting_in(floor..times.end) { + // Its successor is this group's or one below: the group containing the + // time is judged as an owed group at it would be. + if state.poll_drifted(time, &drift.edge.servable, drift.budget, None, waiter) { + continue; + } + if let Some(folds) = state.time_start_folds(time) { + state.cache.watch_wall(folds, waiter); + } + waited.borrow_mut().push(time); + } + Poll::>::Pending + }) + .is_ready(); + let waited = waited.into_inner(); + if closed || waited.is_empty() { + return Poll::Ready(false); + } + // Registered before syncing again, so a change placing one meanwhile is not missed. + self.sync(); + let window = &self.filter.window; + if waited + .iter() + .any(|&time| window.waiting_in(time..=time).next().is_none()) + { + return Poll::Ready(true); + } + Poll::Pending + } + /// Whether the drift budget says to skip `group`, against a [`Drift`] already resolved /// for this poll. fn poll_stale(&self, group: &group::Consumer, drift: &Drift, waiter: &kio::Waiter) -> Poll> { @@ -10142,31 +10207,6 @@ impl Cursor { floor = floor.max(self.floor()).max(group.start.saturating_add(1)); continue; } - // In order means the group a waiting time start resolves to comes first: it - // starts at or before that time, below this group, and may still land. Its - // landing, the answer placing the start, or the grace for a gap below it folding - // wakes us. Registered before syncing again, so a change placing it meanwhile is - // not missed; a closed track places it. - if self.filter.window.waits_below(group.start) { - let window = &self.filter.window; - let closed = self - .poll(waiter, |state| { - for time in window.waiting_below(group.start) { - if let Some(folds) = state.time_start_folds(time) { - state.cache.watch_wall(folds, waiter); - } - } - Poll::>::Pending - }) - .is_ready(); - self.sync(); - if !self.filter.window.waits_below(group.start) { - continue; - } - if !closed { - return Poll::Pending; - } - } } // Skip a group the budget has given up on and keep scanning, so one poll @@ -10181,6 +10221,12 @@ impl Cursor { continue; } + // In order means the group a waiting time start resolves to comes first, until the + // budget gives up on it. + if ready!(self.poll_time_start(from..group.start, &drift, waiter)) { + continue; + } + // In order means a group the live feed still owes below this one comes first, // until the budget gives up on it. Its landing, its drop, or that verdict wakes us. ready!(self.poll_owed(from..group.start, &drift, waiter))?; @@ -10921,9 +10967,21 @@ impl Subscriber { let start = ready!(Consumer::poll_state_start(&cursor.state, waiter)); cursor.sync(); if cursor.filter.window.is_waiting() { - // Registered before syncing again, so a change placing it after this wakes it. A - // closed track places it at once, since nothing more can arrive. - let closed = cursor.state.poll(waiter, |_| Poll::<()>::Pending).is_ready(); + // Registered before syncing again, so a change placing it after this wakes it, as + // does the grace for a gap below one folding. A closed track places it at once, + // since nothing more can arrive. + let window = &cursor.filter.window; + let closed = cursor + .state + .poll(waiter, |state| { + for time in window.waiting_in(..) { + if let Some(folds) = state.time_start_folds(time) { + state.cache.watch_wall(folds, waiter); + } + } + Poll::<()>::Pending + }) + .is_ready(); cursor.sync(); if !closed && cursor.filter.window.is_waiting() { return Poll::Pending; @@ -15914,7 +15972,8 @@ mod test { /// A time start folded into an open range waits out the grace of a gap below its time, /// where the group containing it may still land, and that grace folding alone wakes an - /// ordered read parked on it, with no change to the track. + /// ordered read parked on it, with no change to the track: the group after the time + /// arrived later than the gap opened, so the read's own budget runs out after it. #[kio::test] #[cfg(feature = "async")] async fn a_folded_time_start_wakes_its_read_when_a_gap_below_folds() { @@ -15925,9 +15984,14 @@ mod test { elapse(&producer, Duration::ZERO); producer.start_resolved(1000).unwrap(); producer.time_starts([(0, 1000, None)]); - // Group 1000's stream reset before its header: only 2000 and the skips below it land. - producer.skip_groups(1001..2000); + // Group 1000's stream reset before its header: only 1800, 2000, and the skips around + // them land, 1800 last. + producer.skip_groups(1001..1800); + producer.skip_groups(1801..2000); write_group(&producer, 2000); + elapse(&producer, Duration::from_millis(10)); + write_group(&producer, 1800); + let grace = grace - Duration::from_millis(10); let mut ordered = producer .subscribe(replay().with_ranges([Range { start: Anchor::Time(1500), @@ -15953,9 +16017,169 @@ mod test { Item::Gap { .. } => {} } } + // 1800 came a full budget after 2000 superseded it. + assert_eq!(read, [2000]); + } + + /// A start [`Subscriber::poll_start`] waits on, folded into an open range, wakes it when the + /// grace of a gap below its time folds, with no change to the track. + #[kio::test] + #[cfg(feature = "async")] + async fn a_folded_time_start_wakes_poll_start_when_a_gap_below_folds() { + let grace = replay().budget(); + let mut producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + elapse(&producer, Duration::ZERO); + producer.start_resolved(1000).unwrap(); + producer.time_starts([(0, 1000, None)]); + producer.skip_groups(1001..2000); + write_group(&producer, 2000); + let mut subscriber = producer.subscribe(replay().with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }])); + + let woken = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let waiter = kio::Waiter::new(futures::task::waker(Arc::new(FlagWake(woken.clone())))); + assert!(subscriber.poll_start(&waiter).is_pending(), "1000 may still land"); + elapse(&producer, grace - Duration::from_millis(1)); + assert!(!woken.load(Ordering::SeqCst), "woken before the grace"); + assert!(subscriber.poll_start(&waiter).is_pending()); + elapse(&producer, Duration::from_millis(1)); + assert!(woken.load(Ordering::SeqCst), "the grace never woke the start"); + assert!(subscriber.poll_start(&waiter).is_ready()); + } + + /// A finite budget gives up on the group a waiting time start resolves to as it would on an + /// owed one: once the later group it holds is a full budget old, an ordered read hands it + /// out rather than wait for an answer that may never come. + #[kio::test] + #[cfg(feature = "async")] + async fn a_waiting_time_start_holds_a_read_only_within_its_budget() { + let budget = Duration::from_millis(100); + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + elapse(&producer, Duration::ZERO); + write_group(&producer, 2000); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(budget)), + ) + .ordered(); + + let woken = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let waiter = kio::Waiter::new(futures::task::waker(Arc::new(FlagWake(woken.clone())))); + assert!( + ordered.poll_next_group(&waiter).is_pending(), + "the group at 1500 may land" + ); + elapse(&producer, budget - Duration::from_millis(1)); + assert!(!woken.load(Ordering::SeqCst), "woken before the budget"); + assert!(ordered.poll_next_group(&waiter).is_pending()); + elapse(&producer, Duration::from_millis(1)); + assert!(woken.load(Ordering::SeqCst), "the budget never woke the read"); + let mut read = Vec::new(); + while let Poll::Ready(item) = ordered.poll_next_group(&waiter) { + if let Item::Group(group) = item.unwrap().expect("an item") { + read.push(group.start); + break; + } + } assert_eq!(read, [2000]); } + /// A waiting time start below an ordered read's own floor holds nothing: the group + /// containing its time starts at or before it, which the read never hands out. + #[test] + #[cfg(feature = "async")] + fn a_waiting_time_start_below_the_read_floor_holds_nothing() { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .with_range(2000..) + .ordered(); + match ordered.next_group().now_or_never() { + Some(Ok(Some(Item::Group(group)))) => assert_eq!(group.start, 2000), + other => panic!("2000 waited: {:?}", other.map(|res| res.map(|item| item.is_some()))), + } + } + + /// A skip, even one forgotten into the feed's accounting, names no group, so it can't stand + /// in for the feed's first arrival a waiting time start counts from: the group containing + /// the time may still land below it. + #[test] + #[cfg(feature = "async")] + fn a_forgotten_skip_is_no_group_to_place_a_time_start_from() { + let mut producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + // Asked from the live edge, so the feed's accounting starts wherever it lands. + producer.request_start(None).unwrap(); + // GROUP 2000 says the group before it is 1000, which is still on its way. + producer.skip_groups(1001..2000); + write_group(&producer, 2000); + producer.forget_drops(1001..2000); + assert_eq!( + producer.state.read().arrivals.first(), + Some(1001), + "the skip is accounted" + ); + assert_eq!(producer.state.read().containing(1500), Containing::Unknown); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + let mut next = || match ordered.next_group().now_or_never() { + None => None, + Some(Ok(Some(Item::Group(group)))) => Some(group.start), + Some(res) => panic!("not a group: {:?}", res.map(|item| item.is_some())), + }; + assert_eq!(next(), None, "1000 may still land"); + write_group(&producer, 1000); + assert_eq!(next(), Some(1000)); + assert_eq!(next(), Some(2000)); + } + + /// A time start folded into a range that started at a group since cut places at that + /// range's start, never at an older group held below it, which the range doesn't serve. + #[test] + fn a_folded_time_start_never_places_below_its_covering_range() { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 500); + producer.time_starts([(1000, 1200, Some(1500))]); + let cut = producer.create_group(group::Info { start: 1200 }).unwrap(); + cut.abort(Error::Cancel).unwrap(); + producer.modify().unwrap().reclaim_aborted(1200); + producer.skip_groups(1201..1600); + assert!(!producer.state.read().lookup.contains_key(&1200), "1200 is cut"); + assert_eq!(producer.state.read().containing(1500), Containing::Group(1200)); + } + /// A lite-07 copy holds only part of the track, so a relative start counts back as its /// publisher reported, not over the groups the copy holds, until a group past the latest /// the publisher counted from moves the live edge. From 868fe85e498ed9c587370f93403ea59e5523e10e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 11:01:41 -0800 Subject: [PATCH 07/17] fix(moq-net): a range answered empty holds back no later group within its budget A lite-07 Range Start of 0 says the range has no group to deliver. When the range's end takes in its own time, the session now reports it to the copy as an empty answer under the narrowest budget the subscription asked with (`Answer::Empty`), so an ordered read within that budget stops waiting at that time, or one folded into it: the live feed lands none of the range's groups. A start depends on the budget it was asked with, so a wider reader keeps waiting for the range's own answer, and the empty answer places neither time. js/net mirrors it. Adds the follow-up quest for the relay stall an empty upstream range already causes on main. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 12 +- js/net/src/lite/publisher.test.ts | 4 +- js/net/src/lite/subscriber.test.ts | 47 ++++- js/net/src/lite/subscriber.ts | 26 ++- js/net/src/ordered.test.ts | 41 +++- js/net/src/resume.test.ts | 4 +- js/net/src/track.ts | 87 ++++++--- quest/m1/empty-range-relay.md | 13 ++ .../proptest-regressions/model/track.txt | 1 + rs/moq-net/src/lite/subscriber.rs | 87 ++++++++- rs/moq-net/src/model/track.rs | 182 ++++++++++++++---- 11 files changed, 406 insertions(+), 98 deletions(-) create mode 100644 quest/m1/empty-range-relay.md diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 1985cbae360..2076789ce5a 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -744,11 +744,17 @@ export const hooks: { startsAtKeys: (producer: Producer) => void; /** * Record where the upstream publisher started each of a lite-07 copy's live wire ranges, as - * `[time, start]` pairs by the time each asked from, replacing the last set: a reader's time - * start the copy cannot place among its own groups resolves to it. Mirrors the Rust + * `[time, start, through]` by the time each asked from (0 for the oldest group), `start` + * undefined for one with no group to deliver, replacing the last set: a reader's time start the + * copy cannot place among its own groups resolves to it. An empty answer holds for a reader + * within `budget`, the narrowest the subscription asked in milliseconds. Mirrors the Rust * `track::Producer::time_starts`; assigned by `track.ts`. */ - timeStarts: (producer: Producer, starts: Iterable) => void; + timeStarts: ( + producer: Producer, + starts: Iterable, + budget: number, + ) => void; /** * Tell a lite-07 copy where its publisher started each live range counting back, as * `[back, start, latest]` triples, replacing the last set: the copy holds only part of the diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 86f5cd3c6d3..abb09add39a 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1658,7 +1658,7 @@ for (const version of [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07]) { try { hooks.receiveTrack(sub.track); // A lite-07 source reports where it started the time asked for. - if (hasRanges(version)) hooks.timeStarts(sub.track, [[2n, source]]); + if (hasRanges(version)) hooks.timeStarts(sub.track, [[2n, source]], NO_MAX_DELAY); hooks.startTrack(sub.track, { start: source, requested: 2n }); sub.serve(6n); expect(await sub.servedSequence()).toBe(6n); @@ -1686,7 +1686,7 @@ for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { hooks.startTrack(sub.track, { requested: 2n }); sub.serve(6n); await flush(); - if (hasRanges(version)) hooks.timeStarts(sub.track, [[2n, 3n]]); + if (hasRanges(version)) hooks.timeStarts(sub.track, [[2n, 3n]], NO_MAX_DELAY); hooks.startTrack(sub.track, { start: 3n, requested: 2n }); const response = await decodeSubscribeResponse(sub.client.reader, version); if (!("start" in response)) throw new Error("expected SUBSCRIBE_START"); diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index b4012c01a12..9b2375f40aa 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -23,7 +23,7 @@ import { import type { Consumer as GroupConsumer } from "../group.ts"; import { type Hop, isAnonymous, MAX_HOPS, toHop, UNKNOWN_HOP, WireRoute } from "../hop.ts"; import * as Instance from "../instance.ts"; -import { hooks } from "../internal.ts"; +import { hooks, NO_MAX_DELAY } from "../internal.ts"; import { GRACE } from "../ledger.ts"; import * as Path from "../path.ts"; import { Reader, Writer } from "../stream.ts"; @@ -2883,6 +2883,51 @@ test("a widened range's SUBSCRIBE_STARTED resolves the feed's start", async () = } }); +// A range the publisher answers with no group to deliver lands none of its groups, so the copy is +// told, and an ordered read holds no later group back for its time. Only one whose end takes in its +// time says so: one ending by a count back may still have skipped the group containing it. +test("a lite-07 range answered empty is reported only for a range reaching its time", async () => { + const version = Version.DRAFT_07; + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, toHop(1n)); + const broadcast = subscriber.consume(Path.from("room")); + const reported: (readonly [bigint, bigint | undefined, (bigint | undefined)?])[][] = []; + const timeStarts = hooks.timeStarts; + const budgets: number[] = []; + hooks.timeStarts = (producer, starts, budget) => { + reported.push([...starts]); + budgets.push(budget); + timeStarts(producer, starts, budget); + }; + const track = broadcast.track("video").subscribe({ + delay: { max: null }, + ranges: [ + { start: { time: 10n }, end: { time: 20n } }, + { start: { time: 30n }, end: { relative: 0n } }, + { start: { time: 66n } }, + ], + }); + try { + await opened(0); + await answerTrackInfo(streams[0], version); + const request = await opened(1); + await request.reading; + await respond(request, version, [{ start: new SubscribeStart([undefined, undefined, 66n]) }]); + await drainReactions(); + expect(reported.at(-1)).toEqual([ + [10n, undefined, 20n], + [66n, 66n, undefined], + ]); + // Under the narrowest budget the subscription asked with, here none. + expect(budgets.at(-1)).toEqual(NO_MAX_DELAY); + } finally { + hooks.timeStarts = timeStarts; + track.close(); + broadcast.close(); + subscriber.close(); + } +}); + // A lite-07 stale (OLD) drop accounts for its groups for good, whichever of it and a group's stream // lands first: an update owes none of them again, and a group still arriving, such as one another // subscription sharing the copy delivers, is never aborted by the drop. diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 8df4a8a6f2c..fa49445e16b 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2875,7 +2875,7 @@ export class Subscriber { // Where the publisher started each live range asked from a group's start or the oldest group // (time 0), with the last time it reaches, which a reader's time start there or folded into it // resolves to when the copy holds no group at it. Mirrors the Rust `SubStream::report_starts`. - const times: [bigint, bigint, bigint | undefined][] = []; + const times: [bigint, bigint | undefined, bigint | undefined][] = []; // And each one counting back, which the copy, holding only part of the track, can't count, // with the latest group it counted back from. const relative: [bigint, bigint, bigint | undefined][] = []; @@ -2883,15 +2883,19 @@ export class Subscriber { const range = entry.live.get(id); const time = range?.start?.time ?? range?.start?.absolute; const back = range?.start?.relative; + if (!entry.starts.has(id)) continue; + // Undefined for a range with no group to deliver. const at = entry.starts.get(id); - if (at === undefined) continue; - if (back !== undefined) relative.push([back, at, entry.edges.get(id)]); - else if (time !== undefined || range?.start === undefined) { + if (back !== undefined) { + if (at !== undefined) relative.push([back, at, entry.edges.get(id)]); + } else if (time !== undefined || range?.start === undefined) { const asked = time ?? 0n; - times.push([asked, at, through(range?.end, asked)]); + if (at !== undefined || reaches(range?.end, asked)) times.push([asked, at, through(range?.end, asked)]); } } - hooks.timeStarts(entry.track, times); + // The wire does not say which budget a range resolved under, so an empty one holds only for the + // narrowest asked. + hooks.timeStarts(entry.track, times, entry.staleBudget); hooks.relativeStarts(entry.track, relative); let start: bigint | null = null; @@ -3435,6 +3439,16 @@ class ConsumeBroadcast extends broadcast.Consumer { // The last time a lite-07 range ending at `end` reaches, undefined while it follows the live edge: // an exclusive group end stops at the time before it, and a relative end, unknown here, at the // range's own time `asked`. Mirrors the Rust `SubStream::report_starts`. +// Whether a lite-07 range with no group to deliver says its publisher held none at or before its +// reach: only one whose end takes in the group containing its time, which a range that ends before +// its time, or by a count back, may not. Mirrors the Rust `SubStream::report_starts`. +function reaches(end: WireAnchor | undefined, asked: bigint): boolean { + if (end === undefined) return false; + if (end.time !== undefined) return end.time >= asked; + if (end.absolute !== undefined) return end.absolute > asked; + return false; +} + function through(end: WireAnchor | undefined, asked: bigint): bigint | undefined { if (end === undefined) return undefined; if (end.time !== undefined) return end.time; diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index fa507d18a2c..50edd177d66 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -4,7 +4,7 @@ import { expect, test } from "bun:test"; import { Duration, sleep } from "@moq/clock"; import { Manual } from "@moq/clock/test"; import { StreamCode, StreamError } from "./error.ts"; -import { hooks, staleHole } from "./internal.ts"; +import { hooks, NO_MAX_DELAY, staleHole } from "./internal.ts"; import { textFrame } from "./mock.ts"; import { Timestamp } from "./time.ts"; import { type Ordered, Producer as TrackProducer } from "./track.ts"; @@ -413,7 +413,7 @@ test("an ordered read hands out nothing past a time start still waiting to be pl expect(items(ordered)).toEqual([]); // A session's tail holds the read at the answered start's group until it lands, and says // nothing lies between it and 2000. - hooks.timeStarts(producer, [[1500n, 1000n]]); + hooks.timeStarts(producer, [[1500n, 1000n]], NO_MAX_DELAY); put(producer, 1000n, false); producer.skipGroups(1001n, 2000n); expect(items(ordered)).toEqual(["group 1000", "group 2000"]); @@ -424,13 +424,17 @@ test("a time start folded upstream into an earlier range places from the range c put(producer, 2000n, false); const subscriber = producer.subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }); // A range asked from 1000 that ends at 1200 doesn't reach the time. - hooks.timeStarts(producer, [[1000n, 1000n, 1200n]]); + hooks.timeStarts(producer, [[1000n, 1000n, 1200n]], NO_MAX_DELAY); expect(hooks.servingResolved(subscriber)).toEqual([undefined]); // The oldest group's range, answered at 2000, served nothing at or before it. - hooks.timeStarts(producer, [ - [1000n, 1000n, 1200n], - [0n, 2000n, undefined], - ]); + hooks.timeStarts( + producer, + [ + [1000n, 1000n, 1200n], + [0n, 2000n, undefined], + ], + NO_MAX_DELAY, + ); expect(hooks.servingResolved(subscriber)).toEqual([{ start: 0n, end: undefined, floored: false }]); }); @@ -442,7 +446,7 @@ test("a folded time start wakes its read when the grace for a gap below it passe // An oldest-group reader holds the feed open from its answered start, 1000. const oldest = producer.subscribe({ delay: { max: Duration.secs(1) } }); hooks.startTrack(producer, { start: 1000n }); - hooks.timeStarts(producer, [[0n, 1000n]]); + hooks.timeStarts(producer, [[0n, 1000n]], NO_MAX_DELAY); // Group 1000's stream reset before its header: only 1800, 2000, and the skips around them // land, 1800 last, so the read's own budget for the time runs out after the gap's grace. producer.skipGroups(1001n, 1800n); @@ -525,7 +529,7 @@ test("a skip is no group to place a waiting time start from", () => { test("a folded time start never places below the range covering it", () => { const producer = receivedTrack(); put(producer, 500n, false); - hooks.timeStarts(producer, [[1000n, 1200n, 1500n]]); + hooks.timeStarts(producer, [[1000n, 1200n, 1500n]], NO_MAX_DELAY); // The covering range's first group was given up, and nothing lies between it and the time. drop(producer, 1200n, 1201n); producer.skipGroups(1201n, 1600n); @@ -533,6 +537,25 @@ test("a folded time start never places below the range covering it", () => { expect(hooks.servingResolved(subscriber)).toEqual([{ start: 1200n, end: undefined, floored: true }]); }); +test("a range answered empty holds back no later group within its budget, though it places nothing", () => { + const producer = receivedTrack(); + put(producer, 2000n, false); + // Asked with a one second budget: the answer says nothing about a group a wider reader still wants. + hooks.timeStarts(producer, [[1000n, undefined, 1800n]], 1000); + const at = (time: bigint) => + hooks.servingResolved(producer.subscribe({ delay: { max: null }, ranges: [{ start: { time } }] })); + expect(at(1000n)).toEqual([undefined]); + expect(at(1500n)).toEqual([undefined]); + // GROUP 2000 says nothing lies between the time and it. + producer.skipGroups(1000n, 2000n); + const read = (time: bigint, max: Duration | null) => + items(producer.subscribe({ delay: { max }, ranges: [{ start: { time } }] }).ordered()); + for (const time of [1000n, 1500n]) { + expect(read(time, Duration.secs(1))).toEqual(["group 2000"]); + expect(read(time, null)).toEqual([]); + } +}); + // Run every pending microtask and the ones they queue, as a timer firing would. async function flush() { for (let i = 0; i < 20; i++) await Promise.resolve(); diff --git a/js/net/src/resume.test.ts b/js/net/src/resume.test.ts index 992bf274711..f52259644b7 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -10,7 +10,7 @@ import type { Session as IetfSession } from "./ietf/session.ts"; import { Subscriber as IetfSubscriber } from "./ietf/subscriber.ts"; import { Version as IetfVersion } from "./ietf/version.ts"; import * as Instance from "./instance.ts"; -import { hooks, staleHole } from "./internal.ts"; +import { hooks, NO_MAX_DELAY, staleHole } from "./internal.ts"; import type { Lister } from "./listing.ts"; import { Producer } from "./origin.ts"; import * as Path from "./path.ts"; @@ -2105,7 +2105,7 @@ for (const resolution of ["arrival", "drop", "other copy", "budget"] as const) { ta.write(6n); tb.write(7n); // The session reports where the range the front asked from time 4 started, as with its START. - hooks.timeStarts(tb.producer, [[4n, 7n]]); + hooks.timeStarts(tb.producer, [[4n, 7n]], NO_MAX_DELAY); hooks.startTrack(tb.producer, { start: 7n, requested: 4n }); front.serve(b.consume()); await settle(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index a0d7d8bd7c9..a3e0b4a004c 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1058,12 +1058,18 @@ let timeAnchors: (producer: Producer) => { placing: number; }; -/** Where a received copy's publisher started a range asked from a time, and the last time it reaches. */ -interface TimeStart { - start: bigint; - /** Undefined while the range follows the live edge. */ - through?: bigint; -} +/** + * How a received copy's publisher answered a range asked from a time: the group it starts at, or + * `empty`, the budget in milliseconds under which it had no group to deliver (the publisher starts + * at the oldest group its budget has not expired, so a wider reader may still get one). `through` + * is the last time it reaches, undefined while it follows the live edge. Mirrors the Rust `TimeStart`. + */ +type TimeStart = ({ start: bigint; empty?: never } | { start?: never; empty: number }) & { through?: bigint }; + +// Whether a received copy's range asked from `time`, or the one upstream folded it into, had no +// group to deliver to a reader within `budget`: the live feed brings it none of the range's groups. +// Assigned in the producer's static block. Mirrors the Rust `TrackState::answered_empty`. +let answeredEmpty: (producer: Producer, time: bigint, budget: number) => boolean; // Where a received copy's live feed landed every key it could through `time`, counting from // `covering`, where a range covering the time started, or else its first arrival at or below @@ -1092,13 +1098,14 @@ function withoutRun(runs: readonly Run[], cut: Run): Run[] { return out; } -// Where a range asked from an earlier time that reaches `time` started, which upstream folded a -// range asked from `time` into, so it reports nothing for this time itself. -function coveringStart(starts: ReadonlyMap, time: bigint): bigint | undefined { - let covering: [bigint, bigint] | undefined; +// The range asked from an earlier time that reaches `time`, which upstream folded a range asked from +// `time` into, so it reports nothing for this time itself. Mirrors the Rust +// `TrackState::covering_start`. +function coveringStart(starts: ReadonlyMap, time: bigint): TimeStart | undefined { + let covering: [bigint, TimeStart] | undefined; for (const [asked, reported] of starts) { if (asked >= time || (reported.through !== undefined && reported.through < time)) continue; - if (covering === undefined || asked > covering[0]) covering = [asked, reported.start]; + if (covering === undefined || asked > covering[0]) covering = [asked, reported]; } return covering?.[1]; } @@ -1365,7 +1372,7 @@ export class Producer { #timeStarts = new Map(); // The lowest time and highest start in `#timeStarts`, so the times its ranges may place are // bounded without a walk of them all. - #timeStartReach: { lowest: bigint; highest: bigint } | undefined; + #timeStartReach: { lowest: bigint; highest?: bigint } | undefined; // On a lite-07 copy, where the publisher started each live range counting back, by its count. #relativeStarts = new Map(); // Counts changes to how a time start is placed other than by groups arriving or drops: the wire, @@ -1613,30 +1620,35 @@ export class Producer { producer.#startsAtKeys = true; producer.#placing++; }; - hooks.timeStarts = (producer, starts) => { + hooks.timeStarts = (producer, starts, budget) => { const merged = new Map(); for (const [time, start, through] of starts) { + // An open range never resolves empty, so such an answer says nothing. + if (start === undefined && through === undefined) continue; const held = merged.get(time); // Two ranges asked from one time: the lower start and the farther reach hold for both. + const reach = + held === undefined + ? through + : held.through === undefined || through === undefined + ? undefined + : max(held.through, through); + const lower = + held?.start === undefined || start === undefined ? (held?.start ?? start) : min(held.start, start); merged.set( time, - held === undefined - ? { start, through } - : { - start: min(held.start, start), - through: - held.through === undefined || through === undefined - ? undefined - : max(held.through, through), - }, + lower === undefined ? { empty: budget, through: reach } : { start: lower, through: reach }, ); } producer.#timeStarts = merged; - let reach: { lowest: bigint; highest: bigint } | undefined; + let reach: { lowest: bigint; highest?: bigint } | undefined; for (const [asked, reported] of merged) { - reach = reach - ? { lowest: min(reach.lowest, asked), highest: max(reach.highest, reported.start) } - : { lowest: asked, highest: reported.start }; + const start = reported.start; + const highest = + reach?.highest === undefined || start === undefined + ? (reach?.highest ?? start) + : max(reach.highest, start); + reach = { lowest: reach ? min(reach.lowest, asked) : asked, highest }; } producer.#timeStartReach = reach; producer.#placing++; @@ -1661,9 +1673,16 @@ export class Producer { const end = next.done ? UNBOUNDED : next.value.start; return end > start ? { start, end } : undefined; }; + answeredEmpty = (producer, time, budget) => { + const reported = producer.#timeStarts.get(time) ?? coveringStart(producer.#timeStarts, time); + return reported?.empty !== undefined && budget <= reported.empty; + }; timeStartFolds = (producer, time) => { if (!producer.#wire || producer.#timeStarts.has(time)) return undefined; - const from = producer.#landingFrom(time, coveringStart(producer.#timeStarts, time)); + const covering = coveringStart(producer.#timeStarts, time); + // Its range had no group to deliver, so no read waits on it. + if (covering && covering.start === undefined) return undefined; + const from = producer.#landingFrom(time, covering?.start); return from === undefined ? undefined : producer.#landingThrough(time, from)?.folds; }; hooks.relativeStarts = (producer, starts) => { @@ -3742,10 +3761,13 @@ export class Subscriber { // `TrackState::containing_received`. if (wire) { if (below === time) return { group: time }; - const start = anchors.starts.get(time)?.start; + const reported = anchors.starts.get(time); // The publisher serves nothing at or before it, as for a time older than every group. - if (start !== undefined) return start <= time ? { group: start } : "before"; - const covering = coveringStart(anchors.starts, time); + if (reported?.start !== undefined) + return reported.start <= time ? { group: reported.start } : "before"; + // No group to deliver holds only under the budget it was asked with, which may have left + // out a group a wider reader still gets: it places nothing. + const covering = coveringStart(anchors.starts, time)?.start; if (below !== undefined && (drops.covers(below + 1n, time + 1n) || index < keys.length)) { return { group: below }; } @@ -5518,8 +5540,9 @@ export class Subscriber { const producer = this.#state.producer; let waits = false; for (const time of this.#window.waitingIn(start, key)) { - // Its successor is this group's or one below: the group containing the time is judged as an - // owed group at it would be. + // Its range had no group to deliver, so the feed lands none of it. Its successor is this + // group's or one below: the group containing the time is judged as an owed group at it would be. + if (producer && answeredEmpty(producer, time, drift.budget)) continue; if (this.#staleAt(time, drift, true)) continue; const folds = producer && timeStartFolds(producer, time); if (folds !== undefined) this.#wakeAt(folds.since(Instant.ZERO).asNanos()); diff --git a/quest/m1/empty-range-relay.md b/quest/m1/empty-range-relay.md new file mode 100644 index 00000000000..9dc9a22b2c6 --- /dev/null +++ b/quest/m1/empty-range-relay.md @@ -0,0 +1,13 @@ +# [M] A range answered empty upstream stalls a viewer behind a relay + +## Goal + +A viewer behind a relay whose subscription holds a lite-07 time range the publisher answers with no group to deliver (Range Start 0) still reads every later range. +Today, with a viewer subscribed through a relay to `Time(10)..Time(20)` and `Time(66)..` while the publisher holds only group 66, the viewer reads nothing: on `main` and with [moq-dev/moq#9172](https://github.com/moq-dev/moq/pull/9172) alike, the viewer's ordered read waits on a fill it asked at key 10 that never resolves, and the relay never answers the empty range as such. + +## Plan + +- Reproduce with the fixture in `rs/moq-net/tests/integration/waiting_time_start.rs` (publisher, relay, viewer): an unlimited-budget ordered reader with the two ranges above times out before reading 66. +- The cause is that an empty answer only holds under the `Subscriber Max Age` it was asked with (the draft's start "SHOULD start at the oldest group at or above the floor that Expiration has not expired"), so the copy can't place the time for every reader. [moq-dev/moq#9172](https://github.com/moq-dev/moq/pull/9172) records the answer (`TimeStart::start == None`) and only releases the ordered barrier for it; the time stays unplaced, so the relay's window for the downstream range waits, and a `Before` placement makes the copy look up history the publisher won't serve. +- Recommended: scope the empty answer by the budget it was asked under, so a reader whose budget fits places its time past the range's reach (the range resolves empty, and the relay answers 0 downstream), while a wider reader keeps looking it up. Mirror it in js/net. +- Also find why the viewer's fill at key 10 never resolves through the relay when the publisher's handler drops it: a dropped fill names its keys `NotFound`, which should give the fill up. diff --git a/rs/moq-net/proptest-regressions/model/track.txt b/rs/moq-net/proptest-regressions/model/track.txt index c68e3319b0c..33a0a5c116f 100644 --- a/rs/moq-net/proptest-regressions/model/track.txt +++ b/rs/moq-net/proptest-regressions/model/track.txt @@ -5,3 +5,4 @@ # It is recommended to check this file in to source control so that # everyone who runs the test benefits from these saved cases. cc c443c74044d087822aa18f1202b5106aec905b52dbe5fc307ae900b041b18609 # shrinks to ops = [(2, 0, 1)], wire = true, starts = [(0, 2, None)] +cc 9f25beb5f5012b3975089a5fa29ffdf0eafecf7b9a39677b6a40249575717545 # shrinks to ops = [], wire = true, starts = [(0, None, None)] diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 6caa6e6a6a3..9aff1c11e9c 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -3294,6 +3294,60 @@ mod tests { assert_eq!(active.never_serves(None), None, "nothing to go live at"); } + /// A lite-07 range answered with no group to deliver lands none of its groups, so the copy's + /// ordered reads hold no later group back for its time. Only one whose end takes in its + /// time says so: one ending before it may still have skipped the group containing it. + #[kio::test] + async fn an_empty_answer_releases_only_a_range_reaching_its_time() { + let tail = kio::Producer::new(Tail::default()); + let Sub::Active(mut active) = feed_sub(&tail).await else { + unreachable!("feed_sub is active") + }; + // Times 10 through 20, time 30 ending at group 25, below it, and time 66 onward. + active.live = lite::LiveRanges::new( + Version::Lite07, + &[ + lite::Range::span(Some(10), 0, Some(20), None), + lite::Range::span(Some(30), 0, Some(24), None), + lite::Range::span(Some(66), 0, None, None), + ], + ); + active.starts = [(0, None), (1, None), (2, Some(66))].into(); + // Asked with no limit, so an empty answer holds for every reader. + active.stale_budget = Duration::MAX; + let info = track::Info::default().with_timescale(None).with_max_age(None); + let serving = track::Producer::new(Arc::new(crate::broadcast::Info::default()), "video", info); + serving.received(); + serving.anchored(); + serving + .create_group(group::Info { start: 66 }) + .unwrap() + .finish() + .unwrap(); + active.report_starts(&serving); + + let read = |time: u64| { + let mut ordered = serving + .subscribe( + track::Subscription::default() + .with_ranges([track::Range { + start: track::Anchor::Time(time), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + match ordered.next_group().now_or_never() { + Some(Ok(Some(track::Item::Group(group)))) => Some(group.start), + None => None, + Some(res) => panic!("not a group: {:?}", res.map(|item| item.is_some())), + } + }; + assert_eq!(read(10), Some(66), "its range reached 20, past it"); + assert_eq!(read(15), Some(66), "folded into that range"); + assert_eq!(read(30), None, "its range ended before it"); + } + /// QUIC does not order streams, so group 2's header can reach the relay before group 1's. /// A fetch held for group 1 keeps waiting through the grace and is served when it lands. #[kio::test] @@ -10153,7 +10207,7 @@ impl SubStream { let mut times = Vec::new(); let mut relative = Vec::new(); for (id, range) in self.live.iter() { - let Some(Some(start)) = self.starts.get(&id) else { + let Some(&start) = self.starts.get(&id) else { continue; }; // The last time the range reaches: an exclusive group end stops at the time before @@ -10165,20 +10219,33 @@ impl SubStream { Some(lite::Anchor::Time(end)) => Some(end), Some(lite::Anchor::Relative(_)) => Some(asked), }; - match range.start { - None => times.push((0, *start, through(0))), - Some(lite::Anchor::Absolute(time)) if time.frame == 0 => { - times.push((time.group, *start, through(time.group))) - } + // Whether a range with no group to deliver says its publisher held none at or before + // its reach: only one whose end takes in the group containing its time, which a + // range that ends before its time, or by a count back, may not. + let reaches = |asked: u64| match range.end { + Some(lite::Anchor::Absolute(end)) if end.frame == 0 => end.group > asked, + Some(lite::Anchor::Absolute(end)) => end.group >= asked, + Some(lite::Anchor::Time(end)) => end >= asked, + None | Some(lite::Anchor::Relative(_)) => false, + }; + let asked = match range.start { + None => 0, + Some(lite::Anchor::Absolute(time)) if time.frame == 0 => time.group, Some(lite::Anchor::Relative(back)) => { - if let Some(Some(latest)) = self.edges.get(&id) { - relative.push((back, *start, *latest)); + if let (Some(start), Some(Some(latest))) = (start, self.edges.get(&id)) { + relative.push((back, start, *latest)); } + continue; } - _ => {} + _ => continue, + }; + if start.is_some() || reaches(asked) { + times.push((asked, start, through(asked))); } } - serving.time_starts(times); + // The wire does not say which budget a range resolved under, so an empty one holds only + // for the narrowest asked. + serving.time_starts(times, self.stale_budget); serving.relative_starts(relative); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 055f45a5c82..d91e854862c 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -607,14 +607,34 @@ impl Anchors { } } -/// Where a received copy's publisher started a range asked from a time, and the last time -/// the range reaches, `None` while it follows the live edge. +/// How a received copy's publisher answered a range asked from a time, and the last time the +/// range reaches, `None` while it follows the live edge. #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct TimeStart { - start: u64, + answer: Answer, through: Option, } +/// Where a publisher started a range, as its answer says. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Answer { + /// The group the range starts at. + At(u64), + /// No group to deliver, under at most this budget: the publisher starts at the oldest group + /// its budget has not expired, so a wider reader may still get one. + Empty(Duration), +} + +impl TimeStart { + /// The group the range starts at, `None` when it had none to deliver. + fn start(&self) -> Option { + match self.answer { + Answer::At(start) => Some(start), + Answer::Empty(_) => None, + } + } +} + /// Where a received copy's publisher started a range counting back, and the latest group it /// counted from. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -1299,13 +1319,24 @@ impl TrackState { return Containing::Group(time); } match self.time_starts.get(&time) { - Some(reported) if reported.start <= time => return Containing::Group(reported.start), + Some(&TimeStart { + answer: Answer::At(start), + .. + }) if start <= time => return Containing::Group(start), // The publisher serves nothing at or before it, as it does for a time older than // every group it holds. - Some(_) => return Containing::Before, - None => {} + Some(TimeStart { + answer: Answer::At(_), .. + }) => return Containing::Before, + // No group to deliver holds only under the budget it was asked with, which may + // have left out a group a wider reader still gets: it places nothing. + Some(TimeStart { + answer: Answer::Empty(_), + .. + }) + | None => {} } - let covering = self.covering_start(time); + let covering = self.covering_start(time).and_then(|reported| reported.start()); match self.lookup.range(..=time).next_back() { Some((&key, _)) if self.dropped_cover(key.saturating_add(1)..time.saturating_add(1)) @@ -1333,14 +1364,25 @@ impl TrackState { } } - /// Where a range asked from an earlier time that reaches `time` started, which upstream - /// folded a range asked from `time` into, so it reports nothing for this time itself. - fn covering_start(&self, time: u64) -> Option { + /// Whether the range asked from `time`, or the one upstream folded it into, had no group to + /// deliver to a reader within `budget`: the live feed brings it none of the range's groups. + fn answered_empty(&self, time: u64, budget: Duration) -> bool { + let reported = self + .time_starts + .get(&time) + .copied() + .or_else(|| self.covering_start(time)); + matches!(reported, Some(TimeStart { answer: Answer::Empty(asked), .. }) if budget <= asked) + } + + /// The range asked from an earlier time that reaches `time`, which upstream folded a range + /// asked from `time` into, so it reports nothing for this time itself. + fn covering_start(&self, time: u64) -> Option { self.time_starts .range(..time) .rev() .find(|(_, reported)| reported.through.is_none_or(|through| through >= time)) - .map(|(_, reported)| reported.start) + .map(|(_, reported)| *reported) } /// The times [`Self::landed_through`] may place, wherever the groups and drops leave them: @@ -1374,7 +1416,15 @@ impl TrackState { if !self.anchors.received() || self.time_starts.contains_key(&time) { return None; } - let from = self.landing_from(time, self.covering_start(time))?; + let covering = match self.covering_start(time) { + // Its range had no group to deliver, so no read waits on it. + Some(TimeStart { + answer: Answer::Empty(_), + .. + }) => return None, + covering => covering.and_then(|reported| reported.start()), + }; + let from = self.landing_from(time, covering)?; self.landing_through(time, from).flatten() } @@ -4904,28 +4954,43 @@ impl Producer { } /// Record where the upstream publisher started each of this lite-07 copy's live wire - /// ranges, as `(time, start, through)` by the time each asked from (0 for the oldest group) - /// and the last time it reaches (`None` while open), replacing the last set. A reader's - /// time start this copy cannot place among its own groups resolves to the range asked from - /// it, or to one reaching it that upstream folded it into. + /// ranges, as `(time, start, through)` by the time each asked from (0 for the oldest group), + /// `None` for one with no group to deliver, and the last time it reaches (`None` while + /// open), replacing the last set. A reader's time start this copy cannot place among its + /// own groups resolves to the range asked from it, or to one reaching it that upstream + /// folded it into. An empty answer holds for a reader within `budget`, the narrowest the + /// subscription asked with, since the publisher resolves a start by the budget it asked. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn time_starts(&self, starts: impl IntoIterator)>) { + pub(crate) fn time_starts( + &self, + starts: impl IntoIterator, Option)>, + budget: Duration, + ) { let mut merged: BTreeMap = BTreeMap::new(); for (time, start, through) in starts { + // An open range never resolves empty, so such an answer says nothing. + if start.is_none() && through.is_none() { + continue; + } + let answer = start.map_or(Answer::Empty(budget), Answer::At); // Two ranges asked from one time: the lower start and the farther reach hold for both. merged .entry(time) .and_modify(|held| { - held.start = held.start.min(start); + held.answer = match (held.answer, answer) { + (Answer::At(held), Answer::At(start)) => Answer::At(held.min(start)), + (Answer::At(start), _) | (_, Answer::At(start)) => Answer::At(start), + (empty, _) => empty, + }; held.through = held.through.zip(through).map(|(a, b)| a.max(b)); }) - .or_insert(TimeStart { start, through }); + .or_insert(TimeStart { answer, through }); } let starts = merged; if let Ok(mut state) = self.modify() && state.time_starts != starts { - state.highest_time_start = starts.values().map(|reported| reported.start).max(); + state.highest_time_start = starts.values().filter_map(TimeStart::start).max(); state.time_starts = starts; state.placing += 1; } @@ -9522,9 +9587,12 @@ impl Cursor { // An unjudgeable old gap cannot hold a finite read, as for an owed group. let floor = times.start.max(state.cache_floor(drift.budget)); for time in window.waiting_in(floor..times.end) { - // Its successor is this group's or one below: the group containing the - // time is judged as an owed group at it would be. - if state.poll_drifted(time, &drift.edge.servable, drift.budget, None, waiter) { + // Its range had no group to deliver, so the feed lands none of it. Its + // successor is this group's or one below: the group containing the time is + // judged as an owed group at it would be. + if state.answered_empty(time, drift.budget) + || state.poll_drifted(time, &drift.edge.servable, drift.budget, None, waiter) + { continue; } if let Some(folds) = state.time_start_folds(time) { @@ -12502,13 +12570,16 @@ mod test { fn unplaced_matches_containing( ops in proptest::collection::vec((0u8..5, 0u64..40, 1u64..8), 0..24), wire: bool, - starts in proptest::collection::vec((0u64..48, 0u64..48, proptest::option::of(0u64..56)), 0..3), + starts in proptest::collection::vec( + (0u64..48, proptest::option::of(0u64..48), proptest::option::of(0u64..56)), + 0..3 + ), ) { let producer = track_producer("test", Info::default()); if wire { producer.received(); producer.anchored(); - producer.time_starts(starts.clone()); + producer.time_starts(starts.clone(), Duration::MAX); } for (op, start, len) in ops { let groups = start..start + len; @@ -12523,7 +12594,12 @@ mod test { } let state = producer.state.read(); let unplaced = state.unplaced().expect("a track always tells"); - let reported: BTreeSet = starts.iter().map(|(time, ..)| *time).collect(); + // A range answered with no group to deliver places nothing. + let reported: BTreeSet = starts + .iter() + .filter(|(_, start, _)| start.is_some()) + .map(|(time, ..)| *time) + .collect(); let landing = state.landing_span(); for time in (0..64).chain([u64::MAX - 1, u64::MAX]) { let inside = unplaced.runs.iter().any(|run| run.contains(&time)); @@ -15833,7 +15909,7 @@ mod test { // The publisher started the range asked from 1700 at 1000, and the one from 500 at 1000 // too, past it: nothing at or before 500 is served. - producer.time_starts([(1700, 1000, None), (500, 1000, None)]); + producer.time_starts([(1700, Some(1000), None), (500, Some(1000), None)], Duration::MAX); assert_eq!(placed(&mut answered), Some((1000, true))); assert_eq!(placed(&mut before), Some((0, false))); assert_eq!(placed(&mut later), None); @@ -15880,13 +15956,53 @@ mod test { assert_eq!(next(), None, "2000 waits on the start"); // A session's tail holds the read at the answered start's group until it lands, and // says nothing lies between it and 2000. - producer.time_starts([(1500, 1000, None)]); + producer.time_starts([(1500, Some(1000), None)], Duration::MAX); write_group(&producer, 1000); producer.skip_groups(1001..2000); assert_eq!(next(), Some(Ok(1000))); assert_eq!(next(), Some(Ok(2000))); } + /// A range its publisher answered with no group to deliver, under a budget, lands none of + /// its groups for a reader within that budget, so that reader's ordered read holds no later + /// group back for the time it asks from, or one folded into it. A wider reader may still + /// get one once the budget widens, so it keeps waiting, and the answer places neither time. + #[test] + #[cfg(feature = "async")] + fn an_empty_range_holds_back_no_later_group_within_its_budget() { + let budget = Duration::from_millis(100); + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + producer.time_starts([(1000, None, Some(1800))], budget); + { + let state = producer.state.read(); + assert_eq!(state.containing(1000), Containing::Unknown); + assert_eq!(state.containing(1500), Containing::Unknown, "folded into it"); + } + let read = |time: u64, max: Option| { + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(time), + end: None, + }]) + .with_delay(Delay::default().with_max(max)), + ) + .ordered(); + match ordered.next_group().now_or_never() { + Some(Ok(Some(Item::Group(group)))) => Some(group.start), + None => None, + Some(res) => panic!("not a group: {:?}", res.map(|item| item.is_some())), + } + }; + assert_eq!(read(1000, Some(budget)), Some(2000)); + assert_eq!(read(1500, Some(budget)), Some(2000), "folded into it"); + assert_eq!(read(1500, None), None, "a wider reader may still get 1000"); + } + /// A lite-07 copy places a time start that upstream folded into an earlier range by that /// range's answer, which reports nothing for the time itself: one that started past the /// time served nothing at or before it, so an ordered read goes on. A range ending before @@ -15917,12 +16033,12 @@ mod test { write_group(&producer, 2000); // A range asked from 1000 that ends at 1200 doesn't reach the time. - producer.time_starts([(1000, 1000, Some(1200))]); + producer.time_starts([(1000, Some(1000), Some(1200))], Duration::MAX); let mut bounded = ordered(&producer); assert_eq!(next(&mut bounded), None, "the start still waits"); // The oldest group's range, answered at 2000, reaches it. - producer.time_starts([(1000, 1000, Some(1200)), (0, 2000, None)]); + producer.time_starts([(1000, Some(1000), Some(1200)), (0, Some(2000), None)], Duration::MAX); assert_eq!(next(&mut bounded), Some(2000)); assert_eq!(next(&mut ordered(&producer)), Some(2000)); } @@ -15983,7 +16099,7 @@ mod test { producer.anchored(); elapse(&producer, Duration::ZERO); producer.start_resolved(1000).unwrap(); - producer.time_starts([(0, 1000, None)]); + producer.time_starts([(0, Some(1000), None)], Duration::MAX); // Group 1000's stream reset before its header: only 1800, 2000, and the skips around // them land, 1800 last. producer.skip_groups(1001..1800); @@ -16032,7 +16148,7 @@ mod test { producer.anchored(); elapse(&producer, Duration::ZERO); producer.start_resolved(1000).unwrap(); - producer.time_starts([(0, 1000, None)]); + producer.time_starts([(0, Some(1000), None)], Duration::MAX); producer.skip_groups(1001..2000); write_group(&producer, 2000); let mut subscriber = producer.subscribe(replay().with_ranges([Range { @@ -16171,7 +16287,7 @@ mod test { producer.received(); producer.anchored(); write_group(&producer, 500); - producer.time_starts([(1000, 1200, Some(1500))]); + producer.time_starts([(1000, Some(1200), Some(1500))], Duration::MAX); let cut = producer.create_group(group::Info { start: 1200 }).unwrap(); cut.abort(Error::Cancel).unwrap(); producer.modify().unwrap().reclaim_aborted(1200); From 47c026d8fb7b4033ee5058fe6e8f084cb551ec74 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 12:30:11 -0800 Subject: [PATCH 08/17] fix(moq-net): a waiting time start holds back a gap above its group, and an empty answer still wakes a wider reader An ordered read handed out a gap at a time start still waiting, moving past the group the start resolves to below it: js/net then never read that group. `Ordered::next_gap` now waits while `Cursor::poll_gap_held` finds a time start below the gap's end that `poll_time_start` would hold a group for, under the same floor and budget. A resumed reader's copies hold their own groups. `time_start_folds` skipped any time with its own report, and any covering empty answer, though `containing_received` places both from the feed's own groups: a reader wider than the answer's budget never woke when a gap below folded. It now skips only a reported start. js/net mirrors both. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ordered.test.ts | 13 ++++ js/net/src/track.ts | 20 ++++-- rs/moq-net/src/model/track.rs | 124 ++++++++++++++++++++++++++++++---- 3 files changed, 141 insertions(+), 16 deletions(-) diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index 50edd177d66..5ac7768d846 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -478,6 +478,19 @@ test("a folded time start wakes its read when the grace for a gap below it passe } }); +test("a waiting time start holds back a gap above its group", () => { + const producer = receivedTrack(); + put(producer, 2000n, false); + drop(producer, 1500n, 1600n); + const ordered = producer.subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }).ordered(); + expect(items(ordered)).toEqual([]); + hooks.timeStarts(producer, [[1500n, 1000n]], NO_MAX_DELAY); + put(producer, 1000n, false); + producer.skipGroups(1001n, 1500n); + producer.skipGroups(1600n, 2000n); + expect(items(ordered)).toEqual(["group 1000", "gap 1500..1600 (delivery timeout)", "group 2000"]); +}); + test("a waiting time start holds an ordered read only within its budget", async () => { const clock = new Manual(); const installed = clock.install(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index a3e0b4a004c..f7766cfa70f 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1678,10 +1678,10 @@ export class Producer { return reported?.empty !== undefined && budget <= reported.empty; }; timeStartFolds = (producer, time) => { - if (!producer.#wire || producer.#timeStarts.has(time)) return undefined; + // A reported start places it. An empty answer places nothing, as for `containing`, so a + // reader wider than its budget still waits on folds. + if (!producer.#wire || producer.#timeStarts.get(time)?.start !== undefined) return undefined; const covering = coveringStart(producer.#timeStarts, time); - // Its range had no group to deliver, so no read waits on it. - if (covering && covering.start === undefined) return undefined; const from = producer.#landingFrom(time, covering?.start); return from === undefined ? undefined : producer.#landingThrough(time, from)?.folds; }; @@ -5374,11 +5374,23 @@ export class Subscriber { if (!lowest) return { kind: "idle" }; const owed = this.nextInRange(items.at); const ready = lowest.span.start <= items.at || owed === undefined || owed >= lowest.span.start; - return ready ? this.#takeGap(items, lowest.index, lowest.span, lowest.span.end) : { kind: "idle" }; + if (!ready || this.#gapHeld(items.at, lowest.span.end)) return { kind: "idle" }; + return this.#takeGap(items, lowest.index, lowest.span, lowest.span.end); } } } + // Whether a time start still waiting below `end` holds back a gap an ordered read at `at` would + // return, as #waitTimeStart holds a group: the group containing it may lie below the gap, and an + // ordered read never goes back. A resumed reader's copies hold their own groups, and its gaps go + // as found. Mirrors the Rust `Cursor::poll_gap_held`. + #gapHeld(at: bigint, end: bigint): boolean { + if (this.#resume) return false; + this.#settle(); + const start = max(max(this.#cursor.peek().start, this.#nextKey), at); + return this.#waitTimeStart(start, end, this.#drift()); + } + // The group held behind a gap, while the bounds still cover it, else the cursor's next. A group // read is settled, so no later cut or drop of it is a gap. #readHeld(items: Items): Recv { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index d91e854862c..5bddef0aa40 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1413,17 +1413,17 @@ impl TrackState { /// itself with no change to wake its readers: the arrival grace of the lowest gap /// [`Self::landed_through`] still waits on folds. fn time_start_folds(&self, time: u64) -> Option { - if !self.anchors.received() || self.time_starts.contains_key(&time) { + // A reported start places it. An empty answer places nothing, as for + // [`Self::containing_received`], so a reader wider than its budget still waits on folds. + if !self.anchors.received() + || self + .time_starts + .get(&time) + .is_some_and(|reported| reported.start().is_some()) + { return None; } - let covering = match self.covering_start(time) { - // Its range had no group to deliver, so no read waits on it. - Some(TimeStart { - answer: Answer::Empty(_), - .. - }) => return None, - covering => covering.and_then(|reported| reported.start()), - }; + let covering = self.covering_start(time).and_then(|reported| reported.start()); let from = self.landing_from(time, covering)?; self.landing_through(time, from).flatten() } @@ -9619,6 +9619,22 @@ impl Cursor { Poll::Pending } + /// Pending while a time start below `end` holds a gap there as [`Self::poll_time_start`] + /// holds a group: the group containing it may lie below the gap, and an ordered read never + /// goes back. `from` is where the ordered read stands. + fn poll_gap_held(&mut self, from: u64, end: u64, waiter: &kio::Waiter) -> Poll<()> { + self.sync(); + let drift = match ready!(self.poll_drift(&self.servable, waiter)) { + Ok(drift) => drift, + // The read reports the error. + Err(_) => return Poll::Ready(()), + }; + let from = from.max(self.next_key).max(self.floor()); + // One placed meanwhile changed the runs, which the caller looks at again. + while ready!(self.poll_time_start(from..end, &drift, waiter)) {} + Poll::Ready(()) + } + /// Whether the drift budget says to skip `group`, against a [`Drift`] already resolved /// for this poll. fn poll_stale(&self, group: &group::Consumer, drift: &Drift, waiter: &kio::Waiter) -> Poll> { @@ -10985,6 +11001,16 @@ impl Subscriber { record.tell(keys, Report::Settled); } + /// Pending while a waiting time start below `end` holds back a gap an [`Ordered`] read at + /// `from` would return; see [`Cursor::poll_gap_held`]. A resumed reader's copies hold their + /// own groups, and its gaps go as found. + fn poll_gap_held(&mut self, from: u64, end: u64, waiter: &kio::Waiter) -> Poll<()> { + match &mut self.inner { + Inner::Plain(cursor) => cursor.poll_gap_held(from, end, waiter), + Inner::Resume(..) => Poll::Ready(()), + } + } + /// The key cursor behind [`Ordered`], which owns the only public door to it. pub(super) fn poll_next_group(&mut self, waiter: &kio::Waiter) -> Poll>> { self.poll_group(true, waiter) @@ -11950,7 +11976,7 @@ impl Ordered { } } // What the read still owes lies above the gap, so the gap goes first. - Poll::Pending => match self.next_gap() { + Poll::Pending => match self.next_gap(waiter) { Some(item) => Poll::Ready(Ok(Some(item))), None => Poll::Pending, }, @@ -12067,8 +12093,13 @@ impl Ordered { } } - /// The lowest gap, once nothing the read still owes lies below it. - fn next_gap(&mut self) -> Option { + /// The lowest gap, once nothing the read still owes lies below it, and no time start still + /// waiting below its end may resolve to a group below it. + fn next_gap(&mut self, waiter: &kio::Waiter) -> Option { + let (_, span) = self.lowest_gap()?; + if self.inner.poll_gap_held(self.at, span.end, waiter).is_pending() { + return None; + } let (at, span) = self.lowest_gap()?; let ready = span.start <= self.at || self.inner.next_in_range(self.at).is_none_or(|owed| owed >= span.start); let resume = span.end; @@ -15963,6 +15994,42 @@ mod test { assert_eq!(next(), Some(Ok(2000))); } + /// A gap at a time start still waiting goes after the group the start resolves to, as a + /// later group does: that group may lie below the gap, and an ordered read never goes back. + #[test] + #[cfg(feature = "async")] + fn a_waiting_time_start_holds_back_a_gap_above_its_group() { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + producer.drop_groups(1500..1600, StreamError::NotFound); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + let mut next = || match ordered.next_group().now_or_never() { + None => None, + Some(Ok(Some(Item::Group(group)))) => Some(Ok(group.start)), + Some(Ok(Some(Item::Gap { range, .. }))) => Some(Err(range)), + Some(res) => panic!("ended: {:?}", res.map(|_| ())), + }; + assert_eq!(next(), None, "the gap waits on the start"); + producer.time_starts([(1500, Some(1000), None)], Duration::MAX); + write_group(&producer, 1000); + producer.skip_groups(1001..1500); + producer.skip_groups(1600..2000); + assert_eq!(next(), Some(Ok(1000))); + assert_eq!(next(), Some(Err(1500..1600))); + assert_eq!(next(), Some(Ok(2000))); + } + /// A range its publisher answered with no group to deliver, under a budget, lands none of /// its groups for a reader within that budget, so that reader's ordered read holds no later /// group back for the time it asks from, or one folded into it. A wider reader may still @@ -16167,6 +16234,39 @@ mod test { assert!(subscriber.poll_start(&waiter).is_ready()); } + /// A time start its own range answered empty, under a budget narrower than the reader's, + /// places nothing, so the reader still waits on the feed placing it, and the grace of a gap + /// below folding wakes it as it does any start the copy places from its own groups. + #[kio::test] + #[cfg(feature = "async")] + async fn a_time_start_answered_empty_wakes_poll_start_when_a_gap_below_folds() { + let grace = replay().budget(); + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + elapse(&producer, Duration::ZERO); + producer.time_starts([(1500, None, Some(1800))], Duration::from_millis(1)); + // The feed's first group, cut since, and a later one with the gap between still folding. + let cut = producer.create_group(group::Info { start: 1000 }).unwrap(); + cut.abort(Error::Cancel).unwrap(); + producer.modify().unwrap().reclaim_aborted(1000); + write_group(&producer, 2000); + let mut subscriber = producer.subscribe(replay().with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }])); + + let woken = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let waiter = kio::Waiter::new(futures::task::waker(Arc::new(FlagWake(woken.clone())))); + assert!( + subscriber.poll_start(&waiter).is_pending(), + "a group above 1000 may still land" + ); + elapse(&producer, grace); + assert!(woken.load(Ordering::SeqCst), "the grace never woke the start"); + assert!(subscriber.poll_start(&waiter).is_ready()); + } + /// A finite budget gives up on the group a waiting time start resolves to as it would on an /// owed one: once the later group it holds is a full budget old, an ordered read hands it /// out rather than wait for an answer that may never come. From 4cd53b8fe9ad7ceb9749476ceb44ff6831d9bd39 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 13:39:29 -0800 Subject: [PATCH 09/17] fix(moq-net): answer CodeRabbit's waiting-time-start findings - An empty answer's reach no longer extends a start another range asked from the same time reported (Rust and js/net). - A waiting time start holds no ordered read once every key from the read's floor through its time is accounted for: the group containing it lies below the floor (Rust and js/net). - A js/net front's ordered reader asks its serving copy whether a waiting time start holds a gap, since it reads that copy's runs. The Rust front already holds it through its own window; a test now pins that. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 10 +++ js/net/src/ordered.test.ts | 36 +++++++++++ js/net/src/resume.test.ts | 40 ++++++++++++ js/net/src/resume.ts | 8 +++ js/net/src/track.ts | 57 +++++++++++++---- rs/moq-net/src/model/resume.rs | 43 +++++++++++++ rs/moq-net/src/model/track.rs | 108 ++++++++++++++++++++++++++++++--- 7 files changed, 279 insertions(+), 23 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 77fc93688bb..84a59fb150b 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -519,6 +519,8 @@ export interface ResumeSource { resolved(): (Resolved | undefined)[]; /** The runs of `[start, end)` the serving copy reads, undefined without one. */ runs(start: bigint, end: bigint): Run[] | undefined; + /** Whether a time start the serving copy still waits to place holds back a gap; see `hooks.gapHeld`. */ + gapHeld(at: bigint, end: bigint): boolean; /** Whether a route's copy serves the reader now. */ serving(): boolean; /** Where the run of keys from `key` the serving copy says no group arrives at ends; see `hooks.holeEnd`. */ @@ -823,6 +825,11 @@ export const hooks: { * Assigned by `track.ts`. */ servingRuns: (subscriber: Subscriber, start: bigint, end: bigint) => Run[]; + /** + * Whether a time start still waiting below `end` holds back a gap an ordered read at `at` would + * return. Assigned by `track.ts`. + */ + gapHeld: (subscriber: Subscriber, at: bigint, end: bigint) => boolean; /** * The exclusive end past every group a subscriber reads, or undefined while one of its ranges * stays open. Assigned by `track.ts`. @@ -1187,6 +1194,9 @@ export const hooks: { servingRuns: () => { throw new Error("track.ts not loaded"); }, + gapHeld: () => { + throw new Error("track.ts not loaded"); + }, servingEnd: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index 5ac7768d846..7b066eaf1ef 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -528,6 +528,42 @@ test("a waiting time start below an ordered read's own floor holds nothing", () expect(items(ordered)).toEqual(["group 2000"]); }); +test("a waiting time start holds no gap once nothing from the read's floor through it may land", () => { + const producer = receivedTrack(); + put(producer, 2000n, false); + const ordered = () => + producer + .subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }) + .withRange({ start: { included: 1500n }, end: { excluded: 1600n } }) + .ordered(); + drop(producer, 1501n, 1600n); + const waiting = ordered(); + expect(items(waiting)).toEqual([]); + // The group containing the time, if still to come, lies below the read's floor. + drop(producer, 1500n, 1501n); + expect(items(waiting)[0]).toStartWith("gap 1500.."); + expect(items(ordered())[0]).toStartWith("gap 1500.."); +}); + +test("an empty answer never extends the reach of a start reported for its time", () => { + for (const emptyFirst of [false, true]) { + const producer = receivedTrack(); + const starts: [bigint, bigint | undefined, bigint | undefined][] = [ + [1000n, 1200n, 1300n], + [1000n, undefined, 1800n], + ]; + if (emptyFirst) starts.reverse(); + hooks.timeStarts(producer, starts, NO_MAX_DELAY); + drop(producer, 1200n, 1201n); + producer.skipGroups(1201n, 1600n); + const at = (time: bigint) => + hooks.servingResolved(producer.subscribe({ delay: { max: null }, ranges: [{ start: { time } }] })); + expect(at(1250n)).toEqual([{ start: 1200n, end: undefined, floored: true }]); + // No answer reaches 1500. + expect(at(1500n)).toEqual([undefined]); + } +}); + test("a skip is no group to place a waiting time start from", () => { const producer = receivedTrack(); // GROUP 2000 says the group before it is 1000, which is still on its way. diff --git a/js/net/src/resume.test.ts b/js/net/src/resume.test.ts index 5c7c9d11a0f..9d13bab932c 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -2104,6 +2104,46 @@ test("a replacement spliced before the first copy answers is still checked again b.close(); }); +// A front's key-order reader holds back a gap above a time start its serving copy still waits to +// place, as a plain reader does: the group containing it may land below the gap. +test("a front's ordered reader holds a gap above a waiting time start", async () => { + const front = new Front(); + const a = new BroadcastProducer(); + const ta = copy(a); + hooks.receiveTrack(ta.producer); + front.serve(a.consume()); + ta.write(2000n); + hooks.dropTrackGroups(ta.producer, 1500n, 1600n, { error: new StreamError(StreamCode.NotFound) }); + const ordered = front.consumer + .track("seq") + .subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }) + .ordered(); + const read = () => { + const out: string[] = []; + for (let item = ordered.tryNextItem(); item; item = ordered.tryNextItem()) { + if (item.kind === "group") out.push(`group ${item.group.start}`); + else out.push(`gap ${item.start}..${item.end}`); + } + return out; + }; + await settle(); + expect(read()).toEqual([]); + // A parked read wakes once the start is placed. + const next = ordered.nextItem(); + expect(await isPending(next)).toBe(true); + hooks.timeStarts(ta.producer, [[1500n, 1000n]], NO_MAX_DELAY); + ta.write(1000n); + ta.producer.skipGroups(1001n, 1500n); + ta.producer.skipGroups(1600n, 2000n); + const first = await next; + expect(first?.kind === "group" ? first.group.start : first).toBe(1000n); + expect(read()).toEqual(["gap 1500..1600", "group 2000"]); + + ordered.close(); + front.consumer.close(); + a.close(); +}); + test("an ordered reader takes a replaced copy's buffered group before the new copy's later one", async () => { const front = new Front(); const a = new BroadcastProducer(); diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 5d145e2f683..4a737bdd297 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -1655,6 +1655,14 @@ class Reader implements ResumeSource { return serving && hooks.servingRuns(serving.sub, start, end); } + // The serving copy's runs leave out the keys below a time start it still waits to place, so its + // barrier holds a gap the group containing that start may lie below. + gapHeld(at: bigint, end: bigint): boolean { + this.sync(); + const serving = this.#serving(); + return serving !== undefined && hooks.gapHeld(serving.sub, at, end); + } + servingEnd(): bigint | undefined { this.sync(); const serving = this.#serving(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index ae761b7eeb6..983243d97f5 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1033,6 +1033,12 @@ let answeredEmpty: (producer: Producer, time: bigint, budget: number) => boolean // `TrackState::landing_from` and `TrackState::landed_through`. let landedFrom: (producer: Producer, time: bigint, covering: bigint | undefined) => bigint | undefined; +// The first run of `[start, end)` a received copy's live feed has not accounted for: not arrived, +// not dropped, before the track's end, and in a gap whose grace has not folded. Unlike +// {@link mayLand}, a key below where the feed starts counts. Mirrors the Rust +// `TrackState::find_unaccounted`. +let unaccounted: (producer: Producer, start: bigint, end: bigint) => { folds?: Instant } | undefined; + // When a received copy's time start at `time`, waiting to be placed, next may place itself with no // change to wake its readers: the grace of the lowest gap {@link landedFrom} still waits on // folds. Mirrors the Rust `TrackState::time_start_folds`. @@ -1585,18 +1591,24 @@ export class Producer { // An open range never resolves empty, so such an answer says nothing. if (start === undefined && through === undefined) continue; const held = merged.get(time); + if (held === undefined) { + merged.set(time, start === undefined ? { empty: budget, through } : { start, through }); + continue; + } // Two ranges asked from one time: the lower start and the farther reach hold for both. + // A start reaches only as far as the answers placing it, never an empty one's. + if (held.start !== undefined && start === undefined) continue; + if (held.start === undefined && start !== undefined) { + merged.set(time, { start, through }); + continue; + } const reach = - held === undefined - ? through - : held.through === undefined || through === undefined - ? undefined - : max(held.through, through); - const lower = - held?.start === undefined || start === undefined ? (held?.start ?? start) : min(held.start, start); + held.through === undefined || through === undefined ? undefined : max(held.through, through); merged.set( time, - lower === undefined ? { empty: budget, through: reach } : { start: lower, through: reach }, + held.start === undefined || start === undefined + ? { empty: budget, through: reach } + : { start: min(held.start, start), through: reach }, ); } producer.#timeStarts = merged; @@ -1728,6 +1740,10 @@ export class Producer { } }; mayLand = (producer, land) => producer.#landable(land) !== undefined; + unaccounted = (producer, start, end) => { + const next = producer.#unaccounted(start, end, producer.#grace()).next(); + return next.done ? undefined : { folds: next.value.folds }; + }; wireFed = (producer) => producer.#wire !== undefined; feedFloor = (producer) => { if (!producer.#wire) return undefined; @@ -1786,10 +1802,17 @@ export class Producer { // asks the feed for the groups below it, which a fill must not duplicate. const feed = this.#feedStart(); if (typeof feed === "bigint") start = max(start, feed); + yield* this.#unaccounted(start, land.end, land.grace); + } + + // The runs of `[start, end)` the live feed has not accounted for, lowest first: not arrived, not + // dropped, before the track's end, and in a gap whose grace has not folded. Mirrors the Rust + // `TrackState::find_unaccounted`. + *#unaccounted(start: bigint, end: bigint, limit: Duration): Generator { + if (this.#state.closed.peek() !== undefined) return; const declared = this.#state.end.peek()?.at; - const end = declared === undefined ? land.end : min(land.end, finalKey(declared)); + if (declared !== undefined) end = min(end, finalKey(declared)); const at = now(); - const limit = land.grace; const drops = this.#state.drops; for (const gap of this.#arrivals.gaps(start, end)) { const folds = gap.since?.saturatingAdd(limit); @@ -4428,6 +4451,7 @@ export class Subscriber { hooks.tryDrop = (subscriber) => subscriber.#tryDrop(); // A reader following route copies leaves its ranges to the serving copy, which resolves // their anchors against the groups it holds, then narrows them to its own local bounds. + hooks.gapHeld = (subscriber, at, end) => subscriber.#gapHeld(at, end); hooks.servingRuns = (subscriber, start, end) => { const cursor = subscriber.#cursor.peek(); const from = max(start, cursor.start); @@ -5404,10 +5428,10 @@ export class Subscriber { // Whether a time start still waiting below `end` holds back a gap an ordered read at `at` would // return, as #waitTimeStart holds a group: the group containing it may lie below the gap, and an - // ordered read never goes back. A resumed reader's copies hold their own groups, and its gaps go - // as found. Mirrors the Rust `Cursor::poll_gap_held`. + // ordered read never goes back. A resumed reader asks its serving copy, whose runs it reads. Mirrors + // the Rust `Cursor::poll_gap_held`. #gapHeld(at: bigint, end: bigint): boolean { - if (this.#resume) return false; + if (this.#resume) return this.#resume.gapHeld(at, end); this.#settle(); const start = max(max(this.#cursor.peek().start, this.#nextKey), at); return this.#waitTimeStart(start, end, this.#drift()); @@ -5574,6 +5598,13 @@ export class Subscriber { const producer = this.#state.producer; let waits = false; for (const time of this.#window.waitingIn(start, key)) { + if (producer && wireFed(producer)) { + // With every key from the floor through the time accounted for, a group still to come + // containing it lies below the floor, which the read never hands out. + const open = unaccounted(producer, start, time + 1n); + if (open === undefined) continue; + if (open.folds !== undefined) this.#wakeAt(open.folds.since(Instant.ZERO).asNanos()); + } // Its range had no group to deliver, so the feed lands none of it. Its successor is this // group's or one below: the group containing the time is judged as an owed group at it would be. if (producer && answeredEmpty(producer, time, drift.budget)) continue; diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 4c9bd10f6fa..8adf63192da 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -6173,6 +6173,49 @@ mod test { } } + /// A front's key-order reader holds back a gap above a time start its serving copy still + /// waits to place, as a plain reader does: the group containing it may land below the gap. + #[test] + fn a_front_holds_a_gap_above_a_waiting_time_start() { + let routes = Producer::new(); + let logical = logical(&routes); + let serving = received_copy(); + serving.anchored(); + routes.serve(serving.consume()); + write(&serving, 2000); + serving.drop_groups(1500..1600, StreamError::NotFound); + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_ranges([track::Range { + start: Anchor::Time(1500), + end: None, + }]); + let mut sub = logical + .consume() + .subscribe(subscription) + .now_or_never() + .unwrap() + .unwrap() + .ordered(); + let waiter = kio::Waiter::noop(); + let mut read = || -> Vec { + let mut items = Vec::new(); + while let Poll::Ready(Ok(Some(item))) = sub.poll_next_group(&waiter) { + items.push(match item { + track::Item::Group(group) => format!("group {}", group.start), + track::Item::Gap { range, .. } => format!("gap {range:?}"), + }); + } + items + }; + assert_eq!(read(), Vec::::new(), "the gap waits on the start"); + serving.time_starts([(1500, Some(1000), None)], Duration::MAX); + write(&serving, 1000); + serving.skip_groups(1001..1500); + serving.skip_groups(1600..2000); + assert_eq!(read(), ["group 1000", "gap 1500..1600", "group 2000"]); + } + /// A front's key-order reader reports a group that lands on its copy after the read passed /// its key, as old, and never one it handed out. #[test] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index ba11b80489f..735e80c7680 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3447,16 +3447,28 @@ impl TrackState { groups: ops::Range, floor: bool, grace: Duration, - mut f: impl FnMut(ops::Range, Option) -> Option, + f: impl FnMut(ops::Range, Option) -> Option, ) -> Option { let start = match floor { true => groups.start, false => groups.start.max(self.arrivals.first()?), }; let start = start.max(self.start.key().unwrap_or(0)); - let end = groups.end.min(self.final_key().unwrap_or(u64::MAX)); + self.find_unaccounted(start..groups.end, grace, f) + } + + /// The first `f` answers of the runs of `keys` the live feed has not accounted for, lowest + /// first: not arrived, not dropped, before the track's end, and in a gap whose grace has not + /// folded. Unlike [`Self::find_landable`], a key below where the feed starts counts. + fn find_unaccounted( + &self, + keys: ops::Range, + grace: Duration, + mut f: impl FnMut(ops::Range, Option) -> Option, + ) -> Option { + let end = keys.end.min(self.final_key().unwrap_or(u64::MAX)); let now = self.cache.pool().now_wall(); - for (gap, opened) in self.arrivals.gaps(start..end) { + for (gap, opened) in self.arrivals.gaps(keys.start..end) { let folds = opened.and_then(|opened| opened.checked_add(grace)); if folds.is_some_and(|folds| now >= folds) { continue; @@ -4974,15 +4986,17 @@ impl Producer { } let answer = start.map_or(Answer::Empty(budget), Answer::At); // Two ranges asked from one time: the lower start and the farther reach hold for both. + // A start reaches only as far as the answers placing it, never an empty one's. merged .entry(time) .and_modify(|held| { - held.answer = match (held.answer, answer) { - (Answer::At(held), Answer::At(start)) => Answer::At(held.min(start)), - (Answer::At(start), _) | (_, Answer::At(start)) => Answer::At(start), - (empty, _) => empty, + let reach = held.through.zip(through).map(|(a, b)| a.max(b)); + (held.answer, held.through) = match (held.answer, answer) { + (Answer::At(held), Answer::At(start)) => (Answer::At(held.min(start)), reach), + (Answer::At(_), Answer::Empty(_)) => (held.answer, held.through), + (Answer::Empty(_), Answer::At(_)) => (answer, through), + (Answer::Empty(_), Answer::Empty(_)) => (held.answer, reach), }; - held.through = held.through.zip(through).map(|(a, b)| a.max(b)); }) .or_insert(TimeStart { answer, through }); } @@ -9687,7 +9701,20 @@ impl Cursor { waited.borrow_mut().clear(); // An unjudgeable old gap cannot hold a finite read, as for an owed group. let floor = times.start.max(state.cache_floor(drift.budget)); + let grace = state.arrival_grace(); for time in window.waiting_in(floor..times.end) { + // With every key from the floor through the time accounted for, a group still + // to come containing it lies below the floor, which the read never hands out. + let unaccounted = match state.anchors.received() { + true => state.find_unaccounted(floor..time.saturating_add(1), grace, |_, folds| Some(folds)), + false => Some(None), + }; + let Some(unaccounted) = unaccounted else { + continue; + }; + if let Some(folds) = unaccounted { + state.cache.watch_wall(folds, waiter); + } // Its range had no group to deliver, so the feed lands none of it. Its // successor is this group's or one below: the group containing the time is // judged as an owed group at it would be. @@ -11103,8 +11130,9 @@ impl Subscriber { } /// Pending while a waiting time start below `end` holds back a gap an [`Ordered`] read at - /// `from` would return; see [`Cursor::poll_gap_held`]. A resumed reader's copies hold their - /// own groups, and its gaps go as found. + /// `from` would return; see [`Cursor::poll_gap_held`]. A resumed reader's own window owes + /// every key from the read until a copy places its time starts, so [`Self::next_in_range`] + /// already holds such a gap. fn poll_gap_held(&mut self, from: u64, end: u64, waiter: &kio::Waiter) -> Poll<()> { match &mut self.inner { Inner::Plain(cursor) => cursor.poll_gap_held(from, end, waiter), @@ -16461,6 +16489,66 @@ mod test { } } + /// A waiting time start whose time the read's floor reaches holds no gap once every key from + /// the floor through the time is accounted for: the group containing it lies below the floor, + /// which the read never hands out. + #[test] + #[cfg(feature = "async")] + fn a_waiting_time_start_holds_no_gap_once_nothing_past_the_floor_may_contain_it() { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + let ordered = |producer: &Producer| { + producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .with_range(1500..1600) + .ordered() + }; + let next = |ordered: &mut Ordered| match ordered.next_group().now_or_never() { + None => None, + Some(Ok(Some(Item::Gap { range, .. }))) => Some(range), + Some(res) => panic!("not a gap: {:?}", res.map(|item| item.is_some())), + }; + producer.drop_groups(1501..1600, StreamError::NotFound); + let mut waiting = ordered(&producer); + assert_eq!(next(&mut waiting), None, "1500 may still land"); + producer.drop_groups(1500..1501, StreamError::NotFound); + assert_eq!(next(&mut waiting).map(|gap| gap.start), Some(1500)); + assert_eq!(next(&mut ordered(&producer)).map(|gap| gap.start), Some(1500)); + } + + /// A range answered empty reaches no time for the start another range asked from the same + /// time reported: that start covers only the times its own answer reaches. + #[test] + fn an_empty_answer_never_extends_the_reach_of_a_start() { + for empty_first in [false, true] { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 500); + let mut starts = vec![(1000, Some(1200), Some(1300)), (1000, None, Some(1800))]; + if empty_first { + starts.reverse(); + } + producer.time_starts(starts, Duration::MAX); + let cut = producer.create_group(group::Info { start: 1200 }).unwrap(); + cut.abort(Error::Cancel).unwrap(); + producer.modify().unwrap().reclaim_aborted(1200); + producer.skip_groups(1201..1600); + let state = producer.state.read(); + assert_eq!(state.containing(1250), Containing::Group(1200)); + assert_eq!(state.containing(1500), Containing::Unknown, "no answer reaches it"); + } + } + /// A skip, even one forgotten into the feed's accounting, names no group, so it can't stand /// in for the feed's first arrival a waiting time start counts from: the group containing /// the time may still land below it. From a290bd90824c42ae18e64a47d45395897a12d701 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 13:53:31 -0800 Subject: [PATCH 10/17] fix(moq-net): a stale verdict narrower than a read keeps its time start waiting The time-start barrier counted every drop as accounted for, but a stale verdict judged under a narrower budget settles nothing for a wider reader: the group containing the time may still land there. Rust and js/net now count such keys as unaccounted. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ordered.test.ts | 18 ++++++++ js/net/src/track.ts | 48 ++++++++++++-------- rs/moq-net/src/model/track.rs | 82 ++++++++++++++++++++++++++++++----- 3 files changed, 118 insertions(+), 30 deletions(-) diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index 7b066eaf1ef..9f2165e6e19 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -545,6 +545,24 @@ test("a waiting time start holds no gap once nothing from the read's floor throu expect(items(ordered())[0]).toStartWith("gap 1500.."); }); +test("a stale verdict narrower than an ordered read's budget leaves its time start waiting", () => { + const producer = receivedTrack(); + put(producer, 2000n, false); + hooks.dropTrackGroups(producer, 1000n, 1500n, staleHole(100)); + drop(producer, 1500n, 1600n); + const ordered = producer + .subscribe({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }) + .withRange({ start: { included: 1000n }, end: { excluded: 1600n } }) + .ordered(); + // The group containing 1500 may still land for this budget. + expect(items(ordered)).toEqual([]); + put(producer, 1200n, false); + expect(items(ordered)).toEqual(["group 1200"]); + hooks.timeStarts(producer, [[1500n, 1200n]], NO_MAX_DELAY); + producer.skipGroups(1201n, 1500n); + expect(items(ordered)).toEqual(["gap 1500..1600 (delivery timeout)"]); +}); + test("an empty answer never extends the reach of a start reported for its time", () => { for (const emptyFirst of [false, true]) { const producer = receivedTrack(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 983243d97f5..87fb20d2b95 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1033,11 +1033,10 @@ let answeredEmpty: (producer: Producer, time: bigint, budget: number) => boolean // `TrackState::landing_from` and `TrackState::landed_through`. let landedFrom: (producer: Producer, time: bigint, covering: bigint | undefined) => bigint | undefined; -// The first run of `[start, end)` a received copy's live feed has not accounted for: not arrived, -// not dropped, before the track's end, and in a gap whose grace has not folded. Unlike -// {@link mayLand}, a key below where the feed starts counts. Mirrors the Rust -// `TrackState::find_unaccounted`. -let unaccounted: (producer: Producer, start: bigint, end: bigint) => { folds?: Instant } | undefined; +// The lowest key of `[start, end)` a received copy's live feed has not accounted for a reader within +// `budget`, with when its gap's grace folds; see `Producer.#unaccounted`. Unlike {@link mayLand}, a +// key below where the feed starts counts. Mirrors the Rust `TrackState::unaccounted`. +let unaccounted: (producer: Producer, start: bigint, end: bigint, budget: number) => { folds?: Instant } | undefined; // When a received copy's time start at `time`, waiting to be placed, next may place itself with no // change to wake its readers: the grace of the lowest gap {@link landedFrom} still waits on @@ -1740,10 +1739,7 @@ export class Producer { } }; mayLand = (producer, land) => producer.#landable(land) !== undefined; - unaccounted = (producer, start, end) => { - const next = producer.#unaccounted(start, end, producer.#grace()).next(); - return next.done ? undefined : { folds: next.value.folds }; - }; + unaccounted = (producer, start, end, budget) => producer.#unaccounted(start, end, budget); wireFed = (producer) => producer.#wire !== undefined; feedFloor = (producer) => { if (!producer.#wire) return undefined; @@ -1802,17 +1798,10 @@ export class Producer { // asks the feed for the groups below it, which a fill must not duplicate. const feed = this.#feedStart(); if (typeof feed === "bigint") start = max(start, feed); - yield* this.#unaccounted(start, land.end, land.grace); - } - - // The runs of `[start, end)` the live feed has not accounted for, lowest first: not arrived, not - // dropped, before the track's end, and in a gap whose grace has not folded. Mirrors the Rust - // `TrackState::find_unaccounted`. - *#unaccounted(start: bigint, end: bigint, limit: Duration): Generator { - if (this.#state.closed.peek() !== undefined) return; const declared = this.#state.end.peek()?.at; - if (declared !== undefined) end = min(end, finalKey(declared)); + const end = declared === undefined ? land.end : min(land.end, finalKey(declared)); const at = now(); + const limit = land.grace; const drops = this.#state.drops; for (const gap of this.#arrivals.gaps(start, end)) { const folds = gap.since?.saturatingAdd(limit); @@ -1823,6 +1812,27 @@ export class Producer { } } + // The lowest key of `[start, end)` the live feed has not accounted for a reader within `budget`: + // not arrived, not given up for good or by a stale verdict covering that budget, before the + // track's end, and in a gap whose grace has not folded. Unlike #landing, a key below where the + // feed starts counts. Mirrors the Rust `TrackState::unaccounted`. + #unaccounted(start: bigint, end: bigint, budget: number): { folds?: Instant } | undefined { + if (this.#state.closed.peek() !== undefined) return undefined; + const declared = this.#state.end.peek()?.at; + if (declared !== undefined) end = min(end, finalKey(declared)); + const at = now(); + const limit = this.#grace(); + for (const gap of this.#arrivals.gaps(start, end)) { + const folds = gap.since?.saturatingAdd(limit); + if (folds !== undefined && at.ge(folds)) continue; + const settled = this.#state.drops + .overlapping(gap.start, gap.end) + .filter((run) => run.stale === undefined || budget <= run.stale); + if (runGaps(settled, gap.start, gap.end).length > 0) return { folds }; + } + return undefined; + } + // Record `[start, end)` given up as `hole` says, returning the runs recorded. A skip is final // and the most authoritative verdict, so a drop never replaces one. An upstream drop (`open`) // also ends the groups still arriving in it with its error, so their readers settle now @@ -5601,7 +5611,7 @@ export class Subscriber { if (producer && wireFed(producer)) { // With every key from the floor through the time accounted for, a group still to come // containing it lies below the floor, which the read never hands out. - const open = unaccounted(producer, start, time + 1n); + const open = unaccounted(producer, start, time + 1n, drift.budget); if (open === undefined) continue; if (open.folds !== undefined) this.#wakeAt(open.folds.since(Instant.ZERO).asNanos()); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 735e80c7680..fd1c1143753 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3447,25 +3447,38 @@ impl TrackState { groups: ops::Range, floor: bool, grace: Duration, - f: impl FnMut(ops::Range, Option) -> Option, + mut f: impl FnMut(ops::Range, Option) -> Option, ) -> Option { let start = match floor { true => groups.start, false => groups.start.max(self.arrivals.first()?), }; let start = start.max(self.start.key().unwrap_or(0)); - self.find_unaccounted(start..groups.end, grace, f) + let end = groups.end.min(self.final_key().unwrap_or(u64::MAX)); + let now = self.cache.pool().now_wall(); + for (gap, opened) in self.arrivals.gaps(start..end) { + let folds = opened.and_then(|opened| opened.checked_add(grace)); + if folds.is_some_and(|folds| now >= folds) { + continue; + } + if let Some(found) = self.drops.gaps(&gap).find_map(|open| f(open, folds)) { + return Some(found); + } + } + None } - /// The first `f` answers of the runs of `keys` the live feed has not accounted for, lowest - /// first: not arrived, not dropped, before the track's end, and in a gap whose grace has not - /// folded. Unlike [`Self::find_landable`], a key below where the feed starts counts. - fn find_unaccounted( + /// When the grace folds for the lowest key of `keys` the live feed has not accounted for + /// a reader within `budget`: not arrived, not given up for good or by a stale verdict + /// covering that budget, before the track's end, and in a gap whose grace has not folded. + /// `None` with none left. Unlike [`Self::find_landable`], a key below where the feed starts + /// counts. + fn unaccounted( &self, keys: ops::Range, + budget: Duration, grace: Duration, - mut f: impl FnMut(ops::Range, Option) -> Option, - ) -> Option { + ) -> Option> { let end = keys.end.min(self.final_key().unwrap_or(u64::MAX)); let now = self.cache.pool().now_wall(); for (gap, opened) in self.arrivals.gaps(keys.start..end) { @@ -3473,8 +3486,18 @@ impl TrackState { if folds.is_some_and(|folds| now >= folds) { continue; } - if let Some(found) = self.drops.gaps(&gap).find_map(|open| f(open, folds)) { - return Some(found); + let mut at = gap.start; + for (run, hole) in self.drops.overlapping(&gap) { + if hole.stale_budget().is_some_and(|stale| budget > stale) { + continue; + } + if at < run.start { + return Some(folds); + } + at = at.max(run.end); + } + if at < gap.end { + return Some(folds); } } None @@ -9706,7 +9729,7 @@ impl Cursor { // With every key from the floor through the time accounted for, a group still // to come containing it lies below the floor, which the read never hands out. let unaccounted = match state.anchors.received() { - true => state.find_unaccounted(floor..time.saturating_add(1), grace, |_, folds| Some(folds)), + true => state.unaccounted(floor..time.saturating_add(1), drift.budget, grace), false => Some(None), }; let Some(unaccounted) = unaccounted else { @@ -16525,6 +16548,43 @@ mod test { assert_eq!(next(&mut ordered(&producer)).map(|gap| gap.start), Some(1500)); } + /// A stale verdict under a narrower budget than an ordered read's settles none of its keys for + /// it, so the group containing a waiting time start may still land there, and a gap above + /// the time waits for it. + #[test] + #[cfg(feature = "async")] + fn a_narrower_stale_verdict_leaves_a_time_start_waiting() { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + producer.give_up(1000..1500, Hole::stale(Duration::from_millis(100))); + producer.drop_groups(1500..1600, StreamError::NotFound); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .with_range(1000..1600) + .ordered(); + let mut next = || match ordered.next_group().now_or_never() { + None => None, + Some(Ok(Some(Item::Group(group)))) => Some(Ok(group.start)), + Some(Ok(Some(Item::Gap { range, .. }))) => Some(Err(range)), + Some(res) => panic!("ended: {:?}", res.map(|_| ())), + }; + assert_eq!(next(), None, "the group containing 1500 may still land"); + write_group(&producer, 1200); + assert_eq!(next(), Some(Ok(1200))); + // Passed as a gap, so nothing the read may still take can contain 1500. + assert_eq!(next(), Some(Err(1201..1500))); + assert_eq!(next(), Some(Err(1500..1600))); + } + /// A range answered empty reaches no time for the start another range asked from the same /// time reported: that start covers only the times its own answer reaches. #[test] From 5650182fd6e1d47a4520b258322f82311bd9125f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 17:06:22 -0800 Subject: [PATCH 11/17] fix(moq-net): a time start's barrier counts a verdict only for a reader it settles The release step and an empty range answer judged a stale verdict by budget alone. A capped reader within the budget is not settled by it (Drift::settles), so it asks again through its misses, and only that answer counts. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ordered.test.ts | 34 +++++++ js/net/src/track.ts | 84 +++++++++++----- rs/moq-net/src/model/track.rs | 150 +++++++++++++++++++++++++---- rs/moq-net/src/model/track/miss.rs | 40 +++++--- 4 files changed, 253 insertions(+), 55 deletions(-) diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index 9f2165e6e19..6bf83e49112 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -563,6 +563,21 @@ test("a stale verdict narrower than an ordered read's budget leaves its time sta expect(items(ordered)).toEqual(["gap 1500..1600 (delivery timeout)"]); }); +test("a stale verdict a capped ordered read rejects leaves its time start waiting", () => { + const producer = receivedTrack(); + put(producer, 2000n, false); + hooks.dropTrackGroups(producer, 1000n, 1500n, staleHole(10_000)); + drop(producer, 1500n, 1600n); + const ordered = producer + .subscribe({ delay: { max: Duration.secs(1) }, ranges: [{ start: { time: 1500n } }] }) + .withRange({ start: { included: 1000n }, end: { excluded: 1600n } }) + .ordered(); + // Within the verdict's budget, but the cap keeps the read from the groups it rests on. + expect(items(ordered)).toEqual([]); + hooks.timeStarts(producer, [[1500n, 1500n]], NO_MAX_DELAY); + expect(items(ordered)).toEqual(["gap 1500..1600 (delivery timeout)"]); +}); + test("an empty answer never extends the reach of a start reported for its time", () => { for (const emptyFirst of [false, true]) { const producer = receivedTrack(); @@ -604,6 +619,25 @@ test("a folded time start never places below the range covering it", () => { expect(hooks.servingResolved(subscriber)).toEqual([{ start: 1200n, end: undefined, floored: true }]); }); +test("a range answered empty releases no capped ordered read", () => { + const producer = receivedTrack(); + put(producer, 1850n, false); + put(producer, 2000n, false); + hooks.timeStarts(producer, [[1000n, undefined, 1800n]], 100); + // GROUP 1850 says nothing lies between the time and it. + producer.skipGroups(1000n, 1850n); + const read = (cap: bigint | undefined) => + items( + producer + .subscribe({ delay: { max: Duration.millis(100) }, ranges: [{ start: { time: 1000n } }] }) + .withRange(cap === undefined ? {} : { end: { excluded: cap } }) + .ordered(), + ); + expect(read(undefined)[0]).toEqual("group 1850"); + // The cap keeps it from the groups the answer rests on. + expect(read(1900n)).toEqual([]); +}); + test("a range answered empty holds back no later group within its budget, though it places nothing", () => { const producer = receivedTrack(); put(producer, 2000n, false); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 87fb20d2b95..68d422b6fcc 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1022,9 +1022,11 @@ let timeAnchors: (producer: Producer) => { type TimeStart = ({ start: bigint; empty?: never } | { start?: never; empty: number }) & { through?: bigint }; // Whether a received copy's range asked from `time`, or the one upstream folded it into, had no -// group to deliver to a reader within `budget`: the live feed brings it none of the range's groups. -// Assigned in the producer's static block. Mirrors the Rust `TrackState::answered_empty`. -let answeredEmpty: (producer: Producer, time: bigint, budget: number) => boolean; +// group to deliver to a reader judging under `budget` with `coverage`: the live feed brings it none +// of the range's groups. The answer is a staleness verdict, so it holds only for a reader it settles +// (see {@link settles}). Assigned in the producer's static block. Mirrors the Rust +// `TrackState::answered_empty`. +let answeredEmpty: (producer: Producer, time: bigint, budget: number, coverage: Coverage) => boolean; // Where a received copy's live feed landed every key it could through `time`, counting from // `covering`, where a range covering the time started, or else its first arrival at or below @@ -1033,10 +1035,16 @@ let answeredEmpty: (producer: Producer, time: bigint, budget: number) => boolean // `TrackState::landing_from` and `TrackState::landed_through`. let landedFrom: (producer: Producer, time: bigint, covering: bigint | undefined) => bigint | undefined; -// The lowest key of `[start, end)` a received copy's live feed has not accounted for a reader within -// `budget`, with when its gap's grace folds; see `Producer.#unaccounted`. Unlike {@link mayLand}, a -// key below where the feed starts counts. Mirrors the Rust `TrackState::unaccounted`. -let unaccounted: (producer: Producer, start: bigint, end: bigint, budget: number) => { folds?: Instant } | undefined; +// The lowest key of `[start, end)` a received copy's live feed has not accounted for a reader, with +// when its gap's grace folds; see `Producer.#unaccounted`. `settled` gives the runs of a gap that +// reader has a verdict on. Unlike {@link mayLand}, a key below where the feed starts counts. Mirrors +// the Rust `TrackState::unaccounted`. +let unaccounted: ( + producer: Producer, + start: bigint, + end: bigint, + settled: (start: bigint, end: bigint) => Run[], +) => { folds?: Instant } | undefined; // When a received copy's time start at `time`, waiting to be placed, next may place itself with no // change to wake its readers: the grace of the lowest gap {@link landedFrom} still waits on @@ -1131,9 +1139,13 @@ function reaches(received: bigint, drops: DropSet, end: End, refused: readonly R * above it. Mirrors the Rust `settles`. */ function settles(run: DropRun, budget: number, coverage: Coverage): boolean { - if (run.stale === undefined) return true; - if (budget > run.stale || coverage === undefined || coverage.cap !== undefined) return false; - const above = run.start + 1n; + return run.stale === undefined || staleSettles(run.start, run.stale, budget, coverage); +} + +// Whether a staleness verdict at `start` judged under `stale` settles a reader; see {@link settles}. +function staleSettles(start: bigint, stale: number, budget: number, coverage: Coverage): boolean { + if (budget > stale || coverage === undefined || coverage.cap !== undefined) return false; + const above = start + 1n; const held = coverage.runs.find((run) => run.start <= above && above < run.end); return held !== undefined && held.end >= UNBOUNDED; } @@ -1643,9 +1655,9 @@ export class Producer { const end = next.done ? UNBOUNDED : next.value.start; return end > start ? { start, end } : undefined; }; - answeredEmpty = (producer, time, budget) => { + answeredEmpty = (producer, time, budget, coverage) => { const reported = producer.#timeStarts.get(time) ?? coveringStart(producer.#timeStarts, time); - return reported?.empty !== undefined && budget <= reported.empty; + return reported?.empty !== undefined && staleSettles(time, reported.empty, budget, coverage); }; timeStartFolds = (producer, time) => { // A reported start places it. An empty answer places nothing, as for `containing`, so a @@ -1739,7 +1751,7 @@ export class Producer { } }; mayLand = (producer, land) => producer.#landable(land) !== undefined; - unaccounted = (producer, start, end, budget) => producer.#unaccounted(start, end, budget); + unaccounted = (producer, start, end, settled) => producer.#unaccounted(start, end, settled); wireFed = (producer) => producer.#wire !== undefined; feedFloor = (producer) => { if (!producer.#wire) return undefined; @@ -1812,11 +1824,15 @@ export class Producer { } } - // The lowest key of `[start, end)` the live feed has not accounted for a reader within `budget`: - // not arrived, not given up for good or by a stale verdict covering that budget, before the - // track's end, and in a gap whose grace has not folded. Unlike #landing, a key below where the - // feed starts counts. Mirrors the Rust `TrackState::unaccounted`. - #unaccounted(start: bigint, end: bigint, budget: number): { folds?: Instant } | undefined { + // The lowest key of `[start, end)` the live feed has not accounted for a reader: not arrived, + // outside the runs `settled` gives it a verdict on, before the track's end, and in a gap whose + // grace has not folded. Unlike #landing, a key below where the feed starts counts. Mirrors the + // Rust `TrackState::unaccounted`. + #unaccounted( + start: bigint, + end: bigint, + settled: (start: bigint, end: bigint) => Run[], + ): { folds?: Instant } | undefined { if (this.#state.closed.peek() !== undefined) return undefined; const declared = this.#state.end.peek()?.at; if (declared !== undefined) end = min(end, finalKey(declared)); @@ -1825,10 +1841,7 @@ export class Producer { for (const gap of this.#arrivals.gaps(start, end)) { const folds = gap.since?.saturatingAdd(limit); if (folds !== undefined && at.ge(folds)) continue; - const settled = this.#state.drops - .overlapping(gap.start, gap.end) - .filter((run) => run.stale === undefined || budget <= run.stale); - if (runGaps(settled, gap.start, gap.end).length > 0) return { folds }; + if (runGaps(settled(gap.start, gap.end), gap.start, gap.end).length > 0) return { folds }; } return undefined; } @@ -5599,6 +5612,28 @@ export class Subscriber { if (this.#dropsSeen) this.#pending = insertDrop(this.#pending, run.start, run.end, hole); } + // The runs of `[start, end)` this reader has a verdict on: a drop that settles it, or what its + // own fills answered, a group or a refusal, or it gave up. A stale verdict that does not settle it + // is none: the reader asks the group again, and only that answer counts. Mirrors the Rust + // `Misses::unanswered`. + #verdicts(start: bigint, end: bigint, drift: Drift, coverage: Coverage): Run[] { + const runs: Run[] = []; + for (const drop of this.#state.drops.overlapping(start, end)) { + if (settles({ ...drop, start: max(drop.start, start) }, drift.budget, coverage)) { + insertRun(runs, drop.start, drop.end); + } + } + for (const run of [...this.#state.refused.peek(), ...this.#givenUp]) { + if (run.start < end && start < run.end) insertRun(runs, run.start, run.end); + } + const timeline = this.#state.timeline; + for (let i = timelineIndex(timeline, start); i < timeline.length && timeline[i].start < end; i++) { + const group = timeline[i]; + if (!hooks.groupFailed(group)) insertRun(runs, group.start, group.start + 1n); + } + return runs; + } + // In order, the group a waiting time start resolves to comes first: it starts at or before that // time, below `key`, and may still land at or past the read's floor `start`, until the budget // gives up on it as on an owed group. Its landing, the answer placing the start, the grace for a @@ -5607,17 +5642,18 @@ export class Subscriber { #waitTimeStart(start: bigint, key: bigint, drift: Drift): boolean { const producer = this.#state.producer; let waits = false; + const coverage = this.#coverage(); for (const time of this.#window.waitingIn(start, key)) { if (producer && wireFed(producer)) { // With every key from the floor through the time accounted for, a group still to come // containing it lies below the floor, which the read never hands out. - const open = unaccounted(producer, start, time + 1n, drift.budget); + const open = unaccounted(producer, start, time + 1n, (from, to) => this.#verdicts(from, to, drift, coverage)); if (open === undefined) continue; if (open.folds !== undefined) this.#wakeAt(open.folds.since(Instant.ZERO).asNanos()); } // Its range had no group to deliver, so the feed lands none of it. Its successor is this // group's or one below: the group containing the time is judged as an owed group at it would be. - if (producer && answeredEmpty(producer, time, drift.budget)) continue; + if (producer && answeredEmpty(producer, time, drift.budget, coverage)) continue; if (this.#staleAt(time, drift, true)) continue; const folds = producer && timeStartFolds(producer, time); if (folds !== undefined) this.#wakeAt(folds.since(Instant.ZERO).asNanos()); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index fd1c1143753..8100a8682f4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1365,14 +1365,15 @@ impl TrackState { } /// Whether the range asked from `time`, or the one upstream folded it into, had no group to - /// deliver to a reader within `budget`: the live feed brings it none of the range's groups. - fn answered_empty(&self, time: u64, budget: Duration) -> bool { + /// deliver to a reader under `drift`: the live feed brings it none of the range's groups. The + /// answer is a staleness verdict, so it holds only for a reader it settles (see [`settles`]). + fn answered_empty(&self, time: u64, drift: &Drift) -> bool { let reported = self .time_starts .get(&time) .copied() .or_else(|| self.covering_start(time)); - matches!(reported, Some(TimeStart { answer: Answer::Empty(asked), .. }) if budget <= asked) + matches!(reported, Some(TimeStart { answer: Answer::Empty(asked), .. }) if drift.settles(time, &Hole::stale(asked))) } /// The range asked from an earlier time that reaches `time`, which upstream folded a range @@ -3469,15 +3470,17 @@ impl TrackState { } /// When the grace folds for the lowest key of `keys` the live feed has not accounted for - /// a reader within `budget`: not arrived, not given up for good or by a stale verdict - /// covering that budget, before the track's end, and in a gap whose grace has not folded. + /// a reader under `drift`: not arrived, without a verdict settling it, before the track's + /// end, and in a gap whose grace has not folded. A stale verdict that does not settle the + /// reader is no verdict: it asks the group again, and only the answer, in `misses`, counts. /// `None` with none left. Unlike [`Self::find_landable`], a key below where the feed starts /// counts. fn unaccounted( &self, keys: ops::Range, - budget: Duration, + drift: &Drift, grace: Duration, + misses: Option<&miss::Misses>, ) -> Option> { let end = keys.end.min(self.final_key().unwrap_or(u64::MAX)); let now = self.cache.pool().now_wall(); @@ -3486,17 +3489,11 @@ impl TrackState { if folds.is_some_and(|folds| now >= folds) { continue; } - let mut at = gap.start; - for (run, hole) in self.drops.overlapping(&gap) { - if hole.stale_budget().is_some_and(|stale| budget > stale) { - continue; - } - if at < run.start { - return Some(folds); - } - at = at.max(run.end); - } - if at < gap.end { + let open = match misses { + Some(misses) => misses.unanswered(self, gap, drift), + None => miss::unsettled(self, gap, drift), + }; + if !open.is_empty() { return Some(folds); } } @@ -9729,7 +9726,7 @@ impl Cursor { // With every key from the floor through the time accounted for, a group still // to come containing it lies below the floor, which the read never hands out. let unaccounted = match state.anchors.received() { - true => state.unaccounted(floor..time.saturating_add(1), drift.budget, grace), + true => state.unaccounted(floor..time.saturating_add(1), drift, grace, self.misses.as_deref()), false => Some(None), }; let Some(unaccounted) = unaccounted else { @@ -9741,7 +9738,7 @@ impl Cursor { // Its range had no group to deliver, so the feed lands none of it. Its // successor is this group's or one below: the group containing the time is // judged as an owed group at it would be. - if state.answered_empty(time, drift.budget) + if state.answered_empty(time, drift) || state.poll_drifted(time, &drift.edge.servable, drift.budget, None, waiter) { continue; @@ -16245,6 +16242,40 @@ mod test { assert_eq!(read(1500, None), None, "a wider reader may still get 1000"); } + /// A range answered empty is a staleness verdict, so it releases only a reader it settles: + /// one capped below the live edge, though within its budget, keeps waiting. + #[test] + #[cfg(feature = "async")] + fn an_empty_range_releases_no_capped_read() { + let budget = Duration::from_millis(100); + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 1850); + write_group(&producer, 2000); + producer.time_starts([(1000, None, Some(1800))], budget); + let read = |cap: u64| { + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1000), + end: None, + }]) + .with_delay(Delay::default().with_max(Some(budget))), + ) + .with_range(..cap) + .ordered(); + match ordered.next_group().now_or_never() { + Some(Ok(Some(Item::Group(group)))) => Some(group.start), + None => None, + Some(res) => panic!("not a group: {:?}", res.map(|item| item.is_some())), + } + }; + assert_eq!(read(u64::MAX), Some(1850)); + assert_eq!(read(1900), None, "the cap keeps it from the groups the answer rests on"); + } + /// A lite-07 copy places a time start that upstream folded into an earlier range by that /// range's answer, which reports nothing for the time itself: one that started past the /// time served nothing at or before it, so an ordered read goes on. A range ending before @@ -16585,6 +16616,87 @@ mod test { assert_eq!(next(), Some(Err(1500..1600))); } + /// A stale verdict within a capped read's budget settles none of its keys either: the cap + /// keeps it from the newer groups the verdict rests on. The read asks the run again, and a + /// gap above a waiting time start holds until that answer comes. + #[test] + #[cfg(feature = "async")] + fn a_capped_read_asks_a_stale_run_again_before_passing_a_time_start() { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + let dynamic = producer.requests(); + write_group(&producer, 900); + write_group(&producer, 2000); + producer.skip_groups(901..1000); + producer.give_up(1000..1500, Hole::stale(Duration::from_secs(10))); + producer.drop_groups(1500..1600, StreamError::NotFound); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(Some(Duration::from_secs(1)))), + ) + .with_range(900..1600) + .ordered(); + let mut read = || { + let mut items = Vec::new(); + loop { + match ordered.next_group().now_or_never() { + None => return items, + Some(Ok(Some(Item::Group(group)))) => items.push(Ok(group.start)), + Some(Ok(Some(Item::Gap { range, .. }))) => items.push(Err(range)), + Some(res) => panic!("ended: {:?}", res.map(|_| ())), + } + } + }; + assert_eq!(read(), [Ok(900)], "the read asks the stale run again"); + let Fill::Groups(fill) = dynamic.requested_fill().now_or_never().expect("asked").unwrap() else { + panic!("not a group fill"); + }; + assert_eq!(fill.start().group..fill.end(), 1000..1500); + fill.accept(1200, None).unwrap().expect("a fresh group").finish().unwrap(); + fill.refuse_groups(1000..1200, Error::Old); + fill.refuse_groups(1201..1500, Error::Old); + assert_eq!(read(), [Err(1000..1200), Ok(1200), Err(1201..1500), Err(1500..1600)]); + } + + /// Below a time start placed by nothing held, a stale verdict a capped read rejects leaves its + /// keys to the upstream's answer for the time: the gap above it waits for that answer. + #[test] + #[cfg(feature = "async")] + fn a_stale_verdict_a_capped_read_rejects_leaves_a_time_start_waiting() { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + producer.give_up(1000..1500, Hole::stale(Duration::from_secs(10))); + producer.drop_groups(1500..1600, StreamError::NotFound); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(1500), + end: None, + }]) + .with_delay(Delay::default().with_max(Some(Duration::from_secs(1)))), + ) + .with_range(1000..1600) + .ordered(); + let mut next = || match ordered.next_group().now_or_never() { + None => None, + Some(Ok(Some(Item::Group(group)))) => Some(Ok(group.start)), + Some(Ok(Some(Item::Gap { range, .. }))) => Some(Err(range)), + Some(res) => panic!("ended: {:?}", res.map(|_| ())), + }; + assert_eq!(next(), None, "the group containing 1500 may still land"); + producer.time_starts([(1500, Some(1500), None)], Duration::MAX); + assert_eq!(next(), Some(Err(1500..1600))); + } + /// A range answered empty reaches no time for the start another range asked from the same /// time reported: that start covers only the times its own answer reaches. #[test] diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index c1ae7c43692..0832c5164d5 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -280,6 +280,21 @@ impl Deferred { } } +/// The part of `hole` the track's drops leave open for a reader under `drift`: a received +/// stale run that does not settle it is no verdict for it (see `Drift::settles`), so it asks +/// the group again. +pub(super) fn unsettled(state: &TrackState, hole: ops::Range, drift: &Drift) -> RangeSet { + let mut left = RangeSet::new(); + left.insert(hole.clone()); + // Only what overlaps the hole, so judging every run costs each one the same. + for (run, gone) in state.drops.overlapping(&hole) { + if drift.settles(run.start.max(hole.start), gone) { + left.remove(run.clone()); + } + } + left +} + impl Misses { /// The lowest key still to come from a fill: asked, or answered and not handed out. pub fn lowest(&self) -> Option { @@ -1335,23 +1350,24 @@ impl Misses { /// The part of `hole` nobody has a verdict on yet: a received stale run that does not /// settle this reader is no verdict for it (see `Drift::settles`). fn unclaimed(&self, state: &TrackState, hole: ops::Range, drift: &Drift) -> Vec> { - let mut left = RangeSet::new(); - left.insert(hole.clone()); - // Only what overlaps the hole, so judging every run costs each one the same. - for (run, gone) in state.drops.overlapping(&hole) { - if drift.settles(run.start.max(hole.start), gone) { - left.remove(run.clone()); - } + let mut left = self.unanswered(state, hole.clone(), drift); + for run in self.wanted.overlapping(&hole) { + left.remove(run.clone()); } - for claimed in [&self.given_up, &self.wanted] { - for run in claimed.overlapping(&hole) { - left.remove(run.clone()); - } + left.iter().cloned().collect() + } + + /// The part of `hole` with no verdict for this reader and no answer from its fills: still + /// to ask, or asked and not answered. + pub(super) fn unanswered(&self, state: &TrackState, hole: ops::Range, drift: &Drift) -> RangeSet { + let mut left = unsettled(state, hole.clone(), drift); + for run in self.given_up.overlapping(&hole) { + left.remove(run.clone()); } for key in self.filled.range(hole) { left.remove(*key..*key + 1); } - left.iter().cloned().collect() + left } /// Take in a fill's refusal of `groups`: final where the feed gave up on them too, From 98ac671e70884f2b0f87c37b2ef66a5c30452814 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 17:21:11 -0800 Subject: [PATCH 12/17] quest: the time-start follow-up asks upstream for what a copy cannot prove Co-Authored-By: Claude Opus 5.5 --- quest/m1/empty-range-relay.md | 13 ------------- quest/m1/time-start-asks-upstream.md | 29 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 13 deletions(-) delete mode 100644 quest/m1/empty-range-relay.md create mode 100644 quest/m1/time-start-asks-upstream.md diff --git a/quest/m1/empty-range-relay.md b/quest/m1/empty-range-relay.md deleted file mode 100644 index 9dc9a22b2c6..00000000000 --- a/quest/m1/empty-range-relay.md +++ /dev/null @@ -1,13 +0,0 @@ -# [M] A range answered empty upstream stalls a viewer behind a relay - -## Goal - -A viewer behind a relay whose subscription holds a lite-07 time range the publisher answers with no group to deliver (Range Start 0) still reads every later range. -Today, with a viewer subscribed through a relay to `Time(10)..Time(20)` and `Time(66)..` while the publisher holds only group 66, the viewer reads nothing: on `main` and with [moq-dev/moq#9172](https://github.com/moq-dev/moq/pull/9172) alike, the viewer's ordered read waits on a fill it asked at key 10 that never resolves, and the relay never answers the empty range as such. - -## Plan - -- Reproduce with the fixture in `rs/moq-net/tests/integration/waiting_time_start.rs` (publisher, relay, viewer): an unlimited-budget ordered reader with the two ranges above times out before reading 66. -- The cause is that an empty answer only holds under the `Subscriber Max Age` it was asked with (the draft's start "SHOULD start at the oldest group at or above the floor that Expiration has not expired"), so the copy can't place the time for every reader. [moq-dev/moq#9172](https://github.com/moq-dev/moq/pull/9172) records the answer (`TimeStart::start == None`) and only releases the ordered barrier for it; the time stays unplaced, so the relay's window for the downstream range waits, and a `Before` placement makes the copy look up history the publisher won't serve. -- Recommended: scope the empty answer by the budget it was asked under, so a reader whose budget fits places its time past the range's reach (the range resolves empty, and the relay answers 0 downstream), while a wider reader keeps looking it up. Mirror it in js/net. -- Also find why the viewer's fill at key 10 never resolves through the relay when the publisher's handler drops it: a dropped fill names its keys `NotFound`, which should give the fill up. diff --git a/quest/m1/time-start-asks-upstream.md b/quest/m1/time-start-asks-upstream.md new file mode 100644 index 00000000000..7a4384e09a9 --- /dev/null +++ b/quest/m1/time-start-asks-upstream.md @@ -0,0 +1,29 @@ +# [L] A relay copy asks upstream for every time start it cannot prove + +## Goal + +A lite-07 relay copy places a reader's time start only where it can prove what upstream would answer that reader, as `AGENTS.md` requires of a cache. For any other time start, it updates its upstream subscription and waits for the answer. +Two cases fail today, on `main` and with [moq-dev/moq#9172](https://github.com/moq-dev/moq/pull/9172) alike: + +- A reader whose time falls inside a range another reader already asks for adds no range of its own (`wire_ranges` folds it), so upstream never answers for its time. The copy infers a placement from the covering range's start (#9172), from an earlier range that started past the time ([moq-dev/moq#9174](https://github.com/moq-dev/moq/pull/9174)), or waits out the arrival grace. +- A range answer is judged under the `Subscriber Max Age` in effect when the publisher resolved it. A range never resolves again (draft: "A Range resolves once"). A reader wider than that budget, or capped below the live edge, is never answered for its own terms: an empty answer (Range Start 0) leaves it waiting out the grace, and a start (`Answer::At`) places its time for every reader, though the publisher may have started past a group it expired under the narrower budget. + +Done when a viewer behind a relay reads the same groups for a time start as it would reading the publisher directly, with any budget, joining before or after a narrower viewer. + +## Plan + +- Recommended: no new wire message. A copy adds a reader's `Time` range to its upstream subscription as a range of its own in SUBSCRIBE_UPDATE whenever it cannot prove that reader's placement: a time no answer covers for the reader's budget and coverage, including one folded into another range, or one answered under a narrower budget. A new Range ID resolves fresh under the budget in effect then. Groups the subscription already delivered or dropped stay as they are. A dropped group the reader still wants comes on a request of its own, the miss path #9172's release step already relies on. +- Record each range's answer under the narrowest budget asked since that range was added (today `SubStream::stale_budget` is one value for the whole subscription). Then a range re-asked after a widening holds for every reader no wider than the new budget, as the maintainer noted on #9172. +- Then remove the inference no answer proves: #9172's covering-range placement (`landing_from` with a covering start, `Answer::Empty` releasing the barrier), and #9174's earlier-range `Before`. Keep only what the copy proves from held groups (#8638's Previous Start placement). +- Mirror in js/net (`lite/subscriber.ts` `#declare` and its demand ranges), and run `just test interop --all`. +- Reproduce first with the fixture in `rs/moq-net/tests/integration/waiting_time_start.rs` (publisher, relay, viewer). One case: an unlimited-budget ordered reader asks `Time(10)..Time(20)` and `Time(66)..` while the publisher holds only group 66. A second: a narrow viewer joins first, then a wider one asks from a time whose group the narrow budget expired. +- [fill-lookups](/quest/m2/outcome-enums/fill-lookups.md) plans a time lookup a relay forwards upstream. If it lands first, it may serve the second case instead of a demand range; check before building. + +## Waiting + +- Maintainer: confirm the cost of one more wire range per distinct unprovable reader time. These ranges are bounded by the copy's readers and retired with them, and they replace the folding that keeps the range list short today. Recommended: accept it, since the folding is what leaves those times unanswered. + +## Related + +- [fill-lookups](/quest/m2/outcome-enums/fill-lookups.md) - a time lookup as a group fill, which a relay may forward upstream +- [splice-stall](/quest/m2/multicast/splice-stall.md) - suspects #9174's folding of a spliced copy's time start From b15f570f30541c410ecb756c51d3992e6621bb15 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 17:34:55 -0800 Subject: [PATCH 13/17] style: format Co-Authored-By: Claude Opus 5.5 --- js/net/src/track.test.ts | 5 ++++- js/net/src/track.ts | 4 +++- rs/moq-net/src/model/track.rs | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 8517b41cf4c..1d44feca1d3 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5657,7 +5657,10 @@ test("track: a reader's unplaced times are only times its holdings can't place", const reported = new Set(starts.map(([time]) => time)); let past = 0n; const lowest = new Map(); - for (const [asked, start] of starts) lowest.set(asked, (lowest.get(asked) ?? start) < start ? (lowest.get(asked) ?? start) : start); + for (const [asked, start] of starts) { + const held = lowest.get(asked); + if (held === undefined || start < held) lowest.set(asked, start); + } for (const [asked, start] of lowest) if (asked < start && start > past) past = start; const reader = producer.subscribe(); for (let step = random(16); step >= 0; step--) { diff --git a/js/net/src/track.ts b/js/net/src/track.ts index a9191266cb4..c474c16ff8a 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -5668,7 +5668,9 @@ export class Subscriber { if (producer && wireFed(producer)) { // With every key from the floor through the time accounted for, a group still to come // containing it lies below the floor, which the read never hands out. - const open = unaccounted(producer, start, time + 1n, (from, to) => this.#verdicts(from, to, drift, coverage)); + const open = unaccounted(producer, start, time + 1n, (from, to) => + this.#verdicts(from, to, drift, coverage), + ); if (open === undefined) continue; if (open.folds !== undefined) this.#wakeAt(open.folds.since(Instant.ZERO).asNanos()); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 90bddde306a..12e9e1ac166 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -16860,7 +16860,11 @@ mod test { panic!("not a group fill"); }; assert_eq!(fill.start().group..fill.end(), 1000..1500); - fill.accept(1200, None).unwrap().expect("a fresh group").finish().unwrap(); + fill.accept(1200, None) + .unwrap() + .expect("a fresh group") + .finish() + .unwrap(); fill.refuse_groups(1000..1200, Error::Old); fill.refuse_groups(1201..1500, Error::Old); assert_eq!(read(), [Err(1000..1200), Ok(1200), Err(1201..1500), Err(1500..1600)]); From da9fea4992bd4f5e26a25ab689ee291a6ac63d66 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 17:47:22 -0800 Subject: [PATCH 14/17] fix(moq-net): an empty range releases by its wire terms, and a widened read sees past its stale refusals An empty range answer is judged by the range's wire terms, so a reader's own bounds do not keep it waiting (a bounded range would otherwise stall the read behind it). A JS reader's refusal judged stale under a narrower budget no longer releases the time-start barrier once the reader widened. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ordered.test.ts | 43 +++++++++++++----------- js/net/src/track.ts | 33 +++++++++--------- rs/moq-net/src/model/track.rs | 63 ++++++++++++++++++----------------- 3 files changed, 71 insertions(+), 68 deletions(-) diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index 6bf83e49112..494211fdc92 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -5,7 +5,8 @@ import { Duration, sleep } from "@moq/clock"; import { Manual } from "@moq/clock/test"; import { StreamCode, StreamError } from "./error.ts"; import { hooks, NO_MAX_DELAY, staleHole } from "./internal.ts"; -import { textFrame } from "./mock.ts"; +import { rejectGroups } from "./fill.ts"; +import { requestedGroups, textFrame } from "./mock.ts"; import { Timestamp } from "./time.ts"; import { type Ordered, Producer as TrackProducer } from "./track.ts"; @@ -578,6 +579,27 @@ test("a stale verdict a capped ordered read rejects leaves its time start waitin expect(items(ordered)).toEqual(["gap 1500..1600 (delivery timeout)"]); }); +test("a refusal judged stale under a narrower budget leaves a widened read's time start waiting", async () => { + const producer = receivedTrack(); + const requests = producer.requests(); + put(producer, 2000n, false); + hooks.dropTrackGroups(producer, 1000n, 1500n, staleHole(10_000)); + drop(producer, 1500n, 1600n); + const subscriber = producer + .subscribe({ delay: { max: Duration.millis(100) } }) + .withRange({ start: { included: 1000n }, end: { excluded: 1600n } }); + const ordered = subscriber.ordered(); + // Capped, so the stale run does not settle it: it asks the run again. + expect(items(ordered)).toEqual([]); + const fill = await requestedGroups(requests); + expect([fill?.start, fill?.end]).toEqual([1000n, 1500n]); + if (fill) rejectGroups(fill, 1000n, 1500n, new StreamError(StreamCode.Old), { budget: 100, everyone: false }); + // Widened past the refusal's budget, with a time start nothing held places. + subscriber.update({ delay: { max: null }, ranges: [{ start: { time: 1500n } }] }); + expect(items(ordered)).toEqual([]); + requests.close(); +}); + test("an empty answer never extends the reach of a start reported for its time", () => { for (const emptyFirst of [false, true]) { const producer = receivedTrack(); @@ -619,25 +641,6 @@ test("a folded time start never places below the range covering it", () => { expect(hooks.servingResolved(subscriber)).toEqual([{ start: 1200n, end: undefined, floored: true }]); }); -test("a range answered empty releases no capped ordered read", () => { - const producer = receivedTrack(); - put(producer, 1850n, false); - put(producer, 2000n, false); - hooks.timeStarts(producer, [[1000n, undefined, 1800n]], 100); - // GROUP 1850 says nothing lies between the time and it. - producer.skipGroups(1000n, 1850n); - const read = (cap: bigint | undefined) => - items( - producer - .subscribe({ delay: { max: Duration.millis(100) }, ranges: [{ start: { time: 1000n } }] }) - .withRange(cap === undefined ? {} : { end: { excluded: cap } }) - .ordered(), - ); - expect(read(undefined)[0]).toEqual("group 1850"); - // The cap keeps it from the groups the answer rests on. - expect(read(1900n)).toEqual([]); -}); - test("a range answered empty holds back no later group within its budget, though it places nothing", () => { const producer = receivedTrack(); put(producer, 2000n, false); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index c474c16ff8a..a002d989f34 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1024,11 +1024,11 @@ let timeAnchors: (producer: Producer) => { type TimeStart = ({ start: bigint; empty?: never } | { start?: never; empty: number }) & { through?: bigint }; // Whether a received copy's range asked from `time`, or the one upstream folded it into, had no -// group to deliver to a reader judging under `budget` with `coverage`: the live feed brings it none -// of the range's groups. The answer is a staleness verdict, so it holds only for a reader it settles -// (see {@link settles}). Assigned in the producer's static block. Mirrors the Rust -// `TrackState::answered_empty`. -let answeredEmpty: (producer: Producer, time: bigint, budget: number, coverage: Coverage) => boolean; +// group to deliver to a reader within `budget`: the live feed brings it none of the range's groups. +// Upstream judges a range by its terms on the wire, never a reader's local bounds, so a reader +// within the budget it was asked with gets the same answer from it. Assigned in the producer's +// static block. Mirrors the Rust `TrackState::answered_empty`. +let answeredEmpty: (producer: Producer, time: bigint, budget: number) => boolean; // Where a received copy's live feed landed every key it could through `time`, counting from // `covering`, where a range covering the time started, or else its first arrival at or below @@ -1141,13 +1141,9 @@ function reaches(received: bigint, drops: DropSet, end: End, refused: readonly R * above it. Mirrors the Rust `settles`. */ function settles(run: DropRun, budget: number, coverage: Coverage): boolean { - return run.stale === undefined || staleSettles(run.start, run.stale, budget, coverage); -} - -// Whether a staleness verdict at `start` judged under `stale` settles a reader; see {@link settles}. -function staleSettles(start: bigint, stale: number, budget: number, coverage: Coverage): boolean { - if (budget > stale || coverage === undefined || coverage.cap !== undefined) return false; - const above = start + 1n; + if (run.stale === undefined) return true; + if (budget > run.stale || coverage === undefined || coverage.cap !== undefined) return false; + const above = run.start + 1n; const held = coverage.runs.find((run) => run.start <= above && above < run.end); return held !== undefined && held.end >= UNBOUNDED; } @@ -1666,9 +1662,9 @@ export class Producer { const end = next.done ? UNBOUNDED : next.value.start; return end > start ? { start, end } : undefined; }; - answeredEmpty = (producer, time, budget, coverage) => { + answeredEmpty = (producer, time, budget) => { const reported = producer.#timeStarts.get(time) ?? coveringStart(producer.#timeStarts, time); - return reported?.empty !== undefined && staleSettles(time, reported.empty, budget, coverage); + return reported?.empty !== undefined && budget <= reported.empty; }; timeStartFolds = (producer, time) => { // A reported start places it. An empty answer places nothing, as for `containing`, so a @@ -5634,7 +5630,7 @@ export class Subscriber { } // The runs of `[start, end)` this reader has a verdict on: a drop that settles it, or what its - // own fills answered, a group or a refusal, or it gave up. A stale verdict that does not settle it + // own fills answered, a group or a refusal within its budget, or it gave up. A stale verdict that does not settle it // is none: the reader asks the group again, and only that answer counts. Mirrors the Rust // `Misses::unanswered`. #verdicts(start: bigint, end: bigint, drift: Drift, coverage: Coverage): Run[] { @@ -5644,9 +5640,12 @@ export class Subscriber { insertRun(runs, drop.start, drop.end); } } - for (const run of [...this.#state.refused.peek(), ...this.#givenUp]) { + // A refusal judged stale under a narrower budget is asked again once this reader widened. + for (const run of this.#state.refused.peek()) { + if (run.stale !== undefined && drift.budget > run.stale) continue; if (run.start < end && start < run.end) insertRun(runs, run.start, run.end); } + for (const run of intersect(this.#givenUp, start, end)) insertRun(runs, run.start, run.end); const timeline = this.#state.timeline; for (let i = timelineIndex(timeline, start); i < timeline.length && timeline[i].start < end; i++) { const group = timeline[i]; @@ -5676,7 +5675,7 @@ export class Subscriber { } // Its range had no group to deliver, so the feed lands none of it. Its successor is this // group's or one below: the group containing the time is judged as an owed group at it would be. - if (producer && answeredEmpty(producer, time, drift.budget, coverage)) continue; + if (producer && answeredEmpty(producer, time, drift.budget)) continue; if (this.#staleAt(time, drift, true)) continue; const folds = producer && timeStartFolds(producer, time); if (folds !== undefined) this.#wakeAt(folds.since(Instant.ZERO).asNanos()); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 12e9e1ac166..bc5d3914e37 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1471,15 +1471,16 @@ impl TrackState { } /// Whether the range asked from `time`, or the one upstream folded it into, had no group to - /// deliver to a reader under `drift`: the live feed brings it none of the range's groups. The - /// answer is a staleness verdict, so it holds only for a reader it settles (see [`settles`]). - fn answered_empty(&self, time: u64, drift: &Drift) -> bool { + /// deliver to a reader within `budget`: the live feed brings it none of the range's groups. + /// Upstream judges a range by its terms on the wire, never a reader's local bounds, so a + /// reader within the budget it was asked with gets the same answer from it. + fn answered_empty(&self, time: u64, budget: Duration) -> bool { let reported = self .time_starts .get(&time) .copied() .or_else(|| self.covering_start(time)); - matches!(reported, Some(TimeStart { answer: Answer::Empty(asked), .. }) if drift.settles(time, &Hole::stale(asked))) + matches!(reported, Some(TimeStart { answer: Answer::Empty(asked), .. }) if budget <= asked) } /// The range asked from an earlier time that reaches `time`, which upstream folded a range @@ -9904,7 +9905,7 @@ impl Cursor { // Its range had no group to deliver, so the feed lands none of it. Its // successor is this group's or one below: the group containing the time is // judged as an owed group at it would be. - if state.answered_empty(time, drift) + if state.answered_empty(time, drift.budget) || state.poll_drifted(time, &drift.edge.servable, drift.budget, None, waiter) { continue; @@ -16444,38 +16445,38 @@ mod test { assert_eq!(read(1500, None), None, "a wider reader may still get 1000"); } - /// A range answered empty is a staleness verdict, so it releases only a reader it settles: - /// one capped below the live edge, though within its budget, keeps waiting. + /// A range answered empty releases a reader within its budget whatever that reader's own + /// bounds: the range's wire end, or none past it, leaves it no group to wait for. Here the + /// publisher holds only 66, so a reader asking `Time(10)..Time(20)` and `Time(66)..` reads it. #[test] #[cfg(feature = "async")] - fn an_empty_range_releases_no_capped_read() { - let budget = Duration::from_millis(100); + fn an_empty_bounded_range_holds_back_no_later_range() { let producer = track_producer("test", Info::timed()); producer.received(); producer.anchored(); - write_group(&producer, 1850); - write_group(&producer, 2000); - producer.time_starts([(1000, None, Some(1800))], budget); - let read = |cap: u64| { - let mut ordered = producer - .subscribe( - Subscription::default() - .with_ranges([Range { - start: Anchor::Time(1000), + write_group(&producer, 66); + producer.time_starts([(10, None, Some(20)), (66, Some(66), None)], Duration::MAX); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([ + Range { + start: Anchor::Time(10), + end: Some(Anchor::Time(20)), + }, + Range { + start: Anchor::Time(66), end: None, - }]) - .with_delay(Delay::default().with_max(Some(budget))), - ) - .with_range(..cap) - .ordered(); - match ordered.next_group().now_or_never() { - Some(Ok(Some(Item::Group(group)))) => Some(group.start), - None => None, - Some(res) => panic!("not a group: {:?}", res.map(|item| item.is_some())), - } - }; - assert_eq!(read(u64::MAX), Some(1850)); - assert_eq!(read(1900), None, "the cap keeps it from the groups the answer rests on"); + }, + ]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + match ordered.next_group().now_or_never() { + Some(Ok(Some(Item::Group(group)))) => assert_eq!(group.start, 66), + None => panic!("the empty range holds back 66"), + Some(res) => panic!("not a group: {:?}", res.map(|item| item.is_some())), + } } /// A lite-07 copy places a time start that upstream folded into an earlier range by that From ce843d885ce86473b6339039ace80ef6d4b419bf Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 18:16:07 -0800 Subject: [PATCH 15/17] style: format Co-Authored-By: Claude Opus 5.5 --- js/net/src/ordered.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/net/src/ordered.test.ts b/js/net/src/ordered.test.ts index 494211fdc92..ca03add8c63 100644 --- a/js/net/src/ordered.test.ts +++ b/js/net/src/ordered.test.ts @@ -4,8 +4,8 @@ import { expect, test } from "bun:test"; import { Duration, sleep } from "@moq/clock"; import { Manual } from "@moq/clock/test"; import { StreamCode, StreamError } from "./error.ts"; -import { hooks, NO_MAX_DELAY, staleHole } from "./internal.ts"; import { rejectGroups } from "./fill.ts"; +import { hooks, NO_MAX_DELAY, staleHole } from "./internal.ts"; import { requestedGroups, textFrame } from "./mock.ts"; import { Timestamp } from "./time.ts"; import { type Ordered, Producer as TrackProducer } from "./track.ts"; From 61652b7a8adbd4750be23e328063fc6caa80052b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 04:09:18 -0800 Subject: [PATCH 16/17] test(moq-net): a fill below the live edge stays history; plan the widened reader's stale re-ask Co-Authored-By: Claude Opus 5.5 --- quest/m1/widened-reader-asks-stale-again.md | 15 +++++++++++++++ rs/moq-net/src/model/track.rs | 4 ++++ 2 files changed, 19 insertions(+) create mode 100644 quest/m1/widened-reader-asks-stale-again.md diff --git a/quest/m1/widened-reader-asks-stale-again.md b/quest/m1/widened-reader-asks-stale-again.md new file mode 100644 index 00000000000..5a346612128 --- /dev/null +++ b/quest/m1/widened-reader-asks-stale-again.md @@ -0,0 +1,15 @@ +# [M] A widened Rust ordered reader asks its own stale give-ups again + +## Goal + +When a Rust `track::Ordered` reader widens its `Delay::max`, a run its own fill gave up as stale (`Error::Old`) under the narrower budget is no verdict for it anymore, unless the reader has already returned it as a gap. The reader asks that run again, and the publisher answers under the wider budget, as js/net already does. +This is maintainer decision 1 on [moq-dev/moq#9172](https://github.com/moq-dev/moq/pull/9172#issuecomment-7026835526) applied to a reader's own refusals: a drop judged under a narrower budget never settles a wider reader. + +Today a Rust reader keeps the give-up. Reproduce on a received copy holding 900 and 2000, a stale drop of 1000..1500 under 10s, and keys 901..1000 still owed. An ordered reader of 900..1600 with a 1s budget reads 900 and asks a fill for 1000..1500, and the fill refuses it with `Old`. The reader then widens to `None`, and 901..1000 is skipped. The reader returns `Gap(1000..1500)` and never asks again. js/net, given the same steps, holds and asks again for 901..1500. + +## Plan + +- The give-up lives in three places, and each needs a stale budget to reverse it: `Misses::given_up` (`track/miss.rs`, which `give_up_stale` fills with the asked budget), the record's `reported` runs (`record.rs`, moved there when `Ordered::find_gap` polls the drop), and `Ordered::gaps`, which holds a gap found but not returned yet. +- On a budget that widens past a run's budget, withdraw it from all three: the queued gap, its `Report::Dropped`, and the `given_up` run along with its `judged` keys. The next judgement then asks for it. A gap already returned stands, since a read in key order never goes back. +- Keep #9172's time-start barrier (`Misses::unanswered`) in step: it counts a given-up run as accounted, which is right only while that run stays given up. +- Mirror check: js/net's `refused` runs carry their stale budget, and `#verdicts` skips a narrower one. Port the reproduction to `ordered.test.ts` so both stay pinned. diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 3772d6c2a1e..f0ccf6754e0 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -18980,6 +18980,10 @@ mod test { assert_eq!(drain(&mut subscriber), vec![3], "past the edge, live"); let mut live = producer.subscribe(replay().with_ranges([Range::live()])); assert_eq!(drain(&mut live), vec![3], "the live edge"); + // 3 moved the live edge, so 2 lands below it: the fetch gets it, no subscription does. + assert_eq!(fill(2).await.unwrap().start, 2); + assert!(drain(&mut subscriber).is_empty(), "below the edge, history"); + assert!(drain(&mut live).is_empty(), "below the edge, history"); } /// A failed fill answer is no verdict on a gap the feed owes: an ordered reader neither takes From 9f70e08c3cc514690315a4e58e952125d21996ef Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 16:56:35 -0800 Subject: [PATCH 17/17] quest: time-start-asks-upstream covers the relay's poll_start stall on an empty range Co-Authored-By: Claude Opus 5.5 --- quest/m1/time-start-asks-upstream.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quest/m1/time-start-asks-upstream.md b/quest/m1/time-start-asks-upstream.md index 5cd6272c73e..ea07340d7e7 100644 --- a/quest/m1/time-start-asks-upstream.md +++ b/quest/m1/time-start-asks-upstream.md @@ -17,7 +17,7 @@ Done when a viewer behind a relay reads the same groups for a time start as it w - Accepted (maintainer, [moq-dev/moq#9172](https://github.com/moq-dev/moq/pull/9172#issuecomment-7054040452)): one more wire range per distinct unprovable reader time. These ranges are bounded by the copy's readers and retired with them. They replace the folding that keeps the range list short today, and that folding is what leaves those times unanswered: a partial hit always asks upstream for the miss. - Then remove the inference no answer proves: #9172's covering-range placement (`landing_from` with a covering start, `Answer::Empty` releasing the barrier), #9174's earlier-range `Before`, and the hole placement in `TrackState::containing_received` (and its js/net twin in `track.ts`), which places a time between held groups whenever a higher group is held, with no Previous Start link. With groups 10, 15, and 20 at the publisher, a copy holding only 10 and 20 resolves `Time(18)` to 10 where the publisher answers 15. Keep only what the copy proves from held groups (#8638's Previous Start placement). - Mirror in js/net (`lite/subscriber.ts` `#declare` and its demand ranges), and run `just test interop --all`. -- Reproduce first with the fixture in `rs/moq-net/tests/integration/waiting_time_start.rs` (publisher, relay, viewer). One case: an unlimited-budget ordered reader asks `Time(10)..Time(20)` and `Time(66)..` while the publisher holds only group 66. A second: a narrow viewer joins first, then a wider one asks from a time whose group the narrow budget expired. +- Reproduce first with the fixture in `rs/moq-net/tests/integration/waiting_time_start.rs` (publisher, relay, viewer). One case: an unlimited-budget ordered reader asks `Time(10)..Time(20)` and `Time(66)..` while the publisher holds only group 66. Through a relay this reads nothing, on `main` too: the relay's wire publisher waits in `Subscriber::poll_start` on the unplaced `Time(10)`, which ignores the empty answer, so it never answers its own subscriber or forwards 66. The fix covers that path as well as `Ordered`. A second: a narrow viewer joins first, then a wider one asks from a time whose group the narrow budget expired. - [moq-dev/moq#9228](https://github.com/moq-dev/moq/pull/9228) made a time start an ordinary group fill with a `floor`, but a received copy never asks one: its readers' time ranges go upstream on the demand subscription, which is what this quest extends. - [no-relative-anchors](/quest/m2/time-addressing/no-relative-anchors.md) needs the same ask-and-wait for a Min Age live join, which it resolves as a time start. Whichever lands first builds the mechanism.