From 596d1fecb5ffe72ff0f36e2c5d176063b29bf110 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 09:24:10 -0800 Subject: [PATCH 01/18] chore(quest): claim quest/m2/time-addressing/late-relative-join.md Quest-Claim: cf9040a1fda50698 From 9a730ef2f794092852083ae1f5563b2369afba1d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 09:40:47 -0800 Subject: [PATCH 02/18] fix(moq-net): a later reader's relative start on a partial lite-07 copy reads only its groups A lite-07 copy counts a relative start back only through the groups it holds and the starts its publisher skipped between them, and otherwise reads nothing until SUBSCRIBE_STARTED reports where the range starts, the copy ends, or nothing feeds it any more. Mirrored in @moq/net. Co-Authored-By: Claude Opus 5.5 --- js/net/src/integration.test.ts | 45 ++++++- js/net/src/internal.ts | 9 ++ js/net/src/lite/subscriber.ts | 4 +- js/net/src/track.ts | 44 ++++++- js/net/src/window.test.ts | 36 +++++- js/net/src/window.ts | 56 ++++++--- rs/moq-net/src/lite/subscriber.rs | 3 +- rs/moq-net/src/model/subscription/window.rs | 108 ++++++++++++++--- rs/moq-net/src/model/track.rs | 111 +++++++++++++++--- rs/moq-net/tests/integration/range_anchors.rs | 73 ++++++++++++ 10 files changed, 433 insertions(+), 56 deletions(-) diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index c7f230bdb4..65714afaa2 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -28,7 +28,7 @@ import { Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; import { Stream } from "./stream.ts"; import { Timescale, Timestamp } from "./time.ts"; -import type { Position, Producer as TrackProducer } from "./track.ts"; +import type { Position, Range as TrackRange, Producer as TrackProducer } from "./track.ts"; import { unwanted } from "./util/demand.ts"; import { withTimeout } from "./util/timeout.ts"; import { wireOf } from "./wire.ts"; @@ -483,6 +483,49 @@ test("integration: lite-07 serves sparse and relative ranges", async () => { } }); +// A later reader's relative start on a lite-07 copy already holding part of the track counts back as +// the publisher did, not over the groups the copy happens to hold: over a copy holding groups 1 and +// 2 of 0 to 9, it reads only the groups its range names. Mirrors the Rust +// `a_later_relative_start_on_a_partial_copy_reads_only_its_groups`. +test("integration: lite-07 starts a later reader's relative range where the publisher did", async () => { + const pair = createMockTransportPair(Lite.ALPN_07_WIP); + const origin = new OriginProducer(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + + const broadcast = publish(origin, Path.from("test")); + const producer = broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null }); + for (let key = 0; key < 10; key++) hooks.appendNext(producer).finish(); + + const remote = wireOf(client).consume(Path.from("test")); + const subscribe = (ranges: TrackRange[]) => + remote.track("video").subscribe({ delay: { max: null }, order: "ascending", ranges }).ordered(); + const read = async (reader: ReturnType) => { + const got: (bigint | undefined)[] = []; + for (;;) { + const group = await reader.nextGroup(); + got.push(group?.start); + if (!group) return got; + } + }; + try { + const first = subscribe([{ start: { time: 1n }, end: { time: 2n } }]); + expect(await read(first)).toEqual([1n, 2n, undefined]); + + const second = subscribe([{ start: { relative: 2n }, end: { time: 8n } }]); + expect(await read(second)).toEqual([7n, 8n, undefined]); + first.close(); + second.close(); + } finally { + remote.close(); + broadcast.close(); + client.abort(); + server.abort(); + } +}); + // A publisher counts a relative start back from its newest group, which may hold no frame yet and // so counts neither in the largest it reports nor where the copy resolves the start. The copy reports // the publisher's start, where a reader waiting on each group in sequence starts. Mirrors the Rust diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index eb4718e351..e9f23c86d0 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -756,6 +756,12 @@ export const hooks: { * the `latest` it counted from moves the live edge. Assigned by `track.ts`. */ relativeStarts: (producer: Producer, starts: Iterable) => void; + /** + * Mark a received copy whose publisher reports where each range starts (lite-07), so a reader's + * relative start the copy can't count back over its own groups waits for {@link relativeStarts}. + * Mirrors the Rust `Producer::reported`; assigned by `track.ts`. + */ + reportsStarts: (producer: Producer) => void; /** Mint a track {@link Request}; assigned by `track.ts`. */ makeRequest: (options: TrackRequestOptions) => Request; /** @@ -1156,6 +1162,9 @@ export const hooks: { timeStarts: () => { throw new Error("track.ts not loaded"); }, + reportsStarts: () => { + throw new Error("track.ts not loaded"); + }, relativeStarts: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index dddc9e1133..5f31374134 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1682,7 +1682,9 @@ export class Subscriber { }); // A new upstream subscription: stale runs an earlier one reported were judged under other terms. hooks.supersedeStale(producer); - if (!hasRanges(this.version)) { + // Lite-07 reports where each range starts. + if (hasRanges(this.version)) hooks.reportsStarts(producer); + else { hooks.dropAnchors(producer); // A wire without ranges starts a time anchor at the first group at or after it, so its // readers do too, rather than at an earlier group the copy happens to hold. diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 37ecd48b37..cee89b873a 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1054,6 +1054,8 @@ let timeAnchors: (producer: Producer) => { atKeys: boolean; starts: ReadonlyMap; relative: ReadonlyMap; + /** Whether its publisher reports where each range starts (lite-07), which `relative` holds. */ + reported: boolean; /** Bumped whenever the others, or whether the producer is a received copy, change. */ placing: number; }; @@ -1320,6 +1322,9 @@ export class Producer { #timeStarts = new Map(); // On a lite-07 copy, where the publisher started each live range counting back, by its count. #relativeStarts = new Map(); + // Whether a copy's publisher reports where each range starts, so a relative start the copy + // can't count back over its own groups waits for that. Mirrors the Rust `Anchors::Reported`. + #reportsStarts = false; // Counts changes to how a time start is placed other than by groups arriving or drops: the wire, // `#timeStarts`, and `#startsAtKeys`. Mirrors the Rust `TrackState::placing`. #placing = 0; @@ -1555,6 +1560,7 @@ export class Producer { atKeys: producer.#startsAtKeys, starts: producer.#timeStarts, relative: producer.#relativeStarts, + reported: producer.#reportsStarts, placing: producer.#placing, }); hooks.startsAtKeys = (producer) => { @@ -1565,8 +1571,19 @@ export class Producer { producer.#timeStarts = new Map(starts); producer.#placing++; }; + hooks.reportsStarts = (producer) => { + producer.#reportsStarts = true; + }; hooks.relativeStarts = (producer, starts) => { - producer.#relativeStarts = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); + const next = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); + const last = producer.#relativeStarts; + const same = + next.size === last.size && + [...next].every(([back, at]) => last.get(back)?.start === at.start && last.get(back)?.latest === at.latest); + if (same) return; + producer.#relativeStarts = next; + // A reader whose relative start waits on the report reads again. + for (const state of [producer.#state, ...producer.#sinks]) state.startChanged.update((rev) => rev + 1); }; hooks.producerLargest = (producer) => producer.#state.largest; hooks.askProducer = (producer, ask) => producer.#ask(ask); @@ -3680,10 +3697,29 @@ export class Subscriber { }, nextStart: (from) => keys[keyIndex(keys, from)], // A lite-07 copy holds only part of the track, so it starts where its publisher did, - // counting from the same latest group: a newer one moved the live edge. - reportedStart: (latest, back) => { + // counting from the same latest group: a newer one moved the live edge. One its wire still + // feeds counts only through the groups it holds and the starts skipped between them, since + // any other gap may hold groups it never saw, and waits on the report there; once nothing + // feeds it, over the groups held. Mirrors the Rust `TrackState::counted_start`. + countedStart: (latest, back) => { const reported = receivedCopy(producer) ? anchors.relative.get(back) : undefined; - return reported !== undefined && reported.latest === latest ? reported.start : undefined; + if (reported !== undefined && reported.latest === latest) return { group: reported.start }; + if (!anchors.reported || !receivedCopy(producer) || state.closed.peek() !== undefined) return undefined; + // Each step lands on a group held, so a huge count walks the cache once. + let at = latest; + for (let step = 0n; step < back; step++) { + if (at === 0n) return "before"; + let below = at - 1n; + const run = drops.at(below); + if (run?.skip === true) { + // Skipped down to 0: no group sits below. + if (run.start === 0n) return "before"; + below = run.start - 1n; + } + if (keys[keyIndex(keys, below)] !== below) return "unknown"; + at = below; + } + return { group: at }; }, back: (latest, back) => { if (back === 0n) return latest; diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 1128221166..300d363053 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -180,7 +180,7 @@ describe("Window", () => { containing: (at) => held.containing(at), nextStart: (from) => held.nextStart(from), back: (latest, back) => held.back(latest, back), - reportedStart: (latest, back) => (latest === 9n && back === 2n ? 8n : undefined), + countedStart: (latest, back) => (latest === 9n && back === 2n ? { group: 8n } : undefined), complete: () => false, }; expect(new Window([range(relative(2n))], 9n, reported).start()).toBe(8n); @@ -188,6 +188,40 @@ describe("Window", () => { expect(covered(ended, held.held)).toEqual(keys(0n, 8n)); }); + test("a relative start the groups held can't count yet reads nothing until counted", () => { + const held = new Keys([1n, 2n, 9n]); + let counted: Containing = "unknown"; + const counting: Holdings = { + resolves: () => true, + containing: (at) => held.containing(at), + nextStart: (from) => held.nextStart(from), + back: (latest, back) => held.back(latest, back), + countedStart: () => counted, + complete: () => false, + }; + const window = new Window([range(relative(2n), time(8n)), range(time(1n), time(1n))], 9n, counting); + expect(window.pending).toBe(false); + expect(window.settled).toBe(false); + expect(covered(window, keys(0n, 10n))).toEqual([1n]); + expect([window.floor(), window.start(), window.end()]).toEqual([undefined, undefined, undefined]); + expect(window.resolved()[0]).toBeUndefined(); + + // Nothing to count from while the latest group is unknown. + counted = { group: 7n }; + expect(window.settle(undefined, counting)).toBe(false); + expect(window.settled).toBe(false); + + expect(window.settle(9n, counting)).toBe(true); + expect(window.settled).toBe(true); + expect(covered(window, keys(0n, 10n))).toEqual([1n, 7n, 8n]); + expect([window.start(), window.end()]).toEqual([1n, 9n]); + + // Counting back past every group the track holds reaches back to the oldest. + counted = "before"; + const oldest = new Window([range(relative(5n))], 9n, counting); + expect([oldest.floor(), oldest.start()]).toEqual([0n, undefined]); + }); + test("a relative start past every group asks from the oldest group held when placed", () => { const held = new Keys([33n, 66n, 99n]); const window = new Window([range(relative(10n))], 99n, held); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 670b473034..cf7c58dc97 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -305,13 +305,13 @@ export function clamped( }); } -/** Where a time falls among the groups a hop holds. Mirrors the Rust `Containing`. */ +/** Where a time, or a count back from the latest group, falls among the groups a hop holds. Mirrors the Rust `Containing`. */ export type Containing = - /** The newest group starting at or before it. */ + /** The newest group starting at or before it, or the group counted back to. */ | { group: bigint } /** Every group held starts after it. */ | "before" - /** No group is held. */ + /** The groups held can't place it yet. */ | "unknown"; /** @@ -334,12 +334,13 @@ export interface Holdings { */ back(latest: bigint, back: bigint): bigint | undefined; /** - * Where the publisher reported starting a range counting `back` groups before `latest`, which a - * relative start, but never an end, takes over {@link back}: a delivery start may sit past the - * group counted, such as above groups too stale for the budget. Absent for none. Mirrors the - * Rust `Holdings::reported_start`. + * Where a range counting `back` groups before `latest` starts, which a relative start, but never + * an end, takes over {@link back}: where the publisher reported starting it, since a delivery + * start may sit past the group counted, such as above groups too stale for the budget, or + * `"unknown"` while a received copy can count it neither that way nor over its own groups yet. + * Undefined, or absent, counts it with {@link back}. Mirrors the Rust `Holdings::counted_start`. */ - reportedStart?(latest: bigint, back: bigint): bigint | undefined; + countedStart?(latest: bigint, back: bigint): Containing | undefined; /** Whether nothing more can arrive here, so a time start nothing contains reaches back to the oldest group rather than wait. */ complete(): boolean; /** @@ -463,6 +464,12 @@ export class Window { * live edge, so they read nothing yet: a group held is no reason to start at it. */ #reaching: number[] = []; + /** + * The ranges, by index, whose relative start the groups held can't count back to yet (see + * {@link Holdings.countedStart}), so they read nothing yet: a group held may sit below where the + * publisher counted to. + */ + #counting: number[] = []; /** Nothing is left to resolve as groups arrive. */ #settled = false; /** @@ -517,6 +524,15 @@ export class Window { for (const index of reaching) this.#placeRange(index, latest, holdings); for (let position = placed; position < this.#spans.length; position++) moved.push(position); } + // Counted from the latest group now, as the publisher counts from its own latest when the + // range reaches it. + if (this.#counting.length > 0 && latest !== undefined) { + const placed = this.#spans.length; + const counting = this.#counting; + this.#counting = []; + for (const index of counting) this.#placeRange(index, latest, holdings); + for (let position = placed; position < this.#spans.length; position++) moved.push(position); + } const shrank = this.#waiting.size > 0 && this.#startWaiting(holdings, moved); if (shrank) this.#rebuild(); else if (moved.length === 0) return false; @@ -606,7 +622,7 @@ export class Window { * anchor waits. */ start(): bigint | undefined { - if (this.#pending || this.#reaching.length > 0 || this.#unnamed > 0) return undefined; + if (this.#pending || this.#reaching.length > 0 || this.#counting.length > 0 || this.#unnamed > 0) return undefined; return this.#named[0]?.start; } @@ -727,9 +743,10 @@ export class Window { return this.#named[0]?.start; } - // Whether a time start or a join's reach back waits on the groups held to place it. + // Whether a time start, a join's reach back, or a relative start waits on the groups held to + // place it. #unplaced(): boolean { - return this.#waiting.size > 0 || this.#reaching.length > 0; + return this.#waiting.size > 0 || this.#reaching.length > 0 || this.#counting.length > 0; } // The first run ending past `key`. @@ -743,6 +760,7 @@ export class Window { this.#waiting = new ByTime(); this.#looking = new ByTime(); this.#reaching = []; + this.#counting = []; this.#named = []; this.#unnamed = 0; for (let index = 0; index < this.ranges.length; index++) { @@ -779,6 +797,10 @@ export class Window { this.#pending = true; return; } + if (start === "counting") { + this.#counting.push(index); + return; + } if (start === "waiting") { // Reads only the groups from its time until the groups held place it; see #startWaiting. this.#waiting.add(time(anchor) ?? 0n, this.#spans.length); @@ -1028,20 +1050,24 @@ function joins(range: Range, min: Duration | undefined, holdings: Holdings): Dur /** * The first group a range starts at, and whether it names that group rather than reaching back to - * the oldest one. `"pending"` while it waits on a latest group, reading everything meanwhile, and - * `"waiting"` for a time start the groups held can't place yet. Mirrors the Rust `start`. + * the oldest one. `"pending"` while it waits on a latest group, reading everything meanwhile, + * `"waiting"` for a time start the groups held can't place yet, and `"counting"` for a relative + * start they can't count back to yet, reading nothing meanwhile. Mirrors the Rust `start`. */ function startOf( anchor: Anchor | undefined, latest: bigint | undefined, holdings: Holdings, -): { group: bigint; floored: boolean } | "waiting" | "pending" { +): { group: bigint; floored: boolean } | "waiting" | "pending" | "counting" { // Nothing sits below group 0, so the oldest start needs no latest group. if (anchor === undefined) return { group: 0n, floored: false }; if (anchor.relative !== undefined) { if (latest === undefined) return "pending"; - const group = holdings.reportedStart?.(latest, anchor.relative) ?? holdings.back(latest, anchor.relative); + const counted = holdings.countedStart?.(latest, anchor.relative); + if (counted === "unknown") return "counting"; // Fewer groups are held than it counts back, so reach back to the oldest. + if (counted === "before") return { group: 0n, floored: false }; + const group = counted?.group ?? holdings.back(latest, anchor.relative); return group === undefined ? { group: 0n, floored: false } : { group, floored: true }; } // No group starts before 0, so time 0 names group 0 whatever is held. diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 4abb3ca4f1..32dff7f92f 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -12324,8 +12324,9 @@ impl ServeLoop { // Marked received before any reader sees it, so none resolves an anchor this wire dropped. let serving = request.stage(info); serving.received(); + // Every version that resolves them also reports where each range starts. if serve.subscriber.version.resolves_anchors() { - serving.anchored(); + serving.reported(); } // A wire without ranges starts a time anchor at the first group at or after it, so // its readers do too, rather than at an earlier group the copy happens to hold. diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 6be5dfbc50..7aa84daf65 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -82,10 +82,12 @@ pub(in crate::model) trait Holdings { /// held: `None` once fewer are held than that. fn back(&self, latest: u64, back: u64) -> Option; - /// Where the publisher reported starting a range counting `back` groups before `latest`, - /// which a relative start, but never an end, takes over [`Self::back`]: a delivery start - /// may sit past the group counted, such as above groups too stale for the budget. - fn reported_start(&self, latest: u64, back: u64) -> Option { + /// Where a range counting `back` groups before `latest` starts, which a relative start, + /// but never an end, takes over [`Self::back`]: where the publisher reported starting it, + /// since a delivery start may sit past the group counted, such as above groups too stale + /// for the budget, or [`Containing::Unknown`] while a received copy can count it neither + /// that way nor over its own groups yet. `None` counts it with [`Self::back`]. + fn counted_start(&self, latest: u64, back: u64) -> Option { let _ = (latest, back); None } @@ -111,14 +113,14 @@ pub(in crate::model) trait Holdings { } } -/// Where a time falls among the groups a hop holds. +/// Where a time, or a count back from the latest group, falls among the groups a hop holds. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(in crate::model) enum Containing { - /// The newest group starting at or before it. + /// The newest group starting at or before it, or the group counted back to. Group(u64), /// Every group held starts after it. Before, - /// No group is held. + /// The groups held can't place it yet. Unknown, } @@ -190,6 +192,10 @@ pub(in crate::model) struct Window { /// The ranges of a live join whose reach back waits on the groups held to measure the /// live edge, which read nothing meanwhile: a group held is no reason to start at it. reaching: BTreeSet, + /// The ranges whose relative anchor the groups held can't count back to yet (see + /// [`Holdings::back`]), which read nothing meanwhile: a group held may sit below where + /// the publisher counted to. + counting: BTreeSet, /// The groups the spans cover, shared with what reads them. groups: Cover, /// The part of `groups` a miss may ask for: see [`Self::missable`]. @@ -261,6 +267,7 @@ impl Window { waiting: BTreeSet::new(), waited: None, reaching: BTreeSet::new(), + counting: BTreeSet::new(), groups: Cover::default(), missable: Cover::default(), covered: None, @@ -305,6 +312,7 @@ impl Window { } self.pending.remove(&key); self.reaching.remove(&key); + self.counting.remove(&key); if let Some(span) = self.spans.remove(&key) { self.uncount(key, &span); } @@ -337,6 +345,11 @@ impl Window { !self.waiting.is_empty() } + /// Whether a relative anchor still waits on the groups held to count back to it. + pub fn is_counting(&self) -> bool { + !self.counting.is_empty() + } + /// 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 { @@ -358,6 +371,15 @@ impl Window { } moved = self.spans.len() > placed; } + // Counted from the latest group now, as an edit placing them now would: the publisher + // counts from its own latest when the range reaches it. + if !self.counting.is_empty() && latest.is_some() { + let placed = self.spans.len(); + for key in std::mem::take(&mut self.counting) { + self.place_range(key, latest, holdings); + } + moved |= self.spans.len() > placed; + } moved |= !self.waiting.is_empty() && self.start_waiting(holdings); if moved { self.covered = None; @@ -428,7 +450,7 @@ impl Window { /// The lowest group the ranges name, `None` while one reaches back to the oldest group /// or an anchor waits. pub fn start(&self) -> Option { - match self.is_pending() || !self.reaching.is_empty() || self.unnamed > 0 { + match self.is_pending() || !self.reaching.is_empty() || !self.counting.is_empty() || self.unnamed > 0 { true => None, false => self.lowest_named(), } @@ -608,6 +630,7 @@ impl Window { self.looking.clear(); self.waiting.clear(); self.reaching.clear(); + self.counting.clear(); self.groups = Cover::default(); self.missable = Cover::default(); self.named.clear(); @@ -655,6 +678,10 @@ impl Window { self.pending.insert(key); return; } + Start::Counting => { + self.counting.insert(key); + return; + } // Reads only the groups from its time until the groups held place it; see // [`Self::start_waiting`]. Start::Waiting => { @@ -674,9 +701,10 @@ impl Window { self.count(key, Span::new(anchor, start, end, floored, looking), holdings); } - /// Whether a time start or a join's reach back waits on the groups held to place it. + /// Whether a time start, a join's reach back, or a relative anchor waits on the groups + /// held to place it. fn unplaced(&self) -> bool { - !self.waiting.is_empty() || !self.reaching.is_empty() + !self.waiting.is_empty() || !self.reaching.is_empty() || !self.counting.is_empty() } /// Hold `span` under `key`, counting the groups it reads and where it starts. @@ -835,6 +863,8 @@ enum Start { Waiting, /// Waiting on a latest group, reading everything meanwhile. Pending, + /// A relative start the groups held can't count back to yet, reading nothing meanwhile. + Counting, } fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Start { @@ -843,13 +873,15 @@ fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Start Anchor::Oldest => Start::At(0, false), Anchor::Relative(back) => match latest { None => Start::Pending, - Some(latest) => match holdings - .reported_start(latest, back) - .or_else(|| holdings.back(latest, back)) - { - Some(group) => Start::At(group, true), + Some(latest) => match holdings.counted_start(latest, back).unwrap_or_else(|| { + holdings + .back(latest, back) + .map_or(Containing::Before, Containing::Group) + }) { + Containing::Group(group) => Start::At(group, true), // Fewer groups are held than it counts back, so reach back to the oldest. - None => Start::At(0, false), + Containing::Before => Start::At(0, false), + Containing::Unknown => Start::Counting, }, }, // No group starts before 0, so time 0 names group 0 whatever is held. @@ -923,6 +955,8 @@ mod tests { /// Every key below this was dropped, so a time below it is placed before every /// group even while none is held. dropped: u64, + /// What a relative start counts back to, when not over the groups held. + counted: Option, } impl Keys { @@ -969,6 +1003,10 @@ mod tests { self.held.range(..latest).rev().nth(back as usize).copied() } + fn counted_start(&self, _: u64, _: u64) -> Option { + self.counted + } + fn complete(&self) -> bool { self.complete } @@ -1205,6 +1243,44 @@ mod tests { } } + /// A relative start the groups held can't count yet reads nothing, rather than every group + /// as one waiting on a latest group does, and starts once they can, counted from the + /// latest group then. An end still counts over the groups held. + #[test] + fn a_relative_start_reads_nothing_until_counted() { + let mut keys = Keys::new([1, 2, 9]); + keys.counted = Some(Containing::Unknown); + let ranges = [range(Anchor::Relative(2), time(8)), range(time(1), time(1))]; + let mut window = Window::new(&ranges, None, Some(9), &keys).unwrap(); + assert!(window.is_counting() && !window.is_pending() && !window.is_settled()); + assert_eq!(covered(&window, 0..10), vec![1]); + assert_eq!((window.floor(), window.start(), window.named(), window.end()), (None, None, None, None)); + assert_eq!(window.resolved_key(0), None); + + // Nothing to count from while the latest group is unknown. + keys.counted = Some(Containing::Group(7)); + assert!(!window.settle(None, &keys)); + assert!(window.is_counting()); + + assert!(window.settle(Some(9), &keys)); + assert!(window.is_settled() && !window.is_counting()); + assert_eq!(covered(&window, 0..10), vec![1, 7, 8]); + assert_eq!(window.start(), Some(1)); + assert_eq!(window.end(), Some(9)); + + // Retiring a range still counting takes it out of the count. + keys.counted = Some(Containing::Unknown); + window.edit([], &[(2, range(Anchor::Relative(3), None))], Some(9), &keys).unwrap(); + assert!(window.is_counting()); + window.edit([2], &[], Some(9), &keys).unwrap(); + assert!(window.is_settled()); + + // Counting back past every group the track holds reaches back to the oldest. + keys.counted = Some(Containing::Before); + let window = Window::new(&[range(Anchor::Relative(5), None)], None, Some(9), &keys).unwrap(); + assert_eq!((window.floor(), window.start()), (Some(0), None)); + } + /// No hop can place the oldest group as an end, so it is refused. #[test] fn an_oldest_end_is_refused() { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 7e240bf9f6..3ebb508a3c 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -599,12 +599,20 @@ enum Anchors { /// A received copy whose wire tells it what relative anchors count back from, and carries /// time anchors as group starts, so they resolve here as at the publisher. Wire, + /// A [`Self::Wire`] copy whose publisher also reports where each range starts (lite-07), + /// so a relative anchor the copy can't count back over its own groups waits for that. + Reported, } impl Anchors { fn received(self) -> bool { self != Self::Local } + + /// Whether the wire carries relative and time anchors, resolving them as the publisher. + fn on_wire(self) -> bool { + matches!(self, Self::Wire | Self::Reported) + } } /// Where a received copy's publisher started a range counting back, and the latest group it @@ -1118,7 +1126,7 @@ impl Holdings for TrackState { if self.starts_at_keys { return Containing::Group(time); } - if self.anchors == Anchors::Wire { + if self.anchors.on_wire() { return self.containing_received(time); } match self.lookup.range(..=time).next_back() { @@ -1148,7 +1156,7 @@ impl Holdings for TrackState { match (self.lookup.first_key_value(), self.lookup.last_key_value()) { // Before any group, only a time whose keys down to 0 were all dropped is placed. (None, _) | (_, None) => [uncovered(0), none], - (Some(_), Some(_)) if self.anchors != Anchors::Wire => [none.clone(), none], + (Some(_), Some(_)) if !self.anchors.on_wire() => [none.clone(), none], // A copy places a time below its groups only once the keys down to 0 were // dropped, and one above them only once the keys down to the newest were. (Some((&first, _)), Some((&last, _))) => { @@ -1186,12 +1194,17 @@ impl Holdings for TrackState { } /// A received copy starts where its publisher did, counting from the same latest group: a - /// newer one moved the live edge, which the copy then counts from itself. - fn reported_start(&self, latest: u64, back: u64) -> Option { - self.relative_starts + /// newer one moved the live edge, which the copy then counts from itself. A lite-07 copy + /// that may still learn where, from a report or the groups between, counts only where it + /// holds them (see [`Self::counted_received`]); once it can't, over the groups held. + fn counted_start(&self, latest: u64, back: u64) -> Option { + let reported = self + .relative_starts .get(&back) .filter(|reported| reported.latest == latest) - .map(|reported| reported.start) + .map(|reported| Containing::Group(reported.start)); + let learns = self.anchors == Anchors::Reported && !self.outcome.is_final() && !self.unfed; + reported.or_else(|| learns.then(|| self.counted_received(latest, back))) } fn complete(&self) -> bool { @@ -1295,6 +1308,33 @@ impl TrackState { } } + /// The group `back` groups before `latest` on a received copy, counted only through the + /// groups it holds and the starts its publisher declared skipped between them: any other + /// gap may hold groups the copy never saw, such as one below what a first reader asked + /// for, so the count is [`Containing::Unknown`] there. Each step lands on a group held, so + /// a huge count walks the cache once. + fn counted_received(&self, latest: u64, back: u64) -> Containing { + let mut at = latest; + for _ in 0..back { + let Some(below) = at.checked_sub(1) else { + return Containing::Before; + }; + let below = match self.drops.get_key_value(&below) { + Some((run, Hole::Skip)) => match run.start.checked_sub(1) { + Some(below) => below, + // Skipped down to 0: no group sits below. + None => return Containing::Before, + }, + _ => below, + }; + if !self.lookup.contains_key(&below) { + return Containing::Unknown; + } + at = below; + } + Containing::Group(at) + } + /// 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) @@ -4764,8 +4804,19 @@ impl Producer { /// reports the live edge and media time and asks for every group the budget allows. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn anchored(&self) { + self.set_anchors(Anchors::Wire); + } + + /// [`Self::anchored`], for a wire whose publisher also reports where each range starts: + /// a relative anchor the copy can't count back over its own groups waits for that report. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn reported(&self) { + self.set_anchors(Anchors::Reported); + } + + fn set_anchors(&self, anchors: Anchors) { if let Ok(mut state) = self.modify() { - state.anchors = Anchors::Wire; + state.anchors = anchors; state.placing += 1; } } @@ -10775,12 +10826,13 @@ impl Subscriber { }; let start = ready!(Consumer::poll_state_start(&cursor.state, waiter)); cursor.sync(); - if cursor.filter.window.is_waiting() { + let unplaced = |window: &Window| window.is_waiting() || window.is_counting(); + if unplaced(&cursor.filter.window) { // 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(); cursor.sync(); - if !closed && cursor.filter.window.is_waiting() { + if !closed && unplaced(&cursor.filter.window) { return Poll::Pending; } } @@ -15641,9 +15693,10 @@ mod test { assert_eq!(drain(&mut past), vec![1000, 3000]); } - /// 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. + /// A lite-07 copy holds only part of the track, so a relative anchor counts back as its + /// publisher reported, or through the groups it holds and the starts skipped between them, + /// and otherwise reads nothing until one of those can count it. Once nothing more can + /// arrive, it counts over the groups held. #[test] #[cfg(feature = "async")] fn a_received_copy_counts_a_relative_start_back_as_its_publisher_did() { @@ -15656,9 +15709,14 @@ mod test { .with_delay(Delay::default().with_max(None)) }; let placed = |subscriber: &mut Subscriber| subscriber.resolved_range(0).map(|resolved| resolved.start); + let read = |subscriber: &mut Subscriber| { + let mut read = drain(subscriber); + read.sort_unstable(); + read + }; let producer = track_producer("test", Info::timed()); producer.received(); - producer.anchored(); + producer.reported(); for key in [1, 2, 9] { write_group(&producer, key); } @@ -15668,9 +15726,10 @@ mod test { let mut reported = producer.subscribe(back(2)); assert_eq!(placed(&mut reported), Some(7), "the publisher's start, not group 1"); let mut unreported = producer.subscribe(back(1)); - assert_eq!(placed(&mut unreported), Some(2), "counted over the groups held"); - // A reported start is where delivery began, which may sit past the group counted, so a - // relative end counts over the groups held. + assert_eq!(placed(&mut unreported), None, "group 8 may sit below 9"); + assert_eq!(read(&mut unreported), Vec::::new(), "reads nothing meanwhile"); + // A reported start is where delivery began, which may sit past the group counted, and + // nothing reports an end, so a relative end counts over the groups held. let mut ended = producer.subscribe( Subscription::default() .with_ranges([Range { @@ -15681,15 +15740,33 @@ mod test { ); assert_eq!(ended.resolved_range(0).and_then(|resolved| resolved.end), Some(2)); + write_group(&producer, 8); + assert_eq!(placed(&mut unreported), Some(8), "counted through the groups held"); + assert_eq!(read(&mut unreported), vec![8, 9]); + write_group(&producer, 7); + + // Starts the publisher skipped hold no group to count. + producer.skip_groups(3..7); + let mut skipped = producer.subscribe(back(3)); + assert_eq!(placed(&mut skipped), Some(2)); + let mut gap = producer.subscribe(back(6)); + assert_eq!(placed(&mut gap), None, "group 0 may sit below 1"); + // A newer group moved the live edge, so a later reader counts back from it. write_group(&producer, 10); let mut later = producer.subscribe(back(2)); - assert_eq!(placed(&mut later), Some(2)); + assert_eq!(placed(&mut later), Some(8)); assert_eq!( placed(&mut reported), Some(7), "an earlier reader stays where it started" ); + + // Nothing more can arrive, so it counts over the groups held, fewer than it asks: it + // reaches back to the oldest. + drop(producer.finish().unwrap()); + assert_eq!(placed(&mut gap), Some(0)); + assert_eq!(read(&mut gap), vec![1, 2, 7, 8, 9, 10]); } #[test] diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index fda4f0e699..764820a1ab 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -301,3 +301,76 @@ async fn a_sparse_request_counts_its_relative_start_back_as_the_publisher_did() assert_eq!(end.map(|group| group.start), None, "the ranges are covered"); let _track = track.finish().unwrap(); } + +/// A later reader's relative start on a lite-07 copy already holding part of the track counts +/// back as the publisher did, not over the groups the copy happens to hold: over a copy +/// holding groups 1 and 2 of 0 to 9, it reads only the groups its range names. Mirrors the +/// `@moq/net` `integration: lite-07 starts a later reader's relative range where the +/// publisher did`. +#[kio::test] +async fn a_later_relative_start_on_a_partial_copy_reads_only_its_groups() { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track( + "data", + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER), + ) + .unwrap(); + broadcast.announce().unwrap(); + for key in 0..10 { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group.write_frame(millis(key), &b"x"[..]).unwrap(); + group.finish().unwrap(); + } + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + let subscribe = |ranges: Vec| { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges(ranges); + remote.track("data").unwrap().subscribe(subscription) + }; + let read = async |reader: &mut moq_net::track::Subscriber, count: usize| { + let mut read = Vec::new(); + for _ in 0..count { + let group = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap(); + read.push(group.map(|group| group.start)); + } + read + }; + + let mut first = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Time(1), Some(Anchor::Time(2)))])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut first, 3).await, [Some(1), Some(2), None]); + + let mut second = kio::clock::timeout( + TIMEOUT, + subscribe(vec![range(Anchor::Relative(2), Some(Anchor::Time(8)))]), + ) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!( + read(&mut second, 3).await, + [Some(7), Some(8), None], + "only the groups the publisher counts back to" + ); + drop(first); + let _track = track.finish().unwrap(); +} From 8a27b664862ca1040b457f882008cd0b3e8a7d5a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 09:47:28 -0800 Subject: [PATCH 03/18] chore(quest): finish late-relative-join, follow up with late-relative-end Also formats the new tests. Co-Authored-By: Claude Opus 5.5 --- js/net/src/integration.test.ts | 7 +++- js/net/src/track.ts | 4 +- js/net/src/window.ts | 3 +- quest/m2/time-addressing/late-relative-end.md | 41 +++++++++++++++++++ .../m2/time-addressing/late-relative-join.md | 34 --------------- rs/moq-net/src/model/subscription/window.rs | 9 +++- 6 files changed, 58 insertions(+), 40 deletions(-) create mode 100644 quest/m2/time-addressing/late-relative-end.md delete mode 100644 quest/m2/time-addressing/late-relative-join.md diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index 65714afaa2..5bba0aa8ab 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -28,7 +28,7 @@ import { Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; import { Stream } from "./stream.ts"; import { Timescale, Timestamp } from "./time.ts"; -import type { Position, Range as TrackRange, Producer as TrackProducer } from "./track.ts"; +import type { Position, Producer as TrackProducer, Range as TrackRange } from "./track.ts"; import { unwanted } from "./util/demand.ts"; import { withTimeout } from "./util/timeout.ts"; import { wireOf } from "./wire.ts"; @@ -501,7 +501,10 @@ test("integration: lite-07 starts a later reader's relative range where the publ const remote = wireOf(client).consume(Path.from("test")); const subscribe = (ranges: TrackRange[]) => - remote.track("video").subscribe({ delay: { max: null }, order: "ascending", ranges }).ordered(); + remote + .track("video") + .subscribe({ delay: { max: null }, order: "ascending", ranges }) + .ordered(); const read = async (reader: ReturnType) => { const got: (bigint | undefined)[] = []; for (;;) { diff --git a/js/net/src/track.ts b/js/net/src/track.ts index cee89b873a..385798c585 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1579,7 +1579,9 @@ export class Producer { const last = producer.#relativeStarts; const same = next.size === last.size && - [...next].every(([back, at]) => last.get(back)?.start === at.start && last.get(back)?.latest === at.latest); + [...next].every( + ([back, at]) => last.get(back)?.start === at.start && last.get(back)?.latest === at.latest, + ); if (same) return; producer.#relativeStarts = next; // A reader whose relative start waits on the report reads again. diff --git a/js/net/src/window.ts b/js/net/src/window.ts index cf7c58dc97..c7288e35d3 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -622,7 +622,8 @@ export class Window { * anchor waits. */ start(): bigint | undefined { - if (this.#pending || this.#reaching.length > 0 || this.#counting.length > 0 || this.#unnamed > 0) return undefined; + if (this.#pending || this.#reaching.length > 0 || this.#counting.length > 0 || this.#unnamed > 0) + return undefined; return this.#named[0]?.start; } diff --git a/quest/m2/time-addressing/late-relative-end.md b/quest/m2/time-addressing/late-relative-end.md new file mode 100644 index 0000000000..5fcb9e6459 --- /dev/null +++ b/quest/m2/time-addressing/late-relative-end.md @@ -0,0 +1,41 @@ +# [M] A relative end on a partial lite-07 copy ends where its publisher's does + +## Goal + +A reader whose range ends at a relative anchor, joining a lite-07 received +copy that already holds part of the track, reads through the group its +publisher counts back to and no further, in Rust and `@moq/net`. Today the +copy counts the end back over the groups it happens to hold: over a copy +holding groups 1 and 2 of 0..9, a later reader of `[{time:0..relative:1}]` +counts one back from 9 to group 2, so it reads 1, 2, 0 and ends, where it +should read 0 through 8. + +## Plan + +Found by [moq-dev/moq#9198](https://github.com/moq-dev/moq/pull/9198), which +fixed the same bug for a relative start. A start can wait on SUBSCRIBE_STARTED, +or count through the groups held and the starts skipped between them +(`TrackState::counted_received` in `rs/moq-net/src/model/track.rs`). An end has +neither: the publisher reports where each range starts, never where it ends. +The count can't finish through the groups held either, since the group just +above the end, whose `Previous Start` would close the gap, lies outside the +range and never arrives. Making an end wait on that count would park the +reader forever, so #9198 left ends counting over the cache (`end` in +`model/subscription/window.rs`). + +- Reproduce first, as `range_anchors.rs` and `js/net/src/integration.test.ts` + tests beside `a_later_relative_start_on_a_partial_copy_reads_only_its_groups`. +- Recommendation: carry each range's resolved end beside its start in + SUBSCRIBE_OK and SUBSCRIBE_STARTED. Lite-07 is still `-wip`, so the wire may + change freely. The copy then resolves a relative end from that report, as + `relative_starts` does for starts, and waits for it the way a start does. +- Alternative: the copy treats a bounded range as ended once the subscription + accounts for every group it delivered. That needs no wire change, but the + copy can't tell a range that ended from one still owed a late group. + +## Waiting + +- Maintainer: whether lite-07 SUBSCRIBE_OK and SUBSCRIBE_STARTED gain a + resolved Range End (recommended, since the draft is unpublished), or the + copy infers the end from accounting with no wire change. An agent can't + decide a wire change on its own. diff --git a/quest/m2/time-addressing/late-relative-join.md b/quest/m2/time-addressing/late-relative-join.md deleted file mode 100644 index 629536f579..0000000000 --- a/quest/m2/time-addressing/late-relative-join.md +++ /dev/null @@ -1,34 +0,0 @@ -# [M] A later reader's relative start on a partial lite-07 copy reads only its groups - -## Goal - -A reader that joins a lite-07 received copy already holding part of the -track, with a relative start, reads the groups its publisher counts back to -and nothing below them, in Rust and `@moq/net`. Today it resolves the start -against the groups the copy holds before SUBSCRIBE_STARTED can report the -publisher's: over a copy holding groups 1 and 2 of 0..9, a second reader of -`[{relative:2..time:8}]` reads 1, 2, 7, 8, then 3, 4, 5 and on, where it -should read 7 and 8 and end. - -## Plan - -Found by [moq-dev/moq#9132](https://github.com/moq-dev/moq/pull/9132), which -reports each live range's relative start on the copy -(`TrackState::relative_starts` in `rs/moq-net/src/model/track.rs`) but only -helps a reader whose start resolves after the report lands: the first reader, -whose relative start waits on SUBSCRIBE_START. A later reader's window -resolves at once (`Window::new` in `model/subscription/window.rs`), and -`Holdings::back` counts the copy's cache. - -- Reproduce first: the second-reader case above, as a `range_anchors.rs` - integration test, and the same in `js/net/src/integration.test.ts`. -- Decided 2026-11-19 (coordinator, maintainer away, merge granted), taking - the recommendation: on a received copy, count back over the cache only where - the copy holds every group from the counted one to its latest (received - contiguously since it went live), and otherwise leave the relative start - waiting, as a time start waits (`Start::Waiting`), until the range's - SUBSCRIBE_STARTED reports it. Rejected: always waiting on the report, - since an update that coalesces the reader's range into another one - (`fit` in `lite/subscriber/fit.rs`) gets no SUBSCRIBE_STARTED of its own and would hang. -- The waiting reader must still settle when no report can come: the copy - ends, or its feed goes idle. diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 7aa84daf65..bec8b1b27b 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -1254,7 +1254,10 @@ mod tests { let mut window = Window::new(&ranges, None, Some(9), &keys).unwrap(); assert!(window.is_counting() && !window.is_pending() && !window.is_settled()); assert_eq!(covered(&window, 0..10), vec![1]); - assert_eq!((window.floor(), window.start(), window.named(), window.end()), (None, None, None, None)); + assert_eq!( + (window.floor(), window.start(), window.named(), window.end()), + (None, None, None, None) + ); assert_eq!(window.resolved_key(0), None); // Nothing to count from while the latest group is unknown. @@ -1270,7 +1273,9 @@ mod tests { // Retiring a range still counting takes it out of the count. keys.counted = Some(Containing::Unknown); - window.edit([], &[(2, range(Anchor::Relative(3), None))], Some(9), &keys).unwrap(); + window + .edit([], &[(2, range(Anchor::Relative(3), None))], Some(9), &keys) + .unwrap(); assert!(window.is_counting()); window.edit([2], &[], Some(9), &keys).unwrap(); assert!(window.is_settled()); From 20c8b22b51b6f46d5d73bf8f7b6c7c9f8935d653 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 10:05:06 -0800 Subject: [PATCH 04/18] fix(moq-net): a counted relative start settles on an empty report or a gap the cache made A publisher's "no group to deliver" answer for a range counting back now reaches the copy, as a start past that range's end, which settles a reader whose range ends no later. A count reaching a gap at or below the highest key the cache let go of starts at the lowest group counted, since no report or arrival fills that gap. Co-Authored-By: Claude Opus 5.5 --- js/net/src/integration.test.ts | 5 ++ js/net/src/internal.ts | 9 +- js/net/src/lite/subscriber.ts | 20 ++++- js/net/src/track.ts | 37 ++++++-- js/net/src/window.ts | 11 +-- rs/moq-net/src/lite/subscriber.rs | 24 +++-- rs/moq-net/src/model/subscription/window.rs | 18 ++-- rs/moq-net/src/model/track.rs | 89 +++++++++++++++++-- rs/moq-net/tests/integration/range_anchors.rs | 11 +++ 9 files changed, 184 insertions(+), 40 deletions(-) diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index 5bba0aa8ab..b8c2774d23 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -517,6 +517,11 @@ test("integration: lite-07 starts a later reader's relative range where the publ const first = subscribe([{ start: { time: 1n }, end: { time: 2n } }]); expect(await read(first)).toEqual([1n, 2n, undefined]); + // The publisher counts back past the end, so it has no group there to deliver. + const empty = subscribe([{ start: { relative: 2n }, end: { time: 6n } }]); + expect(await read(empty)).toEqual([undefined]); + empty.close(); + const second = subscribe([{ start: { relative: 2n }, end: { time: 8n } }]); expect(await read(second)).toEqual([7n, 8n, undefined]); first.close(); diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index e9f23c86d0..2719a88f27 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -588,6 +588,13 @@ export interface StagedTrack { * widened budget reaches lower groups through the feed's re-offer. Anywhere else, a reader fills * below the start only the groups its ranges name. */ +/** + * Where a publisher reported starting a range counting back: at a group, or at or past an exclusive + * end where the range held no group, which says nothing of a range ending later. Mirrors the Rust + * `track::CountedStart`. + */ +export type CountedStart = { at: bigint; past?: never } | { past: bigint; at?: never }; + export interface FillStarts { /** The wire's start is resolved from the subscriber's budget: lite SUBSCRIBE_START from lite-06. */ resolvesStart?: boolean; @@ -755,7 +762,7 @@ export const hooks: { * track, so a reader's relative start counts back as the publisher did, until a group past * the `latest` it counted from moves the live edge. Assigned by `track.ts`. */ - relativeStarts: (producer: Producer, starts: Iterable) => void; + relativeStarts: (producer: Producer, starts: Iterable) => void; /** * Mark a received copy whose publisher reports where each range starts (lite-07), so a reader's * relative start the copy can't count back over its own groups waits for {@link relativeStarts}. diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 5f31374134..99eb6adc6e 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -28,6 +28,7 @@ import { Cost, type Hop, MAX_HOPS, routesEqual, UNKNOWN_HOP, type WireRoute } fr import * as Instance from "../instance.ts"; import { type Arriving, + type CountedStart, hiddenBelow, hooks, maxDelayOf, @@ -2879,15 +2880,26 @@ export class Subscriber { const times: [bigint, bigint][] = []; // 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][] = []; + const relative: [bigint, CountedStart, bigint | undefined][] = []; for (const id of entry.live.ids()) { 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 (time !== undefined) times.push([time, at]); - else if (back !== undefined) relative.push([back, at, entry.edges.get(id)]); + if (time !== undefined && at !== undefined) times.push([time, at]); + else if (back !== undefined) { + const latest = entry.edges.get(id); + // With no group to deliver, the start lies at or past the range's exclusive end: an + // absolute end names it, a time end includes its group, and an open one the latest. + const end = range?.end; + const past = + end?.absolute ?? + (end?.time !== undefined ? end.time + 1n : latest === undefined ? undefined : latest + 1n); + const start = at !== undefined ? { at } : past !== undefined ? { past } : undefined; + if (start) relative.push([back, start, latest]); + } } hooks.timeStarts(entry.track, times); hooks.relativeStarts(entry.track, relative); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 385798c585..50e56a6943 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -24,6 +24,7 @@ import { type Frame, type Consumer as GroupConsumer, Producer as GroupProducer } import type { IetfVersion } from "./ietf/version.ts"; import { type Ask, + type CountedStart, cutRun, type DropRun, dropAt, @@ -1053,7 +1054,9 @@ let keysOf: (producer: Producer) => readonly bigint[]; let timeAnchors: (producer: Producer) => { atKeys: boolean; starts: ReadonlyMap; - relative: ReadonlyMap; + relative: ReadonlyMap; + /** The highest key of any group that left the cache. */ + uncached: bigint | undefined; /** Whether its publisher reports where each range starts (lite-07), which `relative` holds. */ reported: boolean; /** Bumped whenever the others, or whether the producer is a received copy, change. */ @@ -1321,7 +1324,11 @@ export class Producer { // it asked from. Mirrors the Rust `TrackState::time_starts`. #timeStarts = new Map(); // On a lite-07 copy, where the publisher started each live range counting back, by its count. - #relativeStarts = new Map(); + #relativeStarts = new Map(); + // The highest key of any group that left the cache: a received copy counting a relative start + // back can't tell a gap at or below it from one it never held. Mirrors the Rust + // `TrackState::uncached`. + #uncached?: bigint; // Whether a copy's publisher reports where each range starts, so a relative start the copy // can't count back over its own groups waits for that. Mirrors the Rust `Anchors::Reported`. #reportsStarts = false; @@ -1561,6 +1568,7 @@ export class Producer { starts: producer.#timeStarts, relative: producer.#relativeStarts, reported: producer.#reportsStarts, + uncached: producer.#uncached, placing: producer.#placing, }); hooks.startsAtKeys = (producer) => { @@ -1579,9 +1587,12 @@ export class Producer { const last = producer.#relativeStarts; const same = next.size === last.size && - [...next].every( - ([back, at]) => last.get(back)?.start === at.start && last.get(back)?.latest === at.latest, - ); + [...next].every(([back, at]) => { + const was = last.get(back); + return ( + was?.start.at === at.start.at && was?.start.past === at.start.past && was?.latest === at.latest + ); + }); if (same) return; producer.#relativeStarts = next; // A reader whose relative start waits on the report reads again. @@ -2163,6 +2174,7 @@ export class Producer { if (this.#cached.get(entry.group.start) === entry) { this.#cached.delete(entry.group.start); removeKey(this.#keys, entry.group.start); + this.#uncached = max(this.#uncached ?? entry.group.start, entry.group.start); } this.#unbound(entry, evicted); this.#account.remove(entry); @@ -3703,9 +3715,14 @@ export class Subscriber { // feeds counts only through the groups it holds and the starts skipped between them, since // any other gap may hold groups it never saw, and waits on the report there; once nothing // feeds it, over the groups held. Mirrors the Rust `TrackState::counted_start`. - countedStart: (latest, back) => { + countedStart: (latest, back, end) => { const reported = receivedCopy(producer) ? anchors.relative.get(back) : undefined; - if (reported !== undefined && reported.latest === latest) return { group: reported.start }; + if (reported !== undefined && reported.latest === latest) { + const { at, past } = reported.start; + if (at !== undefined) return { group: at }; + // Past a range ending no earlier than this one, so this one holds no group either. + if (end <= past) return { group: end }; + } if (!anchors.reported || !receivedCopy(producer) || state.closed.peek() !== undefined) return undefined; // Each step lands on a group held, so a huge count walks the cache once. let at = latest; @@ -3718,7 +3735,11 @@ export class Subscriber { if (run.start === 0n) return "before"; below = run.start - 1n; } - if (keys[keyIndex(keys, below)] !== below) return "unknown"; + if (keys[keyIndex(keys, below)] !== below) { + // The cache may have made this gap, which no report or arrival fills, so start at + // the lowest group counted: whatever the publisher counted to lies below it. + return anchors.uncached !== undefined && below <= anchors.uncached ? { group: at } : "unknown"; + } at = below; } return { group: at }; diff --git a/js/net/src/window.ts b/js/net/src/window.ts index c7288e35d3..9dfdfd43a5 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -334,13 +334,13 @@ export interface Holdings { */ back(latest: bigint, back: bigint): bigint | undefined; /** - * Where a range counting `back` groups before `latest` starts, which a relative start, but never - * an end, takes over {@link back}: where the publisher reported starting it, since a delivery + * Where a range counting `back` groups before `latest`, up to the exclusive `end` ({@link UNBOUNDED} + * for none), starts, which a relative start, but never an end, takes over {@link back}: where the publisher reported starting it, since a delivery * start may sit past the group counted, such as above groups too stale for the budget, or * `"unknown"` while a received copy can count it neither that way nor over its own groups yet. * Undefined, or absent, counts it with {@link back}. Mirrors the Rust `Holdings::counted_start`. */ - countedStart?(latest: bigint, back: bigint): Containing | undefined; + countedStart?(latest: bigint, back: bigint, end: bigint): Containing | undefined; /** Whether nothing more can arrive here, so a time start nothing contains reaches back to the oldest group rather than wait. */ complete(): boolean; /** @@ -793,7 +793,7 @@ export class Window { } anchor = reached.anchor; } - const start = startOf(anchor, latest, holdings); + const start = startOf(anchor, latest, until, holdings); if (start === "pending") { this.#pending = true; return; @@ -1058,13 +1058,14 @@ function joins(range: Range, min: Duration | undefined, holdings: Holdings): Dur function startOf( anchor: Anchor | undefined, latest: bigint | undefined, + until: bigint, holdings: Holdings, ): { group: bigint; floored: boolean } | "waiting" | "pending" | "counting" { // Nothing sits below group 0, so the oldest start needs no latest group. if (anchor === undefined) return { group: 0n, floored: false }; if (anchor.relative !== undefined) { if (latest === undefined) return "pending"; - const counted = holdings.countedStart?.(latest, anchor.relative); + const counted = holdings.countedStart?.(latest, anchor.relative, until); if (counted === "unknown") return "counting"; // Fewer groups are held than it counts back, so reach back to the oldest. if (counted === "before") return { group: 0n, floored: false }; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 32dff7f92f..c15bc1d48b 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -253,7 +253,7 @@ struct Duplicate { /// The copy still arriving, whose readers wait while this holds it. arriving: track::Hold, track: track::Producer, - /// Counted already, and kept open while held, since the stream may still carry the group. + /// CountedStart already, and kept open while held, since the stream may still carry the group. reading: Reading, } @@ -411,7 +411,7 @@ impl TrackEntry { ); } } - // The subscription's end waits until this stream is read. Counted + // The subscription's end waits until this stream is read. CountedStart // whatever the cache said, since the publisher counts every stream it // opens (a resubscribe resends a cached group), and only after the // cache holds the group, so a fetch held for the feed that sees it @@ -5133,7 +5133,7 @@ mod tests { let waiter = kio::Waiter::noop(); assert!(duplicate.poll_serve(&waiter).is_pending(), "first fails: {first_fails}"); assert_eq!(duplicate.state.name(), "Hold", "first fails: {first_fails}"); - // Counted and holding the end open while it waits, since it may carry the group. + // CountedStart and holding the end open while it waits, since it may carry the group. assert_eq!(tail.read().streams(), 1); let mut original = reader.assert_group(); @@ -10152,14 +10152,22 @@ 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 { + // `None` for a range with no group to deliver. + let Some(&start) = self.starts.get(&id) else { continue; }; - match range.start { - Some(lite::Anchor::Absolute(time)) if time.frame == 0 => times.push((time.group, *start)), - Some(lite::Anchor::Relative(back)) => { + match (range.start, start) { + (Some(lite::Anchor::Absolute(time)), Some(start)) if time.frame == 0 => times.push((time.group, start)), + (Some(lite::Anchor::Relative(back)), start) => { if let Some(Some(latest)) = self.edges.get(&id) { - relative.push((back, *start, *latest)); + // With no group to deliver, the start lies at or past the range's end: + // an absolute end names its exclusive group, and an open one the latest. + let start = match (start, range.end) { + (Some(start), _) => track::CountedStart::At(start), + (None, Some(lite::Anchor::Absolute(end))) => track::CountedStart::Past(end.group), + (None, _) => track::CountedStart::Past(latest.saturating_add(1)), + }; + relative.push((back, start, *latest)); } } _ => {} diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index bec8b1b27b..a428233532 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -82,13 +82,14 @@ pub(in crate::model) trait Holdings { /// held: `None` once fewer are held than that. fn back(&self, latest: u64, back: u64) -> Option; - /// Where a range counting `back` groups before `latest` starts, which a relative start, - /// but never an end, takes over [`Self::back`]: where the publisher reported starting it, + /// Where a range counting `back` groups before `latest`, up to the exclusive `end`, starts, + /// which a relative start, but never an end, takes over [`Self::back`]: where the publisher + /// reported starting it, /// since a delivery start may sit past the group counted, such as above groups too stale /// for the budget, or [`Containing::Unknown`] while a received copy can count it neither /// that way nor over its own groups yet. `None` counts it with [`Self::back`]. - fn counted_start(&self, latest: u64, back: u64) -> Option { - let _ = (latest, back); + fn counted_start(&self, latest: u64, back: u64, end: Option) -> Option { + let _ = (latest, back, end); None } @@ -672,7 +673,7 @@ impl Window { } }, }; - let (start, floored) = match start(anchor, latest, holdings) { + let (start, floored) = match start(anchor, latest, end, holdings) { Start::At(start, floored) => (start, floored), Start::Pending => { self.pending.insert(key); @@ -867,13 +868,14 @@ enum Start { Counting, } -fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Start { +/// Where `anchor` starts a range ending at the exclusive `end`. +fn start(anchor: Anchor, latest: Option, end: Option, holdings: &impl Holdings) -> Start { match anchor { // Nothing sits below group 0, so the oldest start needs no latest group. Anchor::Oldest => Start::At(0, false), Anchor::Relative(back) => match latest { None => Start::Pending, - Some(latest) => match holdings.counted_start(latest, back).unwrap_or_else(|| { + Some(latest) => match holdings.counted_start(latest, back, end).unwrap_or_else(|| { holdings .back(latest, back) .map_or(Containing::Before, Containing::Group) @@ -1003,7 +1005,7 @@ mod tests { self.held.range(..latest).rev().nth(back as usize).copied() } - fn counted_start(&self, _: u64, _: u64) -> Option { + fn counted_start(&self, _: u64, _: u64, _: Option) -> Option { self.counted } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 3ebb508a3c..a5f6a264e6 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -619,10 +619,20 @@ impl Anchors { /// counted from. #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct RelativeStart { - start: u64, + start: CountedStart, latest: u64, } +/// Where a publisher reported starting a range counting back. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum CountedStart { + /// At this group. + At(u64), + /// At or past this exclusive end, holding no group of the range there: it says nothing of + /// a range ending later. + Past(u64), +} + #[derive(Default)] pub(crate) struct TrackState { // Ingress stats scope, inherited from a tagged [`broadcast::Producer`] or [`Request`]. @@ -716,6 +726,9 @@ pub(crate) struct TrackState { // group above it is checked against it as well as its cached neighbors; one at or below // it may have lost its lower bound. One entry, so nothing grows with the cache. evicted: Option<(u64, Timestamp)>, + // The highest key of any group that left the cache, checked start or not: a received copy + // counting a relative start back can't tell a gap at or below it from one it never held. + uncached: Option, // The key of the newest cached group: the live edge, protected from // eviction by never entering the eviction order until no producer remains. @@ -1197,12 +1210,17 @@ impl Holdings for TrackState { /// newer one moved the live edge, which the copy then counts from itself. A lite-07 copy /// that may still learn where, from a report or the groups between, counts only where it /// holds them (see [`Self::counted_received`]); once it can't, over the groups held. - fn counted_start(&self, latest: u64, back: u64) -> Option { + fn counted_start(&self, latest: u64, back: u64, end: Option) -> Option { let reported = self .relative_starts .get(&back) .filter(|reported| reported.latest == latest) - .map(|reported| Containing::Group(reported.start)); + .and_then(|reported| match reported.start { + CountedStart::At(start) => Some(start), + // Past a range ending no earlier than this one, so this one holds no group either. + CountedStart::Past(past) => end.filter(|end| *end <= past), + }) + .map(Containing::Group); let learns = self.anchors == Anchors::Reported && !self.outcome.is_final() && !self.unfed; reported.or_else(|| learns.then(|| self.counted_received(latest, back))) } @@ -1328,6 +1346,11 @@ impl TrackState { _ => below, }; if !self.lookup.contains_key(&below) { + // The cache may have made this gap, which no report or arrival fills, so start at + // the lowest group counted: whatever the publisher counted to lies below it. + if self.uncached.is_some_and(|uncached| below <= uncached) { + return Containing::Group(at); + } return Containing::Unknown; } at = below; @@ -1486,6 +1509,7 @@ impl TrackState { /// Take group `key` out of the cache, raising the eviction watermark to its start. fn uncache(&mut self, key: u64) -> Option { let slot = self.remove_slot(key)?; + self.uncached = self.uncached.max(Some(key)); if let Some(start) = slot.bound() && self.evicted.is_none_or(|(evicted, _)| key > evicted) { @@ -4846,9 +4870,9 @@ impl Producer { /// Record where the upstream publisher started each of this lite-07 copy's live wire /// ranges counting back, as `(back, start, latest)` triples, replacing the last set. A - /// reader's relative anchor counting back `back` groups from `latest` resolves to `start`. + /// reader's relative start counting back `back` groups from `latest` resolves to `start`. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn relative_starts(&self, starts: impl IntoIterator) { + pub(crate) fn relative_starts(&self, starts: impl IntoIterator) { let starts: BTreeMap = starts .into_iter() .map(|(back, start, latest)| (back, RelativeStart { start, latest })) @@ -15721,7 +15745,7 @@ mod test { write_group(&producer, key); } producer.report_edge(9); - producer.relative_starts([(2, 7, 9)]); + producer.relative_starts([(2, CountedStart::At(7), 9)]); let mut reported = producer.subscribe(back(2)); assert_eq!(placed(&mut reported), Some(7), "the publisher's start, not group 1"); @@ -15762,6 +15786,25 @@ mod test { "an earlier reader stays where it started" ); + // A publisher with no group to deliver in a range ending at 10 has none in one ending + // sooner, but says nothing of one ending later. + producer.relative_starts([(5, CountedStart::Past(10), 10)]); + let until = |end| { + Subscription::default() + .with_ranges([Range { + start: Anchor::Relative(5), + end: Some(Anchor::Time(end)), + }]) + .with_delay(Delay::default().with_max(None)) + }; + let mut empty = producer.subscribe(until(8)); + assert_eq!( + empty.resolved_range(0).map(|resolved| (resolved.start, resolved.end)), + Some((9, Some(9))) + ); + let mut longer = producer.subscribe(until(10)); + assert_eq!(placed(&mut longer), Some(1), "counted through the groups held instead"); + // Nothing more can arrive, so it counts over the groups held, fewer than it asks: it // reaches back to the oldest. drop(producer.finish().unwrap()); @@ -15769,6 +15812,40 @@ mod test { assert_eq!(read(&mut gap), vec![1, 2, 7, 8, 9, 10]); } + /// A gap the cache made in a lite-07 copy is one no report or arrival fills, so a relative + /// start counting into it starts at the lowest group counted, while a gap above everything + /// the cache let go of still waits. + #[test] + #[cfg(feature = "async")] + fn a_received_copy_counts_a_relative_start_to_a_gap_the_cache_made() { + let back = |back| { + Subscription::default() + .with_ranges([Range { + start: Anchor::Relative(back), + end: None, + }]) + .with_delay(Delay::default().with_max(None)) + }; + let placed = |subscriber: &mut Subscriber| subscriber.resolved_range(0).map(|resolved| resolved.start); + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.reported(); + for key in [1, 5, 8, 9] { + write_group(&producer, key); + } + producer.skip_groups(6..8); + producer.report_edge(9); + + evict(&producer, 1); + let mut unknown = producer.subscribe(back(3)); + assert_eq!(placed(&mut unknown), None, "groups the copy never held may sit below 5"); + + evict(&producer, 8); + let mut evicted = producer.subscribe(back(1)); + assert_eq!(placed(&mut evicted), Some(9)); + assert_eq!(placed(&mut unknown), Some(9), "a later count passes the gap too"); + } + #[test] #[cfg(feature = "async")] fn a_late_lower_group_within_the_budget_is_delivered() { diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index 764820a1ab..ebc4f3d787 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -359,6 +359,17 @@ async fn a_later_relative_start_on_a_partial_copy_reads_only_its_groups() { .unwrap(); assert_eq!(read(&mut first, 3).await, [Some(1), Some(2), None]); + // The publisher counts back past the end, so it has no group there to deliver. + let mut empty = kio::clock::timeout( + TIMEOUT, + subscribe(vec![range(Anchor::Relative(2), Some(Anchor::Time(6)))]), + ) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut empty, 1).await, [None], "nothing to deliver"); + drop(empty); + let mut second = kio::clock::timeout( TIMEOUT, subscribe(vec![range(Anchor::Relative(2), Some(Anchor::Time(8)))]), From 14aab9dd70e3fca45aef2dad32ddba54cfe62ba2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 10:12:37 -0800 Subject: [PATCH 05/18] fix(moq-net): a cache-made gap counts over the groups held, and reports merge by count A relative count reaching a gap the cache may have made counts over the groups held, as a publisher that let them go does, instead of starting above the gap and dropping requested history. Ranges counting back the same number of groups merge their reports, so a range holding no group never hides another's start. Co-Authored-By: Claude Opus 5.5 --- js/net/src/track.ts | 21 +++++++-- rs/moq-net/src/model/track.rs | 80 ++++++++++++++++++++++++++--------- 2 files changed, 76 insertions(+), 25 deletions(-) diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 50e56a6943..21bdcab078 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1583,7 +1583,20 @@ export class Producer { producer.#reportsStarts = true; }; hooks.relativeStarts = (producer, starts) => { - const next = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); + // Ranges counting back the same number of groups report together: the one from the newer + // latest group, and from the same one, a start over any range holding none, since a start + // holds for every range ending past it, or else the furthest of them. Mirrors the Rust + // `RelativeStart::merge`. + const next = new Map(); + for (const [back, start, latest] of starts) { + const last = next.get(back); + if (last === undefined || (latest ?? -1n) > (last.latest ?? -1n)) { + next.set(back, { start, latest }); + } else if (latest === last.latest && last.start.at === undefined) { + if (start.at !== undefined || (start.past ?? 0n) > (last.start.past ?? 0n)) + next.set(back, { start, latest }); + } + } const last = producer.#relativeStarts; const same = next.size === last.size && @@ -3736,9 +3749,9 @@ export class Subscriber { below = run.start - 1n; } if (keys[keyIndex(keys, below)] !== below) { - // The cache may have made this gap, which no report or arrival fills, so start at - // the lowest group counted: whatever the publisher counted to lies below it. - return anchors.uncached !== undefined && below <= anchors.uncached ? { group: at } : "unknown"; + // The cache may have made this gap, which no report or arrival fills, so count over + // the groups held, as a publisher that let them go does. + return anchors.uncached !== undefined && below <= anchors.uncached ? undefined : "unknown"; } at = below; } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index a5f6a264e6..d3805d0860 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -623,6 +623,22 @@ struct RelativeStart { latest: u64, } +impl RelativeStart { + /// What two ranges counting back the same number of groups report together: the one from + /// the newer latest group, and from the same one, a start over any range holding none, + /// since a start holds for every range ending past it, or else the furthest of them. + fn merge(self, other: Self) -> Self { + if self.latest != other.latest { + return if self.latest > other.latest { self } else { other }; + } + let start = match (self.start, other.start) { + (CountedStart::At(start), _) | (_, CountedStart::At(start)) => CountedStart::At(start), + (CountedStart::Past(a), CountedStart::Past(b)) => CountedStart::Past(a.max(b)), + }; + Self { start, ..self } + } +} + /// Where a publisher reported starting a range counting back. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum CountedStart { @@ -1222,7 +1238,7 @@ impl Holdings for TrackState { }) .map(Containing::Group); let learns = self.anchors == Anchors::Reported && !self.outcome.is_final() && !self.unfed; - reported.or_else(|| learns.then(|| self.counted_received(latest, back))) + reported.or_else(|| learns.then(|| self.counted_received(latest, back)).flatten()) } fn complete(&self) -> bool { @@ -1329,33 +1345,31 @@ impl TrackState { /// The group `back` groups before `latest` on a received copy, counted only through the /// groups it holds and the starts its publisher declared skipped between them: any other /// gap may hold groups the copy never saw, such as one below what a first reader asked - /// for, so the count is [`Containing::Unknown`] there. Each step lands on a group held, so - /// a huge count walks the cache once. - fn counted_received(&self, latest: u64, back: u64) -> Containing { + /// for, so the count is [`Containing::Unknown`] there. A gap the cache may have made is one + /// no report or arrival fills, so it is `None` there, to count over the groups held as a + /// publisher that let them go does. Each step lands on a group held, so a huge count walks + /// the cache once. + fn counted_received(&self, latest: u64, back: u64) -> Option { let mut at = latest; for _ in 0..back { let Some(below) = at.checked_sub(1) else { - return Containing::Before; + return Some(Containing::Before); }; let below = match self.drops.get_key_value(&below) { Some((run, Hole::Skip)) => match run.start.checked_sub(1) { Some(below) => below, // Skipped down to 0: no group sits below. - None => return Containing::Before, + None => return Some(Containing::Before), }, _ => below, }; if !self.lookup.contains_key(&below) { - // The cache may have made this gap, which no report or arrival fills, so start at - // the lowest group counted: whatever the publisher counted to lies below it. - if self.uncached.is_some_and(|uncached| below <= uncached) { - return Containing::Group(at); - } - return Containing::Unknown; + let uncached = self.uncached.is_some_and(|uncached| below <= uncached); + return (!uncached).then_some(Containing::Unknown); } at = below; } - Containing::Group(at) + Some(Containing::Group(at)) } /// Whether drops, for whatever reason, cover every key in `keys`. An empty run is covered. @@ -4873,10 +4887,16 @@ impl Producer { /// reader's relative start counting back `back` groups from `latest` resolves to `start`. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn relative_starts(&self, starts: impl IntoIterator) { - let starts: BTreeMap = starts - .into_iter() - .map(|(back, start, latest)| (back, RelativeStart { start, latest })) - .collect(); + let mut merged = BTreeMap::::new(); + for (back, start, latest) in starts { + let next = RelativeStart { start, latest }; + let start = match merged.get(&back) { + Some(last) => last.merge(next), + None => next, + }; + merged.insert(back, start); + } + let starts = merged; if let Ok(mut state) = self.modify() && state.relative_starts != starts { @@ -15812,9 +15832,27 @@ mod test { assert_eq!(read(&mut gap), vec![1, 2, 7, 8, 9, 10]); } + /// Ranges counting back the same number of groups report together: a start holds for every + /// range ending past it, so it outlasts a range reporting none, whatever order they come in. + #[test] + fn relative_starts_merge_by_count() { + let at = |start, latest| RelativeStart { + start: CountedStart::At(start), + latest, + }; + let past = |past, latest| RelativeStart { + start: CountedStart::Past(past), + latest, + }; + assert_eq!(at(9, 9).merge(past(7, 9)), at(9, 9)); + assert_eq!(past(7, 9).merge(at(9, 9)), at(9, 9)); + assert_eq!(past(7, 9).merge(past(8, 9)), past(8, 9)); + assert_eq!(at(9, 9).merge(past(7, 10)), past(7, 10), "a newer latest moved the count"); + } + /// A gap the cache made in a lite-07 copy is one no report or arrival fills, so a relative - /// start counting into it starts at the lowest group counted, while a gap above everything - /// the cache let go of still waits. + /// start counting into it counts over the groups held, as a publisher that let them go does, + /// while a gap above everything the cache let go of still waits. #[test] #[cfg(feature = "async")] fn a_received_copy_counts_a_relative_start_to_a_gap_the_cache_made() { @@ -15842,8 +15880,8 @@ mod test { evict(&producer, 8); let mut evicted = producer.subscribe(back(1)); - assert_eq!(placed(&mut evicted), Some(9)); - assert_eq!(placed(&mut unknown), Some(9), "a later count passes the gap too"); + assert_eq!(placed(&mut evicted), Some(5), "group 5 is the one held before 9"); + assert_eq!(placed(&mut unknown), Some(0), "fewer are held than it counts"); } #[test] From 43cb4055adfc734c42df198ac0350fc40322a9a0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 10:18:36 -0800 Subject: [PATCH 06/18] fix(moq-net): two starts reported for one count merge to the lower Co-Authored-By: Claude Opus 5.5 --- js/net/src/track.ts | 14 ++++++++------ rs/moq-net/src/model/track.rs | 7 +++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 21bdcab078..560f04e40c 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1584,17 +1584,19 @@ export class Producer { }; hooks.relativeStarts = (producer, starts) => { // Ranges counting back the same number of groups report together: the one from the newer - // latest group, and from the same one, a start over any range holding none, since a start - // holds for every range ending past it, or else the furthest of them. Mirrors the Rust - // `RelativeStart::merge`. + // latest group, and from the same one, the lower start, nearer the group both counted to, + // over any range holding none, since a start holds for every range ending past it, or else + // the furthest of those. Mirrors the Rust `RelativeStart::merge`. const next = new Map(); for (const [back, start, latest] of starts) { const last = next.get(back); if (last === undefined || (latest ?? -1n) > (last.latest ?? -1n)) { next.set(back, { start, latest }); - } else if (latest === last.latest && last.start.at === undefined) { - if (start.at !== undefined || (start.past ?? 0n) > (last.start.past ?? 0n)) - next.set(back, { start, latest }); + } else if (latest === last.latest) { + const lower = start.at !== undefined && (last.start.at === undefined || start.at < last.start.at); + const further = + last.start.at === undefined && start.past !== undefined && start.past > (last.start.past ?? 0n); + if (lower || further) next.set(back, { start, latest }); } } const last = producer.#relativeStarts; diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index d3805d0860..75ac5afff5 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -625,13 +625,15 @@ struct RelativeStart { impl RelativeStart { /// What two ranges counting back the same number of groups report together: the one from - /// the newer latest group, and from the same one, a start over any range holding none, - /// since a start holds for every range ending past it, or else the furthest of them. + /// the newer latest group, and from the same one, the lower start, nearer the group both + /// counted to, over any range holding none, since a start holds for every range ending past + /// it, or else the furthest of those. fn merge(self, other: Self) -> Self { if self.latest != other.latest { return if self.latest > other.latest { self } else { other }; } let start = match (self.start, other.start) { + (CountedStart::At(a), CountedStart::At(b)) => CountedStart::At(a.min(b)), (CountedStart::At(start), _) | (_, CountedStart::At(start)) => CountedStart::At(start), (CountedStart::Past(a), CountedStart::Past(b)) => CountedStart::Past(a.max(b)), }; @@ -15847,6 +15849,7 @@ mod test { assert_eq!(at(9, 9).merge(past(7, 9)), at(9, 9)); assert_eq!(past(7, 9).merge(at(9, 9)), at(9, 9)); assert_eq!(past(7, 9).merge(past(8, 9)), past(8, 9)); + assert_eq!(at(9, 9).merge(at(7, 9)), at(7, 9), "a wider budget delivered from lower"); assert_eq!(at(9, 9).merge(past(7, 10)), past(7, 10), "a newer latest moved the count"); } From 2f6a2524b98476867c69c601860159742246a913 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 10:22:51 -0800 Subject: [PATCH 07/18] style(moq-net): format the merge test Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 75ac5afff5..6015455633 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -15849,8 +15849,16 @@ mod test { assert_eq!(at(9, 9).merge(past(7, 9)), at(9, 9)); assert_eq!(past(7, 9).merge(at(9, 9)), at(9, 9)); assert_eq!(past(7, 9).merge(past(8, 9)), past(8, 9)); - assert_eq!(at(9, 9).merge(at(7, 9)), at(7, 9), "a wider budget delivered from lower"); - assert_eq!(at(9, 9).merge(past(7, 10)), past(7, 10), "a newer latest moved the count"); + assert_eq!( + at(9, 9).merge(at(7, 9)), + at(7, 9), + "a wider budget delivered from lower" + ); + assert_eq!( + at(9, 9).merge(past(7, 10)), + past(7, 10), + "a newer latest moved the count" + ); } /// A gap the cache made in a lite-07 copy is one no report or arrival fills, so a relative From 59fb7a68e86a817ab2ae32853f9f6df3af7a9d82 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 10:24:52 -0800 Subject: [PATCH 08/18] fix(moq-net): CountedStart::Past is async-only Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 6015455633..a03c796b0e 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -648,6 +648,7 @@ pub(crate) enum CountedStart { At(u64), /// At or past this exclusive end, holding no group of the range there: it says nothing of /// a range ending later. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] Past(u64), } From 2b599e54bcc0b809f6e8bd1a0f8eaeef66dd70f3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 13:20:01 -0800 Subject: [PATCH 09/18] fix(moq-net): a counting relative start stops waiting once nothing arrives, and keeps an empty report past a newer edge A copy that completes, even with its producer still held, or no longer fed, falls back to counting over the groups held, in Rust and JS. An empty report counted from an older latest group still holds, since counting from a newer one only starts later. Only a group that leaves the cache for good raises the uncached watermark, not one displaced and held or sent again. Also fixes three comments a rename broke and the CountedStart doc order in internal.ts. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 14 +++--- js/net/src/track.test.ts | 52 +++++++++++++++++++ js/net/src/track.ts | 26 +++++++--- js/net/src/window.ts | 3 +- rs/moq-net/src/lite/subscriber.rs | 6 +-- rs/moq-net/src/model/track.rs | 83 ++++++++++++++++++++++++++----- 6 files changed, 152 insertions(+), 32 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index f3e1100311..26b600e919 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -578,6 +578,13 @@ export interface StagedTrack { commit(): Producer; } +/** + * Where a publisher reported starting a range counting back: at a group, or at or past an exclusive + * end where the range held no group, which says nothing of a range ending later. Mirrors the Rust + * `track::CountedStart`. + */ +export type CountedStart = { at: bigint; past?: never } | { past: bigint; at?: never }; + /** * How a received copy's start bounds the fills its readers ask below it. Mirrors the Rust * `Start::Resolved` and `Producer::fills_from_start`. @@ -589,13 +596,6 @@ export interface StagedTrack { * widened budget reaches lower groups through the feed's re-offer. Anywhere else, a reader fills * below the start only the groups its ranges name. */ -/** - * Where a publisher reported starting a range counting back: at a group, or at or past an exclusive - * end where the range held no group, which says nothing of a range ending later. Mirrors the Rust - * `track::CountedStart`. - */ -export type CountedStart = { at: bigint; past?: never } | { past: bigint; at?: never }; - export interface FillStarts { /** The wire's start is resolved from the subscriber's budget: lite SUBSCRIBE_START from lite-06. */ resolvesStart?: boolean; diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index d925383297..c608d76b9b 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5788,3 +5788,55 @@ test("the counted aggregates match a full fold of every subscriber", async () => producer.close(); } }); + +// A lite-07 copy holding part of the track counts a relative start back only through the groups it +// holds, or as its publisher reported, and reads nothing at a gap until one of those can count it, +// or nothing more arrives. Mirrors the Rust `a_received_copy_counts_a_relative_start_back_as_its_publisher_did`. +test("a received copy counts a relative start back as its publisher did", async () => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + for (const key of [1n, 2n, 7n, 8n, 9n, 10n]) putGroup(producer, key, Number(key)); + producer.skipGroups(3n, 7n); + // Counting 4 back from 10, the publisher had no group to deliver before 11. + hooks.relativeStarts(producer, [[4n, { past: 11n }, 10n]]); + putGroup(producer, 11n, 11); + const subscribe = (start: bigint, end?: bigint) => + producer.subscribe({ + delay: { max: null }, + ranges: [{ start: { relative: start }, end: end === undefined ? undefined : { time: end } }], + }); + + // Counting from a newer latest group only starts later, so the empty report still holds, where + // the groups held would count back to group 7. + const empty = subscribe(4n, 8n); + expect(drain(empty)).toEqual([]); + expect(await empty.recvGroup()).toBeUndefined(); + + // Through the starts skipped, group 0 may sit below 1, so it reads nothing meanwhile. + const gap = subscribe(7n); + expect(drain(gap)).toEqual([]); + // Once the copy reaches its end, nothing more can arrive, so it counts over the groups held. + hooks.endTrack(producer, { group: 12n, frame: 0 }); + await settle(); + expect(drain(gap)).toEqual([1n, 2n, 7n, 8n, 9n, 10n, 11n]); + expect(await gap.recvGroup()).toBeUndefined(); +}); + +// A failed group a new copy replaces leaves no gap the cache made, so a relative start counting into +// the gap below it still waits. Mirrors the Rust `a_received_copy_waits_on_a_gap_a_displaced_group_left`. +test("a received copy waits on a gap a replaced group left", () => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + for (const key of [1n, 2n]) putGroup(producer, key, Number(key)); + const failed = producer.createGroup(9n); + failed.writeFrame(textFrame("x", Timestamp.fromMillis(9))); + failed.abort(new Error("reset")); + putGroup(producer, 9n, 9); + + const reader = producer.subscribe({ + delay: { max: null }, + ranges: [{ start: { relative: 2n }, end: { time: 8n } }], + }); + expect(drain(reader)).toEqual([]); + expect(reader.floor()).toBeUndefined(); +}); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 721e338eb8..b154ddacb3 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1284,9 +1284,9 @@ export class Producer { #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 of any group that left the cache: a received copy counting a relative start - // back can't tell a gap at or below it from one it never held. Mirrors the Rust - // `TrackState::uncached`. + // The highest key of any group the cache evicted, rather than one a new copy replaced: a received + // copy counting a relative start back can't tell a gap at or below it from one it never held. + // Mirrors the Rust `TrackState::uncached`. #uncached?: bigint; // Whether a copy's publisher reports where each range starts, so a relative start the copy // can't count back over its own groups waits for that. Mirrors the Rust `Anchors::Reported`. @@ -1389,6 +1389,8 @@ export class Producer { evict: (entry) => { this.#evict(entry); this.#uncache(entry, true); + // Gone for good, unlike a group a new copy replaces at once. + this.#uncached = max(this.#uncached ?? entry.group.start, entry.group.start); }, settle: () => this.#settle(), expireDrops: (now) => this.#expireDrops(now), @@ -2175,7 +2177,6 @@ export class Producer { if (this.#cached.get(entry.group.start) === entry) { this.#cached.delete(entry.group.start); removeKey(this.#keys, entry.group.start); - this.#uncached = max(this.#uncached ?? entry.group.start, entry.group.start); } this.#unbound(entry, evicted); this.#account.remove(entry); @@ -3717,13 +3718,22 @@ export class Subscriber { // feeds it, over the groups held. Mirrors the Rust `TrackState::counted_start`. countedStart: (latest, back, end) => { const reported = receivedCopy(producer) ? anchors.relative.get(back) : undefined; - if (reported !== undefined && reported.latest === latest) { + if (reported?.latest !== undefined && reported.latest <= latest) { const { at, past } = reported.start; - if (at !== undefined) return { group: at }; + if (at !== undefined && reported.latest === latest) return { group: at }; // Past a range ending no earlier than this one, so this one holds no group either. - if (end <= past) return { group: end }; + // Counting from a newer latest group only starts later, so it holds there too. + if (past !== undefined && end <= past) return { group: end }; } - if (!anchors.reported || !receivedCopy(producer) || state.closed.peek() !== undefined) return undefined; + // Nothing more arrives once the copy closes or completes, so no report can settle it + // either. + if ( + !anchors.reported || + !receivedCopy(producer) || + state.closed.peek() !== undefined || + this.#complete() + ) + return undefined; // Each step lands on a group held, so a huge count walks the cache once. let at = latest; for (let step = 0n; step < back; step++) { diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 6abf69d639..dd3030920f 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -675,8 +675,7 @@ export class Window { * anchor waits. */ start(): bigint | undefined { - if (this.pending || this.#reaching.size > 0 || this.#counting.size > 0 || this.#unnamed > 0) - return undefined; + if (this.pending || this.#reaching.size > 0 || this.#counting.size > 0 || this.#unnamed > 0) return undefined; return this.#named.first(); } diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index f503aa1888..31b6bffb73 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -253,7 +253,7 @@ struct Duplicate { /// The copy still arriving, whose readers wait while this holds it. arriving: track::Hold, track: track::Producer, - /// CountedStart already, and kept open while held, since the stream may still carry the group. + /// Counted already, and kept open while held, since the stream may still carry the group. reading: Reading, } @@ -411,7 +411,7 @@ impl TrackEntry { ); } } - // The subscription's end waits until this stream is read. CountedStart + // The subscription's end waits until this stream is read. Counted // whatever the cache said, since the publisher counts every stream it // opens (a resubscribe resends a cached group), and only after the // cache holds the group, so a fetch held for the feed that sees it @@ -5133,7 +5133,7 @@ mod tests { let waiter = kio::Waiter::noop(); assert!(duplicate.poll_serve(&waiter).is_pending(), "first fails: {first_fails}"); assert_eq!(duplicate.state.name(), "Hold", "first fails: {first_fails}"); - // CountedStart and holding the end open while it waits, since it may carry the group. + // Counted and holding the end open while it waits, since it may carry the group. assert_eq!(tail.read().streams(), 1); let mut original = reader.assert_group(); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 76eeacf6e6..b975ec1320 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -745,7 +745,8 @@ pub(crate) struct TrackState { // group above it is checked against it as well as its cached neighbors; one at or below // it may have lost its lower bound. One entry, so nothing grows with the cache. evicted: Option<(u64, Timestamp)>, - // The highest key of any group that left the cache, checked start or not: a received copy + // The highest key of any group that left the cache for good (evicted or reclaimed, not + // displaced by a copy that holds or sends it again), checked start or not: a received copy // counting a relative start back can't tell a gap at or below it from one it never held. uncached: Option, @@ -1233,14 +1234,16 @@ impl Holdings for TrackState { let reported = self .relative_starts .get(&back) - .filter(|reported| reported.latest == latest) .and_then(|reported| match reported.start { - CountedStart::At(start) => Some(start), + CountedStart::At(start) => (reported.latest == latest).then_some(start), // Past a range ending no earlier than this one, so this one holds no group either. - CountedStart::Past(past) => end.filter(|end| *end <= past), + // Counting from a newer latest group only starts later, so it holds there too. + CountedStart::Past(past) => end.filter(|end| reported.latest <= latest && *end <= past), }) .map(Containing::Group); - let learns = self.anchors == Anchors::Reported && !self.outcome.is_final() && !self.unfed; + // Nothing more arrives once the copy is released, complete, or unfed, so no report can + // settle it either. + let learns = self.anchors == Anchors::Reported && !self.outcome.is_released() && !self.nothing_arrives(); reported.or_else(|| learns.then(|| self.counted_received(latest, back)).flatten()) } @@ -1526,7 +1529,6 @@ impl TrackState { /// Take group `key` out of the cache, raising the eviction watermark to its start. fn uncache(&mut self, key: u64) -> Option { let slot = self.remove_slot(key)?; - self.uncached = self.uncached.max(Some(key)); if let Some(start) = slot.bound() && self.evicted.is_none_or(|(evicted, _)| key > evicted) { @@ -1539,6 +1541,7 @@ impl TrackState { /// it for the readers that judged it cached to judge again. fn evict_group(&mut self, key: u64, err: Error) { let slot = self.uncache(key).expect("a cached group"); + self.let_go(key); slot.abort(err); let landed = self .inboxes @@ -1550,6 +1553,12 @@ impl TrackState { } } + /// Record that group `key` left the cache for good, with nothing holding or sending it + /// again: a gap at or below it may be one the cache made. + fn let_go(&mut self, key: u64) { + self.uncached = self.uncached.max(Some(key)); + } + /// [`Self::uncache`] a group that another copy displaces rather than one that ends, so a /// teardown still ends the copies its readers hold. fn displace(&mut self, key: u64) { @@ -1868,7 +1877,11 @@ impl TrackState { /// Remove an aborted slot, keeping a cut as a drop: reclaiming it must not erase a /// disposition a relay still has to forward. fn reclaim_aborted(&mut self, key: u64) { - if let Some(slot) = self.uncache(key) + let slot = self.uncache(key); + if slot.is_some() { + self.let_go(key); + } + if let Some(slot) = slot && slot.visible && let Some(err) = slot.verdict() && let Some(end) = key.checked_add(1) @@ -15952,11 +15965,57 @@ mod test { let mut longer = producer.subscribe(until(10)); assert_eq!(placed(&mut longer), Some(1), "counted through the groups held instead"); - // Nothing more can arrive, so it counts over the groups held, fewer than it asks: it - // reaches back to the oldest. - drop(producer.finish().unwrap()); - assert_eq!(placed(&mut gap), Some(0)); - assert_eq!(read(&mut gap), vec![1, 2, 7, 8, 9, 10]); + // Counting from a newer latest group only starts later, so the empty report still holds, + // where the groups held would count back to group 2. + write_group(&producer, 11); + let mut newer = producer.subscribe(until(8)); + assert_eq!( + newer.resolved_range(0).map(|resolved| (resolved.start, resolved.end)), + Some((9, Some(9))) + ); + assert_eq!(placed(&mut gap), Some(1), "counted from 11 through the groups held"); + let mut deeper = producer.subscribe(back(7)); + assert_eq!(placed(&mut deeper), None, "group 0 may sit below 1"); + + // Nothing more can arrive once the copy reaches its end, though its producer is still + // held, so it counts over the groups held, fewer than it asks: it reaches back to the + // oldest. + let _finished = producer.finish().unwrap(); + assert_eq!(placed(&mut deeper), Some(0)); + assert_eq!(read(&mut deeper), vec![1, 2, 7, 8, 9, 10, 11]); + } + + /// A group an idle copy displaces is sent again by the next feed, so the gap it leaves is + /// not one the cache made: a relative start counting into it still waits. + #[test] + #[cfg(feature = "async")] + fn a_received_copy_waits_on_a_gap_a_displaced_group_left() { + let mut producer = track_producer("test", Info::timed()); + producer.received(); + producer.reported(); + for key in [1, 2] { + write_group(&producer, key); + } + let mut open = producer.create_group(group::Info { start: 9 }).unwrap(); + open.write_frame(Timestamp::from_millis(9).unwrap(), &b"x"[..]).unwrap(); + producer.report_edge(9); + producer.set_idle(); + producer.set_live(Some(Position::group(9)), None).unwrap(); + write_group(&producer, 9); + + let mut reader = producer.subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Relative(2), + end: Some(Anchor::Time(8)), + }]) + .with_delay(Delay::default().with_max(None)), + ); + assert_eq!( + reader.resolved_range(0).map(|resolved| resolved.start), + None, + "group 8 may sit below 9" + ); } /// Ranges counting back the same number of groups report together: a start holds for every From 0d3e35c233a51d0a7786691f7fb0265cf384ef44 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 13:44:58 -0800 Subject: [PATCH 10/18] fix(moq-net): a later relative start counts back to where a range from the oldest group started A lite-07 copy reading from the oldest group records where its publisher started, as a range asked from group 0. A later reader counting back past that start starts there, as the publisher does, instead of waiting on a group below that never arrives. Co-Authored-By: Claude Opus 5.5 --- js/net/src/integration.test.ts | 47 +++++++++++++ js/net/src/lite/subscriber.ts | 11 +-- js/net/src/track.ts | 4 ++ rs/moq-net/src/lite/subscriber.rs | 6 +- rs/moq-net/src/model/track.rs | 10 ++- rs/moq-net/tests/integration/range_anchors.rs | 69 +++++++++++++++++++ 6 files changed, 139 insertions(+), 8 deletions(-) diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index f7364b52d5..231b910319 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -535,6 +535,53 @@ test("integration: lite-07 starts a later reader's relative range where the publ } }); +// A copy reading a track from its oldest group learns where the publisher's groups start, so a later +// reader counting back past them starts there, as the publisher does, rather than wait on a group +// below that never arrives. Mirrors the Rust +// `a_later_relative_start_counts_back_to_where_the_oldest_range_started`. +test("integration: lite-07 counts a later relative start back to where the oldest range started", async () => { + const pair = createMockTransportPair(Lite.ALPN_07_WIP); + const origin = new OriginProducer(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + + const broadcast = publish(origin, Path.from("test")); + const producer = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + for (let key = 1n; key < 4n; key++) { + const group = producer.createGroup(key); + group.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.fromMillis(Number(key)) }); + group.finish(); + } + + const remote = wireOf(client).consume(Path.from("test")); + const subscribe = (ranges: TrackRange[]) => + remote + .track("video") + .subscribe({ delay: { max: null }, order: "ascending", ranges }) + .ordered(); + const read = async (reader: ReturnType, count: number) => { + const got: (bigint | undefined)[] = []; + for (let i = 0; i < count; i++) + got.push((await withTimeout(reader.nextGroup(), Duration.millis(2_000), "group timed out"))?.start); + return got; + }; + try { + const oldest = subscribe([{}]); + expect(await read(oldest, 3)).toEqual([1n, 2n, 3n]); + const later = subscribe([{ start: { relative: 5n } }]); + expect(await read(later, 3)).toEqual([1n, 2n, 3n]); + oldest.close(); + later.close(); + } finally { + remote.close(); + broadcast.close(); + client.abort(); + server.abort(); + } +}); + // A publisher counts a relative start back from its newest group, which may hold no frame yet and // so counts neither in the largest it reports nor where the copy resolves the start. The copy reports // the publisher's start, where a reader waiting on each group in sequence starts. Mirrors the Rust diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 3fd9fc7cd0..75ae4d38d6 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2877,8 +2877,8 @@ 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 + // Where the publisher started each live range asked from a group's start or the oldest group, + // 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][] = []; // And each one counting back, which the copy, holding only part of the track, can't count, @@ -2886,9 +2886,10 @@ export class Subscriber { const relative: [bigint, CountedStart, bigint | undefined][] = []; for (const id of entry.live.ids()) { 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; + if (range === undefined || !entry.starts.has(id)) continue; + // One from the oldest group is asked from group 0. + const time = range.start === undefined ? 0n : (range.start.time ?? range.start.absolute); + const back = range.start?.relative; // Undefined for a range with no group to deliver. const at = entry.starts.get(id); if (time !== undefined && at !== undefined) times.push([time, at]); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index b154ddacb3..69cf320c36 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -3746,6 +3746,10 @@ export class Subscriber { below = run.start - 1n; } if (keys[keyIndex(keys, below)] !== below) { + // The publisher delivers nothing below where a range from group 0 started, so a count + // reaching below it starts there. + const floor = anchors.starts.get(0n); + if (floor !== undefined && below < floor) return { group: floor }; // The cache may have made this gap, which no report or arrival fills, so count over // the groups held, as a publisher that let them go does. return anchors.uncached !== undefined && below <= anchors.uncached ? undefined : "unknown"; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 31b6bffb73..6af6a954aa 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10142,8 +10142,8 @@ 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. + /// start or the oldest group, 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. fn report_starts(&self, serving: &track::Producer) { if !self.live.reports_starts() { @@ -10158,6 +10158,8 @@ impl SubStream { }; match (range.start, start) { (Some(lite::Anchor::Absolute(time)), Some(start)) if time.frame == 0 => times.push((time.group, start)), + // From the oldest group, as one asked from group 0. + (None, Some(start)) => times.push((0, start)), (Some(lite::Anchor::Relative(back)), start) => { if let Some(Some(latest)) = self.edges.get(&id) { // With no group to deliver, the start lies at or past the range's end: diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index b975ec1320..b13782bd31 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1351,7 +1351,8 @@ impl TrackState { /// The group `back` groups before `latest` on a received copy, counted only through the /// groups it holds and the starts its publisher declared skipped between them: any other /// gap may hold groups the copy never saw, such as one below what a first reader asked - /// for, so the count is [`Containing::Unknown`] there. A gap the cache may have made is one + /// for, so the count is [`Containing::Unknown`] there, unless a range from group 0 + /// started above it. A gap the cache may have made is one /// no report or arrival fills, so it is `None` there, to count over the groups held as a /// publisher that let them go does. Each step lands on a group held, so a huge count walks /// the cache once. @@ -1370,6 +1371,13 @@ impl TrackState { _ => below, }; if !self.lookup.contains_key(&below) { + // The publisher delivers nothing below where a range from group 0 started, so + // a count reaching below it starts there. + if let Some(&start) = self.time_starts.get(&0) + && below < start + { + return Some(Containing::Group(start)); + } let uncached = self.uncached.is_some_and(|uncached| below <= uncached); return (!uncached).then_some(Containing::Unknown); } diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index ebc4f3d787..1dd7383268 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -385,3 +385,72 @@ async fn a_later_relative_start_on_a_partial_copy_reads_only_its_groups() { drop(first); let _track = track.finish().unwrap(); } + +/// A copy reading a track from its oldest group learns where the publisher's groups start, so +/// a later reader counting back past them starts there, as the publisher does, rather than +/// wait on a group below that never arrives. Mirrors the `@moq/net` `integration: lite-07 +/// counts a later relative start back to where the oldest range started`. +#[kio::test] +async fn a_later_relative_start_counts_back_to_where_the_oldest_range_started() { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track( + "data", + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER), + ) + .unwrap(); + broadcast.announce().unwrap(); + for key in 1..4 { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group.write_frame(millis(key), &b"x"[..]).unwrap(); + group.finish().unwrap(); + } + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + let subscribe = |ranges: Vec| { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges(ranges); + remote.track("data").unwrap().subscribe(subscription) + }; + let read = async |reader: &mut moq_net::track::Subscriber, count: usize| { + let mut read = Vec::new(); + for _ in 0..count { + let group = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap(); + read.push(group.map(|group| group.start)); + } + read + }; + + let mut oldest = kio::clock::timeout(TIMEOUT, subscribe(vec![Range::oldest()])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut oldest, 3).await, [Some(1), Some(2), Some(3)]); + + let mut later = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Relative(5), None)])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!( + read(&mut later, 3).await, + [Some(1), Some(2), Some(3)], + "counted back past group 1, as the publisher does" + ); + drop(oldest); + let _track = track.finish().unwrap(); +} From 9512f64bf200c306662ecab32c3d10dd3386e821 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 16:55:30 -0800 Subject: [PATCH 11/18] fix(moq-net): a relative start on a lite-07 copy resolves only from its publisher's report A reader whose relative start no report answers asks the copy's upstream for its range, and waits. The copy no longer counts over the groups it holds, and a moq-transport copy counts back by group ID. Co-Authored-By: Claude Opus 5.5 --- .../proptest-regressions/model/track.txt | 7 + rs/moq-net/src/lite/subscriber.rs | 155 ++++++++-- rs/moq-net/src/lite/subscriber/fit.rs | 17 ++ rs/moq-net/src/model/subscription/window.rs | 79 +++--- rs/moq-net/src/model/track.rs | 265 +++++++----------- rs/moq-net/src/model/track/demand.rs | 70 +++++ rs/moq-net/tests/integration/range_anchors.rs | 114 +++++++- 7 files changed, 475 insertions(+), 232 deletions(-) create mode 100644 rs/moq-net/proptest-regressions/model/track.txt diff --git a/rs/moq-net/proptest-regressions/model/track.txt b/rs/moq-net/proptest-regressions/model/track.txt new file mode 100644 index 0000000000..8e470d0497 --- /dev/null +++ b/rs/moq-net/proptest-regressions/model/track.txt @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc dc24e7ccea720fe2e18cea7e4bf6f9ce0876cb5bf500446c96abd65758cb2083 # shrinks to held = {}, back = 1, reported = None diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 0d10d4a4b2..e61d6ddcd6 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -3214,6 +3214,7 @@ mod tests { live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::OLDEST]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: tail.clone(), started: None, @@ -3376,6 +3377,7 @@ mod tests { live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::span(Some(floor), 0, None, None)]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), started: None, @@ -3855,6 +3857,7 @@ mod tests { live: lite::LiveRanges::new(VERSION, &[lite::Range::span(Some(2), 0, None, None)]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), started: None, @@ -3877,7 +3880,7 @@ mod tests { // An update moves the floor and the ranges together, as `begin_subscription` does. let update = |sub: &mut SubStream, start: Option, ranges: Vec| { sub.start = start; - sub.edit(&ranges, 0).unwrap(); + sub.edit(&ranges, 0, 0, None).unwrap(); }; sub.started = Some(None); update(&mut sub, Some(1), vec![lite::Range::span(Some(1), 0, None, None)]); @@ -3894,6 +3897,8 @@ mod tests { lite::Range::span(Some(6), 0, None, None), ], 0, + 0, + None, ) .unwrap(); assert_eq!( @@ -3972,6 +3977,7 @@ mod tests { live: lite::LiveRanges::new(Version::Lite07, &[demand]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), started: Some(Some(4)), @@ -3988,18 +3994,18 @@ mod tests { assert_eq!(sub.fill_range(0), None, "the demand's range is no fill"); // The same bounds again, as a fill. - sub.edit(&[demand, demand], 1).unwrap(); + sub.edit(&[demand, demand], 0, 1, None).unwrap(); assert_eq!(sub.fill_range(0), None, "the demand keeps its range"); assert_eq!(sub.fill_range(1), Some(4..8), "the copy the edit added is the fill"); // Another fill joins, and the first keeps its Range ID. let other = groups(10..12); - sub.edit(&[demand, demand, other], 2).unwrap(); + sub.edit(&[demand, demand, other], 0, 2, None).unwrap(); assert_eq!(sub.fill_ids.iter().copied().collect::>(), [1, 2]); // The demand moves away while the fill stays: the fill keeps Range ID 1. let moved = lite::Range::span(Some(20), 0, None, None); - sub.edit(&[moved, demand, other], 2).unwrap(); + sub.edit(&[moved, demand, other], 0, 2, None).unwrap(); assert_eq!(sub.fill_ids.iter().copied().collect::>(), [1, 2]); assert_eq!(sub.fill_range(1), Some(4..8)); assert_eq!(sub.fill_range(3), None, "the demand's new range is no fill"); @@ -4007,11 +4013,11 @@ mod tests { // Before SUBSCRIBE_START, a fill an update retires keeps its Range ID, since the start // still names it, and it must not count as the demand's. sub.started = None; - sub.edit(&[moved, demand], 1).unwrap(); + sub.edit(&[moved, demand], 0, 1, None).unwrap(); assert_eq!(sub.fill_range(2), None, "retired"); assert!(sub.fill_ids.contains(&2), "still a fill's"); sub.started = Some(Some(4)); - sub.edit(&[moved, demand], 1).unwrap(); + sub.edit(&[moved, demand], 0, 1, None).unwrap(); assert!(!sub.fill_ids.contains(&2), "forgotten once started"); } @@ -4041,6 +4047,7 @@ mod tests { live: lite::LiveRanges::new(Version::Lite07, &[first]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), started: Some(Some(4)), @@ -4061,7 +4068,7 @@ mod tests { start: Some(lite::Anchor::Relative(2)), end: None, }; - let (edit, changed) = sub.edit(&[first, back], 0).unwrap(); + let (edit, changed) = sub.edit(&[first, back], 0, 0, None).unwrap(); assert!(changed); assert_eq!( edit, @@ -4075,7 +4082,7 @@ mod tests { assert_eq!(sub.owed(None), Some(vec![4..10, 1..10])); // Retiring the first range forgets its start. - sub.edit(&[back], 0).unwrap(); + sub.edit(&[back], 0, 0, None).unwrap(); assert_eq!(sub.starts.keys().copied().collect::>(), [1]); assert_eq!(sub.owed(Some(8)), Some(vec![1..8])); } @@ -5563,6 +5570,7 @@ mod tests { live: lite::LiveRanges::new(version, &[lite::Range::OLDEST]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: tail.clone(), started: None, @@ -7351,6 +7359,7 @@ mod tests { subscription: Subscription::default(), demand: Some(Subscription::default()), demand_ranges: vec![lite::Range::OLDEST], + asks: 0, ranges: vec![lite::Range::OLDEST], }; let establish = serve.prepare_establish(&mut serving.serving, wire, &Default::default()); @@ -7517,6 +7526,7 @@ mod tests { subscription: Subscription::default(), demand: Some(Subscription::default()), demand_ranges: vec![lite::Range::OLDEST], + asks: 0, ranges: vec![lite::Range::OLDEST], }; let reader = serving.serving.subscribe(None); @@ -7582,6 +7592,7 @@ mod tests { subscription: Subscription::default(), demand: Some(Subscription::default()), demand_ranges: vec![lite::Range::OLDEST], + asks: 0, ranges: vec![lite::Range::OLDEST], }; let establish = serve.prepare_establish(&mut serving.serving, wire, &Default::default()); @@ -10070,6 +10081,10 @@ struct SubStream { /// and until SUBSCRIBE_START even once retired, since it answers every range the /// SUBSCRIBE carried. fill_ids: std::collections::BTreeSet, + /// The Range IDs of the asks' ranges: a relative range a reader of the copy waits on the + /// publisher to start (see [`track::Producer::asks`]), which says nothing of the live feed + /// either. Kept like [`Self::fill_ids`]. + ask_ids: std::collections::BTreeSet, /// The groups the ranges ask for fills. fill_groups: rangemap::RangeSet, priority: i8, @@ -10130,7 +10145,7 @@ impl SubStream { return None; } let mut start: Option = None; - for (id, _) in self.live.iter().filter(|(id, _)| !self.fill_ids.contains(id)) { + for (id, _) in self.live.iter().filter(|(id, _)| self.feeds(*id)) { match self.starts.get(&id) { // A range with no group there serves nothing to start from. Some(Some(at)) => start = Some(start.map_or(*at, |start| start.min(*at))), @@ -10292,33 +10307,78 @@ impl SubStream { } } - /// Edit the live ranges into `ranges`, the last `fills` of which are the held fills', - /// forgetting the starts of those retired. A fill asking for what a demand range does - /// still takes a range of its own, so each Range ID stays the demand's or a fill's. + /// Whether the live range `id` is the demand's, which alone says where the live feed is. + fn feeds(&self, id: u64) -> bool { + !self.fill_ids.contains(&id) && !self.ask_ids.contains(&id) + } + + /// Edit the live ranges into `ranges`: the demand's, then `asks` asks', then `fills` held + /// fills', forgetting the starts of those retired. A fill or ask asking for what a demand + /// range does still takes a range of its own, so each Range ID stays one owner's. An ask + /// its publisher answered counting from before `latest`, the copy's latest group, is + /// asked again: a reader waiting on it joined since. /// Returns the edit, and whether it changed anything. - fn edit(&mut self, ranges: &[lite::Range], fills: usize) -> Result<(lite::RangeEdit, bool), Error> { + fn edit( + &mut self, + ranges: &[lite::Range], + asks: usize, + fills: usize, + latest: Option, + ) -> Result<(lite::RangeEdit, bool), Error> { let before: Vec<_> = self.live.iter().map(|(id, _)| id).collect(); - let demand = ranges.len().saturating_sub(fills); + let demand = ranges.len().saturating_sub(asks + fills); let keyed: Vec<_> = ranges .iter() .enumerate() - .map(|(index, range)| (index >= demand, *range)) + .map(|(index, range)| { + let owner = match index { + index if index < demand => Owner::Demand, + index if index < demand + asks => Owner::Ask, + _ => Owner::Fill, + }; + (owner, *range) + }) .collect(); - let fill_ids = &self.fill_ids; - let (edit, added) = self.live.edit_keyed(&keyed, |id| fill_ids.contains(&id))?; + let (fill_ids, ask_ids, starts, edges) = (&self.fill_ids, &self.ask_ids, &self.starts, &self.edges); + let owner = |id| match () { + _ if fill_ids.contains(&id) => Owner::Fill, + _ if ask_ids.contains(&id) && starts.contains_key(&id) && edges.get(&id).is_some_and(|edge| *edge < latest) => { + Owner::Stale + } + _ if ask_ids.contains(&id) => Owner::Ask, + _ => Owner::Demand, + }; + let (edit, added) = self.live.edit_keyed(&keyed, owner)?; let changed = !self.live.iter().map(|(id, _)| id).eq(before); let live = &self.live; self.starts.retain(|id, _| live.get(*id).is_some()); self.edges.retain(|id, _| live.get(*id).is_some()); if self.started.is_some() { self.fill_ids.retain(|id| live.get(*id).is_some()); + self.ask_ids.retain(|id| live.get(*id).is_some()); + } + for (id, owner) in added { + match owner { + Owner::Fill => self.fill_ids.insert(id), + Owner::Ask => self.ask_ids.insert(id), + Owner::Demand | Owner::Stale => false, + }; } - self.fill_ids - .extend(added.into_iter().filter(|(_, fill)| *fill).map(|(id, _)| id)); Ok((edit, changed)) } } +/// Who a live range on a lite-07 subscription asks for. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +enum Owner { + Demand, + /// A reader waiting on the publisher to start a relative range. + Ask, + /// An ask answered before the copy's latest group, which no wanted range keeps. + Stale, + Fill, +} + #[expect( clippy::large_enum_variant, reason = "one per track copy, which holds a live stream while anyone reads it, so boxing it would add an allocation per upstream subscription without saving memory" @@ -10711,6 +10771,17 @@ impl TrackServe { (pref, _) => pref, }; let request = self.wire_request(pref, producer.idle_newest().filter(|_| opening)); + // The relative ranges readers wait on the publisher to start, each asked as a range of + // its own (see `Owner::Ask`), alongside a demand that covers their groups. + let mut asks = match (ranged, &request) { + (true, Some(_)) => { + wire_ranges(&Subscription::default().with_ranges(producer.asks())).unwrap_or_default() + } + _ => Vec::new(), + }; + // A relative end goes upstream open, so two asks may carry the same range. + let mut seen = std::collections::HashSet::new(); + asks.retain(|range| seen.insert(*range)); // The demand's ranges and the fills' groups, widened together until they fit. let (request, groups) = match ranged { true => { @@ -10722,9 +10793,11 @@ impl TrackServe { Some((demand, ranges)) => (Some(demand), ranges), None => (None, Vec::new()), }; - let count = demand_ranges.len() + fills.groups.iter().count(); + let count = demand_ranges.len() + asks.len() + fills.groups.iter().count(); let fields = self.subscribe_fields(demand.as_ref().unwrap_or(&fills.subscription())); - let budget = fit::Budget::new(self.subscriber.version, fields, next, count); + let mut budget = fit::Budget::new(self.subscriber.version, fields, next, count); + // The asks that don't fit wait for those sent to be answered, which retires them. + asks = budget.take(asks); let (demand_ranges, groups) = fit::fit(demand_ranges, &fills.groups, &range_fills.avoid(), budget)?; // A fill's range starting past its run speaks only while that run is asked whole. range_fills.retire_skipped(&groups); @@ -10736,16 +10809,18 @@ impl TrackServe { true => &range_fills.ask, false => &none, }; - // What goes on the wire: the demand's ranges, then the fills', under the demand's - // parameters. The demand alone sets the floor. + // What goes on the wire: the demand's ranges, the asks', then the fills', under the + // demand's parameters. The demand alone sets the floor. let request = match request { Some((demand, demand_ranges)) => { let mut ranges = demand_ranges.clone(); + ranges.extend(asks.iter().copied()); ranges.extend(FillAsk::wire(&groups)); Some(Wire { subscription: fills.merge(demand.clone()), demand: Some(demand), demand_ranges, + asks: asks.len(), ranges, }) } @@ -10754,6 +10829,7 @@ impl TrackServe { subscription: fills.subscription(), demand: None, demand_ranges: Vec::new(), + asks: 0, ranges: FillAsk::wire(&groups).collect(), }), }; @@ -10772,6 +10848,7 @@ impl TrackServe { subscription, demand, demand_ranges, + asks, ranges, } = wire; // Downstream preferences changed: forward them upstream as a @@ -10791,7 +10868,8 @@ impl TrackServe { // So do changed ranges that keep the floor, such as a widening that joins // two: each gap they reach waits a fresh grace, which only delays giving up // on one still owed. - let (edit, changed) = active.edit(&ranges, ranges.len() - demand_ranges.len())?; + let fills = ranges.len() - demand_ranges.len() - asks; + let (edit, changed) = active.edit(&ranges, asks, fills, producer.latest_live())?; let widened = (changed && demand.is_some()) .then(|| subscription.start().or(active.started.flatten()).unwrap_or(0)..u64::MAX); if let Ok(mut tail) = active.tail.write() { @@ -10951,6 +11029,7 @@ impl TrackServe { subscription, demand, demand_ranges, + asks, ranges, } = wire; // One asking only for fills is no live feed, so it leaves the floor alone. @@ -10968,6 +11047,7 @@ impl TrackServe { ); establish.demand = demand; establish.demand_ranges = demand_ranges; + establish.asks = asks; establish.fill_groups = fills.clone(); establish } @@ -11037,6 +11117,7 @@ impl TrackServe { id, demand: Some(subscription.clone()), demand_ranges: ranges.clone(), + asks: 0, fill_groups: Default::default(), fills_changed: false, subscription, @@ -11064,6 +11145,7 @@ impl TrackServe { subscription: subscription.clone(), demand: Some(subscription), demand_ranges: ranges.clone(), + asks: 0, ranges, }; let mut est = Box::new(self.prepare_establish(producer, wire, &Default::default())); @@ -11146,6 +11228,8 @@ struct Wire { subscription: Subscription, demand: Option, demand_ranges: Vec, + /// How many asks follow the demand's ranges in `ranges`, before the fills'. + asks: usize, ranges: Vec, } @@ -11167,11 +11251,13 @@ struct Establish { demand: Option, /// The demand's ranges, which come first on the wire. demand_ranges: Vec, + /// How many asks follow them, before the fills' ranges. + asks: usize, /// Fill preferences changed while opening and need an update once active. fills_changed: bool, /// The groups it asks for fills. fill_groups: rangemap::RangeSet, - /// The ranges the SUBSCRIBE carries: the demand's, then the fills'. + /// The ranges the SUBSCRIBE carries: the demand's, the asks', then the fills'. ranges: Vec, tail: kio::Producer, /// Its group streams stay parked once it is active; see [`SubStream::parked`]. @@ -11250,8 +11336,9 @@ impl Establish { start: self.subscription.start(), live: lite::LiveRanges::new(self.serve.subscriber.version, &self.ranges), demand: self.demand.clone(), - // The ranges take Range IDs in order, the demand's first. - fill_ids: (self.demand_ranges.len() as u64..self.ranges.len() as u64).collect(), + // The ranges take Range IDs in order: the demand's, the asks', then the fills'. + ask_ids: (self.demand_ranges.len() as u64..(self.demand_ranges.len() + self.asks) as u64).collect(), + fill_ids: ((self.demand_ranges.len() + self.asks) as u64..self.ranges.len() as u64).collect(), fill_groups: self.fill_groups.clone(), priority: self.subscription.priority, order: self.subscription.order, @@ -12524,8 +12611,8 @@ impl ServeLoop { } self.range_fills.skip(groups, skipped); } - // A fill an update retired since says nothing of the live feed either. - None if active.fill_ids.contains(&id) => {} + // Nor does an ask, or a fill an update retired since. + None if !active.feeds(id) => {} // Only the demand's ranges say where the live feed is served from. No range // starting anywhere leaves nothing owed. None => { @@ -12546,9 +12633,10 @@ impl ServeLoop { { self.serving.report_edge(largest.position.group); } - // Only a live fill's Range ID matters from here. + // Only a live fill's or ask's Range ID matters from here. let live = &active.live; active.fill_ids.retain(|id| live.get(*id).is_some()); + active.ask_ids.retain(|id| live.get(*id).is_some()); // One asking only for fills feeds nothing live. if active.demand.is_some() && serve.subscriber.version.has_largest() { // A bounded feed that never delivers Largest goes live at the @@ -12935,6 +13023,13 @@ impl ServeLoop { Poll::Ready(Err(err)) => return Poll::Ready(ServeEnd::GiveBack(err)), Poll::Pending => {} } + // A reader waiting on the publisher to start a relative range asks upstream. + if serve.subscriber.version.has_ranges() && self.serving.poll_asks_changed(waiter).is_ready() { + if let Err(err) = self.begin(serve, self.serving.subscription()) { + return Poll::Ready(ServeEnd::GiveBack(err)); + } + continue; + } // The wanted group is subscription state: an update announces each new one // past our largest, and every later update restates it. Joins asking for diff --git a/rs/moq-net/src/lite/subscriber/fit.rs b/rs/moq-net/src/lite/subscriber/fit.rs index 9561d91302..b04fa0e615 100644 --- a/rs/moq-net/src/lite/subscriber/fit.rs +++ b/rs/moq-net/src/lite/subscriber/fit.rs @@ -49,6 +49,23 @@ impl Budget { } } + /// The longest prefix of `ranges` fitting half of what is left, which it then takes: the + /// other half stays for the demand and fills, which [`fit`] can only widen so far. + pub fn take(&mut self, ranges: Vec) -> Vec { + let room = self.bytes / 2; + let mut used = 0; + let mut taken = Vec::new(); + for range in ranges { + match self.cost(&range).map(|cost| used + cost) { + Some(next) if next <= room => used = next, + _ => break, + } + taken.push(range); + } + self.bytes -= used; + taken + } + /// What `range` costs, or `None` when it can't go out. fn cost(&self, range: &Range) -> Option { Some(lite::range_size(range, self.form)? + self.id) diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index a51108637f..d3e0793432 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -83,11 +83,11 @@ pub(in crate::model) trait Holdings { fn back(&self, latest: u64, back: u64) -> Option; /// Where a range counting `back` groups before `latest`, up to the exclusive `end`, starts, - /// which a relative start, but never an end, takes over [`Self::back`]: where the publisher - /// reported starting it, - /// since a delivery start may sit past the group counted, such as above groups too stale - /// for the budget, or [`Containing::Unknown`] while a received copy can count it neither - /// that way nor over its own groups yet. `None` counts it with [`Self::back`]. + /// which a relative start, but never an end, takes over [`Self::back`]: on a received copy, + /// where its publisher reported starting one counted from `latest` or a newer group, since + /// the copy holds only part of the track and a delivery start may sit past the group + /// counted, or [`Containing::Unknown`] until it reports one. `None` counts it with + /// [`Self::back`]. fn counted_start(&self, latest: u64, back: u64, end: Option) -> Option { let _ = (latest, back, end); None @@ -193,10 +193,10 @@ pub(in crate::model) struct Window { /// The ranges of a live join whose reach back waits on the groups held to measure the /// live edge, which read nothing meanwhile: a group held is no reason to start at it. reaching: BTreeSet, - /// The ranges whose relative anchor the groups held can't count back to yet (see - /// [`Holdings::back`]), which read nothing meanwhile: a group held may sit below where - /// the publisher counted to. - counting: BTreeSet, + /// The ranges whose relative start waits on its publisher to say where it counted back to + /// (see [`Holdings::counted_start`]), each with the latest group it counted from then, + /// which read nothing meanwhile: a group held may sit below where the publisher started. + counting: BTreeMap, /// The groups the spans cover, shared with what reads them. groups: Cover, /// The part of `groups` a miss may ask for: see [`Self::missable`]. @@ -268,7 +268,7 @@ impl Window { waiting: BTreeSet::new(), waited: None, reaching: BTreeSet::new(), - counting: BTreeSet::new(), + counting: BTreeMap::new(), groups: Cover::default(), missable: Cover::default(), covered: None, @@ -349,11 +349,17 @@ impl Window { self.waiting.last().is_some_and(|&(time, _)| time > floor) } - /// Whether a relative anchor still waits on the groups held to count back to it. + /// Whether a relative start still waits on its publisher to say where it starts. pub fn is_counting(&self) -> bool { !self.counting.is_empty() } + /// The ranges whose relative start waits on its publisher to say where it starts, which a + /// received copy asks its upstream for. + pub fn counting(&self) -> impl Iterator + '_ { + self.counting.keys().map(|key| self.ranges[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 { @@ -375,12 +381,12 @@ impl Window { } moved = self.spans.len() > placed; } - // Counted from the latest group now, as an edit placing them now would: the publisher - // counts from its own latest when the range reaches it. - if !self.counting.is_empty() && latest.is_some() { + // Each from the latest group it first counted from: a report counted from that one or + // a newer one starts it. + if !self.counting.is_empty() { let placed = self.spans.len(); - for key in std::mem::take(&mut self.counting) { - self.place_range(key, latest, holdings); + for (key, since) in std::mem::take(&mut self.counting) { + self.place_range(key, Some(since), holdings); } moved |= self.spans.len() > placed; } @@ -519,11 +525,12 @@ impl Window { } /// The keys of the ranges not yet settled, which keep [`Self::is_settled`] false: waiting - /// on a first group, on the groups held to place or count back to a start, or on a lookup. Costs those. + /// on a first group, on the groups held to place a start, on a publisher to say where a + /// relative one starts, or on a lookup. Costs those. pub fn unsettled(&self) -> BTreeSet { let mut keys = self.pending.clone(); keys.extend(self.reaching.iter().copied()); - keys.extend(self.counting.iter().copied()); + keys.extend(self.counting.keys().copied()); keys.extend(self.waiting.iter().map(|(_, key)| *key)); keys.extend(self.looking.iter().map(|(_, key)| *key)); keys @@ -709,8 +716,8 @@ impl Window { self.pending.insert(key); return; } - Start::Counting => { - self.counting.insert(key); + Start::Counting(since) => { + self.counting.insert(key, since); return; } // Reads only the groups from its time until the groups held place it; see @@ -732,8 +739,8 @@ impl Window { self.count(key, Span::new(anchor, start, end, floored, looking), holdings); } - /// Whether a time start, a join's reach back, or a relative anchor waits on the groups - /// held to place it. + /// Whether a time start or a join's reach back waits on the groups held to place it, or a + /// relative start on its publisher. fn unplaced(&self) -> bool { !self.waiting.is_empty() || !self.reaching.is_empty() || !self.counting.is_empty() } @@ -894,8 +901,9 @@ enum Start { Waiting, /// Waiting on a latest group, reading everything meanwhile. Pending, - /// A relative start the groups held can't count back to yet, reading nothing meanwhile. - Counting, + /// A relative start counted from this latest group that its publisher has yet to say + /// where it starts, reading nothing meanwhile. + Counting(u64), } /// Where `anchor` starts a range ending at the exclusive `end`. @@ -913,7 +921,7 @@ fn start(anchor: Anchor, latest: Option, end: Option, holdings: &impl Containing::Group(group) => Start::At(group, true), // Fewer groups are held than it counts back, so reach back to the oldest. Containing::Before => Start::At(0, false), - Containing::Unknown => Start::Counting, + Containing::Unknown => Start::Counting(latest), }, }, // No group starts before 0, so time 0 names group 0 whatever is held. @@ -989,6 +997,8 @@ mod tests { dropped: u64, /// What a relative start counts back to, when not over the groups held. counted: Option, + /// The latest group the last relative start was counted from. + counted_from: std::cell::Cell>, } impl Keys { @@ -1035,7 +1045,8 @@ mod tests { self.held.range(..latest).rev().nth(back as usize).copied() } - fn counted_start(&self, _: u64, _: u64, _: Option) -> Option { + fn counted_start(&self, latest: u64, _: u64, _: Option) -> Option { + self.counted_from.set(Some(latest)); self.counted } @@ -1280,9 +1291,10 @@ mod tests { } } - /// A relative start the groups held can't count yet reads nothing, rather than every group - /// as one waiting on a latest group does, and starts once they can, counted from the - /// latest group then. An end still counts over the groups held. + /// A relative start its publisher has yet to say where it starts reads nothing, rather than + /// every group as one waiting on a latest group does, and starts once it says, counted + /// from the latest group it first counted from, however far the live edge moved since. + /// An end still counts over the groups held. #[test] fn a_relative_start_reads_nothing_until_counted() { let mut keys = Keys::new([1, 2, 9]); @@ -1297,12 +1309,9 @@ mod tests { ); assert_eq!(window.resolved_key(0), None); - // Nothing to count from while the latest group is unknown. keys.counted = Some(Containing::Group(7)); - assert!(!window.settle(None, &keys)); - assert!(window.is_counting()); - - assert!(window.settle(Some(9), &keys)); + assert!(window.settle(Some(12), &keys)); + assert_eq!(keys.counted_from.get(), Some(9), "from where it first counted"); assert!(window.is_settled() && !window.is_counting()); assert_eq!(covered(&window, 0..10), vec![1, 7, 8]); assert_eq!(window.start(), Some(1)); @@ -1314,7 +1323,9 @@ mod tests { .edit([], &[(2, range(Anchor::Relative(3), None))], Some(9), &keys) .unwrap(); assert!(window.is_counting()); + assert_eq!(window.counting().collect::>(), [range(Anchor::Relative(3), None)]); window.edit([2], &[], Some(9), &keys).unwrap(); + assert_eq!(window.counting().count(), 0); assert!(window.is_settled()); // Counting back past every group the track holds reaches back to the oldest. diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index e635c18921..08864f6fe2 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -30,7 +30,7 @@ use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; mod demand; pub(crate) use demand::Edit; pub(super) use demand::Preferences; -use demand::Subscriptions; +use demand::{Asked, Subscriptions}; mod fill; mod hole; @@ -600,7 +600,7 @@ enum Anchors { /// time anchors as group starts, so they resolve here as at the publisher. Wire, /// A [`Self::Wire`] copy whose publisher also reports where each range starts (lite-07), - /// so a relative anchor the copy can't count back over its own groups waits for that. + /// so a relative start waits for that report rather than count over the groups held. Reported, } @@ -745,10 +745,6 @@ pub(crate) struct TrackState { // group above it is checked against it as well as its cached neighbors; one at or below // it may have lost its lower bound. One entry, so nothing grows with the cache. evicted: Option<(u64, Timestamp)>, - // The highest key of any group that left the cache for good (evicted or reclaimed, not - // displaced by a copy that holds or sends it again), checked start or not: a received copy - // counting a relative start back can't tell a gap at or below it from one it never held. - uncached: Option, // The key of the newest cached group: the live edge, protected from // eviction by never entering the eviction order until no producer remains. @@ -1232,6 +1228,11 @@ impl Holdings for TrackState { } fn back(&self, latest: u64, back: u64) -> Option { + // Keys that are group IDs count back by ID, as a moq-transport joining FETCH does, so + // the count never depends on what this copy holds. + if self.starts_at_keys { + return Some(latest.saturating_sub(back)); + } let Some(back) = back.checked_sub(1) else { return Some(latest); }; @@ -1240,25 +1241,27 @@ impl Holdings for TrackState { self.lookup.range(..latest).rev().nth(back).map(|(&key, _)| key) } - /// A received copy starts where its publisher did, counting from the same latest group: a - /// newer one moved the live edge, which the copy then counts from itself. A lite-07 copy - /// that may still learn where, from a report or the groups between, counts only where it - /// holds them (see [`Self::counted_received`]); once it can't, over the groups held. + /// A lite-07 copy holds only part of the track, so it starts only where its publisher + /// reported starting, counting from the same latest group or a newer one, and waits for + /// that report otherwise, whatever it holds: a reader asks its upstream for one (see + /// [`Producer::poll_asks_changed`]). The latest group itself needs no count. fn counted_start(&self, latest: u64, back: u64, end: Option) -> Option { + if self.anchors != Anchors::Reported { + return None; + } + if back == 0 { + return Some(Containing::Group(latest)); + } let reported = self .relative_starts .get(&back) .and_then(|reported| match reported.start { - CountedStart::At(start) => (reported.latest == latest).then_some(start), + CountedStart::At(start) => (reported.latest >= latest).then_some(start), // Past a range ending no earlier than this one, so this one holds no group either. - // Counting from a newer latest group only starts later, so it holds there too. - CountedStart::Past(past) => end.filter(|end| reported.latest <= latest && *end <= past), - }) - .map(Containing::Group); - // Nothing more arrives once the copy is released, complete, or unfed, so no report can - // settle it either. - let learns = self.anchors == Anchors::Reported && !self.outcome.is_released() && !self.nothing_arrives(); - reported.or_else(|| learns.then(|| self.counted_received(latest, back)).flatten()) + // Counting from another latest group starts no earlier, so it holds there too. + CountedStart::Past(past) => end.filter(|end| *end <= past), + }); + Some(reported.map_or(Containing::Unknown, Containing::Group)) } fn complete(&self) -> bool { @@ -1375,44 +1378,6 @@ impl TrackState { } } - /// The group `back` groups before `latest` on a received copy, counted only through the - /// groups it holds and the starts its publisher declared skipped between them: any other - /// gap may hold groups the copy never saw, such as one below what a first reader asked - /// for, so the count is [`Containing::Unknown`] there, unless a range from group 0 - /// started above it. A gap the cache may have made is one - /// no report or arrival fills, so it is `None` there, to count over the groups held as a - /// publisher that let them go does. Each step lands on a group held, so a huge count walks - /// the cache once. - fn counted_received(&self, latest: u64, back: u64) -> Option { - let mut at = latest; - for _ in 0..back { - let Some(below) = at.checked_sub(1) else { - return Some(Containing::Before); - }; - let below = match self.drops.get_key_value(&below) { - Some((run, Hole::Skip)) => match run.start.checked_sub(1) { - Some(below) => below, - // Skipped down to 0: no group sits below. - None => return Some(Containing::Before), - }, - _ => below, - }; - if !self.lookup.contains_key(&below) { - // The publisher delivers nothing below where a range from group 0 started, so - // a count reaching below it starts there. - if let Some(&start) = self.time_starts.get(&0) - && below < start - { - return Some(Containing::Group(start)); - } - let uncached = self.uncached.is_some_and(|uncached| below <= uncached); - return (!uncached).then_some(Containing::Unknown); - } - at = below; - } - Some(Containing::Group(at)) - } - /// 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) @@ -1576,7 +1541,6 @@ impl TrackState { /// it for the readers that judged it cached to judge again. fn evict_group(&mut self, key: u64, err: Error) { let slot = self.uncache(key).expect("a cached group"); - self.let_go(key); slot.abort(err); let landed = self .inboxes @@ -1588,12 +1552,6 @@ impl TrackState { } } - /// Record that group `key` left the cache for good, with nothing holding or sending it - /// again: a gap at or below it may be one the cache made. - fn let_go(&mut self, key: u64) { - self.uncached = self.uncached.max(Some(key)); - } - /// [`Self::uncache`] a group that another copy displaces rather than one that ends, so a /// teardown still ends the copies its readers hold. fn displace(&mut self, key: u64) { @@ -1912,11 +1870,7 @@ impl TrackState { /// Remove an aborted slot, keeping a cut as a drop: reclaiming it must not erase a /// disposition a relay still has to forward. fn reclaim_aborted(&mut self, key: u64) { - let slot = self.uncache(key); - if slot.is_some() { - self.let_go(key); - } - if let Some(slot) = slot + if let Some(slot) = self.uncache(key) && slot.visible && let Some(err) = slot.verdict() && let Some(end) = key.checked_add(1) @@ -4502,6 +4456,8 @@ pub struct Producer { broadcast: Arc, state: kio::Producer, prev_subscription: Option, + // The revision of the asks [`Self::poll_asks_changed`] last reported. + asks_seen: u64, // Shared with every clone and every `Requests`: its `Drop` is the teardown. alive: Arc, } @@ -4538,6 +4494,7 @@ impl Producer { state, broadcast, prev_subscription: None, + asks_seen: 0, alive, } } @@ -4897,7 +4854,7 @@ impl Producer { } /// [`Self::anchored`], for a wire whose publisher also reports where each range starts: - /// a relative anchor the copy can't count back over its own groups waits for that report. + /// a relative start waits for that report, which its reader asks for. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn reported(&self) { self.set_anchors(Anchors::Reported); @@ -4940,7 +4897,8 @@ impl Producer { /// Record where the upstream publisher started each of this lite-07 copy's live wire /// ranges counting back, as `(back, start, latest)` triples, replacing the last set. A - /// reader's relative start counting back `back` groups from `latest` resolves to `start`. + /// reader's relative start counting back `back` groups from `latest`, or from an older + /// group, resolves to `start`. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn relative_starts(&self, starts: impl IntoIterator) { let mut merged = BTreeMap::::new(); @@ -5588,6 +5546,23 @@ impl Producer { poll_combined_changed(&subs, bound, &mut self.prev_subscription, waiter).map(Ok) } + /// Ready once a reader of this lite-07 copy starts or stops waiting on its publisher to + /// start a relative range (see [`Self::asks`]): its session asks upstream for each as a + /// range of its own, so the publisher reports where it starts. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn poll_asks_changed(&mut self, waiter: &kio::Waiter) -> Poll<()> { + let subs = self.state.read().subscriptions.clone(); + subs.lock().poll_asks(&mut self.asks_seen, waiter) + } + + /// The relative ranges this copy's readers wait on its publisher to start; see + /// [`Self::poll_asks_changed`]. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn asks(&self) -> Vec { + let subs = self.state.read().subscriptions.clone(); + subs.lock().asks() + } + /// Count `seen` as the aggregate already acted on, so /// [`Self::poll_subscription_changed`] reports only a change from it: the demand a /// subscriber session asked upstream for before accepting the track, or `None` @@ -8616,6 +8591,9 @@ struct Cursor { cache: Arc, subscription: Preferences, + /// The relative ranges its window waits on the publisher to start, which a received + /// copy's session asks upstream for. + asked: Asked, /// Arrival-order cursor used by `recv_group`. index: usize, /// Minimum key to return from any `recv` method. Set by `set_range`. @@ -9026,10 +9004,13 @@ impl Cursor { }; let live = Arc::::default(); let published = Published::new(Servable::of(&filter.window, &live), filter.resolved, None); + let mut asked = subscription.asked(); + asked.set(filter.window.counting().collect()); Self { state, cache, subscription, + asked, min_key: 0, filter: Box::new(filter), index: 0, @@ -9230,6 +9211,7 @@ impl Cursor { self.filter.revision += 1; self.update_servable(); } + self.asked.set(self.filter.window.counting().collect()); moved } @@ -12434,6 +12416,7 @@ impl Request { broadcast: self.broadcast, state: self.state, prev_subscription: None, + asks_seen: 0, alive: self.alive, }), _dynamic: self._dynamic, @@ -16083,10 +16066,9 @@ mod test { assert_eq!(drain(&mut floored), vec![1000]); } - /// A lite-07 copy holds only part of the track, so a relative anchor counts back as its - /// publisher reported, or through the groups it holds and the starts skipped between them, - /// and otherwise reads nothing until one of those can count it. Once nothing more can - /// arrive, it counts over the groups held. + /// A lite-07 copy holds only part of the track, so a relative start counts back only as its + /// publisher reported, from the same latest group or a newer one, and reads nothing until + /// it does, whatever groups the copy holds or receives: its reader asks for that report. #[test] #[cfg(feature = "async")] fn a_received_copy_counts_a_relative_start_back_as_its_publisher_did() { @@ -16115,9 +16097,12 @@ mod test { let mut reported = producer.subscribe(back(2)); assert_eq!(placed(&mut reported), Some(7), "the publisher's start, not group 1"); + let mut latest = producer.subscribe(back(0)); + assert_eq!(placed(&mut latest), Some(9), "the latest group needs no count"); let mut unreported = producer.subscribe(back(1)); assert_eq!(placed(&mut unreported), None, "group 8 may sit below 9"); assert_eq!(read(&mut unreported), Vec::::new(), "reads nothing meanwhile"); + assert_eq!(producer.asks(), back(1).ranges, "and asks for a report"); // A reported start is where delivery began, which may sit past the group counted, and // nothing reports an end, so a relative end counts over the groups held. let mut ended = producer.subscribe( @@ -16130,22 +16115,19 @@ mod test { ); assert_eq!(ended.resolved_range(0).and_then(|resolved| resolved.end), Some(2)); + // Holding every group in between counts nothing either. write_group(&producer, 8); - assert_eq!(placed(&mut unreported), Some(8), "counted through the groups held"); - assert_eq!(read(&mut unreported), vec![8, 9]); - write_group(&producer, 7); - - // Starts the publisher skipped hold no group to count. - producer.skip_groups(3..7); - let mut skipped = producer.subscribe(back(3)); - assert_eq!(placed(&mut skipped), Some(2)); - let mut gap = producer.subscribe(back(6)); - assert_eq!(placed(&mut gap), None, "group 0 may sit below 1"); + assert_eq!(placed(&mut unreported), None, "holding group 8 says nothing"); - // A newer group moved the live edge, so a later reader counts back from it. + // A report counted from a newer latest group starts it, as the publisher placed it then. write_group(&producer, 10); + producer.relative_starts([(2, CountedStart::At(7), 9), (1, CountedStart::At(9), 10)]); + assert_eq!(placed(&mut unreported), Some(9)); + assert_eq!(read(&mut unreported), vec![9, 10]); + assert!(producer.asks().is_empty(), "answered, so asked no more"); + // But one from an older latest group says nothing of a reader counting from a newer one. let mut later = producer.subscribe(back(2)); - assert_eq!(placed(&mut later), Some(8)); + assert_eq!(placed(&mut later), None); assert_eq!( placed(&mut reported), Some(7), @@ -16169,59 +16151,52 @@ mod test { Some((9, Some(9))) ); let mut longer = producer.subscribe(until(10)); - assert_eq!(placed(&mut longer), Some(1), "counted through the groups held instead"); + assert_eq!(placed(&mut longer), None); - // Counting from a newer latest group only starts later, so the empty report still holds, - // where the groups held would count back to group 2. + // Counting from a newer latest group only starts later, so the empty report still holds. write_group(&producer, 11); let mut newer = producer.subscribe(until(8)); assert_eq!( newer.resolved_range(0).map(|resolved| (resolved.start, resolved.end)), Some((9, Some(9))) ); - assert_eq!(placed(&mut gap), Some(1), "counted from 11 through the groups held"); - let mut deeper = producer.subscribe(back(7)); - assert_eq!(placed(&mut deeper), None, "group 0 may sit below 1"); - // Nothing more can arrive once the copy reaches its end, though its producer is still - // held, so it counts over the groups held, fewer than it asks: it reaches back to the - // oldest. + // Nothing counts over the groups held once the copy ends either. let _finished = producer.finish().unwrap(); - assert_eq!(placed(&mut deeper), Some(0)); - assert_eq!(read(&mut deeper), vec![1, 2, 7, 8, 9, 10, 11]); + assert_eq!(placed(&mut later), None); } - /// A group an idle copy displaces is sent again by the next feed, so the gap it leaves is - /// not one the cache made: a relative start counting into it still waits. - #[test] - #[cfg(feature = "async")] - fn a_received_copy_waits_on_a_gap_a_displaced_group_left() { - let mut producer = track_producer("test", Info::timed()); - producer.received(); - producer.reported(); - for key in [1, 2] { - write_group(&producer, key); + proptest::proptest! { + /// Whatever groups a lite-07 copy holds, a reader's relative start lands only where its + /// publisher reported it, or waits: the cache never moves it. + #[test] + fn a_copys_cache_never_moves_a_relative_start( + held in proptest::collection::btree_set(0u64..16, 0..12), + back in 0u64..6, + reported in proptest::option::of(0u64..17), + ) { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.reported(); + for key in held.iter().copied().chain([16]) { + write_group(&producer, key); + } + producer.report_edge(16); + if let Some(start) = reported { + producer.relative_starts([(back, CountedStart::At(start), 16)]); + } + let mut reader = producer.subscribe( + Subscription::default() + .with_ranges([Range { start: Anchor::Relative(back), end: None }]) + .with_delay(Delay::default().with_max(None)), + ); + let expected = match back { + 0 => Some(16), + _ => reported, + }; + proptest::prop_assert_eq!(reader.resolved_range(0).map(|resolved| resolved.start), expected); + proptest::prop_assert_eq!(producer.asks().is_empty(), expected.is_some()); } - let mut open = producer.create_group(group::Info { start: 9 }).unwrap(); - open.write_frame(Timestamp::from_millis(9).unwrap(), &b"x"[..]).unwrap(); - producer.report_edge(9); - producer.set_idle(); - producer.set_live(Some(Position::group(9)), None).unwrap(); - write_group(&producer, 9); - - let mut reader = producer.subscribe( - Subscription::default() - .with_ranges([Range { - start: Anchor::Relative(2), - end: Some(Anchor::Time(8)), - }]) - .with_delay(Delay::default().with_max(None)), - ); - assert_eq!( - reader.resolved_range(0).map(|resolved| resolved.start), - None, - "group 8 may sit below 9" - ); } /// Ranges counting back the same number of groups report together: a start holds for every @@ -16251,40 +16226,6 @@ mod test { ); } - /// A gap the cache made in a lite-07 copy is one no report or arrival fills, so a relative - /// start counting into it counts over the groups held, as a publisher that let them go does, - /// while a gap above everything the cache let go of still waits. - #[test] - #[cfg(feature = "async")] - fn a_received_copy_counts_a_relative_start_to_a_gap_the_cache_made() { - let back = |back| { - Subscription::default() - .with_ranges([Range { - start: Anchor::Relative(back), - end: None, - }]) - .with_delay(Delay::default().with_max(None)) - }; - let placed = |subscriber: &mut Subscriber| subscriber.resolved_range(0).map(|resolved| resolved.start); - let producer = track_producer("test", Info::timed()); - producer.received(); - producer.reported(); - for key in [1, 5, 8, 9] { - write_group(&producer, key); - } - producer.skip_groups(6..8); - producer.report_edge(9); - - evict(&producer, 1); - let mut unknown = producer.subscribe(back(3)); - assert_eq!(placed(&mut unknown), None, "groups the copy never held may sit below 5"); - - evict(&producer, 8); - let mut evicted = producer.subscribe(back(1)); - assert_eq!(placed(&mut evicted), Some(5), "group 5 is the one held before 9"); - assert_eq!(placed(&mut unknown), Some(0), "fewer are held than it counts"); - } - #[test] #[cfg(feature = "async")] fn a_late_lower_group_within_the_budget_is_delivered() { diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index 8c482dbe8b..2834e4fce3 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -20,6 +20,12 @@ pub(super) struct Subscriptions { // Only stated minimums count, so an unstated one never reads as zero. min_delay: BTreeMap, ranges: Ranges, + /// The relative ranges readers of a received copy wait on its publisher to say where they + /// start, counted: its session asks each upstream as a range of its own. + asks: BTreeMap, + /// Bumped whenever a reader starts or stops waiting on one of `asks`, even one another + /// reader already waits on: that reader may have joined after the last answer. + asks_revision: u64, descending: usize, /// Subscribers that need the live feed; see [`Subscription::feed`]. feed: usize, @@ -301,6 +307,33 @@ impl Subscriptions { pub(super) fn len(&self) -> usize { self.count } + + /// Count `came` and uncount `gone` among [`Self::asks`]. + fn ask(&mut self, gone: &[Range], came: &[Range]) -> kio::WaiterList { + for range in came { + add(&mut self.asks, *range); + } + for range in gone { + remove(&mut self.asks, *range); + } + self.asks_revision += 1; + self.waiters.take() + } + + /// Ready once a reader started or stopped waiting on one of [`Self::asks`] since `seen`. + pub(super) fn poll_asks(&mut self, seen: &mut u64, waiter: &kio::Waiter) -> Poll<()> { + if *seen == self.asks_revision { + waiter.register(&mut self.waiters); + return Poll::Pending; + } + *seen = self.asks_revision; + Poll::Ready(()) + } + + /// The ranges readers wait on their publisher to start. + pub(super) fn asks(&self) -> Vec { + self.asks.keys().copied().collect() + } } /// The ranges `old` holds more of than `new`, then those `new` holds more of than `old`. @@ -631,6 +664,14 @@ impl Preferences { Ok(()) } + /// What this subscriber's reader counts among the track's asks, none yet. + pub(in crate::model) fn asked(&self) -> Asked { + Asked { + subscriptions: self.0.subscriptions.clone(), + ranges: Vec::new(), + } + } + pub(in crate::model) fn same_channel(&self, other: &Self) -> bool { self.0.value.same_channel(&other.0.value) } @@ -648,6 +689,35 @@ impl Preferences { } } +/// The relative ranges one reader waits on its publisher to start, counted among the track's +/// asks while held. +pub(in crate::model) struct Asked { + subscriptions: Option>, + /// Sorted, so the same ranges compare equal however the reader keys them. + ranges: Vec, +} + +impl Asked { + /// Count `ranges` in place of those counted before. + pub(in crate::model) fn set(&mut self, mut ranges: Vec) { + ranges.sort_unstable(); + if ranges == self.ranges { + return; + } + let ranges = std::mem::replace(&mut self.ranges, ranges); + if let Some(subs) = &self.subscriptions { + let mut wakes = subs.lock().ask(&ranges, &self.ranges); + wakes.wake(); + } + } +} + +impl Drop for Asked { + fn drop(&mut self) { + self.set(Vec::new()); + } +} + impl Drop for Preference { fn drop(&mut self) { if let Some(subs) = &self.subscriptions { diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index 1dd7383268..45348a2015 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -4,8 +4,9 @@ //! the track is held, and on a copy received over a wire that tells it enough to resolve //! the anchor the same way. Every wire carries a time anchor, as the group ID with that key, //! but only lite-07 resolves a time start to the group containing it: an older wire's -//! publisher starts at the first group at or after it. A moq-transport copy resolves a -//! relative anchor from the Largest Object its live join reports. A copy received over any +//! publisher starts at the first group at or after it. A moq-transport copy counts a +//! relative anchor back by group ID from the Largest Object its live join reports. A copy +//! received over any //! other wire refuses a relative anchor other than the live start instead of resolving it //! against the part of the track it happens to hold. @@ -143,6 +144,17 @@ async fn each_wire_refuses_only_the_anchors_it_drops() { if let (false, Anchor::Time(start)) = (resolves, range.start) { held.retain(|&key| key >= start); } + // A moq-transport copy counts a relative anchor back by group ID, as a joining FETCH + // does, never over the groups it holds: these groups' IDs are their starts, a second + // apart, so two back from 4000 is 3998. + if transport { + if let Anchor::Relative(back) = range.start { + held.retain(|&key| key >= 4000 - back); + } + if let Some(Anchor::Relative(back)) = range.end { + held.retain(|&key| key <= 4000 - back); + } + } let mut result = read(version, range).await; // Any other wire carries time anchors and the live start, and drops the rest. let wire = matches!(range.start, Anchor::Time(_) | Anchor::Oldest | Anchor::Relative(0)) @@ -386,10 +398,11 @@ async fn a_later_relative_start_on_a_partial_copy_reads_only_its_groups() { let _track = track.finish().unwrap(); } -/// A copy reading a track from its oldest group learns where the publisher's groups start, so -/// a later reader counting back past them starts there, as the publisher does, rather than -/// wait on a group below that never arrives. Mirrors the `@moq/net` `integration: lite-07 -/// counts a later relative start back to where the oldest range started`. +/// A later reader's relative start on a copy reading a track from its oldest group, which +/// already delivers every group the reader asks for, still asks the publisher where it starts, +/// and starts there: counting back past group 1, at group 1. Mirrors the `@moq/net` +/// `integration: lite-07 counts a later relative start back to where the oldest range +/// started`. #[kio::test] async fn a_later_relative_start_counts_back_to_where_the_oldest_range_started() { let publisher = produce_origin(1); @@ -454,3 +467,92 @@ async fn a_later_relative_start_counts_back_to_where_the_oldest_range_started() drop(oldest); let _track = track.finish().unwrap(); } + +/// A relative start on a lite-07 copy never resolves against the groups the copy holds: over a +/// copy holding groups 9 and 10, a reader counting 5 back starts where the publisher counts +/// to, not at 9. While the subscription that delivered them is live, the copy asks it for the +/// reader's range, and the publisher counts back from its latest group, 10, to 5. Once it is +/// gone, the copy subscribes again and the reader waits for that answer, counted back from the +/// Largest it reports: 11, written while the copy was idle, so 6. Mirrors the `@moq/net` +/// `integration: lite-07 never resolves a relative start against the groups a copy holds`. +#[kio::test] +async fn a_relative_start_never_resolves_against_the_groups_a_copy_holds() { + for live in [true, false] { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track( + "data", + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER), + ) + .unwrap(); + broadcast.announce().unwrap(); + let write = |key: u64| { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group.write_frame(millis(key), &b"x"[..]).unwrap(); + group.finish().unwrap(); + }; + for key in 0..=10 { + write(key); + } + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + let subscribe = |ranges: Vec| { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges(ranges); + remote.track("data").unwrap().subscribe(subscription) + }; + let read = async |reader: &mut moq_net::track::Subscriber| { + let mut read = Vec::new(); + loop { + let group = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap(); + match group { + Some(group) => read.push(group.start), + None => break, + } + } + read.sort_unstable(); + read + }; + + // The copy holds groups 9 and 10. + let mut first = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Time(9), Some(Anchor::Time(10)))])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut first).await, [9, 10]); + let (expected, end) = match live { + true => (vec![5, 6, 7, 8, 9, 10], 10), + false => { + // Its upstream subscription leaves with its last reader, and the track moves on. + drop(first); + kio::clock::sleep(Duration::from_secs(1)).await; + write(11); + (vec![6, 7, 8, 9, 10, 11], 11) + } + }; + let mut later = kio::clock::timeout( + TIMEOUT, + subscribe(vec![range(Anchor::Relative(5), Some(Anchor::Time(end)))]), + ) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut later).await, expected, "live: {live}"); + let _track = track.finish().unwrap(); + } +} From 529020b20f85e9f7c049f972b27e46978547d8c8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 17:16:59 -0800 Subject: [PATCH 12/18] fix(net): a relative start on a lite-07 copy resolves only from its publisher's report in JS too Mirrors the Rust change: `countedStart` no longer walks the groups held or falls back to them once the copy is unfed or evicted. A reader no report answers asks the copy's upstream for its range (`hooks.asks`), which the lite subscriber sends as a range of its own. The lite-07 draft and the concept doc say a relay never resolves a relative start against the part of the track it holds. Co-Authored-By: Claude Opus 5.5 --- doc/concept/moq-lite.md | 13 +- drafts/draft-lcurley-moq-lite.md | 3 +- js/net/src/integration.test.ts | 68 ++++++++++ js/net/src/internal.ts | 27 +++- js/net/src/lite/fit.ts | 19 +++ js/net/src/lite/subscribe.test.ts | 4 +- js/net/src/lite/subscribe.ts | 45 ++++--- js/net/src/lite/subscriber.ts | 59 ++++++++- js/net/src/track.test.ts | 120 ++++++++++++------ js/net/src/track.ts | 119 ++++++++--------- js/net/src/window.test.ts | 18 ++- js/net/src/window.ts | 49 ++++--- quest/m2/time-addressing/late-relative-end.md | 20 +-- rs/moq-net/src/lite/subscriber.rs | 9 +- rs/moq-net/src/model/subscription/window.rs | 5 +- 15 files changed, 408 insertions(+), 170 deletions(-) diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index 38a72e4ff0..e1f409c564 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -329,11 +329,14 @@ A group is addressed by its start: where it starts in the track's timescale, or a bare number that grows with each group on a track without one. A time start is the group containing that time, the newest starting at or before it, and a time end includes the group containing it, so neither waits on a frame. -A relative anchor counts the groups the hop holds back from the latest one. It -resolves on the hop that publishes the track, and over moq-lite-07, which -carries it upstream and reports the live edge, so a copy resolves the ends -itself. A Rust moq-transport copy resolves a relative anchor from the Largest -Object its live join reports. Older moq-lite versions refuse relative anchors +A relative anchor counts the groups back from the latest one. It resolves on +the hop that publishes the track, and over moq-lite-07, which carries it +upstream and reports the live edge. A moq-lite-07 copy never counts a relative +start over the groups it happens to hold: it takes the start its publisher +reports for the same range, asking upstream for one when none has, so a reader +reads the same groups from a warm relay as from a cold one. A Rust +moq-transport copy counts a relative anchor back by group ID from the Largest +Object its live join reports, as a joining FETCH does. Older moq-lite versions refuse relative anchors other than the live and default starts. Older moq-lite versions and moq-transport carry a group ID equal to the start, so a time anchor goes upstream as that ID, where a start selects the first group at or after it diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index b1c1da4606..522f1d8bb3 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1530,7 +1530,8 @@ Which fields follow, and what the Anchor names: Any other value is a PROTOCOL_VIOLATION. Both ends include the Group they name, so a Range from Time `a` to Time `b` delivers every Group from the one containing `a` through the one containing `b`. -The publisher resolves a Relative or Time Anchor against the Track as it holds it. +The publisher resolves a Relative or Time Anchor against the Track as it holds it, except a Relative `Start` of one or more `Groups Back` on a relay, which never depends on the part of the Track the relay holds. +A relay resolves one to the `Range Start` its upstream reported for a Range with the same `Start`, counted from the same latest Group or a newer one: when none has, it asks its upstream for such a Range in its SUBSCRIBE or a SUBSCRIBE_UPDATE, and resolves the Range only once that answer arrives. A Range resolves once: an update that leaves it live does not resolve it again. A `Start` is a floor, not a request; `Subscriber Max Age` and `Subscriber Min Age` are the only things that ask for data. diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index 231b910319..44151ba2c1 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -582,6 +582,74 @@ test("integration: lite-07 counts a later relative start back to where the oldes } }); +// A relative start on a lite-07 copy never resolves against the groups the copy holds: over a copy +// holding groups 9 and 10, a reader counting 5 back starts where the publisher counts to, not at 9. +// While the subscription that delivered them is live, the copy asks it for the reader's range, and +// the publisher counts back from its latest group, 10, to 5. Once it is gone, the copy subscribes +// again and the reader waits for that answer, counted back from the Largest it reports: 11, written +// while the copy was idle, so 6. Mirrors the Rust +// `a_relative_start_never_resolves_against_the_groups_a_copy_holds`. +for (const live of [true, false]) { + test(`integration: lite-07 never resolves a relative start against the groups a copy holds (live: ${live})`, async () => { + const pair = createMockTransportPair(Lite.ALPN_07_WIP); + const origin = new OriginProducer(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + + const broadcast = publish(origin, Path.from("test")); + const producer = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + const write = (key: bigint) => { + const group = producer.createGroup(key); + group.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.fromMillis(Number(key)) }); + group.finish(); + }; + for (let key = 0n; key <= 10n; key++) write(key); + + const remote = wireOf(client).consume(Path.from("test")); + const subscribe = (ranges: TrackRange[]) => + remote.track("video").subscribe({ delay: { max: null }, order: "ascending", ranges }); + const read = async (reader: ReturnType) => { + const got: bigint[] = []; + for (;;) { + const group = await withTimeout(reader.recvGroup(), Duration.millis(2_000), "group timed out"); + if (!group) return got.sort((a, b) => Number(a - b)); + got.push(group.start); + } + }; + let later: ReturnType | undefined; + try { + // The copy holds groups 9 and 10. + const first = subscribe([{ start: { time: 9n }, end: { time: 10n } }]); + expect(await read(first)).toEqual([9n, 10n]); + let end = 10n; + if (!live) { + // Its upstream subscription leaves with its last reader, and the track moves on. + first.close(); + await withTimeout( + (async () => { + while (producer.subscription.peek() !== undefined) await yieldNow(); + })(), + Duration.millis(2_000), + "the upstream subscription never left", + ); + write(11n); + end = 11n; + } + later = subscribe([{ start: { relative: 5n }, end: { time: end } }]); + expect(await read(later)).toEqual(live ? [5n, 6n, 7n, 8n, 9n, 10n] : [6n, 7n, 8n, 9n, 10n, 11n]); + first.close(); + } finally { + later?.close(); + remote.close(); + broadcast.close(); + client.abort(); + server.abort(); + } + }); +} + // A publisher counts a relative start back from its newest group, which may hold no frame yet and // so counts neither in the largest it reports nor where the copy resolves the start. The copy reports // the publisher's start, where a reader waiting on each group in sequence starts. Mirrors the Rust diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 26b600e919..059a4f8d3a 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -27,6 +27,7 @@ import type { InfoInit, Position, Producer, + Range, Request, Subscriber, Subscription, @@ -760,16 +761,28 @@ export const hooks: { /** * 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 - * track, so a reader's relative start counts back as the publisher did, until a group past - * the `latest` it counted from moves the live edge. Assigned by `track.ts`. + * track, so a reader's relative start counting back from `latest`, or an older group, starts + * where the publisher did. Assigned by `track.ts`. */ relativeStarts: (producer: Producer, starts: Iterable) => void; /** * Mark a received copy whose publisher reports where each range starts (lite-07), so a reader's - * relative start the copy can't count back over its own groups waits for {@link relativeStarts}. - * Mirrors the Rust `Producer::reported`; assigned by `track.ts`. + * relative start waits for {@link relativeStarts} rather than count over the groups held, and + * asks for it (see {@link asks}). Mirrors the Rust `Producer::reported`; assigned by `track.ts`. */ reportsStarts: (producer: Producer) => void; + /** + * The relative ranges a lite-07 copy's readers wait on its publisher to start, which its + * subscription asks upstream for, each as a range of its own, so the publisher reports where it + * starts. Mirrors the Rust `Producer::asks`; assigned by `track.ts`. + */ + asks: (producer: Producer) => readonly Range[]; + /** + * Bumped whenever a reader starts or stops waiting on one of {@link asks}, even one another + * reader already waits on: that reader may have joined after the last answer. Mirrors the Rust + * `Producer::poll_asks_changed`; assigned by `track.ts`. + */ + asksChanged: (producer: Producer) => Getter; /** Mint a track {@link Request}; assigned by `track.ts`. */ makeRequest: (options: TrackRequestOptions) => Request; /** @@ -1191,6 +1204,12 @@ export const hooks: { reportsStarts: () => { throw new Error("track.ts not loaded"); }, + asks: () => { + throw new Error("track.ts not loaded"); + }, + asksChanged: () => { + throw new Error("track.ts not loaded"); + }, relativeStarts: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/fit.ts b/js/net/src/lite/fit.ts index 902357d58b..a853b7e4e9 100644 --- a/js/net/src/lite/fit.ts +++ b/js/net/src/lite/fit.ts @@ -43,6 +43,25 @@ export function budget(fields: number, next: bigint, count: number): Budget { }; } +/** + * The longest prefix of `ranges` fitting half of what `budget` has left, which it then takes: the + * other half stays for the demand and fills, which {@link fit} can only widen so far. Mirrors the + * Rust `Budget::take`. + */ +export function take(budget: Budget, ranges: readonly WireRange[]): WireRange[] { + const room = Math.floor(budget.bytes / 2); + let used = 0; + const taken: WireRange[] = []; + for (const range of ranges) { + const bytes = cost(budget, range); + if (bytes === undefined || used + bytes > room) break; + used += bytes; + taken.push(range); + } + budget.bytes -= used; + return taken; +} + /** What `range` costs, or undefined when it can't go out. */ function cost(budget: Budget, range: WireRange): number | undefined { // An end at group 0 asks for nothing, which encoding refuses. diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 6d9ecea858..e7c5b7e713 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -604,7 +604,7 @@ test("lite-07 updates edit only the changed ranges", async () => { const live = new LiveRanges(version, [a, b]); const step = async (ranges: WireRange[], remove: bigint[], add: WireRange[]) => { const edit = sent.edit(ranges); - expect(edit).toEqual({ remove, add, fillIds: [] }); + expect(edit).toEqual({ remove, add, askIds: [], fillIds: [] }); const update = new SubscribeUpdate({ priority: 0, remove: edit.remove, ranges: edit.add }); const got = await SubscribeUpdate.decode( new Reader(undefined, await encodeMessage(version, update), version), @@ -660,7 +660,7 @@ test("lite-07 a refused edit leaves the ranges as sent", () => { expect(() => live.edit(wide)).toThrow("too many ranges"); expect(live.entries()).toEqual([[0n, {}]]); expect(live.next()).toBe(1n); - expect(live.edit(wide.slice(0, 2))).toEqual({ remove: [0n], add: wide.slice(0, 2), fillIds: [] }); + expect(live.edit(wide.slice(0, 2))).toEqual({ remove: [0n], add: wide.slice(0, 2), askIds: [], fillIds: [] }); }); test("SUBSCRIBE_STARTED is type 4 on lite-07 and unknown before it", async () => { diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index f933359135..7a6322746c 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -359,7 +359,7 @@ export function rangeSize(range: WireRange): number { } /** A range's anchors as a key, so equal ranges compare equal. */ -function rangeKey(range: WireRange): string { +export function wireRangeKey(range: WireRange): string { const anchor = (anchor: WireAnchor | undefined) => { if (anchor?.absolute !== undefined) return `a${anchor.absolute}`; if (anchor?.relative !== undefined) return `r${anchor.relative}`; @@ -375,6 +375,13 @@ export interface Applied { added: [bigint, WireRange][]; } +/** + * Who a live range on a lite-07 subscription asks for: the demand, a reader waiting on the publisher + * to start a relative range, or a fill. A `"stale"` ask was answered before the copy's latest group, + * so no wanted range keeps it. Mirrors the Rust `Owner`. + */ +export type RangeOwner = "demand" | "ask" | "stale" | "fill"; + /** * A subscription's live ranges by Range ID, which both ends of a Subscribe Stream keep: the * subscriber to write each update as an edit, the publisher to resolve one. Mirrors the Rust @@ -475,39 +482,42 @@ export class LiveRanges { * changes one range sends only that one, and the publisher keeps where the rest resolved. * Before lite-07 replaced ranges take fresh Range IDs. * - * The last `fills` of `ranges` ask for fills, and `isFill` names the live ones that do: a fill - * asking for what a demand range does still takes a range of its own, so each Range ID stays - * the demand's or a fill's. `fillIds` are the Range IDs the added fill ranges took. + * The demand's ranges come first, then `asks` asks', then `fills` fills', and `ownerOf` names who + * asked for each live one: an ask or fill asking for what a demand range does still takes a range + * of its own, so each Range ID stays one owner's, and a live range `ownerOf` calls stale is kept + * for no one. `askIds` and `fillIds` are the Range IDs the added asks' and fills' ranges took. */ edit( ranges: readonly WireRange[], - fills = 0, - isFill: (id: bigint) => boolean = () => false, - ): { remove: bigint[]; add: WireRange[]; fillIds: bigint[] } { + { asks = 0, fills = 0 }: { asks?: number; fills?: number } = {}, + ownerOf: (id: bigint) => RangeOwner = () => "demand", + ): { remove: bigint[]; add: WireRange[]; askIds: bigint[]; fillIds: bigint[] } { if (ranges.length === 0) throw new Error(EMPTY_RANGE); if (!hasRanges(this.#version)) { const same = ranges.length === this.#live.size && - this.ranges().every((range, index) => rangeKey(range) === rangeKey(ranges[index])); + this.ranges().every((range, index) => wireRangeKey(range) === wireRangeKey(ranges[index])); if (!same) { this.#live.clear(); this.#add(ranges); } - return { remove: [], add: [...ranges], fillIds: [] }; + return { remove: [], add: [...ranges], askIds: [], fillIds: [] }; } - const demand = ranges.length - fills; - const key = (range: WireRange, fill: boolean) => `${fill ? "f" : "d"}${rangeKey(range)}`; + const demand = ranges.length - asks - fills; + const owner = (index: number): RangeOwner => + index < demand ? "demand" : index < demand + asks ? "ask" : "fill"; + const key = (range: WireRange, owner: RangeOwner) => `${owner}:${wireRangeKey(range)}`; // Each live range keeps at most one wanted copy, so duplicates edit like any other. const wanted = new Map(); ranges.forEach((range, index) => { - const k = key(range, index >= demand); + const k = key(range, owner(index)); wanted.set(k, (wanted.get(k) ?? 0) + 1); }); const remove: bigint[] = []; // The bytes the live ranges take once the edit applies. let size = this.#size; for (const [id, range] of this.#live) { - const k = key(range, isFill(id)); + const k = key(range, ownerOf(id)); const count = wanted.get(k) ?? 0; if (count > 0) { wanted.set(k, count - 1); @@ -518,22 +528,25 @@ export class LiveRanges { } // In the order asked for, so the publisher numbers them the way this side does. const add: WireRange[] = []; + const askIds: bigint[] = []; const fillIds: bigint[] = []; const first = this.#next; ranges.forEach((range, index) => { - const k = key(range, index >= demand); + const k = key(range, owner(index)); const count = wanted.get(k) ?? 0; if (count === 0) return; wanted.set(k, count - 1); add.push(range); - if (index >= demand) fillIds.push(first + BigInt(add.length - 1)); + const id = first + BigInt(add.length - 1); + if (owner(index) === "ask") askIds.push(id); + if (owner(index) === "fill") fillIds.push(id); }); // Checked before anything changes, so a refused edit leaves the ranges as last sent. for (const range of add) size += rangeSize(range); if (size > Message.MAX_MESSAGE_SIZE) throw new Error("too many ranges for one SUBSCRIBE"); for (const id of remove) this.#remove(id); this.#add(add); - return { remove, add, fillIds }; + return { remove, add, askIds, fillIds }; } } diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 868778014e..9ec88be07e 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -86,11 +86,13 @@ import { EMPTY_RANGE, inclusiveGroupEnd, LiveRanges, + type RangeOwner, requestedRange, Subscribe, SubscribeUpdate, toWireRanges, type WireRange, + wireRangeKey, } from "./subscribe.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; import { decodeTracksEventMaybe, Tracks as TracksMessage } from "./tracks.ts"; @@ -237,6 +239,9 @@ interface SubscribeEntry { // The Range IDs of the fills' live ranges; the rest are the demand's, which alone say where the // live feed starts. fillIds: Set; + // The Range IDs of the asks' live ranges: a relative range a reader of the copy waits on the + // publisher to start (see `hooks.asks`), which says nothing of the live feed either. + askIds: Set; // The groups the fills' ranges ask for, as last sent: widened with the demand's ranges until // they fit, so a run may cover more than the fills ask. fillGroups: readonly Run[]; @@ -1711,6 +1716,7 @@ export class Subscriber { sent: 0, stream: { kind: "setup" }, fillIds: new Set(), + askIds: new Set(), fillGroups: [], fillsChanged: new Signal(0), }; @@ -2189,6 +2195,7 @@ export class Subscriber { stream: { kind: "setup" }, filling: true, fillIds: new Set(), + askIds: new Set(), fillGroups: [], fillsChanged: new Signal(0), }; @@ -2768,16 +2775,20 @@ export class Subscriber { const fills = entry.fills?.ask; const fillGroups = fills?.groups ?? []; const priority = Math.max(current?.priority ?? 0, fills?.priority ?? -128); + // The relative ranges readers wait on the publisher to start, each asked as a range of its own + // (see `RangeOwner`), alongside a demand that covers their groups. + const asks = hasRanges(this.version) ? this.#asks(entry) : []; if ( current === undefined || (refresh === undefined && priority === sentPriority && sameRuns(fillGroups, sentFills) && + this.#asked(entry, asks) && this.#sameSubscription(current, lastSent)) ) { // Each wake releases the quiet signals too, including when termination wins. const next = await race([ - Signal.race(hooks.requested(track), asked, edge, entry.fillsChanged), + Signal.race(hooks.requested(track), asked, edge, entry.fillsChanged, hooks.asksChanged(track)), stopped, ]); if (next === null) return; @@ -2810,7 +2821,9 @@ export class Subscriber { maxDelay: wireMaxDelay(current), minDelay: minDelayOf(current), }); - const budget = Fit.budget(fields, entry.live.next(), demanded.length + fillGroups.length); + const budget = Fit.budget(fields, entry.live.next(), demanded.length + asks.length + fillGroups.length); + // The asks that don't fit wait for those sent to be answered, which retires them. + const askRanges = Fit.take(budget, asks); const fitted = Fit.fit(demanded, fillGroups, entry.fills?.avoid() ?? [], budget); // A fill's range starting past its run speaks only while that run is asked whole. entry.fills?.retireSkipped(fitted.fills); @@ -2818,9 +2831,11 @@ export class Subscriber { start: { absolute: run.start }, end: { absolute: run.end }, })); - // The demand's ranges, then the fills'. - const edited = entry.live.edit([...fitted.ranges, ...fillRanges], fillRanges.length, (id) => - entry.fillIds.has(id), + // The demand's ranges, the asks', then the fills'. + const edited = entry.live.edit( + [...fitted.ranges, ...askRanges, ...fillRanges], + { asks: askRanges.length, fills: fillRanges.length }, + (id) => this.#ownerOf(entry, id), ); edit = edited; for (const id of entry.starts.keys()) @@ -2830,6 +2845,8 @@ export class Subscriber { } for (const id of entry.fillIds) if (!entry.live.get(id)) entry.fillIds.delete(id); for (const id of edited.fillIds) entry.fillIds.add(id); + for (const id of entry.askIds) if (!entry.live.get(id)) entry.askIds.delete(id); + for (const id of edited.askIds) entry.askIds.add(id); // The groups a fill newly asks for wait a fresh grace too. for (const run of fitted.fills) for (const gap of runGaps(entry.fillGroups, run.start, run.end)) @@ -2873,6 +2890,36 @@ export class Subscriber { } } + // The relative ranges the copy's readers wait on the publisher to start, as wire ranges, once each. + #asks(entry: SubscribeEntry): WireRange[] { + const asks = new Map(); + // A relative end goes upstream open, so two asks may carry the same range. + for (const range of toWireRanges(wireRanges(hooks.asks(entry.track)))) asks.set(wireRangeKey(range), range); + return [...asks.values()]; + } + + // Who asked for the live range `id`. An ask its publisher answered counting from before the copy's + // latest group is stale: a reader waiting on it joined since. Mirrors the Rust `SubStream::edit`. + #ownerOf(entry: SubscribeEntry, id: bigint): RangeOwner { + if (entry.fillIds.has(id)) return "fill"; + if (!entry.askIds.has(id)) return "demand"; + const largest = hooks.producerLargest(entry.track).peek()?.group; + const edge = entry.edges.get(id); + const stale = entry.starts.has(id) && largest !== undefined && (edge === undefined || edge < largest); + return stale ? "stale" : "ask"; + } + + // Whether the live asks' ranges are `asks`, none of them stale. + #asked(entry: SubscribeEntry, asks: readonly WireRange[]): boolean { + const live = new Set(); + for (const id of entry.askIds) { + const range = entry.live.get(id); + if (!range || this.#ownerOf(entry, id) === "stale") return false; + live.add(wireRangeKey(range)); + } + return live.size === asks.length && asks.every((range) => live.has(wireRangeKey(range))); + } + // Declare where a lite-07 feed starts: the lowest start the live ranges report, or the // requested floor, pending, while SUBSCRIBE_START or a range's SUBSCRIBE_STARTED has yet to // say where one starts. Mirrors the Rust `SubStream::declare`. @@ -2913,7 +2960,7 @@ export class Subscriber { for (const id of entry.live.ids()) { // Only the demand's ranges say where the live feed starts. - if (entry.fillIds.has(id)) continue; + if (entry.fillIds.has(id) || entry.askIds.has(id)) continue; if (!entry.starts.has(id)) { pending = true; break; diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 84b53015d3..bf659e2033 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5803,54 +5803,100 @@ test("the counted aggregates match a full fold of every subscriber", async () => } }); -// A lite-07 copy holding part of the track counts a relative start back only through the groups it -// holds, or as its publisher reported, and reads nothing at a gap until one of those can count it, -// or nothing more arrives. Mirrors the Rust `a_received_copy_counts_a_relative_start_back_as_its_publisher_did`. +// A lite-07 copy holds only part of the track, so a relative start counts back only as its publisher +// reported, from the same latest group or a newer one, and reads nothing until it does, whatever +// groups the copy holds or receives: its reader asks for that report. Mirrors the Rust +// `a_received_copy_counts_a_relative_start_back_as_its_publisher_did`. test("a received copy counts a relative start back as its publisher did", async () => { const producer = receivedTrack({ timescale: Timescale.MILLI }); hooks.reportsStarts(producer); - for (const key of [1n, 2n, 7n, 8n, 9n, 10n]) putGroup(producer, key, Number(key)); - producer.skipGroups(3n, 7n); - // Counting 4 back from 10, the publisher had no group to deliver before 11. - hooks.relativeStarts(producer, [[4n, { past: 11n }, 10n]]); - putGroup(producer, 11n, 11); - const subscribe = (start: bigint, end?: bigint) => + for (const key of [1n, 2n, 9n]) putGroup(producer, key, Number(key)); + hooks.relativeStarts(producer, [[2n, { at: 7n }, 9n]]); + const back = (back: bigint, end?: bigint) => producer.subscribe({ delay: { max: null }, - ranges: [{ start: { relative: start }, end: end === undefined ? undefined : { time: end } }], + ranges: [{ start: { relative: back }, end: end === undefined ? undefined : { time: end } }], }); - // Counting from a newer latest group only starts later, so the empty report still holds, where - // the groups held would count back to group 7. - const empty = subscribe(4n, 8n); + const reported = back(2n); + expect(drain(reported)).toEqual([9n]); + expect(reported.floor()).toBe(7n); + const latest = back(0n); + expect(drain(latest)).toEqual([9n]); + expect(latest.floor()).toBe(9n); + // Group 8 may sit below 9, so it reads nothing meanwhile, and asks for a report. + const unreported = back(1n); + expect(drain(unreported)).toEqual([]); + expect(unreported.floor()).toBeUndefined(); + expect(hooks.asks(producer)).toEqual([{ start: { relative: 1n } }]); + + // Holding every group in between counts nothing either. + putGroup(producer, 8n, 8); + await settle(); + expect(drain(unreported)).toEqual([]); + + // A report counted from a newer latest group starts it, as the publisher placed it then. + putGroup(producer, 10n, 10); + hooks.relativeStarts(producer, [ + [2n, { at: 7n }, 9n], + [1n, { at: 9n }, 10n], + ]); + await settle(); + expect(unreported.floor()).toBe(9n); + expect(drain(unreported).sort((a, b) => Number(a - b))).toEqual([9n, 10n]); + expect(hooks.asks(producer)).toEqual([]); + // But one from an older latest group says nothing of a reader counting from a newer one. + const later = back(2n); + expect(drain(later)).toEqual([]); + expect(reported.floor()).toBe(7n); + + // A publisher with no group to deliver in a range ending at 10 has none in one ending sooner, but + // says nothing of one ending later. + hooks.relativeStarts(producer, [[5n, { past: 10n }, 10n]]); + const empty = back(5n, 8n); expect(drain(empty)).toEqual([]); expect(await empty.recvGroup()).toBeUndefined(); + const longer = back(5n, 10n); + expect(drain(longer)).toEqual([]); + expect(longer.floor()).toBeUndefined(); - // Through the starts skipped, group 0 may sit below 1, so it reads nothing meanwhile. - const gap = subscribe(7n); - expect(drain(gap)).toEqual([]); - // Once the copy reaches its end, nothing more can arrive, so it counts over the groups held. - hooks.endTrack(producer, { group: 12n, frame: 0 }); + // Nothing counts over the groups held once the copy ends either. + hooks.endTrack(producer, { group: 11n, frame: 0 }); await settle(); - expect(drain(gap)).toEqual([1n, 2n, 7n, 8n, 9n, 10n, 11n]); - expect(await gap.recvGroup()).toBeUndefined(); + expect(drain(later)).toEqual([]); + expect(later.floor()).toBeUndefined(); }); -// A failed group a new copy replaces leaves no gap the cache made, so a relative start counting into -// the gap below it still waits. Mirrors the Rust `a_received_copy_waits_on_a_gap_a_displaced_group_left`. -test("a received copy waits on a gap a replaced group left", () => { - const producer = receivedTrack({ timescale: Timescale.MILLI }); - hooks.reportsStarts(producer); - for (const key of [1n, 2n]) putGroup(producer, key, Number(key)); - const failed = producer.createGroup(9n); - failed.writeFrame(textFrame("x", Timestamp.fromMillis(9))); - failed.abort(new Error("reset")); - putGroup(producer, 9n, 9); - - const reader = producer.subscribe({ - delay: { max: null }, - ranges: [{ start: { relative: 2n }, end: { time: 8n } }], - }); - expect(drain(reader)).toEqual([]); - expect(reader.floor()).toBeUndefined(); +// Whatever groups a lite-07 copy holds, a reader's relative start lands only where its publisher +// reported it, or waits: the cache never moves it. Mirrors the Rust +// `a_copys_cache_never_moves_a_relative_start`. +test("a copy's cache never moves a relative start", async () => { + let seed = 0x9e3779b9; + const next = (n: number) => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + return (seed >>> 16) % n; + }; + for (let run = 0; run < 64; run++) { + const held = new Set(); + for (let i = next(12); i > 0; i--) held.add(BigInt(next(16))); + const back = BigInt(next(6)); + // Unreported half the time. + const report = next(2) === 0 ? undefined : BigInt(next(17)); + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + for (const key of [...held].sort((a, b) => Number(a - b))) putGroup(producer, key, Number(key)); + putGroup(producer, 16n, 16); + if (report !== undefined) hooks.relativeStarts(producer, [[back, { at: report }, 16n]]); + const reader = producer.subscribe({ delay: { max: null }, ranges: [{ start: { relative: back } }] }); + await settle(); + const expected = back === 0n ? 16n : report; + const at_ = { held: [...held], back, report }; + expect({ ...at_, floor: reader.floor() }).toEqual({ ...at_, floor: expected }); + expect({ ...at_, asked: hooks.asks(producer).length }).toEqual({ + ...at_, + asked: expected === undefined ? 1 : 0, + }); + reader.close(); + producer.close(); + } }); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index befd2bb54b..222ff813f3 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -58,7 +58,7 @@ import { rescale } from "./rescale.ts"; import { floorAt, Timescale, type Timestamp } from "./time.ts"; import { checkEnd, checkKey, max, min, UNBOUNDED } from "./util/key.ts"; import { checkName } from "./util/utf8.ts"; -import { bounded, type Holdings, joinsLive, rangesOf, UNHELD, Window, wireSpan } from "./window.ts"; +import { bounded, type Holdings, joinsLive, rangeKey, rangesOf, UNHELD, Window, wireSpan } from "./window.ts"; import { type Broadcast as BroadcastWire, registerTrackConsumer } from "./wire.ts"; export { type Fill, FillGroups, FillLookup, Requests } from "./fill.ts"; @@ -1004,6 +1004,10 @@ let fillFloor: (producer: Producer, named: bigint | undefined) => bigint | undef // The keys of the groups the producer caches, sorted: what relative and time anchors resolve // against, as the Rust `TrackState::lookup` keys; assigned in its static block. let keysOf: (producer: Producer) => readonly bigint[]; +// Count `ranges`, in a stable order, as the relative ranges the reader reading through `sink` waits +// on the publisher to start, in place of those counted before; see `hooks.asks`. Assigned in the +// producer's static block. +let askStarts: (producer: Producer, sink: TrackState, ranges: readonly Range[]) => void; // How a received copy resolves its readers' time anchors, as its wire tells it; assigned in the // producer's static block. See `hooks.startsAtKeys` and `hooks.timeStarts`. let timeAnchors: (producer: Producer) => { @@ -1012,8 +1016,6 @@ let timeAnchors: (producer: Producer) => { /** The newest of `starts` past the time its range asked from, 0 when none is. */ past: bigint; relative: ReadonlyMap; - /** The highest key of any group that left the cache. */ - uncached: bigint | undefined; /** Whether its publisher reports where each range starts (lite-07), which `relative` holds. */ reported: boolean; /** Bumped whenever the others, or whether the producer is a received copy, change. */ @@ -1289,13 +1291,14 @@ export class Producer { #startsPast = 0n; // On a lite-07 copy, where the publisher started each live range counting back, by its count. #relativeStarts = new Map(); - // The highest key of any group the cache evicted, rather than one a new copy replaced: a received - // copy counting a relative start back can't tell a gap at or below it from one it never held. - // Mirrors the Rust `TrackState::uncached`. - #uncached?: bigint; - // Whether a copy's publisher reports where each range starts, so a relative start the copy - // can't count back over its own groups waits for that. Mirrors the Rust `Anchors::Reported`. + // Whether a copy's publisher reports where each range starts, so a relative start waits for that + // rather than count over the groups held. Mirrors the Rust `Anchors::Reported`. #reportsStarts = false; + // The relative ranges each reader waits on the publisher to start, which the copy's subscription + // asks upstream for. Mirrors the Rust `Subscriptions::asks`. + #readerAsks = new Map(); + // Bumped whenever a reader's entry in `#readerAsks` changes. Mirrors the Rust `asks_revision`. + #asksChanged = new Signal(0); // Counts changes to how a time start is placed other than by groups arriving or drops: the wire, // `#timeStarts`, and `#startsAtKeys`. Mirrors the Rust `TrackState::placing`. #placing = 0; @@ -1394,8 +1397,6 @@ export class Producer { evict: (entry) => { this.#evict(entry); this.#uncache(entry, true); - // Gone for good, unlike a group a new copy replaces at once. - this.#uncached = max(this.#uncached ?? entry.group.start, entry.group.start); }, settle: () => this.#settle(), expireDrops: (now) => this.#expireDrops(now), @@ -1535,7 +1536,6 @@ export class Producer { past: producer.#startsPast, relative: producer.#relativeStarts, reported: producer.#reportsStarts, - uncached: producer.#uncached, placing: producer.#placing, }); hooks.startsAtKeys = (producer) => { @@ -1550,6 +1550,23 @@ export class Producer { } producer.#placing++; }; + hooks.asks = (producer) => { + const asks = new Map(); + for (const ranges of producer.#readerAsks.values()) + for (const range of ranges) asks.set(rangeKey(range), range); + return [...asks.values()]; + }; + hooks.asksChanged = (producer) => producer.#asksChanged; + askStarts = (producer, sink, ranges) => { + const last = producer.#readerAsks.get(sink) ?? []; + const same = + last.length === ranges.length && + last.every((range, index) => rangeKey(range) === rangeKey(ranges[index])); + if (same) return; + if (ranges.length === 0) producer.#readerAsks.delete(sink); + else producer.#readerAsks.set(sink, ranges); + producer.#asksChanged.update((revision) => revision + 1); + }; hooks.reportsStarts = (producer) => { producer.#reportsStarts = true; }; @@ -2092,6 +2109,7 @@ export class Producer { // Take `sink` out of the aggregates. #leave(sink: TrackState): void { sink.update.watch = undefined; + askStarts(this, sink, []); this.#moved(sink, { before: sink.update.contribution(), gone: [...sink.update.ranges()], came: [] }); this.#counted.delete(sink); this.#subscribed(); @@ -3648,10 +3666,23 @@ export class Subscriber { this.#moved(); } - // The window moved: narrow the bounds to it. + // The window moved: narrow the bounds to it, and ask for the relative starts it waits on. #moved(): void { this.#revision++; this.#bound(); + this.#ask(); + } + + // Count the relative ranges the window waits on the publisher to start, which a received copy's + // subscription asks upstream for. Mirrors the Rust `Asked::set`. + #ask(): void { + const producer = this.#state.producer; + if (!producer || this.#left) return; + const counting = this.#window.counting().sort((a, b) => { + const [x, y] = [rangeKey(a), rangeKey(b)]; + return x < y ? -1 : x > y ? 1 : 0; + }); + askStarts(producer, this.#state, counting); } // The groups this reader's track holds, by start, for resolving its relative and time anchors: @@ -3736,54 +3767,26 @@ export class Subscriber { return { runs, mark }; }, nextStart: (from) => keys[keyIndex(keys, from)], - // A lite-07 copy holds only part of the track, so it starts where its publisher did, - // counting from the same latest group: a newer one moved the live edge. One its wire still - // feeds counts only through the groups it holds and the starts skipped between them, since - // any other gap may hold groups it never saw, and waits on the report there; once nothing - // feeds it, over the groups held. Mirrors the Rust `TrackState::counted_start`. + // A lite-07 copy holds only part of the track, so it starts only where its publisher + // reported starting, counting from the same latest group or a newer one, and waits for that + // report otherwise, whatever it holds: its reader asks upstream for one (see `hooks.asks`). + // The latest group itself needs no count. Mirrors the Rust `TrackState::counted_start`. countedStart: (latest, back, end) => { - const reported = receivedCopy(producer) ? anchors.relative.get(back) : undefined; - if (reported?.latest !== undefined && reported.latest <= latest) { - const { at, past } = reported.start; - if (at !== undefined && reported.latest === latest) return { group: at }; - // Past a range ending no earlier than this one, so this one holds no group either. - // Counting from a newer latest group only starts later, so it holds there too. - if (past !== undefined && end <= past) return { group: end }; - } - // Nothing more arrives once the copy closes or completes, so no report can settle it - // either. - if ( - !anchors.reported || - !receivedCopy(producer) || - state.closed.peek() !== undefined || - this.#complete() - ) - return undefined; - // Each step lands on a group held, so a huge count walks the cache once. - let at = latest; - for (let step = 0n; step < back; step++) { - if (at === 0n) return "before"; - let below = at - 1n; - const run = drops.at(below); - if (run?.skip === true) { - // Skipped down to 0: no group sits below. - if (run.start === 0n) return "before"; - below = run.start - 1n; - } - if (keys[keyIndex(keys, below)] !== below) { - // The publisher delivers nothing below where a range from group 0 started, so a count - // reaching below it starts there. - const floor = anchors.starts.get(0n); - if (floor !== undefined && below < floor) return { group: floor }; - // The cache may have made this gap, which no report or arrival fills, so count over - // the groups held, as a publisher that let them go does. - return anchors.uncached !== undefined && below <= anchors.uncached ? undefined : "unknown"; - } - at = below; - } - return { group: at }; + if (!anchors.reported || !receivedCopy(producer)) return undefined; + if (back === 0n) return { group: latest }; + const reported = anchors.relative.get(back); + const { at, past } = reported?.start ?? {}; + if (at !== undefined && reported?.latest !== undefined && reported.latest >= latest) + return { group: at }; + // Past a range ending no earlier than this one, so this one holds no group either. Counting + // from another latest group starts no earlier, so it holds there too. + if (past !== undefined && end <= past) return { group: end }; + return "unknown"; }, back: (latest, back) => { + // Keys that are group IDs count back by ID, as a moq-transport joining FETCH does, so the + // count never depends on what this copy holds. + if (anchors.atKeys) return latest > back ? latest - back : 0n; if (back === 0n) return latest; // Bounded by the cache: a huge count reaches back to the oldest. const index = BigInt(keyIndex(keys, latest)) - back; diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 7a012ee74b..8672173b56 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -188,15 +188,19 @@ describe("Window", () => { expect(covered(ended, held.held)).toEqual(keys(0n, 8n)); }); - test("a relative start the groups held can't count yet reads nothing until counted", () => { + test("a relative start reads nothing until counted", () => { const held = new Keys([1n, 2n, 9n]); let counted: Containing = "unknown"; + let countedFrom: bigint | undefined; const counting: Holdings = { resolves: () => true, containing: (at) => held.containing(at), nextStart: (from) => held.nextStart(from), back: (latest, back) => held.back(latest, back), - countedStart: () => counted, + countedStart: (latest) => { + countedFrom = latest; + return counted; + }, complete: () => false, }; const window = new Window([range(relative(2n), time(8n)), range(time(1n), time(1n))], 9n, counting); @@ -205,14 +209,14 @@ describe("Window", () => { expect(covered(window, keys(0n, 10n))).toEqual([1n]); expect([window.floor(), window.start(), window.end()]).toEqual([undefined, undefined, undefined]); expect(window.resolved()[0]).toBeUndefined(); + expect(window.counting()).toEqual([range(relative(2n), time(8n))]); - // Nothing to count from while the latest group is unknown. + // A report counted from the latest group it first counted from, or a newer one, starts it. counted = { group: 7n }; - expect(window.settle(undefined, counting)).toBe(false); - expect(window.settled).toBe(false); - - expect(window.settle(9n, counting)).toBe(true); + expect(window.settle(12n, counting)).toBe(true); + expect(countedFrom).toBe(9n); expect(window.settled).toBe(true); + expect(window.counting()).toEqual([]); expect(covered(window, keys(0n, 10n))).toEqual([1n, 7n, 8n]); expect([window.start(), window.end()]).toEqual([1n, 9n]); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index e7ac7f25dd..dd81a1c3c7 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -337,10 +337,11 @@ export interface Holdings { back(latest: bigint, back: bigint): bigint | undefined; /** * Where a range counting `back` groups before `latest`, up to the exclusive `end` ({@link UNBOUNDED} - * for none), starts, which a relative start, but never an end, takes over {@link back}: where the publisher reported starting it, since a delivery - * start may sit past the group counted, such as above groups too stale for the budget, or - * `"unknown"` while a received copy can count it neither that way nor over its own groups yet. - * Undefined, or absent, counts it with {@link back}. Mirrors the Rust `Holdings::counted_start`. + * for none), starts, which a relative start, but never an end, takes over {@link back}: on a + * received copy, where its publisher reported starting one counted from `latest` or a newer + * group, since the copy holds only part of the track and a delivery start may sit past the group + * counted, or `"unknown"` until it reports one. Undefined, or absent, counts it with + * {@link back}. Mirrors the Rust `Holdings::counted_start`. */ countedStart?(latest: bigint, back: bigint, end: bigint): Containing | undefined; /** Whether nothing more can arrive here, so a time start nothing contains reaches back to the oldest group rather than wait. */ @@ -472,11 +473,11 @@ export class Window { */ #reaching = new Set(); /** - * The ranges, by key, whose relative start the groups held can't count back to yet (see - * {@link Holdings.countedStart}), so they read nothing yet: a group held may sit below where the - * publisher counted to. + * The ranges, by key, whose relative start waits on its publisher to say where it counted back to + * (see {@link Holdings.countedStart}), each with the latest group it counted from then, so they + * read nothing yet: a group held may sit below where the publisher started. */ - #counting = new Set(); + #counting = new Map(); // How many spans name each start, and how many name none: see {@link start}. #named = new Counts(); #unnamed = 0; @@ -569,11 +570,19 @@ export class Window { } /** - * The keys of the ranges still waiting on a relative anchor, a live join's reach back, or a - * time start. Mirrors the Rust `Window::unsettled`. + * The ranges whose relative start waits on its publisher to say where it starts, which a received + * copy asks its upstream for. Mirrors the Rust `Window::counting`. + */ + counting(): Range[] { + return [...this.#counting.keys()].flatMap((key) => this.#ranges.get(key) ?? []); + } + + /** + * The keys of the ranges still waiting on a relative anchor, its publisher's report, a live + * join's reach back, or a time start. Mirrors the Rust `Window::unsettled`. */ unsettled(): Set { - const keys = new Set([...this.#pending, ...this.#reaching]); + const keys = new Set([...this.#pending, ...this.#reaching, ...this.#counting.keys()]); for (const entry of this.#waiting.all()) for (const key of entry.keys) keys.add(key); for (const entry of this.#looking.all()) for (const key of entry.keys) keys.add(key); return keys; @@ -603,13 +612,13 @@ export class Window { for (const key of reaching) this.#placeRange(key, latest, holdings); moved = this.#spans.size > placed; } - // Counted from the latest group now, as the publisher counts from its own latest when the - // range reaches it. - if (this.#counting.size > 0 && latest !== undefined) { + // Each from the latest group it first counted from: a report counted from that one or a newer + // one starts it. + if (this.#counting.size > 0) { const placed = this.#spans.size; const counting = [...this.#counting]; this.#counting.clear(); - for (const key of counting) this.#placeRange(key, latest, holdings); + for (const [key, since] of counting) this.#placeRange(key, since, holdings); if (this.#spans.size > placed) moved = true; } if (this.#waiting.size > 0 && this.#startWaiting(holdings)) moved = true; @@ -806,8 +815,8 @@ export class Window { return this.#named.first(); } - // Whether a time start, a join's reach back, or a relative start waits on the groups held to - // place it. + // Whether a time start or a join's reach back waits on the groups held to place it, or a + // relative start on its publisher. #unplaced(): boolean { return this.#waiting.size > 0 || this.#reaching.size > 0 || this.#counting.size > 0; } @@ -859,7 +868,8 @@ export class Window { return; } if (start === "counting") { - this.#counting.add(key); + // Counting needs a latest group to count from. + if (latest !== undefined) this.#counting.set(key, latest); return; } if (start === "waiting") { @@ -1042,7 +1052,8 @@ function joins(range: Range, min: Duration | undefined, holdings: Holdings): Dur * The first group a range starts at, and whether it names that group rather than reaching back to * the oldest one. `"pending"` while it waits on a latest group, reading everything meanwhile, * `"waiting"` for a time start the groups held can't place yet, and `"counting"` for a relative - * start they can't count back to yet, reading nothing meanwhile. Mirrors the Rust `start`. + * start its publisher has yet to say where it starts, reading nothing meanwhile. Mirrors the Rust + * `start`. */ function startOf( anchor: Anchor | undefined, diff --git a/quest/m2/time-addressing/late-relative-end.md b/quest/m2/time-addressing/late-relative-end.md index 5fcb9e6459..910fa94763 100644 --- a/quest/m2/time-addressing/late-relative-end.md +++ b/quest/m2/time-addressing/late-relative-end.md @@ -13,22 +13,22 @@ should read 0 through 8. ## Plan Found by [moq-dev/moq#9198](https://github.com/moq-dev/moq/pull/9198), which -fixed the same bug for a relative start. A start can wait on SUBSCRIBE_STARTED, -or count through the groups held and the starts skipped between them -(`TrackState::counted_received` in `rs/moq-net/src/model/track.rs`). An end has -neither: the publisher reports where each range starts, never where it ends. -The count can't finish through the groups held either, since the group just -above the end, whose `Previous Start` would close the gap, lies outside the -range and never arrives. Making an end wait on that count would park the -reader forever, so #9198 left ends counting over the cache (`end` in -`model/subscription/window.rs`). +fixed the same bug for a relative start. Under the maintainer's rule there, a +subscription never changes behavior based on cache state: a start resolves only +from its publisher's report (SUBSCRIBE_OK or SUBSCRIBE_STARTED, kept in +`TrackState::relative_starts` in `rs/moq-net/src/model/track.rs`), and a reader +no report answers asks the copy's upstream for its range and waits (the asks in +`model/track/demand.rs`). An end has no such report: the publisher reports +where each range starts, never where it ends. So #9198 left ends counting over +the cache (`end` in `model/subscription/window.rs`), which breaks the same rule. - Reproduce first, as `range_anchors.rs` and `js/net/src/integration.test.ts` tests beside `a_later_relative_start_on_a_partial_copy_reads_only_its_groups`. - Recommendation: carry each range's resolved end beside its start in SUBSCRIBE_OK and SUBSCRIBE_STARTED. Lite-07 is still `-wip`, so the wire may change freely. The copy then resolves a relative end from that report, as - `relative_starts` does for starts, and waits for it the way a start does. + `relative_starts` does for starts, and asks for it and waits the way a start + does. - Alternative: the copy treats a bounded range as ended once the subscription accounts for every group it delivered. That needs no wire change, but the copy can't tell a range that ended from one still owed a late group. diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index e61d6ddcd6..fa97e7d562 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10342,7 +10342,10 @@ impl SubStream { let (fill_ids, ask_ids, starts, edges) = (&self.fill_ids, &self.ask_ids, &self.starts, &self.edges); let owner = |id| match () { _ if fill_ids.contains(&id) => Owner::Fill, - _ if ask_ids.contains(&id) && starts.contains_key(&id) && edges.get(&id).is_some_and(|edge| *edge < latest) => { + _ if ask_ids.contains(&id) + && starts.contains_key(&id) + && edges.get(&id).is_some_and(|edge| *edge < latest) => + { Owner::Stale } _ if ask_ids.contains(&id) => Owner::Ask, @@ -10774,9 +10777,7 @@ impl TrackServe { // The relative ranges readers wait on the publisher to start, each asked as a range of // its own (see `Owner::Ask`), alongside a demand that covers their groups. let mut asks = match (ranged, &request) { - (true, Some(_)) => { - wire_ranges(&Subscription::default().with_ranges(producer.asks())).unwrap_or_default() - } + (true, Some(_)) => wire_ranges(&Subscription::default().with_ranges(producer.asks())).unwrap_or_default(), _ => Vec::new(), }; // A relative end goes upstream open, so two asks may carry the same range. diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index d3e0793432..4ad6d3058d 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -1323,7 +1323,10 @@ mod tests { .edit([], &[(2, range(Anchor::Relative(3), None))], Some(9), &keys) .unwrap(); assert!(window.is_counting()); - assert_eq!(window.counting().collect::>(), [range(Anchor::Relative(3), None)]); + assert_eq!( + window.counting().collect::>(), + [range(Anchor::Relative(3), None)] + ); window.edit([2], &[], Some(9), &keys).unwrap(); assert_eq!(window.counting().count(), 0); assert!(window.is_settled()); From 14f763eb7611551b2a33b8931e8325e70e8006aa Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 18:36:59 -0800 Subject: [PATCH 13/18] fix(net): a relay waits on a counting relative start before answering, and asks once per change A lite-07 relay serving a reader whose relative start still waits on its publisher's report keeps the feed's start pending in JS, as Rust's poll_start does, rather than answer that range as empty. The JS subscriber sends the asks when their revision moves, as Rust does, rather than whenever a live ask looks stale or a budget-limited batch left some unsent. An ask for a range the demand adds in the same edit takes no range of its own in either language, since the publisher answers the demand's from its latest group. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscriber.test.ts | 74 +++++++++++++++++++++++++- js/net/src/lite/subscriber.ts | 38 ++++++++------ js/net/src/track.test.ts | 24 +++++++++ js/net/src/track.ts | 4 ++ js/net/src/window.ts | 5 ++ rs/moq-net/src/lite/subscriber.rs | 84 ++++++++++++++++++++++++++++-- 6 files changed, 209 insertions(+), 20 deletions(-) diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index b4012c01a1..6bf7872dc6 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -28,7 +28,7 @@ import { GRACE } from "../ledger.ts"; import * as Path from "../path.ts"; import { Reader, Writer } from "../stream.ts"; import { Timescale } from "../time.ts"; -import type { Producer } from "../track.ts"; +import type { Producer, Range as TrackRange, Subscriber as TrackSubscriber } from "../track.ts"; import { SEGMENT_BYTES } from "../util/limits.ts"; import { Budget } from "../util/yield.ts"; import * as Varint from "../varint.ts"; @@ -2883,6 +2883,78 @@ test("a widened range's SUBSCRIBE_STARTED resolves the feed's start", async () = } }); +// A reader of a lite-07 copy whose relative start no report answers asks the copy's upstream for +// its range, in an update of its own, and once SUBSCRIBE_STARTED answers it the copy retires the +// ask: one update each way, however many groups arrive in between. The demand's own range, added +// fresh, answers the first reader with no ask. Mirrors the Rust +// `a_lite07_ask_the_demand_adds_takes_no_range_of_its_own`. +test("a lite-07 copy asks upstream for a relative start once, and retires the ask once answered", 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 subscribe = (ranges: TrackRange[]) => broadcast.track("video").subscribe({ delay: { max: null }, ranges }); + const live = subscribe([{ start: { time: 0n } }]); + const readers: TrackSubscriber[] = []; + const group = async (key: bigint) => { + await subscriber.runGroup(new Group({ subscribe: 0n, key }), await groupStream(Number(key), version)); + (await live.recvGroup())?.close(); + await drainReactions(); + }; + try { + await opened(0); + await answerTrackInfo(streams[0], version, 1000); + const request = await opened(1); + const sent = new Reader(request.outbound, undefined, version); + expect(await sent.u53()).toBe(StreamId.Subscribe); + await Subscribe.decode(sent, version); + await request.reading; + await respond(request, version, [{ start: new SubscribeStart([0n]) }]); + for (const key of [0n, 1n, 2n]) await group(key); + + // The demand adds the first reader's range, which its answer starts. + const first = subscribe([{ start: { relative: 1n } }]); + readers.push(first); + const demanded = await SubscribeUpdate.decode(sent, version); + expect([demanded.remove, demanded.ranges]).toEqual([[], [{ start: { relative: 1n } }]]); + await respond(request, version, [{ started: new SubscribeStarted(1n, 1n) }]); + await drainReactions(); + expect(first.floor()).toBe(1n); + + // A later reader counting from group 3 can't use that answer, counted from 2, so it asks. + await group(3n); + const later = subscribe([{ start: { relative: 1n } }]); + readers.push(later); + // Waiting on a group, as a reader does, so the answer settles it. + const waiting = later.recvGroup(); + const asked = await SubscribeUpdate.decode(sent, version); + expect([asked.remove, asked.ranges]).toEqual([[], [{ start: { relative: 1n } }]]); + // A group arriving while the ask is out asks nothing again. + await group(4n); + await respond(request, version, [{ started: new SubscribeStarted(2n, 3n) }]); + const retired = await SubscribeUpdate.decode(sent, version); + expect([retired.remove, retired.ranges]).toEqual([[2n], []]); + (await waiting)?.close(); + expect(later.floor()).toBe(3n); + + // Nothing more to say. + await drainReactions(); + const more = await Promise.race([ + SubscribeUpdate.decode(sent, version).then(() => "update"), + (async () => { + for (let i = 0; i < 5; i++) await drainReactions(); + return "quiet"; + })(), + ]); + expect(more).toBe("quiet"); + } finally { + for (const reader of readers) reader.close(); + live.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 9ec88be07e..f5cfdff42a 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2756,6 +2756,10 @@ export class Subscriber { // What the fills asked with the last update, and the priority it carried. let sentFills: Run[] = []; let sentPriority = msg.priority; + // The revision of the asks the last update carried, as many of them as fit: answers moving the + // readers change it again, which asks for the rest. Mirrors the Rust `asks_seen`. + let sentAsks: number | undefined; + const asksChanged = hooks.asksChanged(track); const asked = hooks.demandAsked(track.demand()); const edge = hooks.producerLargest(track); for (;;) { @@ -2777,18 +2781,20 @@ export class Subscriber { const priority = Math.max(current?.priority ?? 0, fills?.priority ?? -128); // The relative ranges readers wait on the publisher to start, each asked as a range of its own // (see `RangeOwner`), alongside a demand that covers their groups. + const asksRevision = asksChanged.peek(); const asks = hasRanges(this.version) ? this.#asks(entry) : []; + const askedAll = sentAsks === asksRevision || (asks.length === 0 && entry.askIds.size === 0); if ( current === undefined || (refresh === undefined && priority === sentPriority && sameRuns(fillGroups, sentFills) && - this.#asked(entry, asks) && + askedAll && this.#sameSubscription(current, lastSent)) ) { // Each wake releases the quiet signals too, including when termination wins. const next = await race([ - Signal.race(hooks.requested(track), asked, edge, entry.fillsChanged, hooks.asksChanged(track)), + Signal.race(hooks.requested(track), asked, edge, entry.fillsChanged, asksChanged), stopped, ]); if (next === null) return; @@ -2831,10 +2837,19 @@ export class Subscriber { start: { absolute: run.start }, end: { absolute: run.end }, })); + // An ask for a range the demand adds here takes no range of its own: the publisher answers + // the demand's from its latest group too. Mirrors the Rust `SubStream::edit`. + const fed = new Set(); + for (const id of entry.live.ids()) { + const range = entry.live.get(id); + if (range && !entry.fillIds.has(id) && !entry.askIds.has(id)) fed.add(wireRangeKey(range)); + } + const added = new Set(fitted.ranges.map(wireRangeKey).filter((key) => !fed.has(key))); + const ownAsks = askRanges.filter((range) => !added.has(wireRangeKey(range))); // The demand's ranges, the asks', then the fills'. const edited = entry.live.edit( - [...fitted.ranges, ...askRanges, ...fillRanges], - { asks: askRanges.length, fills: fillRanges.length }, + [...fitted.ranges, ...ownAsks, ...fillRanges], + { asks: ownAsks.length, fills: fillRanges.length }, (id) => this.#ownerOf(entry, id), ); edit = edited; @@ -2886,6 +2901,7 @@ export class Subscriber { lastSent = { ...current }; sentFills = fillGroups; sentPriority = priority; + sentAsks = asksRevision; console.debug(`subscribe update: id=${id} broadcast=${broadcast} track=${track.name}`); } } @@ -2899,7 +2915,8 @@ export class Subscriber { } // Who asked for the live range `id`. An ask its publisher answered counting from before the copy's - // latest group is stale: a reader waiting on it joined since. Mirrors the Rust `SubStream::edit`. + // latest group is stale when the asks change: a reader waiting on it joined since. Mirrors the + // Rust `SubStream::edit`. #ownerOf(entry: SubscribeEntry, id: bigint): RangeOwner { if (entry.fillIds.has(id)) return "fill"; if (!entry.askIds.has(id)) return "demand"; @@ -2909,17 +2926,6 @@ export class Subscriber { return stale ? "stale" : "ask"; } - // Whether the live asks' ranges are `asks`, none of them stale. - #asked(entry: SubscribeEntry, asks: readonly WireRange[]): boolean { - const live = new Set(); - for (const id of entry.askIds) { - const range = entry.live.get(id); - if (!range || this.#ownerOf(entry, id) === "stale") return false; - live.add(wireRangeKey(range)); - } - return live.size === asks.length && asks.every((range) => live.has(wireRangeKey(range))); - } - // Declare where a lite-07 feed starts: the lowest start the live ranges report, or the // requested floor, pending, while SUBSCRIBE_START or a range's SUBSCRIBE_STARTED has yet to // say where one starts. Mirrors the Rust `SubStream::declare`. diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index bf659e2033..fa498198ba 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5867,6 +5867,30 @@ test("a received copy counts a relative start back as its publisher did", async expect(later.floor()).toBeUndefined(); }); +// A relay serving a reader whose relative start still waits on its publisher's report can't say +// where that range starts, so the feed's start stays pending rather than answer the range as empty, +// until the report arrives or the copy closes. Mirrors the Rust `is_counting` in `poll_start`. +test("a received copy's feed start waits on a relative start still counting", async () => { + const report = (close: boolean) => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + for (let key = 0n; key < 10n; key++) putGroup(producer, key, Number(key)); + const reader = producer.subscribe({ + delay: { max: null }, + ranges: [ + { start: { time: 1n }, end: { time: 2n } }, + { start: { relative: 2n }, end: { time: 8n } }, + ], + }); + expect(hooks.sourceStart(reader)).toBe("pending"); + if (close) producer.close(); + else hooks.relativeStarts(producer, [[2n, { at: 7n }, 9n]]); + return hooks.sourceStart(reader); + }; + expect(report(false)).not.toBe("pending"); + expect(report(true)).not.toBe("pending"); +}); + // Whatever groups a lite-07 copy holds, a reader's relative start lands only where its publisher // reported it, or waits: the cache never moves it. Mirrors the Rust // `a_copys_cache_never_moves_a_relative_start`. diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 222ff813f3..b3766124ce 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -4367,6 +4367,10 @@ export class Subscriber { // A relative anchor still pending reads every group, so a fill asked below would claim // the history it never names. Mirrors the Rust `cursor.sync()` in `poll_start`. subscriber.#settle(); + // A relative start still waiting on its publisher's report names no group yet, so where it + // starts can't be answered; a closed track answers at once, since nothing more can arrive. + // Mirrors the Rust `is_counting` in `poll_start`. + if (subscriber.#window.isCounting() && subscriber.#state.closed.peek() === undefined) return "pending"; const producer = subscriber.#state.producer; const start = producer && sourceStart(producer); if (start === "pending") return start; diff --git a/js/net/src/window.ts b/js/net/src/window.ts index dd81a1c3c7..1712f93d58 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -569,6 +569,11 @@ export class Window { return this.#pending.size > 0; } + /** Whether a relative start still waits on its publisher to say where it starts. Mirrors the Rust `is_counting`. */ + isCounting(): boolean { + return this.#counting.size > 0; + } + /** * The ranges whose relative start waits on its publisher to say where it starts, which a received * copy asks its upstream for. Mirrors the Rust `Window::counting`. diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index fa97e7d562..6e26272edb 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -4021,6 +4021,74 @@ mod tests { assert!(!sub.fill_ids.contains(&2), "forgotten once started"); } + /// An ask for a relative range the demand adds in the same edit takes no range of its own, + /// since the publisher answers the demand's from its latest group too; one for a range the + /// demand already has live does, since that one's answer may predate the reader. + #[kio::test] + async fn a_lite07_ask_the_demand_adds_takes_no_range_of_its_own() { + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + let stream = Stream::open(&crate::driver::Transport::new(session.clone()), Version::Lite07) + .await + .unwrap(); + let live = lite::Range { + start: Some(lite::Anchor::Time(0)), + end: None, + }; + let back = lite::Range { + start: Some(lite::Anchor::Relative(1)), + end: None, + }; + let mut sub = SubStream { + order: crate::track::Order::Descending, + stream, + id: 0, + max_delay: None, + stale_budget: Duration::ZERO, + min_delay: None, + start: Some(0), + priority: 0, + requested: Some(0), + live: lite::LiveRanges::new(Version::Lite07, &[live]), + demand: Some(Subscription::default()), + fill_ids: Default::default(), + ask_ids: Default::default(), + fill_groups: Default::default(), + tail: Default::default(), + started: Some(Some(0)), + finishing: None, + parked: false, + starts: [(0, Some(0))].into(), + edges: Default::default(), + declared: None, + end: None, + wanted: None, + unanswered: None, + _working: crate::session::Work::default().start(), + }; + + let (edit, _) = sub.edit(&[live, back, back], 1, 0, Some(3)).unwrap(); + assert_eq!( + edit, + lite::RangeEdit::Edit { + remove: vec![], + add: vec![back] + }, + "the demand's range answers the ask" + ); + assert!(sub.ask_ids.is_empty()); + + // The demand's range is live now, so another reader's ask takes one of its own. + let (edit, _) = sub.edit(&[live, back, back], 1, 0, Some(5)).unwrap(); + assert_eq!( + edit, + lite::RangeEdit::Edit { + remove: vec![], + add: vec![back] + } + ); + assert_eq!(sub.ask_ids.iter().copied().collect::>(), [2]); + } + /// A lite-07 range an update adds owes nothing known until SUBSCRIBE_STARTED says where it /// starts, and then owes from there; one retired since owes nothing. #[kio::test] @@ -10314,9 +10382,10 @@ impl SubStream { /// Edit the live ranges into `ranges`: the demand's, then `asks` asks', then `fills` held /// fills', forgetting the starts of those retired. A fill or ask asking for what a demand - /// range does still takes a range of its own, so each Range ID stays one owner's. An ask - /// its publisher answered counting from before `latest`, the copy's latest group, is - /// asked again: a reader waiting on it joined since. + /// range does still takes a range of its own, so each Range ID stays one owner's, except an + /// ask for a range the demand adds here: the publisher answers that one from its latest + /// group too. An ask its publisher answered counting from before `latest`, the copy's + /// latest group, is asked again: a reader waiting on it joined since. /// Returns the edit, and whether it changed anything. fn edit( &mut self, @@ -10327,9 +10396,18 @@ impl SubStream { ) -> Result<(lite::RangeEdit, bool), Error> { let before: Vec<_> = self.live.iter().map(|(id, _)| id).collect(); let demand = ranges.len().saturating_sub(asks + fills); + let fed: std::collections::HashSet<_> = self + .live + .iter() + .filter(|(id, _)| self.feeds(*id)) + .map(|(_, range)| *range) + .collect(); + let added: std::collections::HashSet<_> = + ranges[..demand].iter().filter(|range| !fed.contains(range)).collect(); let keyed: Vec<_> = ranges .iter() .enumerate() + .filter(|(index, range)| !(demand..demand + asks).contains(index) || !added.contains(range)) .map(|(index, range)| { let owner = match index { index if index < demand => Owner::Demand, From 34fb1cf34cbfe891125c91ae3c345d7c25bf4a20 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 04:16:51 -0800 Subject: [PATCH 14/18] fix(net): a finished lite-07 copy asks upstream for a later reader's relative start A finished copy has no subscription left to carry an ask, so a later reader's relative start read nothing and ended cleanly. The copy now opens a SUBSCRIBE of its own for the asks (Rust `ask::Ask`, JS `#askFinished`), whose start answers the reader and whose groups land in the copy, and a reader still counting waits rather than end while a producer remains. Co-Authored-By: Claude Opus 5.5 --- doc/concept/moq-lite.md | 5 +- drafts/draft-lcurley-moq-lite.md | 2 +- js/net/src/integration.test.ts | 67 ++++++ js/net/src/lite/subscriber.ts | 165 +++++++++++++-- js/net/src/track.ts | 5 + rs/moq-net/src/lite/subscriber.rs | 78 +++++-- rs/moq-net/src/lite/subscriber/ask.rs | 191 ++++++++++++++++++ rs/moq-net/src/model/track.rs | 45 ++++- rs/moq-net/tests/integration/range_anchors.rs | 72 +++++++ 9 files changed, 589 insertions(+), 41 deletions(-) create mode 100644 rs/moq-net/src/lite/subscriber/ask.rs diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index e1f409c564..b60734faf6 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -333,8 +333,9 @@ A relative anchor counts the groups back from the latest one. It resolves on the hop that publishes the track, and over moq-lite-07, which carries it upstream and reports the live edge. A moq-lite-07 copy never counts a relative start over the groups it happens to hold: it takes the start its publisher -reports for the same range, asking upstream for one when none has, so a reader -reads the same groups from a warm relay as from a cold one. A Rust +reports for the same range, asking upstream for one when none has, even once the +track ended, so a reader reads the same groups from a warm relay as from a cold +one. A Rust moq-transport copy counts a relative anchor back by group ID from the Largest Object its live join reports, as a joining FETCH does. Older moq-lite versions refuse relative anchors other than the live and default starts. Older moq-lite versions and diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index b2330682df..bed7001f69 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1539,7 +1539,7 @@ Which fields follow, and what the Anchor names: Any other value is a PROTOCOL_VIOLATION. Both ends include the Group they name, so a Range from Time `a` to Time `b` delivers every Group from the one containing `a` through the one containing `b`. The publisher resolves a Relative or Time Anchor against the Track as it holds it, except a Relative `Start` of one or more `Groups Back` on a relay, which never depends on the part of the Track the relay holds. -A relay resolves one to the `Range Start` its upstream reported for a Range with the same `Start`, counted from the same latest Group or a newer one: when none has, it asks its upstream for such a Range in its SUBSCRIBE or a SUBSCRIBE_UPDATE, and resolves the Range only once that answer arrives. +A relay resolves one to the `Range Start` its upstream reported for a Range with the same `Start`, counted from the same latest Group or a newer one: when none has, it asks its upstream for such a Range in its SUBSCRIBE or a SUBSCRIBE_UPDATE, or in a SUBSCRIBE of its own once its subscription is over, as when the Track ended, and resolves the Range only once that answer arrives. A Range resolves once: an update that leaves it live does not resolve it again. A `Start` is a floor, not a request; `Subscriber Max Age` and `Subscriber Min Age` are the only things that ask for data. diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index 8dc68c3c06..d8d8cefd29 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -650,6 +650,73 @@ for (const live of [true, false]) { }); } +// A finished copy has no upstream subscription left to carry a later reader's relative start, so +// the reader still reads what the publisher counts back to, not nothing, whether the copy holds +// every group it counts back to or only some, and whether it is still open or closed, kept for its +// cache. Mirrors the Rust `a_relative_start_on_a_finished_copy_reads_the_publishers_groups`. +for (const [from, closed] of [ + [0n, false], + [9n, false], + [9n, true], +] as const) { + test(`integration: lite-07 starts a relative start on a finished copy where the publisher does (from ${from}, closed: ${closed})`, async () => { + const pair = createMockTransportPair(Lite.ALPN_07_WIP); + const origin = new OriginProducer(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + + const broadcast = publish(origin, Path.from("test")); + const producer = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + for (let key = 0n; key <= 10n; key++) { + const group = producer.createGroup(key); + group.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.fromMillis(Number(key)) }); + group.finish(); + } + producer.finish(); + + const remote = wireOf(client).consume(Path.from("test")); + const subscribe = (ranges: TrackRange[]) => + remote.track("video").subscribe({ delay: { max: null }, order: "ascending", ranges }); + const read = async (reader: ReturnType) => { + const got: bigint[] = []; + for (;;) { + const group = await withTimeout(reader.recvGroup(), Duration.millis(2_000), "group timed out"); + if (!group) return got.sort((a, b) => Number(a - b)); + got.push(group.start); + } + }; + // The first reader holds the finished copy. + const first = subscribe([{ start: { time: from } }]); + let later: ReturnType | undefined; + try { + const held: bigint[] = []; + for (let key = from; key <= 10n; key++) held.push(key); + expect(await read(first)).toEqual(held); + if (closed) { + // The copy closes once its subscription is over. + await withTimeout( + (async () => { + while (producer.subscription.peek() !== undefined) await yieldNow(); + })(), + Duration.millis(2_000), + "the upstream subscription never left", + ); + } + later = subscribe([{ start: { relative: 5n } }]); + expect(await read(later)).toEqual([5n, 6n, 7n, 8n, 9n, 10n]); + } finally { + later?.close(); + first.close(); + remote.close(); + broadcast.close(); + client.abort(); + server.abort(); + } + }); +} + // A publisher counts a relative start back from its newest group, which may hold no frame yet and // so counts neither in the largest it reports nor where the copy resolves the start. The copy keeps // the publisher's start, so a reader waiting on each group in sequence starts there rather than diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index e3133467ec..b489013c87 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1512,6 +1512,18 @@ export class Subscriber { // Only a received SUBSCRIBE_FINISH, or before lite-05 a FIN, declares the track's end: a // subscription nobody wants anymore is a released copy, not finished content. + // A reader still waiting on its publisher to start a relative range asks on a subscription of + // its own before the finished copy closes, one at a time: an answer moving the readers changes + // the asks again. Once closed, a later reader asks with a copy of its own. + if (tailSettled && entry.end !== undefined && hasRanges(this.version)) { + const asksChanged = hooks.asksChanged(producer); + for (let asked: number | undefined; hooks.asks(producer).length > 0; ) { + // The same asks after an answer are ones it could not settle. + if (asked === asksChanged.peek()) break; + asked = asksChanged.peek(); + await this.#askFinished(broadcast, consumed, entry); + } + } if (tailSettled && entry.stream.kind === "finished") producer.finish(); hooks.releaseTrack(producer); } catch (err) { @@ -2927,6 +2939,142 @@ export class Subscriber { return stale ? "stale" : "ask"; } + // Ask where the relative starts `copy`'s readers wait on begin, on a lite-07 SUBSCRIBE of its own: + // the subscription that fed the finished copy is over, so nothing else can carry them, and the copy + // never counts them over the groups it holds. Its SUBSCRIBE_START answers them, and the groups it + // delivers land in the copy, so a reader gets what a cold copy would. It FINs once the publisher + // declared the end and every group from the lowest start up to it is accounted for and read. + // Rejects when it fails, leaving its readers no answer. Mirrors the Rust `ask::Ask`. + async #askFinished(broadcast: Path.Valid, consumed: Consumed, copy: SubscribeEntry): Promise { + const producer = copy.track; + const id = this.#subscribeNext++; + const ranges = wireRanges(hooks.asks(producer)); + const terms = { ...hooks.requested(producer).peek(), order: "ascending" as const }; + const msg = this.#withRanges( + this.#subscribeMessage(id, broadcast, consumed, producer.name, terms, ranges), + ranges, + ); + // The asks that don't fit wait for those sent to be answered, which retires them. + if (msg.ranges) { + const fields = await this.#subscribeFields(msg, msg); + msg.ranges = Fit.take(Fit.budget(fields, 0n, msg.ranges.length), msg.ranges); + } + const entry: SubscribeEntry = { + track: producer, + staleBudget: msg.maxDelay ?? NO_MAX_DELAY, + compress: copy.compress, + timescale: copy.timescale, + tail: new Tail({ grace: () => hooks.tailGrace(producer) }), + requested: msg.startGroup, + floor: msg.startGroup, + asked: ranges, + live: new LiveRanges(this.version, msg.ranges ?? []), + starts: new Map(), + edges: new Map(), + sent: 0, + // Its responses are read here, which owns this stream's end. + stream: { kind: "setup" }, + fillIds: new Set(), + askIds: new Set(), + fillGroups: [], + fillsChanged: new Signal(0), + }; + this.#subscribes.set(id, entry); + let stream: Stream | undefined; + // Whether our FIN went out: the answer arrived and every group it owes is in, so a reset after + // it, such as the publisher cutting a response short, costs nothing. + let fin = false; + try { + if (this.#denied(broadcast)) throw unauthorized(broadcast); + const opened = await this.#exchange( + { sendOrder: sendOrder({ priority: msg.priority }), version: this.version }, + async (stream) => { + await stream.writer.u53(StreamId.Subscribe); + await msg.encode(stream.writer, this.version); + return stream; + }, + consumed.valid, + new Deadline(), + ); + stream = opened; + const finish = async () => { + const covered = () => + entry.start !== undefined && + entry.end !== undefined && + entry.tail.covers(entry.start ?? entry.end, entry.end); + await entry.tail.settle(covered, hooks.trackReleased(producer)); + fin = true; + opened.writer.close(); + }; + for (;;) { + const resp = await decodeSubscribeResponseMaybe(opened.reader, this.version); + if (!resp) { + // The publisher FINs in reply to ours, or once it finished the track. + if (fin || entry.end !== undefined) break; + throw new ProtocolViolation("ask subscribe ended without SUBSCRIBE_FINISH"); + } + if ("start" in resp) { + if (entry.start !== undefined || BigInt(resp.start.starts.length) !== entry.live.initial) + throw new ProtocolViolation("unexpected ask SUBSCRIBE_OK"); + const edge = resp.start.largest?.position.group; + resp.start.starts.forEach((start, index) => { + entry.starts.set(BigInt(index), start); + entry.edges.set(BigInt(index), edge); + }); + entry.start = resp.start.starts.reduce( + (lowest, start) => + start === undefined || (lowest !== null && lowest <= start) ? lowest : start, + null, + ); + entry.tail.touch(); + hooks.relativeStarts(producer, this.#counted(entry)); + } else if ("finish" in resp) { + if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_FINISH"); + entry.end = resp.finish.group; + entry.tail.touch(); + void finish().catch(() => {}); + } else if ("drop" in resp) { + entry.tail.account(resp.drop.start, resp.drop.end); + } else if ("skip" in resp) { + this.#skipped(entry, resp.skip.start, resp.skip.end, "SUBSCRIBE_SKIP"); + } else { + throw new ProtocolViolation("unexpected ask subscribe response"); + } + } + opened.close(); + } catch (err) { + closeViolation(this.#quic, err); + stream?.abort(error(err)); + if (!fin) throw err; + } finally { + this.#subscribes.delete(id); + entry.tail.close(); + } + } + + // Where the publisher started each of `entry`'s live ranges counting back, which the copy, holding + // only part of the track, can't count, with the latest group it counted back from. + #counted(entry: SubscribeEntry): [bigint, CountedStart, bigint | undefined][] { + const relative: [bigint, CountedStart, bigint | undefined][] = []; + for (const id of entry.live.ids()) { + const range = entry.live.get(id); + const back = range?.start?.relative; + if (back === undefined || !entry.starts.has(id)) continue; + // Undefined for a range with no group to deliver. + const at = entry.starts.get(id); + const latest = entry.edges.get(id); + // With no group to deliver, the start lies at or past the range's exclusive end: an + // absolute end names it, a time end includes its group, and an open one the latest. + const end = range?.end; + const past = + end?.absolute ?? + (end?.time !== undefined ? end.time + 1n : latest === undefined ? undefined : latest + 1n); + const start = at !== undefined ? { at } : past !== undefined ? { past } : undefined; + if (start) relative.push([back, start, latest]); + } + return relative; + } + // Declare where a lite-07 feed starts: the lowest start the live ranges report, or the // requested floor, pending, while SUBSCRIBE_START or a range's SUBSCRIBE_STARTED has yet to // say where one starts. Mirrors the Rust `SubStream::declare`. @@ -2935,32 +3083,17 @@ export class Subscriber { // time start there resolves to when the copy holds no group at it, and one asked from the // oldest group as asked from 0. Mirrors the Rust `SubStream::report_starts`. const times: [bigint, bigint][] = []; - // 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, CountedStart, bigint | undefined][] = []; for (const id of entry.live.ids()) { const range = entry.live.get(id); if (range === undefined || !entry.starts.has(id)) continue; // One from the oldest group is asked from group 0. const time = range.start === undefined ? 0n : (range.start.time ?? range.start.absolute); - const back = range.start?.relative; // Undefined for a range with no group to deliver. const at = entry.starts.get(id); if (time !== undefined && at !== undefined) times.push([time, at]); - else if (back !== undefined) { - const latest = entry.edges.get(id); - // With no group to deliver, the start lies at or past the range's exclusive end: an - // absolute end names it, a time end includes its group, and an open one the latest. - const end = range?.end; - const past = - end?.absolute ?? - (end?.time !== undefined ? end.time + 1n : latest === undefined ? undefined : latest + 1n); - const start = at !== undefined ? { at } : past !== undefined ? { past } : undefined; - if (start) relative.push([back, start, latest]); - } } hooks.timeStarts(entry.track, times); - hooks.relativeStarts(entry.track, relative); + hooks.relativeStarts(entry.track, this.#counted(entry)); let start: bigint | null = null; let pending = entry.start === undefined; diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 89d100e1e1..dc9ce934c6 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -5064,6 +5064,9 @@ export class Subscriber { if (this.#askFills(start, end ?? UNBOUNDED, drift) !== undefined) return { kind: "idle" }; // A time start still looked up may yet take in a group held already. if (this.#window.lookups().length > 0) return { kind: "idle" }; + // So may a relative start its publisher has yet to report, which a finished copy still asks + // for. Mirrors the Rust `Cursor::awaits_report`. + if (this.#window.isCounting()) return { kind: "idle" }; // A bounded window ends once nothing in it is left to deliver, as in Rust. if (this.#exhausted()) return { kind: "done" }; if (!this.#complete() || this.#starting(groups)) return { kind: "idle" }; @@ -5708,6 +5711,8 @@ export class Subscriber { const end = this.#state.end.peek()?.at; // A time start still looked up may yet take in a group held already. if (this.#window.lookups().length > 0) return { kind: "idle" }; + // So may a relative start its publisher has yet to report, while a producer remains to record it. + if (this.#window.isCounting() && this.#state.closed.peek() === undefined) return { kind: "idle" }; if (this.#exhausted()) { this.#passEnd(); return { kind: "done" }; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 3b3ba9e61f..7737d15713 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -21,6 +21,7 @@ use crate::tail::{self, Reading, Settle, Tail}; use kio::Lock; +mod ask; mod fills; use fills::{FillAsk, RangeFills}; mod fit; @@ -6064,13 +6065,14 @@ mod tests { crate::track::Info::timed(), ); let track = serve_loop.serving.clone(); - let finished = FinishedServe { - serving: Some(serve_loop.serving), - demand: serve_loop.demand, - dynamic: serve_loop.dynamic.fetches_only(), - fetches: serve_loop.fetches, - closed: serve_loop.closed, - }; + let finished = FinishedServe::new( + &serve, + serve_loop.serving, + serve_loop.demand, + serve_loop.dynamic.fetches_only(), + serve_loop.fetches, + serve_loop.closed, + ); // A reader holds the finished copy. let _reader = broadcast .consume() @@ -11608,13 +11610,37 @@ struct FinishedServe { dynamic: track::Requests, fetches: kio::Tasks, closed: Transport, + /// Lite-07: the subscription asking where the readers' relative starts begin, which no + /// other subscription is left to carry. + ask: Option, + /// Whether the readers' asks changed since the last [`ask::Ask`] went out, or none has. + ask_due: bool, } impl FinishedServe { + fn new( + serve: &TrackServe, + serving: track::Producer, + demand: track::Demand, + dynamic: track::Requests, + fetches: kio::Tasks, + closed: Transport, + ) -> Self { + Self { + serving: Some(serving), + demand, + dynamic, + fetches, + closed, + ask: None, + ask_due: serve.subscriber.version.has_ranges(), + } + } + /// Ready once nobody reads the copy and no fetch is in flight, or the session closed. fn poll(&mut self, serve: &TrackServe, waiter: &kio::Waiter) -> Poll<()> { loop { - let Some(serving) = &self.serving else { + let Some(serving) = &mut self.serving else { return Poll::Ready(()); }; match self.dynamic.poll_requested_groups(waiter) { @@ -11631,6 +11657,29 @@ impl FinishedServe { Poll::Pending => {} } let _ = self.fetches.poll(waiter); + // A reader waiting on the publisher to start a relative range asks on a subscription + // of its own, one at a time: an answer moving the readers changes the asks again. + if serve.subscriber.version.has_ranges() && serving.poll_asks_changed(waiter).is_ready() { + self.ask_due = true; + } + if let Some(ask) = &mut self.ask + && let Poll::Ready(res) = ask.poll(waiter) + { + self.ask = None; + // Its readers have no answer coming, as when a live copy's subscription fails. + if let Err(err) = res { + if let Some(serving) = self.serving.take() { + let _ = serving.abort(err); + } + return Poll::Ready(()); + } + } + if self.ask.is_none() && std::mem::take(&mut self.ask_due) { + self.ask = ask::Ask::new(serve, serving); + if self.ask.is_some() { + continue; + } + } // The groups it already holds stay readable, its clean end too once settled. if let Poll::Ready(err) = self.closed.poll_closed(waiter) { if let Some(serving) = self.serving.take() { @@ -12063,13 +12112,14 @@ impl TrackServeRun { // A reader's miss in flight is settled by the tail instead. let mut fetches = serve_loop.fetches; fetches.retain(|run| run.fill.is_fetch()); - self.finished = Some(FinishedServe { - serving: Some(serve_loop.serving), - demand: serve_loop.demand, - dynamic: serve_loop.dynamic.fetches_only(), + self.finished = Some(FinishedServe::new( + &self.serve, + serve_loop.serving, + serve_loop.demand, + serve_loop.dynamic.fetches_only(), fetches, - closed: serve_loop.closed, - }); + serve_loop.closed, + )); } if let Some(datagrams) = &mut self.datagrams { datagrams.end(Ok(())); diff --git a/rs/moq-net/src/lite/subscriber/ask.rs b/rs/moq-net/src/lite/subscriber/ask.rs new file mode 100644 index 0000000000..5f93bf65c0 --- /dev/null +++ b/rs/moq-net/src/lite/subscriber/ask.rs @@ -0,0 +1,191 @@ +//! A finished lite-07 copy asking its upstream where its readers' relative starts begin. + +use std::task::{Poll, ready}; + +use super::{Accepted, Establish, SubStream, TrackServe, wire_ranges}; +use crate::{ + Error, + coding::Recv, + lite, + track::{self, Order}, +}; + +/// A SUBSCRIBE of its own for the relative ranges a finished copy's readers wait on their +/// publisher to start (see [`track::Producer::asks`]): the subscription that fed the copy is +/// over, so nothing else can carry them, and the copy never counts them over the groups it +/// holds. Its SUBSCRIBE_START answers them, and the groups it delivers land in the copy, so a +/// reader gets what a cold copy would. It FINs once the publisher declared the end and every +/// group from the lowest start up to it is accounted for and read, then waits for the +/// publisher's FIN in reply. +pub(super) struct Ask { + serve: TrackServe, + serving: track::Producer, + id: u64, + state: AskState, +} + +enum AskState { + Establish(Box), + Active { + sub: Box, + /// Whether our FIN went out. + finished: bool, + }, + Done, +} + +impl Ask { + /// Ask for what `serving`'s readers wait on, `None` when they wait on nothing. + pub fn new(serve: &TrackServe, serving: &track::Producer) -> Option { + let asks = serving.asks(); + if asks.is_empty() { + return None; + } + // Under the readers' own preferences, in key order, as a cold copy's reader asks. + let subscription = serving + .subscription() + .unwrap_or_default() + .with_ranges(asks) + .with_order(Order::Ascending); + let mut ranges = wire_ranges(&subscription)?; + // A relative end goes upstream open, so two asks may carry the same range. + let mut seen = std::collections::HashSet::new(); + ranges.retain(|range| seen.insert(*range)); + // The asks that don't fit wait for those sent to be answered, which changes the asks. + let fields = serve.subscribe_fields(&subscription); + let ranges = super::fit::Budget::new(serve.subscriber.version, fields, 0, ranges.len()).take(ranges); + if ranges.is_empty() { + return None; + } + let copy = Accepted { + producer: serving.clone(), + framing: lite::Framing::new(serving.info(), serve.subscriber.version), + }; + tracing::debug!(track = %serve.name, ?ranges, "asking where relative starts begin"); + let establish = serve.register(subscription, ranges, Some(copy)); + Some(Self { + serve: serve.clone(), + serving: serving.clone(), + id: establish.id, + state: AskState::Establish(Box::new(establish)), + }) + } + + /// Ready once the ask is over, or with why it failed: its readers then have no answer + /// coming. + pub fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { + loop { + match &mut self.state { + AskState::Establish(est) => match ready!(est.poll(waiter)) { + Ok(active) => { + self.state = AskState::Active { + sub: Box::new(active), + finished: false, + } + } + Err(err) => { + self.end(); + return Poll::Ready(Err(err)); + } + }, + AskState::Active { sub, finished } => { + if !*finished && let (Some(started), Some(end)) = (sub.started, &sub.end) { + let owed = started.unwrap_or(end.group)..end.group; + let settled = sub + .tail + .consume() + .poll(waiter, |tail| match tail.covers(owed.clone()) && !tail.is_reading() { + true => Poll::Ready(()), + false => Poll::Pending, + }) + .is_ready(); + if settled { + let _ = sub.stream.writer.finish(); + *finished = true; + } + } + if let Poll::Ready(Err(err)) = sub.stream.writer.poll_flush(waiter) { + tracing::debug!(%err, "ask subscribe write failed"); + } + let res = ready!(sub.stream.reader.poll_recv::(waiter)); + let now = self.serve.subscriber.runtime.now(); + match res { + Recv::Message(lite::SubscribeResponse::Start(start)) + if sub.started.is_none() && start.starts.len() as u64 == sub.live.initial_count() => + { + let edge = start.largest.map(|largest| largest.position.group); + for (id, start) in (0..).zip(&start.starts) { + sub.starts.insert(id, *start); + sub.edges.insert(id, edge); + } + sub.started = Some(start.starts.iter().flatten().min().copied()); + sub.report_starts(&self.serving); + } + Recv::Message(lite::SubscribeResponse::Drop(dropped)) => { + if let Ok(mut tail) = sub.tail.write() { + tail.account(dropped.groups, now); + } + } + // Never produced: the copy records the skip, as the subscription itself does. + Recv::Message(lite::SubscribeResponse::Skip(skipped)) => { + if !skipped.groups.is_empty() { + self.serving.skip_received(skipped.groups.clone()); + if let Ok(mut tail) = sub.tail.write() { + tail.account(skipped.groups, now); + } + } + } + Recv::Message(lite::SubscribeResponse::Finish(finish)) if sub.end.is_none() => { + sub.end = Some(finish); + } + Recv::Message(lite::SubscribeResponse::Ok(_)) => {} + // A second START or FINISH, or a STARTED for a range no update added. + Recv::Message(msg) => { + tracing::warn!(?msg, "unexpected ask subscribe response"); + self.end(); + return Poll::Ready(Err(Error::ProtocolViolation)); + } + // The publisher FINs in reply to ours, or once it finished the track. + Recv::Ended if *finished || sub.end.is_some() => { + self.end(); + return Poll::Ready(Ok(())); + } + Recv::Ended | Recv::Malformed(_) => { + tracing::warn!("ask subscribe ended without SUBSCRIBE_FINISH"); + self.end(); + return Poll::Ready(Err(Error::ProtocolViolation)); + } + // After our FIN everything it owes is in, so a reset, such as the publisher + // cutting a response short, costs nothing. + Recv::Reset(err) => { + tracing::debug!(%err, "ask subscribe ended"); + let finished = *finished; + self.end(); + return Poll::Ready(if finished { Ok(()) } else { Err(err) }); + } + } + } + AskState::Done => return Poll::Ready(Ok(())), + } + } + } + + /// Stop routing the subscription's streams, resetting it unless our FIN already went out. + fn end(&mut self) { + let state = std::mem::replace(&mut self.state, AskState::Done); + self.serve.subscriber.remove_subscribe(self.id); + match state { + AskState::Establish(est) => est.abort(&Error::Cancel), + AskState::Active { sub, finished: false } => sub.stream.writer.abort(&Error::Cancel), + _ => {} + } + } +} + +impl Drop for Ask { + fn drop(&mut self) { + if !matches!(self.state, AskState::Done) { + self.end(); + } + } +} diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 1dcd585fed..5aff8b96dd 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -10027,13 +10027,29 @@ impl Cursor { (misses.lowest_wanted(), misses.has_filled(), misses.is_waiting()) } - /// Whether a fill still owes this cursor a group, so the end of the feed is not its end. + /// Whether a fill still owes this cursor a group, or a relative start waits on its + /// publisher's report while a producer remains to record it, as a finished copy asks for + /// one: either way the end of the feed is not its end. fn awaits_fill(&self) -> bool { - self.misses.as_ref().is_some_and(|misses| misses.is_waiting()) + self.awaits_report() + || self.misses.as_ref().is_some_and(|misses| misses.is_waiting()) || self.filter.window.lookups().next().is_some() || self.heads.as_ref().is_some_and(|heads| !heads.fetching.is_empty()) } + /// Whether a relative start waits on its publisher's report while a producer remains to + /// record it. + fn awaits_report(&self) -> bool { + self.filter.window.is_counting() && !self.state.read().outcome.is_released() + } + + /// Wake on the track's next change while [`Self::awaits_report`], which a report is. + fn park_on_report(&self, waiter: &kio::Waiter) { + if self.awaits_report() { + let _ = self.state.poll(waiter, |_| Poll::<()>::Pending); + } + } + /// Move past the group `peeked` found and hand it out: the exact group, even if its /// producer evicted it since, so eviction cannot advance the reader past a gap. fn take(&mut self, peeked: Peeked) -> group::Consumer { @@ -10099,7 +10115,9 @@ impl Cursor { continue; } let Some((producer, found_index)) = ready!(polled)? else { - // The feed ended, but a fill still owes a group. Its answer wakes us. + // The feed ended, but a fill still owes a group. Its answer wakes us, as the + // report a relative start waits on does. + self.park_on_report(waiter); if self.awaits_fill() { return Poll::Pending; } @@ -10401,7 +10419,9 @@ impl Cursor { continue; } let Some(producer) = ready!(polled)? else { - // The feed ended, but a fill still owes a group. Its answer wakes us. + // The feed ended, but a fill still owes a group. Its answer wakes us, as the report + // a relative start waits on does. + self.park_on_report(waiter); if self.awaits_fill() { return Poll::Pending; } @@ -11205,10 +11225,11 @@ impl Subscriber { let unplaced = |window: &Window, floor| window.is_waiting_above(floor) || window.is_counting(); if unplaced(&cursor.filter.window, cursor.min_key) { // 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. + // closed track places it at once, since nothing more can arrive, except a relative + // start a finished copy still asks its publisher for. let closed = cursor.state.poll(waiter, |_| Poll::<()>::Pending).is_ready(); cursor.sync(); - if !closed && unplaced(&cursor.filter.window, cursor.min_key) { + if (!closed || cursor.awaits_report()) && unplaced(&cursor.filter.window, cursor.min_key) { return Poll::Pending; } } @@ -16478,9 +16499,17 @@ mod test { Some((9, Some(9))) ); - // Nothing counts over the groups held once the copy ends either. - let _finished = producer.finish().unwrap(); + // Nothing counts over the groups held once the copy ends either, and its reader waits for + // the report rather than end with nothing: a finished copy asks on a subscription of its + // own. + producer.finish_shared().unwrap(); assert_eq!(placed(&mut later), None); + assert!(later.recv_group().now_or_never().is_none(), "waits for the report"); + assert!(later.poll_start(&kio::Waiter::noop()).is_pending(), "so does its start"); + assert!(producer.asks().contains(&back(2).ranges[0]), "and asks for it"); + producer.relative_starts([(2, CountedStart::At(9), 11)]); + assert_eq!(read(&mut later), vec![9, 10, 11]); + assert!(matches!(later.recv_group().now_or_never(), Some(Ok(None))), "then ends"); } proptest::proptest! { diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index d4928a6c66..effd8ad8fb 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -612,6 +612,78 @@ async fn a_relative_start_never_resolves_against_the_groups_a_copy_holds() { } } +/// A finished copy has no upstream subscription left to carry a later reader's relative start, +/// so the copy asks again with a subscription of its own, and the reader still reads what the +/// publisher counts back to rather than nothing: the same groups a cold copy reads. Mirrors the +/// `@moq/net` `integration: lite-07 starts a relative start on a finished copy where the +/// publisher does`. +#[kio::test] +async fn a_relative_start_on_a_finished_copy_reads_the_publishers_groups() { + // Whether the copy holds every group the reader counts back to or only some. + for from in [0, 9] { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track( + "data", + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER), + ) + .unwrap(); + broadcast.announce().unwrap(); + for key in 0..=10 { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group.write_frame(millis(key), &b"x"[..]).unwrap(); + group.finish().unwrap(); + } + let _track = track.finish().unwrap(); + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + let subscribe = |ranges: Vec| { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges(ranges); + remote.track("data").unwrap().subscribe(subscription) + }; + let read = async |reader: &mut moq_net::track::Subscriber| { + let mut read = Vec::new(); + while let Some(group) = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap() + { + read.push(group.start); + } + read.sort_unstable(); + read + }; + + // The first reader holds the finished copy. + let mut first = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Time(from), None)])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut first).await, (from..=10).collect::>()); + // Its subscription is over by now. + kio::clock::sleep(Duration::from_secs(1)).await; + let mut later = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Relative(5), None)])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut later).await, [5, 6, 7, 8, 9, 10], "holding from {from}"); + drop(first); + } +} + /// A time end between two groups ends an ordered read once the publisher created a group past /// it: handing that group out would have passed the starts between. Through the publisher's own /// origin that is its own state. A lite-07 copy ends there only on what its wire proves, the From 7efd14b5fc347cbf0c4b3eec848514a5837bfdf4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 05:23:12 -0800 Subject: [PATCH 15/18] fix(net): a finished copy's ask settles each range's own groups before answering its readers An ask ending before the track waited for groups it never asked for, so it never FINed and held back the next ask. Its answer also reached readers before its groups did, letting one run out at a group still arriving. Co-Authored-By: Claude Opus 5.5 --- js/net/src/integration.test.ts | 9 ++++ js/net/src/lite/subscriber.ts | 30 ++++++++----- rs/moq-net/src/lite/subscriber/ask.rs | 42 ++++++++++++++----- rs/moq-net/tests/integration/range_anchors.rs | 12 ++++++ 4 files changed, 72 insertions(+), 21 deletions(-) diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index d8d8cefd29..05618ed458 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -690,6 +690,8 @@ for (const [from, closed] of [ // The first reader holds the finished copy. const first = subscribe([{ start: { time: from } }]); let later: ReturnType | undefined; + let short: ReturnType | undefined; + let next: ReturnType | undefined; try { const held: bigint[] = []; for (let key = from; key <= 10n; key++) held.push(key); @@ -706,7 +708,14 @@ for (const [from, closed] of [ } later = subscribe([{ start: { relative: 5n } }]); expect(await read(later)).toEqual([5n, 6n, 7n, 8n, 9n, 10n]); + // An ask ending before the track does is over at its own end, so the next one still goes out. + short = subscribe([{ start: { relative: 4n }, end: { time: 6n } }]); + expect(await read(short)).toEqual([6n]); + next = subscribe([{ start: { relative: 3n } }]); + expect(await read(next)).toEqual([7n, 8n, 9n, 10n]); } finally { + next?.close(); + short?.close(); later?.close(); first.close(); remote.close(); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index b489013c87..a1bed76a1a 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2941,9 +2941,9 @@ export class Subscriber { // Ask where the relative starts `copy`'s readers wait on begin, on a lite-07 SUBSCRIBE of its own: // the subscription that fed the finished copy is over, so nothing else can carry them, and the copy - // never counts them over the groups it holds. Its SUBSCRIBE_START answers them, and the groups it - // delivers land in the copy, so a reader gets what a cold copy would. It FINs once the publisher - // declared the end and every group from the lowest start up to it is accounted for and read. + // never counts them over the groups it holds. The groups it delivers land in the copy, and its + // SUBSCRIBE_START answers the readers once every group a range owes below the publisher's end is + // accounted for and read. It then FINs. // Rejects when it fails, leaving its readers no answer. Mirrors the Rust `ask::Ask`. async #askFinished(broadcast: Path.Valid, consumed: Consumed, copy: SubscribeEntry): Promise { const producer = copy.track; @@ -2997,12 +2997,21 @@ export class Subscriber { new Deadline(), ); stream = opened; - const finish = async () => { - const covered = () => - entry.start !== undefined && - entry.end !== undefined && - entry.tail.covers(entry.start ?? entry.end, entry.end); - await entry.tail.settle(covered, hooks.trackReleased(producer)); + // Every group a range owes below the publisher's end, from where it starts up to its own end. + const covered = (end: bigint) => + entry.start !== undefined && + entry.live.ids().every((id) => { + const start = entry.starts.get(id); + const range = entry.live.get(id); + if (start === undefined || range === undefined) return true; + const until = range.end?.absolute ?? (range.end?.time !== undefined ? range.end.time + 1n : end); + return entry.tail.covers(start, max(start, min(until, end))); + }); + // The readers take the answer only once those groups are in, since the copy's own tail settled + // long ago and a reader started earlier could run out at one still arriving. Then FIN. + const finish = async (end: bigint) => { + await entry.tail.settle(() => covered(end), hooks.trackReleased(producer)); + hooks.relativeStarts(producer, this.#counted(entry)); fin = true; opened.writer.close(); }; @@ -3027,12 +3036,11 @@ export class Subscriber { null, ); entry.tail.touch(); - hooks.relativeStarts(producer, this.#counted(entry)); } else if ("finish" in resp) { if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_FINISH"); entry.end = resp.finish.group; entry.tail.touch(); - void finish().catch(() => {}); + void finish(entry.end).catch(() => {}); } else if ("drop" in resp) { entry.tail.account(resp.drop.start, resp.drop.end); } else if ("skip" in resp) { diff --git a/rs/moq-net/src/lite/subscriber/ask.rs b/rs/moq-net/src/lite/subscriber/ask.rs index 5f93bf65c0..0a0c522600 100644 --- a/rs/moq-net/src/lite/subscriber/ask.rs +++ b/rs/moq-net/src/lite/subscriber/ask.rs @@ -13,10 +13,9 @@ use crate::{ /// A SUBSCRIBE of its own for the relative ranges a finished copy's readers wait on their /// publisher to start (see [`track::Producer::asks`]): the subscription that fed the copy is /// over, so nothing else can carry them, and the copy never counts them over the groups it -/// holds. Its SUBSCRIBE_START answers them, and the groups it delivers land in the copy, so a -/// reader gets what a cold copy would. It FINs once the publisher declared the end and every -/// group from the lowest start up to it is accounted for and read, then waits for the -/// publisher's FIN in reply. +/// holds. The groups it delivers land in the copy, and its SUBSCRIBE_START answers the readers +/// once every group a range owes below the publisher's end is accounted for and read. It then +/// FINs and waits for the publisher's FIN in reply. pub(super) struct Ask { serve: TrackServe, serving: track::Producer, @@ -89,17 +88,29 @@ impl Ask { } }, AskState::Active { sub, finished } => { - if !*finished && let (Some(started), Some(end)) = (sub.started, &sub.end) { - let owed = started.unwrap_or(end.group)..end.group; + if !*finished + && sub.started.is_some() + && let Some(end) = &sub.end + { + let owed: Vec<_> = sub + .live + .iter() + .filter_map(|(id, range)| Some(owed(range, (*sub.starts.get(&id)?)?, end.group))) + .collect(); let settled = sub .tail .consume() - .poll(waiter, |tail| match tail.covers(owed.clone()) && !tail.is_reading() { - true => Poll::Ready(()), - false => Poll::Pending, + .poll(waiter, |tail| { + match owed.iter().all(|owed| tail.covers(owed.clone())) && !tail.is_reading() { + true => Poll::Ready(()), + false => Poll::Pending, + } }) .is_ready(); if settled { + // Only now: the copy's own tail settled long ago, so a reader started + // earlier could run out at a group still arriving. + sub.report_starts(&self.serving); let _ = sub.stream.writer.finish(); *finished = true; } @@ -119,7 +130,6 @@ impl Ask { sub.edges.insert(id, edge); } sub.started = Some(start.starts.iter().flatten().min().copied()); - sub.report_starts(&self.serving); } Recv::Message(lite::SubscribeResponse::Drop(dropped)) => { if let Ok(mut tail) = sub.tail.write() { @@ -189,3 +199,15 @@ impl Drop for Ask { } } } + +/// The groups `range`, delivered from `start`, owes below the track's exclusive `end`. +fn owed(range: &lite::Range, start: u64, end: u64) -> std::ops::Range { + let until = match range.end { + // Stops just before it, taking the group it falls in when partway through one. + Some(lite::Anchor::Absolute(at)) => at.group.saturating_add(u64::from(at.frame > 0)), + // Includes the group containing it. + Some(lite::Anchor::Time(time)) => time.saturating_add(1), + _ => end, + }; + start..until.min(end).max(start) +} diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index effd8ad8fb..4740a6c1cb 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -680,6 +680,18 @@ async fn a_relative_start_on_a_finished_copy_reads_the_publishers_groups() { .expect("subscribe timeout") .unwrap(); assert_eq!(read(&mut later).await, [5, 6, 7, 8, 9, 10], "holding from {from}"); + // An ask ending before the track does is over at its own end, so the next one still goes + // out. + for (ranges, expected) in [ + (range(Anchor::Relative(4), Some(Anchor::Time(6))), vec![6]), + (range(Anchor::Relative(3), None), vec![7, 8, 9, 10]), + ] { + let mut reader = kio::clock::timeout(TIMEOUT, subscribe(vec![ranges])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut reader).await, expected, "holding from {from}"); + } drop(first); } } From 36618c59aa9292b49009bb5b499282585eec0769 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 05:34:35 -0800 Subject: [PATCH 16/18] fix(net): readers read a finished copy's ask as its groups land, waiting while it is out Withholding the ask's answer until all its groups arrived kept readers from consuming history that cache pressure could then evict. The answer goes to the readers at once again, and the copy counts the ask as out, so a reader waits for its groups rather than end at the boundary. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 9 ++++++ js/net/src/lite/subscriber.ts | 16 ++++++---- js/net/src/track.test.ts | 34 ++++++++++++++++++++- js/net/src/track.ts | 10 +++++- rs/moq-net/src/lite/subscriber/ask.rs | 15 +++++---- rs/moq-net/src/model/track.rs | 44 +++++++++++++++++++++++++-- 6 files changed, 111 insertions(+), 17 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 62e0507547..610aeb7c8e 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -788,6 +788,12 @@ export const hooks: { * `Producer::poll_asks_changed`; assigned by `track.ts`. */ asksChanged: (producer: Producer) => Getter; + /** + * Count an ask a finished lite-07 copy's session sends upstream as out while `asking`, and done + * once not: the groups it delivers land below the end, so a reader waits for them rather than end + * at the boundary. Mirrors the Rust `Producer::asking`; assigned by `track.ts`. + */ + asking: (producer: Producer, asking: boolean) => void; /** Mint a track {@link Request}; assigned by `track.ts`. */ makeRequest: (options: TrackRequestOptions) => Request; /** @@ -1221,6 +1227,9 @@ export const hooks: { asksChanged: () => { throw new Error("track.ts not loaded"); }, + asking: () => { + throw new Error("track.ts not loaded"); + }, relativeStarts: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index a1bed76a1a..f6914d5ab9 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2941,9 +2941,10 @@ export class Subscriber { // Ask where the relative starts `copy`'s readers wait on begin, on a lite-07 SUBSCRIBE of its own: // the subscription that fed the finished copy is over, so nothing else can carry them, and the copy - // never counts them over the groups it holds. The groups it delivers land in the copy, and its - // SUBSCRIBE_START answers the readers once every group a range owes below the publisher's end is - // accounted for and read. It then FINs. + // never counts them over the groups it holds. Its SUBSCRIBE_START answers the readers, and the + // groups it delivers land in the copy, which its readers wait for while it is out (see + // `hooks.asking`). It FINs once every group a range owes below the publisher's end is accounted for + // and read. // Rejects when it fails, leaving its readers no answer. Mirrors the Rust `ask::Ask`. async #askFinished(broadcast: Path.Valid, consumed: Consumed, copy: SubscribeEntry): Promise { const producer = copy.track; @@ -2980,6 +2981,7 @@ export class Subscriber { fillsChanged: new Signal(0), }; this.#subscribes.set(id, entry); + hooks.asking(producer, true); let stream: Stream | undefined; // Whether our FIN went out: the answer arrived and every group it owes is in, so a reset after // it, such as the publisher cutting a response short, costs nothing. @@ -3007,11 +3009,9 @@ export class Subscriber { const until = range.end?.absolute ?? (range.end?.time !== undefined ? range.end.time + 1n : end); return entry.tail.covers(start, max(start, min(until, end))); }); - // The readers take the answer only once those groups are in, since the copy's own tail settled - // long ago and a reader started earlier could run out at one still arriving. Then FIN. + // FIN once they are in. const finish = async (end: bigint) => { await entry.tail.settle(() => covered(end), hooks.trackReleased(producer)); - hooks.relativeStarts(producer, this.#counted(entry)); fin = true; opened.writer.close(); }; @@ -3036,6 +3036,8 @@ export class Subscriber { null, ); entry.tail.touch(); + // Read as they arrive: the readers wait for the rest while the ask is out. + hooks.relativeStarts(producer, this.#counted(entry)); } else if ("finish" in resp) { if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_FINISH"); entry.end = resp.finish.group; @@ -3057,6 +3059,8 @@ export class Subscriber { } finally { this.#subscribes.delete(id); entry.tail.close(); + // Its groups are in, or never coming: the readers end at the boundary again. + hooks.asking(producer, false); } } diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index fa498198ba..1b73b06e9d 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5860,11 +5860,43 @@ test("a received copy counts a relative start back as its publisher did", async expect(drain(longer)).toEqual([]); expect(longer.floor()).toBeUndefined(); - // Nothing counts over the groups held once the copy ends either. + // Nothing counts over the groups held once the copy ends either, and its reader waits for the + // report rather than end with nothing: a finished copy asks on a subscription of its own. hooks.endTrack(producer, { group: 11n, frame: 0 }); await settle(); expect(drain(later)).toEqual([]); expect(later.floor()).toBeUndefined(); + let ended = false; + void later.recvGroup().then((group) => { + ended = group === undefined; + }); + await settle(); + expect(ended).toBe(false); + + // While the ask is out, a reader waits for the groups it delivers rather than end at the + // boundary, reading each as it lands. + hooks.asking(producer, true); + const joined = producer.subscribe({ delay: { max: null }, ranges: [{ start: { time: 3n } }] }); + expect(drain(joined).sort((a, b) => Number(a - b))).toEqual([2n, 8n, 9n, 10n]); + putGroup(producer, 5n, 5); + await settle(); + expect(drain(joined)).toEqual([5n]); + // The rest were never produced, as the ask's GROUP Previous Starts say. + for (const [start, end] of [ + [0n, 1n], + [3n, 5n], + [6n, 8n], + ]) + hooks.skipTrackGroups(producer, start, end); + let done = false; + void joined.recvGroup().then((group) => { + done = group === undefined; + }); + await settle(); + expect(done).toBe(false); + hooks.asking(producer, false); + await settle(); + expect(done).toBe(true); }); // A relay serving a reader whose relative start still waits on its publisher's report can't say diff --git a/js/net/src/track.ts b/js/net/src/track.ts index dc9ce934c6..3369d8e435 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1301,6 +1301,8 @@ export class Producer { // Whether a copy's publisher reports where each range starts, so a relative start waits for that // rather than count over the groups held. Mirrors the Rust `Anchors::Reported`. #reportsStarts = false; + // The asks a finished copy's session has out (see `hooks.asking`). + #asking = 0; // The relative ranges each reader waits on the publisher to start, which the copy's subscription // asks upstream for. Mirrors the Rust `Subscriptions::asks`. #readerAsks = new Map(); @@ -1569,6 +1571,12 @@ export class Producer { return [...asks.values()]; }; hooks.asksChanged = (producer) => producer.#asksChanged; + hooks.asking = (producer, asking) => { + producer.#asking = Math.max(0, producer.#asking + (asking ? 1 : -1)); + const end = producer.#state.end.peek(); + const owed = producer.#asking > 0 || (end?.tail !== undefined && producer.#owes(finalKey(end.at))); + for (const state of [producer.#state, ...producer.#sinks]) state.owed.set(owed); + }; askStarts = (producer, sink, ranges) => { const last = producer.#readerAsks.get(sink) ?? []; const same = @@ -2736,7 +2744,7 @@ export class Producer { #owe(): void { const end = this.#state.end.peek(); if (!end?.tail) return; - const owed = this.#owes(finalKey(end.at)); + const owed = this.#asking > 0 || this.#owes(finalKey(end.at)); for (const state of [this.#state, ...this.#sinks]) state.owed.set(owed); this.#endLargest(); } diff --git a/rs/moq-net/src/lite/subscriber/ask.rs b/rs/moq-net/src/lite/subscriber/ask.rs index 0a0c522600..eecbf24a7c 100644 --- a/rs/moq-net/src/lite/subscriber/ask.rs +++ b/rs/moq-net/src/lite/subscriber/ask.rs @@ -13,9 +13,10 @@ use crate::{ /// A SUBSCRIBE of its own for the relative ranges a finished copy's readers wait on their /// publisher to start (see [`track::Producer::asks`]): the subscription that fed the copy is /// over, so nothing else can carry them, and the copy never counts them over the groups it -/// holds. The groups it delivers land in the copy, and its SUBSCRIBE_START answers the readers -/// once every group a range owes below the publisher's end is accounted for and read. It then -/// FINs and waits for the publisher's FIN in reply. +/// holds. Its SUBSCRIBE_START answers the readers, and the groups it delivers land in the copy, +/// which its readers wait for while it is out (see [`track::Producer::asking`]). It FINs once +/// every group a range owes below the publisher's end is accounted for and read, then waits for +/// the publisher's FIN in reply. pub(super) struct Ask { serve: TrackServe, serving: track::Producer, @@ -62,6 +63,7 @@ impl Ask { }; tracing::debug!(track = %serve.name, ?ranges, "asking where relative starts begin"); let establish = serve.register(subscription, ranges, Some(copy)); + serving.asking(true); Some(Self { serve: serve.clone(), serving: serving.clone(), @@ -108,9 +110,6 @@ impl Ask { }) .is_ready(); if settled { - // Only now: the copy's own tail settled long ago, so a reader started - // earlier could run out at a group still arriving. - sub.report_starts(&self.serving); let _ = sub.stream.writer.finish(); *finished = true; } @@ -130,6 +129,8 @@ impl Ask { sub.edges.insert(id, edge); } sub.started = Some(start.starts.iter().flatten().min().copied()); + // Read as they arrive: the readers wait for the rest while the ask is out. + sub.report_starts(&self.serving); } Recv::Message(lite::SubscribeResponse::Drop(dropped)) => { if let Ok(mut tail) = sub.tail.write() { @@ -183,6 +184,8 @@ impl Ask { /// Stop routing the subscription's streams, resetting it unless our FIN already went out. fn end(&mut self) { let state = std::mem::replace(&mut self.state, AskState::Done); + // Its groups are in, or never coming: the readers end at the boundary again. + self.serving.asking(false); self.serve.subscriber.remove_subscribe(self.id); match state { AskState::Establish(est) => est.abort(&Error::Cancel), diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 5aff8b96dd..5d33db810d 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -807,6 +807,9 @@ pub(crate) struct TrackState { // holds only part of the track, so it can't count back over its own groups. Replaced whole // as the ranges change, like `time_starts`. relative_starts: BTreeMap, + // The asks a finished copy's session has out (see [`Producer::asking`]): their groups may + // still land below the end, so no reader ends at it meanwhile. + asking: usize, // Counts changes to how a time start is placed other than by groups arriving or drops: // `time_starts`, `anchors`, and `starts_at_keys`. See [`Unplaced::mark`]. placing: u64, @@ -5037,6 +5040,19 @@ impl Producer { } } + /// Count an ask this finished copy's session sends upstream as out while `asking`, and done + /// once not: the groups it delivers land below the end, so a reader waits for them rather + /// than end at the boundary, while still reading those already in. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn asking(&self, asking: bool) { + if let Ok(mut state) = self.modify() { + state.asking = match asking { + true => state.asking + 1, + false => state.asking.saturating_sub(1), + }; + } + } + /// Where this copy's readers count a relative start back from now (see /// [`TrackState::latest_live`]). #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] @@ -10037,10 +10053,11 @@ impl Cursor { || self.heads.as_ref().is_some_and(|heads| !heads.fetching.is_empty()) } - /// Whether a relative start waits on its publisher's report while a producer remains to - /// record it. + /// Whether a relative start waits on its publisher's report, or an ask's groups may still + /// arrive (see [`Producer::asking`]), while a producer remains to record them. fn awaits_report(&self) -> bool { - self.filter.window.is_counting() && !self.state.read().outcome.is_released() + let state = self.state.read(); + (self.filter.window.is_counting() || state.asking > 0) && !state.outcome.is_released() } /// Wake on the track's next change while [`Self::awaits_report`], which a report is. @@ -16510,6 +16527,27 @@ mod test { producer.relative_starts([(2, CountedStart::At(9), 11)]); assert_eq!(read(&mut later), vec![9, 10, 11]); assert!(matches!(later.recv_group().now_or_never(), Some(Ok(None))), "then ends"); + + // While an ask is out, a reader waits for the groups it delivers rather than end at the + // boundary, reading each as it lands. + producer.asking(true); + let mut joined = producer.subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(3), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ); + assert_eq!(read(&mut joined), vec![2, 8, 9, 10, 11]); + assert!(joined.recv_group().now_or_never().is_none(), "waits on the ask"); + write_group(&producer, 5); + assert_eq!(read(&mut joined), vec![5]); + producer.asking(false); + assert!( + matches!(joined.recv_group().now_or_never(), Some(Ok(None))), + "then ends" + ); } proptest::proptest! { From 59360fc8551b1b98881c9148341a7f2d63eacadc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 05:54:14 -0800 Subject: [PATCH 17/18] fix(net): an ordered read waits on the gaps a finished copy's ask may fill Co-Authored-By: Claude Opus 5.5 --- js/net/src/track.test.ts | 35 +++++++++++++++++++++++ js/net/src/track.ts | 15 ++++++++++ rs/moq-net/src/model/track.rs | 52 +++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 1b73b06e9d..42bdbbe3f6 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5899,6 +5899,41 @@ test("a received copy counts a relative start back as its publisher did", async expect(done).toBe(true); }); +// While a finished copy's ask is out, a reader in key order waits at a gap below where the copy's +// feed started, which the ask may still fill, rather than pass it. Mirrors the Rust +// `an_ordered_read_waits_on_the_gaps_an_ask_may_fill`. +test("an ordered read waits on the gaps an ask may fill", async () => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + hooks.startTrack(producer, { start: 9n }); + for (const key of [9n, 10n]) putGroup(producer, key, Number(key)); + hooks.endTrack(producer, { group: 11n, frame: 0 }); + hooks.asking(producer, true); + hooks.relativeStarts(producer, [[5n, { at: 5n }, 10n]]); + const ordered = producer.subscribe({ delay: { max: null }, ranges: [{ start: { relative: 5n } }] }).ordered(); + const got: (bigint | undefined)[] = []; + const reading = (async () => { + for (;;) { + const group = await ordered.nextGroup(); + got.push(group?.start); + if (!group) return; + } + })(); + await settle(); + expect(got).toEqual([]); + putGroup(producer, 5n, 5); + await settle(); + expect(got).toEqual([5n]); + // The rest were never produced, as the ask's next GROUP says. + hooks.skipTrackGroups(producer, 6n, 9n); + await settle(); + expect(got).toEqual([5n, 9n, 10n]); + hooks.asking(producer, false); + await reading; + expect(got).toEqual([5n, 9n, 10n, undefined]); + producer.close(); +}); + // A relay serving a reader whose relative start still waits on its publisher's report can't say // where that range starts, so the feed's start stays pending rather than answer the range as empty, // until the report arrives or the copy closes. Mirrors the Rust `is_counting` in `poll_start`. diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 3369d8e435..d18956e2f8 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1718,6 +1718,9 @@ export class Producer { }; owedRuns = (producer, start, end) => { if (!producer.#wire) return []; + // An ask out may still deliver any gap the arrivals leave, whatever the feed's floor or grace + // (see `hooks.asking`). Mirrors the Rust `TrackState::owed`. + if (producer.#asking > 0) return [...producer.#askable(start, end)]; const floor = feedFloor(producer); return producer.#landing({ start: max(start, floor ?? 0n), @@ -1741,6 +1744,18 @@ export class Producer { return undefined; } + // The gaps an ask out may still fill in `[start, end)`: every one neither held nor given up, up + // to the declared end. + *#askable(start: bigint, end: bigint): Generator { + if (this.#state.closed.peek() !== undefined) return; + const declared = this.#state.end.peek()?.at; + if (declared !== undefined) end = min(end, finalKey(declared)); + const drops = this.#state.drops; + for (const gap of this.#arrivals.gaps(start, end)) { + yield* runGaps(drops.overlapping(gap.start, gap.end), gap.start, gap.end); + } + } + *#landing(land: Land): Generator { if (this.#state.closed.peek() !== undefined) return; let start = land.start; diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 5d33db810d..9aa9f16926 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3500,6 +3500,16 @@ impl TrackState { if !self.anchors.received() || self.outcome.is_released() || !self.readable() { return None; } + // An ask out may still deliver any gap the arrivals leave, whatever the feed's floor or + // grace (see [`Producer::asking`]). + if self.asking > 0 { + let end = groups.end.min(self.final_key().unwrap_or(u64::MAX)); + return self.arrivals.gaps(groups.start..end).find_map(|(gap, _)| { + self.drops + .gaps(&gap) + .find_map(|open| window.clip(open).map(|run| run.start)) + }); + } let asked = self.start.key().is_some(); self.find_landable(groups, asked, self.arrival_grace(), |open, _| { window.clip(open).map(|run| run.start) @@ -16550,6 +16560,48 @@ mod test { ); } + /// While a finished copy's ask is out, a reader in key order waits at a gap below where the + /// copy's feed started, which the ask may still fill, rather than pass it. + #[test] + #[cfg(feature = "async")] + fn an_ordered_read_waits_on_the_gaps_an_ask_may_fill() { + let mut producer = track_producer("test", Info::timed()); + producer.received(); + producer.reported(); + producer.start_resolved(9).unwrap(); + for key in [9, 10] { + write_group(&producer, key); + } + producer.report_edge(10); + producer.finish_shared().unwrap(); + producer.asking(true); + producer.relative_starts([(5, CountedStart::At(5), 10)]); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Relative(5), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + let mut next = || match ordered.poll_next_group(&kio::Waiter::noop()) { + Poll::Ready(Ok(Some(group))) => Some(group.start), + Poll::Ready(Ok(None)) => Some(u64::MAX), + _ => None, + }; + assert_eq!(next(), None, "group 5 may still land"); + write_group(&producer, 5); + assert_eq!(next(), Some(5)); + // The rest were never produced, as the ask's next GROUP says. + producer.skip_received(6..9); + assert_eq!(next(), Some(9)); + assert_eq!(next(), Some(10)); + producer.asking(false); + assert_eq!(next(), Some(u64::MAX), "then ends"); + } + proptest::proptest! { /// Whatever groups a lite-07 copy holds, a reader's relative start lands only where its /// publisher reported it, or waits: the cache never moves it. From 672dddac8dddb5566de4c0c294aeb0b23e0b1033 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 06:05:39 -0800 Subject: [PATCH 18/18] fix(net): an ordered read waits on an evicted group a finished copy's ask redelivers Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 5 +++ js/net/src/track.test.ts | 34 ++++++++++++++++++ js/net/src/track.ts | 18 +++++++--- rs/moq-net/src/model/track.rs | 66 +++++++++++++++++++++++++++++++---- 4 files changed, 112 insertions(+), 11 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 610aeb7c8e..f4abc105bc 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -794,6 +794,8 @@ export const hooks: { * at the boundary. Mirrors the Rust `Producer::asking`; assigned by `track.ts`. */ asking: (producer: Producer, asking: boolean) => void; + /** Evict group `key` from the cache, as the pool reclaiming it does. Tests only. */ + evict: (producer: Producer, key: bigint) => void; /** Mint a track {@link Request}; assigned by `track.ts`. */ makeRequest: (options: TrackRequestOptions) => Request; /** @@ -1230,6 +1232,9 @@ export const hooks: { asking: () => { throw new Error("track.ts not loaded"); }, + evict: () => { + throw new Error("track.ts not loaded"); + }, relativeStarts: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 42bdbbe3f6..3acb577838 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5934,6 +5934,40 @@ test("an ordered read waits on the gaps an ask may fill", async () => { producer.close(); }); +// While a finished copy's ask is out, a reader in key order waits for a group the copy held and +// evicted, which the ask delivers again, rather than pass it. Mirrors the Rust +// `an_ordered_read_waits_on_an_evicted_group_an_ask_redelivers`. +test("an ordered read waits on an evicted group an ask redelivers", async () => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + hooks.startTrack(producer, { start: 0n }); + for (let key = 0n; key <= 10n; key++) putGroup(producer, key, Number(key)); + hooks.endTrack(producer, { group: 11n, frame: 0 }); + for (let key = 0n; key < 9n; key++) hooks.evict(producer, key); + hooks.asking(producer, true); + hooks.relativeStarts(producer, [[5n, { at: 5n }, 10n]]); + const ordered = producer.subscribe({ delay: { max: null }, ranges: [{ start: { relative: 5n } }] }).ordered(); + const got: (bigint | undefined)[] = []; + const reading = (async () => { + for (;;) { + const group = await ordered.nextGroup(); + got.push(group?.start); + if (!group) return; + } + })(); + await settle(); + expect(got).toEqual([]); + for (let key = 5n; key < 9n; key++) { + putGroup(producer, key, Number(key)); + await settle(); + } + expect(got).toEqual([5n, 6n, 7n, 8n, 9n, 10n]); + hooks.asking(producer, false); + await reading; + expect(got).toEqual([5n, 6n, 7n, 8n, 9n, 10n, undefined]); + producer.close(); +}); + // A relay serving a reader whose relative start still waits on its publisher's report can't say // where that range starts, so the feed's start stays pending rather than answer the range as empty, // until the report arrives or the copy closes. Mirrors the Rust `is_counting` in `poll_start`. diff --git a/js/net/src/track.ts b/js/net/src/track.ts index d18956e2f8..d98fca21f3 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1571,6 +1571,12 @@ export class Producer { return [...asks.values()]; }; hooks.asksChanged = (producer) => producer.#asksChanged; + hooks.evict = (producer, key) => { + const entry = producer.#cached.get(key); + if (!entry) return; + producer.#evict(entry); + producer.#uncache(entry, true); + }; hooks.asking = (producer, asking) => { producer.#asking = Math.max(0, producer.#asking + (asking ? 1 : -1)); const end = producer.#state.end.peek(); @@ -1744,15 +1750,19 @@ export class Producer { return undefined; } - // The gaps an ask out may still fill in `[start, end)`: every one neither held nor given up, up - // to the declared end. + // The groups an ask out may still deliver in `[start, end)`: every one the copy doesn't hold and + // nothing gave up, up to the declared end, including one it held and evicted, which the ask + // delivers again. Mirrors the Rust `TrackState::owed`. *#askable(start: bigint, end: bigint): Generator { if (this.#state.closed.peek() !== undefined) return; const declared = this.#state.end.peek()?.at; if (declared !== undefined) end = min(end, finalKey(declared)); const drops = this.#state.drops; - for (const gap of this.#arrivals.gaps(start, end)) { - yield* runGaps(drops.overlapping(gap.start, gap.end), gap.start, gap.end); + let next = start; + for (let index = keyIndex(this.#keys, start); next < end; index++) { + const key = min(this.#keys[index] ?? end, end); + if (key > next) yield* runGaps(drops.overlapping(next, key), next, key); + next = key + 1n; } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 9aa9f16926..3b484b5108 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3500,15 +3500,22 @@ impl TrackState { if !self.anchors.received() || self.outcome.is_released() || !self.readable() { return None; } - // An ask out may still deliver any gap the arrivals leave, whatever the feed's floor or - // grace (see [`Producer::asking`]). + // An ask out may still deliver any group the copy doesn't hold, whatever the feed's floor + // or grace, including one it held and evicted, which the ask delivers again (see + // [`Producer::asking`]). if self.asking > 0 { let end = groups.end.min(self.final_key().unwrap_or(u64::MAX)); - return self.arrivals.gaps(groups.start..end).find_map(|(gap, _)| { - self.drops - .gaps(&gap) - .find_map(|open| window.clip(open).map(|run| run.start)) - }); + let mut next = groups.start; + let held = self.visible.range(groups.start..end).copied().chain([end]); + for key in held { + if key > next + && let Some(owed) = self.drops.gaps(&(next..key)).find_map(|open| window.clip(open)) + { + return Some(owed.start); + } + next = key.saturating_add(1); + } + return None; } let asked = self.start.key().is_some(); self.find_landable(groups, asked, self.arrival_grace(), |open, _| { @@ -16602,6 +16609,51 @@ mod test { assert_eq!(next(), Some(u64::MAX), "then ends"); } + /// While a finished copy's ask is out, a reader in key order waits for a group the copy held + /// and evicted, which the ask redelivers, rather than pass it. + #[test] + #[cfg(feature = "async")] + fn an_ordered_read_waits_on_an_evicted_group_an_ask_redelivers() { + let mut producer = track_producer("test", Info::timed()); + producer.received(); + producer.reported(); + producer.start_resolved(0).unwrap(); + for key in 0..=10 { + write_group(&producer, key); + } + producer.report_edge(10); + producer.finish_shared().unwrap(); + for key in 0..9 { + producer.evict(key); + } + producer.asking(true); + producer.relative_starts([(5, CountedStart::At(5), 10)]); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Relative(5), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + let mut next = || match ordered.poll_next_group(&kio::Waiter::noop()) { + Poll::Ready(Ok(Some(group))) => Some(group.start), + Poll::Ready(Ok(None)) => Some(u64::MAX), + _ => None, + }; + assert_eq!(next(), None, "group 5 is coming again"); + for key in 5..9 { + write_group(&producer, key); + assert_eq!(next(), Some(key)); + } + assert_eq!(next(), Some(9)); + assert_eq!(next(), Some(10)); + producer.asking(false); + assert_eq!(next(), Some(u64::MAX), "then ends"); + } + proptest::proptest! { /// Whatever groups a lite-07 copy holds, a reader's relative start lands only where its /// publisher reported it, or waits: the cache never moves it.