From c9f011ae86664809471698650339f0f592b40695 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 01:23:36 -0800 Subject: [PATCH 01/21] chore(quest): claim quest/m1/copy-recover.md Quest-Claim: 72fa5eb05288df94 From 9fbb39eb502f69123108c446bc46b33f87e379e6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 01:44:36 -0800 Subject: [PATCH 02/21] feat(net): a reader on a failed copy continues on the held re-offer A lite-07 re-offer held while the copy it duplicates still arrives now holds that copy's readers too: once the copy fails, a reader waits out the hold and continues on the copy that took the sequence, from its frame. Rust and js/net alike. Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 74 ++++++++++++++-- js/net/src/internal.ts | 29 ++++++- js/net/src/lite/subscriber.test.ts | 39 +++++++-- js/net/src/lite/subscriber.ts | 20 +++-- js/net/src/track.test.ts | 56 ++++++++++++ js/net/src/track.ts | 12 ++- rs/moq-net/src/lite/subscriber.rs | 31 ++++--- rs/moq-net/src/model/group.rs | 51 ++++++++++- rs/moq-net/src/model/track.rs | 132 +++++++++++++++++++++++++++-- 9 files changed, 406 insertions(+), 38 deletions(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 9c304c50550..d8d63638153 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -130,12 +130,23 @@ class GroupState { // Tells the group this copy was mirrored from whether its reader will still read it, so a // copy buffered for a reader that passed it stops keeping the group wanted. reads?: (reads: boolean) => void; + // A received copy's, shared with every mirror: what may take the group over once it fails. + succession?: Succession; constructor(sequence: bigint) { this.sequence = sequence; } } +// Another copy of a received group's sequence that may take the group over once this one fails, +// so its readers continue there (see `Consumer.#succeed`). A group's name fixes its content. +class Succession { + // Duplicates waiting on this copy while it arrives (see `hooks.holdGroup`). + readonly holds = new Signal(0); + // The copy that took the sequence after this one failed. + next?: Producer; +} + // A written frame and when it landed. It keeps the writer's frame rather than copying it, so a // lazy payload is read only when a reader takes it. class Landed implements Frame { @@ -300,6 +311,23 @@ export class Producer { if (state.closed.peek() !== undefined) flate.finish(); }; hooks.groupOffset = (group) => group.#state.offset; + hooks.receivedGroup = (group, failed) => { + group.#state.succession = new Succession(); + if (failed === undefined) return; + const succession = failed.#state.succession; + if (succession) succession.next = group; + }; + hooks.holdGroup = (group) => { + const holds = group.#state.succession?.holds; + if (!holds) return () => {}; + holds.update((n) => n + 1); + let held = true; + return () => { + if (!held) return; + held = false; + holds.update((n) => n - 1); + }; + }; hooks.startGroup = (group, start) => { if (!Number.isSafeInteger(start) || start < 0) throw new RangeError("invalid group frame start"); const state = group.#state; @@ -390,6 +418,7 @@ export class Producer { dst.flate = this.#state.flate; dst.compressed = this.#state.compressed; dst.charge = this.#state.charge; + dst.succession = this.#state.succession; // A track restamps its own mirror with when the group joined it. dst.arrived = this.#state.arrived; if (this.#state.start === this.#state.offset && !headShed(this.#state.frames.peek().first)) { @@ -983,9 +1012,36 @@ export class Consumer { return closed instanceof Error && !this.#recovers(closed) ? closed : undefined; } - // Whether a failure of the copy being read may still be continued from another route. + // Whether a failure of the copy being read may still be continued from another copy. #recovers(failed: Error): boolean { - return !this.#verdict.peek() && !this.#torn() && this.#recover?.wants(failed) === true; + return !this.#verdict.peek() && !this.#torn() && (this.#succeeds() || this.#recover?.wants(failed) === true); + } + + // Whether another copy in the track may take over the failed one being read: one is held, or took it. + #succeeds(): boolean { + const succession = this.#state.succession; + return succession !== undefined && (succession.holds.peek() > 0 || succession.next !== undefined); + } + + // Continue on the copy that took this group's sequence in its track once the one being read + // failed, waiting out a duplicate still held for it. Returns whether the read should look again. + async #succeed(): Promise { + const succession = this.#state.succession; + if (!succession || this.#verdict.peek() || this.#torn()) return false; + while (succession.holds.peek() > 0) { + await Signal.race(succession.holds, this.#verdict); + if (this.#verdict.peek()) return true; + } + const next = succession.next; + if (!next) return false; + const group = next.mirror(); + // It starts past the frame read next, which no copy holds now. + if (group.#state.start > this.#index()) { + group.close(); + return false; + } + this.#switch(group, this.#expiry); + return true; } // Whether a frame handed out to stream was cut short. @@ -1173,11 +1229,17 @@ export class Consumer { return; } replacement.adopt(); + this.#switch(replacement.group, replacement.expiry); + } + + // Read the rest of the group from `group`, which starts at or below the frame this one reached. + #switch(group: Consumer, expiry: Expiry | undefined): void { + const index = this.#index(); // The copy left behind stops teeing into the state this consumer read. if (this.#state.closed.peek() === undefined) this.#state.closed.set(null); this.#arrived ??= this.#state.arrived; - this.#state = next; - this.#expiry = replacement.expiry; + this.#state = group.#state; + this.#expiry = expiry; this.#skip = index; this.#closed?.refresh(); this.#expired?.refresh(); @@ -1370,9 +1432,11 @@ export class Consumer { const closed = this.#state.closed.peek(); if (closed instanceof Error) { + if (await this.#succeed()) continue; if (await this.#resume(closed)) continue; this.#abandon(); - throw closed; + // Ends the reader, whose failure a copy that might have taken over left open. + throw this.#fail(closed); } if (closed !== undefined) { // The copy finished the group: a replacement asked for meanwhile is moot. diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index a909dde28b3..315833603ac 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -393,6 +393,14 @@ export interface GroupRecover { replace(index: number, failed: Error | undefined, signal: AbortSignal): Promise; } +/** A copy of a group still arriving, held for a duplicate that waits on it. */ +export interface Arriving { + /** How the copy ends. */ + readonly settled: GetPromise; + /** Stop holding it, once the duplicate took the group or gave up. Idempotent. */ + release(): void; +} + /** A candidate copy of a group, committed only once the read can use it. */ export interface Replacement { readonly group: GroupConsumer; @@ -621,8 +629,12 @@ export const hooks: { * fill's subscription answered, which the feed never promised. Assigned by `track.ts`. */ receiveGroup: (producer: Producer, group: { sequence: bigint; start: number; fill?: boolean }) => GroupProducer; - /** How the copy of `sequence` still arriving ends, if one is, for a duplicate to wait on. Assigned by `track.ts`. */ - arrivingGroup: (producer: Producer, sequence: bigint) => GetPromise | undefined; + /** + * Hold the copy of `sequence` still arriving, if one is, for a duplicate to wait on: until + * released, a reader of that copy that fails waits for the duplicate to take the group. Assigned + * by `track.ts`. + */ + arrivingGroup: (producer: Producer, sequence: bigint) => Arriving | undefined; /** The producer's largest reported location and its subsequent changes. */ producerLargest: (producer: Producer) => Getter; @@ -900,6 +912,13 @@ export const hooks: { * out, so its `closed` never settles on a failure the recovery continues past. */ recoverGroup: (group: GroupConsumer, recover: GroupRecover) => void; + /** + * Mark `group` a received copy, which a later copy of its sequence may take over once it fails, + * and `failed`, if given, as the copy it took over from. Assigned by `group.ts`. + */ + receivedGroup: (group: GroupProducer, failed?: GroupProducer) => void; + /** Hold a received copy still arriving for a duplicate: see {@link Arriving}. Assigned by `group.ts`. */ + holdGroup: (group: GroupProducer) => () => void; /** Read a payload chunk, releasing its wait when the output stream closes. */ readFrameChunk: (frame: FrameConsumer, closed: Promise) => Promise; /** Read the next frame, as its stored DEFLATE slice when `compressed`, so a lite-07 publisher forwards it without inflating. */ @@ -1171,6 +1190,12 @@ export const hooks: { arrivingGroup: () => { throw new Error("track.ts not loaded"); }, + receivedGroup: () => { + throw new Error("group.ts not loaded"); + }, + holdGroup: () => { + throw new Error("group.ts not loaded"); + }, groupOffset: () => { throw new Error("group.ts not loaded"); }, diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index dc3d26819cf..e76b14a51dc 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -1371,7 +1371,8 @@ test("a group that beats its track info waits for it", async () => { }); // A lite-07 re-offer can overtake the reset of the stream it replaces: it waits, unread, until that -// copy ends. It is refused as a duplicate once the copy finishes, and takes the group once it fails. +// copy ends. It is refused as a duplicate once the copy finishes, and takes the group once it fails, +// continuing the reads of that copy from their frame. test.each([false, true])("a duplicate of an arriving copy waits for it to settle (first fails: %p)", async (fails) => { const version = Version.DRAFT_07; const { quic, streams, opened } = fakeSession(); @@ -1405,7 +1406,29 @@ test.each([false, true])("a duplicate of an arriving copy waits for it to settle const group = await track.recvGroup(); expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([7])); - const duplicate = await groupStream(0, version); + const resent: Uint8Array[] = []; + const resend = new Writer( + new WritableStream({ write: (chunk) => void resent.push(new Uint8Array(chunk)) }), + version, + ); + for (const [timestamp, payload] of [ + [0n, 7], + [2n, 8], + ] as const) { + await resend.u62(timestamp); + await resend.u53(1); + await resend.write(new Uint8Array([payload])); + } + const duplicate = new Reader( + new ReadableStream({ + start: (controller) => { + for (const chunk of resent) controller.enqueue(chunk); + controller.close(); + }, + }), + undefined, + version, + ); const stop = spyOn(duplicate, "stop"); let held = true; const receiving = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0n }), duplicate).finally(() => { @@ -1424,16 +1447,22 @@ test.each([false, true])("a duplicate of an arriving copy waits for it to settle return; } - // The reset lands: the held stream takes the group. + // The reset lands: the reader already handed the original waits on the held stream rather + // than failing, and continues on it from its frame once it takes the group. + const next = group?.readFrame(); first.error(new Error("peer reset")); await original; await receiving; - expect(await group?.closed).toBeInstanceOf(Error); + expect((await next)?.payload).toEqual(new Uint8Array([8])); + expect(await group?.readFrame()).toBeUndefined(); + expect(await group?.closed).toBeNull(); + // One that had not reached it reads the re-offer whole. const later = subscriber.consume(Path.from("room")).track("video").subscribe(); try { const again = await later.recvGroup(); expect(again?.sequence).toBe(0n); - expect((await again?.readFrame())?.payload).toEqual(new Uint8Array([0])); + expect((await again?.readFrame())?.payload).toEqual(new Uint8Array([7])); + expect((await again?.readFrame())?.payload).toEqual(new Uint8Array([8])); } finally { later.close(); } diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 808db75da13..5eaf9ed5b58 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -27,6 +27,7 @@ import * as netGroup from "../group.ts"; import { Cost, type Hop, MAX_HOPS, routesEqual, UNKNOWN_HOP, WireRoute } from "../hop.ts"; import * as Instance from "../instance.ts"; import { + type Arriving, bestCandidate, type DropRun, hiddenBelow, @@ -3126,6 +3127,7 @@ export class Subscriber { const { track, timescale, tail } = entry; let producer: netGroup.Producer | undefined; + let arriving: Arriving | undefined; const read = tail.open(group.sequence); this.#reoffered(track, group.sequence, group.sequence + 1n); @@ -3142,18 +3144,25 @@ export class Subscriber { } // A duplicate of a copy still arriving, such as a lite-07 re-offer that overtook the reset // of the stream it replaces, waits unread, which flow control bounds. Created once that copy - // ends, it is refused as a duplicate if the copy finished and takes the group if it failed. - // Its own reset ends the wait; a clean end leaves it on the copy alone. - for (let arriving = hooks.arrivingGroup(track, group.sequence); arriving !== undefined; ) { - const settled = arriving; + // ends, it is refused as a duplicate if the copy finished and takes the group if it failed, + // continuing the reads of that copy, which wait while it is held. Its own reset ends the + // wait; a clean end leaves it on the copy alone. + arriving = hooks.arrivingGroup(track, group.sequence); + while (arriving !== undefined) { + const settled = arriving.settled; await race([settled, stream.closed.then(() => settled)]); - arriving = hooks.arrivingGroup(track, group.sequence); + const next = hooks.arrivingGroup(track, group.sequence); + if (next === undefined) break; + arriving.release(); + arriving = next; } producer = hooks.receiveGroup(track, { sequence: group.sequence, start: group.frameStart, fill: entry.filling, }); + // Released once this copy took the group, so the failed copy's readers find it. + arriving?.release(); const reading = readFrames(stream, producer, { scale: timescale, compress: entry.compress }); await (entry.filling ? untilAbandoned(producer, reading) : reading); @@ -3174,6 +3183,7 @@ export class Subscriber { producer?.abort(e); stream.stop(e); } finally { + arriving?.release(); read(); } } diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 7738ac5977e..00bd6e06bdb 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3402,6 +3402,62 @@ test("an upstream drop ends a group still arriving with its error", async () => producer.close(); }); +// A reader on a received copy that fails waits out a duplicate still held for its group, then +// continues on it from its frame with nothing repeated or missed. A released hold leaves the +// failure, as does a duplicate starting past the reader's frame. +test.each(["reoffer", "released", "past"])("a failed copy continues on its held re-offer (%p)", async (kind) => { + const producer = new TrackProducer("video").accept({ maxAge: null }); + hooks.receiveTrack(producer); + const first = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + first.writeFrame({ payload: enc.encode("a") }); + const reader = first.consume(); + expect(await reader.readString()).toBe("a"); + + const arriving = hooks.arrivingGroup(producer, 3n); + expect(arriving).toBeDefined(); + const failed = new Error("reset"); + first.abort(failed); + let read: string | undefined | Error; + const reading = reader.readString().then( + (frame) => { + read = frame; + }, + (err: Error) => { + read = err; + }, + ); + await settle(); + expect(read).toBeUndefined(); + expect(reader.closed.peek()).toBeUndefined(); + + if (kind === "released") { + arriving?.release(); + await reading; + expect(read).toBe(failed); + expect(await reader.closed).toBe(failed); + producer.close(); + return; + } + const again = hooks.receiveGroup(producer, { sequence: 3n, start: kind === "past" ? 2 : 0 }); + arriving?.release(); + if (kind === "past") { + await reading; + expect(read).toBe(failed); + producer.close(); + return; + } + await settle(); + expect(read).toBeUndefined(); + again.writeFrame({ payload: enc.encode("a") }); + again.writeFrame({ payload: enc.encode("b") }); + again.finish(); + await reading; + expect(read).toBe("b"); + expect(await reader.readString()).toBeUndefined(); + expect(await reader.closed).toBeNull(); + producer.close(); +}); + test("a drop below the window is reported once the window lowers to it", () => { const producer = new TrackProducer("video").accept({}); const reader = producer diff --git a/js/net/src/track.ts b/js/net/src/track.ts index cbf920d49f0..d6ad38820d4 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1337,10 +1337,12 @@ export class Producer { hooks.receiveTrack = (producer, starts = {}) => { producer.#wire = { starts, served: producer.#wire?.served }; }; - hooks.receiveGroup = (producer, { sequence, start, fill }) => producer.#createGroup(sequence, start, fill); + hooks.receiveGroup = (producer, { sequence, start, fill }) => + producer.#createGroup(sequence, start, fill, true); hooks.arrivingGroup = (producer, sequence) => { - const closed = producer.#cached.get(sequence)?.group.closed; - return closed?.peek() === undefined ? closed : undefined; + const group = producer.#cached.get(sequence)?.group; + if (!group || group.closed.peek() !== undefined) return undefined; + return { settled: group.closed, release: hooks.holdGroup(group) }; }; sourceStart = (producer) => producer.#wire?.served && producer.#feedStart(); fillFloor = (producer, named) => producer.#fillFloor(named); @@ -2176,7 +2178,7 @@ export class Producer { return this.#createGroup(sequence, 0); } - #createGroup(sequence: bigint, start: number, fetched = false): GroupProducer { + #createGroup(sequence: bigint, start: number, fetched = false, received = false): GroupProducer { this.#writable(sequence); // A skip says the sequence will never be produced, so it is final. A drop is not: a // publisher may re-create a group it cut or aborted. @@ -2194,6 +2196,8 @@ export class Producer { const group = new GroupProducer(sequence); // Mirrors copy the absolute indices, so set them before publishing to any sink. hooks.startGroup(group, start); + // Before any mirror, which shares it: the failed copy's readers continue on this one. + if (received) hooks.receivedGroup(group, existing?.group); this.#bind(group); this.#cap(group); this.#publish(group, fetched); diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 8cbbce1efd3..a466b282feb 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -243,7 +243,8 @@ struct Opened { /// overtook the reset of the stream it replaces. It waits unread, which its stream credit /// bounds, and is discarded if that copy finishes or takes the group if it fails. struct Duplicate { - arriving: group::Handle, + /// 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. reading: Reading, @@ -1505,7 +1506,7 @@ impl GroupRecv { if let Poll::Ready(err) = duplicate.track.poll_aborted(waiter) { break Poll::Ready(Err(err)); } - match duplicate.arriving.poll_closed(waiter) { + match duplicate.arriving.arriving().poll_closed(waiter) { Poll::Pending => { *open = Some(Open::Duplicate(duplicate)); break Poll::Pending; @@ -1517,7 +1518,12 @@ impl GroupRecv { let Some(entry) = subs.get(&hdr.subscribe) else { break Poll::Ready(Err(Error::Cancel)); }; - match entry.reopen(hdr, self.subscriber.version, duplicate.reading) { + let Duplicate { arriving, reading, .. } = duplicate; + let reopened = entry.reopen(hdr, self.subscriber.version, reading); + // Released once the group is opened again, so the failed copy's readers + // waiting on it find this copy. + drop(arriving); + match reopened { Ok(reopened) => *open = Some(reopened), Err(err) => break Poll::Ready(Err(err)), } @@ -5043,7 +5049,7 @@ mod tests { /// A lite-07 re-offer can overtake the reset of the stream it replaces: its stream waits, /// unread, until that copy settles. It is discarded once the copy finishes, and takes the - /// group once the copy fails. + /// group once the copy fails, continuing the reads of that copy. #[kio::test] async fn a_duplicate_of_an_arriving_copy_waits_for_it_to_settle() { use crate::transport::poll::Session as _; @@ -5060,6 +5066,8 @@ mod tests { .encode(&mut encoder, version) .unwrap(); encoder.varint(crate::coding::varint::zigzag(300)).unwrap(); + encoder.bytes(b"first").unwrap(); + encoder.varint(crate::coding::varint::zigzag(1)).unwrap(); encoder.bytes(b"again").unwrap(); let session = crate::lite::test_transport::ScriptedSession::eof(script); let clock = crate::time::Deadlines::new(crate::time::Instant::now()); @@ -5118,26 +5126,29 @@ mod tests { assert_eq!(tail.read().streams(), 1); let mut original = reader.assert_group(); + let frame = original.read_frame().await.unwrap().unwrap(); + assert_eq!(&frame.payload[..], b"first"); if !first_fails { first.finish().unwrap(); let res = kio::wait(|waiter| duplicate.poll_serve(waiter)).await; assert!(matches!(res, Err(Error::Duplicate)), "{res:?}"); - let frame = original.read_frame().await.unwrap().unwrap(); - assert_eq!(&frame.payload[..], b"first"); assert!(original.read_frame().await.unwrap().is_none()); continue; } - // The reset lands: the held stream takes the group. + // The reset lands: the reader already handed the original waits on the held stream + // rather than failing, and continues on it from its frame once it takes the group. first.abort(Error::Cancel).unwrap(); + assert!(original.poll_read_frame(&waiter).is_pending()); let res = kio::wait(|waiter| duplicate.poll_serve(waiter)).await; assert!(res.is_ok(), "{res:?}"); - // A reader already handed the original sees it fail, since no sequence is handed - // out twice; one that had not reached it reads the re-offer. - assert!(original.read_frame().await.is_err()); + assert_eq!(&original.read_frame().await.unwrap().unwrap().payload[..], b"again"); + assert!(original.read_frame().await.unwrap().is_none()); + // One that had not reached it reads the re-offer whole. let mut later = track.subscribe(subscription); let mut group = later.assert_group(); assert_eq!(group.sequence, 3); + assert_eq!(&group.read_frame().await.unwrap().unwrap().payload[..], b"first"); assert_eq!(&group.read_frame().await.unwrap().unwrap().payload[..], b"again"); assert!(group.read_frame().await.unwrap().is_none()); } diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 2a28ef5afe6..1e70657a858 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -4313,7 +4313,16 @@ impl Consumer { ride.skip -= 1; continue; } - if !matches!(res, Poll::Ready(Err(_))) || !self.shed_next() { + if !matches!(res, Poll::Ready(Err(_))) { + return res; + } + match self.poll_successor(waiter) { + Poll::Ready(true) => continue, + Poll::Ready(false) => {} + // Waiting on a re-offer is blocked like a read at the edge. + Poll::Pending => return self.poll_expired_if_blocked(waiter, Poll::Pending), + } + if !self.shed_next() { return res; } // The copy just boarded shed the frame before the reader took it: pool debt @@ -4341,6 +4350,46 @@ impl Consumer { } } + /// Continue on another copy of this group in the same track once the copy read failed, + /// such as a re-offer held while that copy was still arriving (see [`track::successor`]). + /// Returns whether the reader moved to one. + fn poll_successor(&mut self, waiter: &kio::Waiter) -> Poll { + let (index, raw) = (self.cursor.index, self.cursor.raw); + { + let state = self.cursor.state.read(); + // Only a delivery failure: content that failed to inflate is the group's under its + // name, and a frame handed out partly is lost with the copy. Ordinals stand for + // frames only on a dense copy. + if self.expired + || self.carry.as_ref().is_some_and(|carry| carry.ride.is_some()) + || state.outcome.error().is_none() + || (!raw && state.malformed()) + || state.committed() < index + || state.sparse() + { + return Poll::Ready(false); + } + } + let Some(track) = self.cursor.cache.track() else { + return Poll::Ready(false); + }; + let Some(copy) = ready!(track::successor(&track, self, waiter)) else { + return Poll::Ready(false); + }; + { + let state = copy.cursor.state.read(); + // A plain read inflates from frame 0, and a copy that shed the frame can't serve it. + if state.sparse() + || (!raw && state.flate.is_some() && state.offset > 0) + || state.check_read(index, raw).is_err() + { + return Poll::Ready(false); + } + } + self.adopt_copy(copy); + Poll::Ready(true) + } + fn recover(&self) -> Option<&super::resume::Recover> { self.carry.as_ref()?.recover.as_ref() } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index ddd992be98b..07679e74e46 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -42,7 +42,7 @@ pub(crate) use hole::Hole; pub(crate) use fill::{GroupFill, Readers as FillReaders}; use std::{ - collections::{BTreeMap, BTreeSet, VecDeque}, + collections::{BTreeMap, BTreeSet, VecDeque, btree_map}, ops::{self, Bound, RangeBounds}, sync::Arc, sync::OnceLock, @@ -583,6 +583,9 @@ pub(crate) struct TrackState { // the readers riding them; see [`Self::board`]. Held weakly, so the last rider leaving // frees one, and pruned as they are looked up. flights: BTreeMap>, + // How many received copies wait on another copy of each sequence still arriving (see + // [`Hold`]), which a reader of that copy waits out when it fails; see [`Self::successor`]. + holds: BTreeMap, // Publisher-produced groups in arrival order as (sequence, stamp), walked by // subscriptions; an entry only resolves while its stamp matches the slot's. @@ -3780,6 +3783,33 @@ impl TrackState { } } +/// The copy of its group `reader` continues on once the copy it reads failed: one holding the +/// group from the reader's frame, once no re-offer of it is still held (see [`Hold`]). A +/// group's name fixes its content, so any such copy continues it. +pub(super) fn successor( + track: &kio::Producer, + reader: &group::Consumer, + waiter: &kio::Waiter, +) -> Poll> { + let sequence = reader.sequence; + let found = track.poll_ref(waiter, |state| { + if state.holds.contains_key(&sequence) { + return Poll::Pending; + } + Poll::Ready( + state + .covering_group(sequence, reader.index()) + .filter(|copy| !copy.is_read_by(reader)) + .and_then(group::Handle::try_consume), + ) + }); + Poll::Ready(match found { + Poll::Ready(Ok(copy)) => copy, + Poll::Ready(Err(_closed)) => None, + Poll::Pending => return Poll::Pending, + }) +} + /// Where a reader at a frame its copy shed goes: see [`board`]. pub(super) enum Board { /// Ride this copy, a flight or a cached copy, which holds the frame. @@ -3996,7 +4026,41 @@ pub(crate) enum Received { /// Another copy of the sequence that is still arriving: whether this one is needed /// depends on how that one settles. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - Held(group::Handle), + Held(Hold), +} + +/// A received copy waiting on another copy of its sequence still arriving. While it waits, +/// a reader of that copy that fails waits for this one to take the group instead of failing. +#[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] +pub(crate) struct Hold { + arriving: group::Handle, + track: kio::Weak, + sequence: u64, +} + +#[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] +impl Hold { + /// The copy still arriving. + pub(crate) fn arriving(&self) -> &group::Handle { + &self.arriving + } +} + +impl Drop for Hold { + fn drop(&mut self) { + let Some(track) = self.track.upgrade() else { + return; + }; + let Ok(mut state) = track.write() else { + return; + }; + if let btree_map::Entry::Occupied(mut holds) = state.holds.entry(self.sequence) { + *holds.get_mut() -= 1; + if *holds.get() == 0 { + holds.remove(); + } + } + } } /// A producer for a track, used to create new groups. @@ -4152,10 +4216,16 @@ impl Producer { let start = received.unwrap_or(0); let content = if received.is_some() { if let Some(holding) = state.holding_group(info.sequence, start) { - return match holding.is_settled() { - true => Err(Error::Duplicate), - false => Ok(Received::Held(holding.clone())), - }; + if holding.is_settled() { + return Err(Error::Duplicate); + } + let arriving = holding.clone(); + *state.holds.entry(info.sequence).or_default() += 1; + return Ok(Received::Held(Hold { + arriving, + track: self.state.downgrade(), + sequence: info.sequence, + })); } state.content_for_copy(info.sequence)? } else { @@ -17100,6 +17170,56 @@ mod test { producer } + /// A reader on a received copy that fails waits out a re-offer still held for its group, + /// then continues on it from its frame with nothing repeated or missed. A released hold + /// leaves the failure, as does a re-offer starting past the reader's frame. + #[kio::test] + #[cfg(feature = "async")] + async fn a_failed_copy_continues_on_its_held_reoffer() { + for case in ["reoffer", "released", "past"] { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(300), &b"a"[..]).unwrap(); + let mut reader = first.consume(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a", "{case}"); + + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let waiter = kio::Waiter::noop(); + assert!( + reader.poll_read_frame(&waiter).is_pending(), + "{case}: waits on the held re-offer" + ); + + if case == "released" { + drop(hold); + assert!(reader.read_frame().await.is_err(), "{case}"); + continue; + } + let start = if case == "past" { 2 } else { 0 }; + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, start) else { + panic!("the re-offer takes the group"); + }; + drop(hold); + if case == "past" { + assert!(reader.read_frame().await.is_err(), "{case}: frame 1 is in no copy"); + continue; + } + // Parked on frame 1, which the re-offer has yet to write. + assert!(reader.poll_read_frame(&waiter).is_pending()); + again.write_frame(at(300), &b"a"[..]).unwrap(); + again.write_frame(at(301), &b"b"[..]).unwrap(); + again.finish().unwrap(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + assert!(reader.read_frame().await.unwrap().is_none()); + } + } + /// Create and finish group `sequence`, with one frame at `millis` on a timed track. #[cfg(feature = "async")] fn put(producer: &Producer, sequence: u64, millis: Option) { From 45b5692a1f1886fef68c9c582b1f716b570e46e2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 01:45:13 -0800 Subject: [PATCH 03/21] quest: finish copy-recover Co-Authored-By: Claude Opus 5.5 --- quest/m1/copy-recover.md | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 quest/m1/copy-recover.md diff --git a/quest/m1/copy-recover.md b/quest/m1/copy-recover.md deleted file mode 100644 index aeabf7e4f7c..00000000000 --- a/quest/m1/copy-recover.md +++ /dev/null @@ -1,26 +0,0 @@ -# [M] A reader on a failed copy continues on the held re-offer - -## Goal - -A live reader already handed a copy of a group that then fails keeps the -group: its `group::Consumer` continues on the successor copy the held -re-offer carries in the same track, at the same frame, instead of surfacing -the failure. Rust and `js/net` behave the same. - -## Plan - -Planned 2026-11-05 (maintainer, /quest-plan of the follow-ups from the -2026-11-05 /quest-complete run), from #7670. Milestone: ✅ m1. - -`with_recover` already continues a reader across a front's routes; reuse it -or its mechanism for the same-track successor rather than adding a parallel -path. A successor that diverges from what the reader already consumed fails -the reader, as a route switch does. Test on mock time: a reader mid-group on a -copy that fails continues on the re-offer with no duplicate or missing frame. - -Decided 2026-11-05 (maintainer, /quest-audit): ✅ the wait on -[moq-dev/moq#7670](https://github.com/moq-dev/moq/pull/7670), Drop set's own -PR, becomes a `Required` link to Drop set ([moq-dev/moq#8476](https://github.com/moq-dev/moq/pull/8476)), since -#7670 adds the held re-offer (its decision 24) this builds on. - -Public API: none expected. Wire: none. From ac0042d22247e06f407ad85e038cd48997c7c025 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 01:56:16 -0800 Subject: [PATCH 04/21] quest: follow up the io_uring memlock test that fails on newer kernels Co-Authored-By: Claude Opus 5.5 --- quest/m0/test-flakes-2/uring-memlock.md | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 quest/m0/test-flakes-2/uring-memlock.md diff --git a/quest/m0/test-flakes-2/uring-memlock.md b/quest/m0/test-flakes-2/uring-memlock.md new file mode 100644 index 00000000000..6d41de195b0 --- /dev/null +++ b/quest/m0/test-flakes-2/uring-memlock.md @@ -0,0 +1,30 @@ +# [S] The relay's io_uring memlock test holds on every kernel + +## Goal + +`moq-relay`'s `runtime_uring::uring_startup_reports_the_memlock_limit` passes +on every Linux host `just check` runs on, or is skipped where the kernel never +charges a ring to `RLIMIT_MEMLOCK`. The relay keeps reporting the memlock hint +where the limit does refuse a worker. + +## Plan + +Found 2026-11-12 while checking +[moq-dev/moq#8546](https://github.com/moq-dev/moq/pull/8546). On a Linux +7.3.3 host, `cargo nextest run -p moq-relay --all-features -E +'test(uring_startup_reports_the_memlock_limit)'` fails the same way on +`origin/main` and on that PR's branch: `a worker needs locked memory: ()` +(`rs/moq-relay/tests/integration/runtime_uring.rs:1328`). The test sets +`RLIMIT_MEMLOCK` to 0 and expects the first io_uring worker to fail. On this +kernel the ring starts anyway, so the relay never refuses. + +Newer kernels charge io_uring memory to the memory cgroup rather than to +`RLIMIT_MEMLOCK`. Find which ring setups still hit the limit, then either +make the test force a setup that does, or detect at startup whether the limit +applies and skip the test where it cannot fail. Never loosen the assertion on +the message the relay prints. If no kernel the relay supports still charges +the limit, the startup hint and this test may go. + +## Hosts + +- linux - io_uring From 0a4ba81264e671238fa288bbe5578438aad157fa Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 02:04:25 -0800 Subject: [PATCH 05/21] fix(net): held re-offers wait per copy, and JS readiness and close end the wait Rust keys a hold by the copy it waits on, so a second duplicate held on the successor no longer stalls the failed copy's readers. JS readable() adopts the successor as reads do, and closing a reader ends a read waiting on one. Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 12 ++-- js/net/src/track.test.ts | 114 +++++++++++++++++++--------------- rs/moq-net/src/model/group.rs | 5 ++ rs/moq-net/src/model/track.rs | 44 +++++++++---- 4 files changed, 111 insertions(+), 64 deletions(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index d8d63638153..f680a6c089d 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -1367,9 +1367,11 @@ export class Consumer { } if (this.#ready()) return; const closed = this.#state.closed.peek(); - if (closed instanceof Error && (await this.#resume(closed))) continue; + if (closed instanceof Error && ((await this.#succeed()) || (await this.#resume(closed)))) continue; if (closed !== undefined) { this.#abandon(); + // Ends the reader, whose failure a copy that might have taken over left open. + if (closed instanceof Error) this.#fail(closed); this.#closed?.refresh(); return; } @@ -1514,9 +1516,11 @@ export class Consumer { // Release this reader, failing it with `abort` if given. Every reader owns its mirrored // state, so closing that state drops its demand without touching the producer. #release(abort: Error | undefined): void { - // A copy failure recovery was continuing past ends with the abort instead. - if (abort && this.#recover && this.#state.closed.peek() instanceof Error) { - this.#fail(abort); + // A copy failure recovery was continuing past ends with the abort instead, and one a copy + // that might take over left open ends with it, which also ends a read waiting on that copy. + const failed = this.#state.closed.peek(); + if (failed instanceof Error && (this.#succeeds() || (abort && this.#recover))) { + this.#fail(abort ?? failed); } this.#abandon(); this.#recover?.close?.(); diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 00bd6e06bdb..ad2c296494e 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3404,59 +3404,75 @@ test("an upstream drop ends a group still arriving with its error", async () => // A reader on a received copy that fails waits out a duplicate still held for its group, then // continues on it from its frame with nothing repeated or missed. A released hold leaves the -// failure, as does a duplicate starting past the reader's frame. -test.each(["reoffer", "released", "past"])("a failed copy continues on its held re-offer (%p)", async (kind) => { - const producer = new TrackProducer("video").accept({ maxAge: null }); - hooks.receiveTrack(producer); - const first = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); - first.writeFrame({ payload: enc.encode("a") }); - const reader = first.consume(); - expect(await reader.readString()).toBe("a"); - - const arriving = hooks.arrivingGroup(producer, 3n); - expect(arriving).toBeDefined(); - const failed = new Error("reset"); - first.abort(failed); - let read: string | undefined | Error; - const reading = reader.readString().then( - (frame) => { - read = frame; - }, - (err: Error) => { - read = err; - }, - ); - await settle(); - expect(read).toBeUndefined(); - expect(reader.closed.peek()).toBeUndefined(); +// failure, as does a duplicate starting past the reader's frame, and a reader closed while it waits +// ends its read. +test.each(["reoffer", "readable", "released", "closed", "past"])( + "a failed copy continues on its held re-offer (%p)", + async (kind) => { + const producer = new TrackProducer("video").accept({ maxAge: null }); + hooks.receiveTrack(producer); + const first = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + first.writeFrame({ payload: enc.encode("a") }); + const reader = first.consume(); + expect(await reader.readString()).toBe("a"); + + const arriving = hooks.arrivingGroup(producer, 3n); + expect(arriving).toBeDefined(); + const failed = new Error("reset"); + first.abort(failed); + let read: string | Uint8Array | undefined | Error; + // A readiness poll waits the same way, and adopts the re-offer rather than spinning on the + // failed copy. + const pending = + kind === "readable" ? reader.readable().then(() => reader.tryReadFrame()?.payload) : reader.readString(); + const reading = pending.then( + (frame) => { + read = frame; + }, + (err: Error) => { + read = err; + }, + ); + await settle(); + expect(read).toBeUndefined(); + expect(reader.closed.peek()).toBeUndefined(); - if (kind === "released") { + if (kind === "closed") { + reader.close(); + await reading; + expect(read).toBe(failed); + arriving?.release(); + producer.close(); + return; + } + if (kind === "released") { + arriving?.release(); + await reading; + expect(read).toBe(failed); + expect(await reader.closed).toBe(failed); + producer.close(); + return; + } + const again = hooks.receiveGroup(producer, { sequence: 3n, start: kind === "past" ? 2 : 0 }); arriving?.release(); + if (kind === "past") { + await reading; + expect(read).toBe(failed); + producer.close(); + return; + } + await settle(); + expect(read).toBeUndefined(); + again.writeFrame({ payload: enc.encode("a") }); + again.writeFrame({ payload: enc.encode("b") }); + again.finish(); await reading; - expect(read).toBe(failed); - expect(await reader.closed).toBe(failed); - producer.close(); - return; - } - const again = hooks.receiveGroup(producer, { sequence: 3n, start: kind === "past" ? 2 : 0 }); - arriving?.release(); - if (kind === "past") { - await reading; - expect(read).toBe(failed); + expect(read).toEqual(kind === "readable" ? enc.encode("b") : "b"); + expect(await reader.readString()).toBeUndefined(); + expect(await reader.closed).toBeNull(); producer.close(); - return; - } - await settle(); - expect(read).toBeUndefined(); - again.writeFrame({ payload: enc.encode("a") }); - again.writeFrame({ payload: enc.encode("b") }); - again.finish(); - await reading; - expect(read).toBe("b"); - expect(await reader.readString()).toBeUndefined(); - expect(await reader.closed).toBeNull(); - producer.close(); -}); + }, +); test("a drop below the window is reported once the window lowers to it", () => { const producer = new TrackProducer("video").accept({}); diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 1e70657a858..f3a4c4e0f4c 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -2499,6 +2499,11 @@ impl Handle { Arc::ptr_eq(&self.alive, &reader.cursor.alive) } + /// Whether `other` is a handle of this same copy. + pub(super) fn is_copy(&self, other: &Handle) -> bool { + Arc::ptr_eq(&self.alive, &other.alive) + } + /// Abort the group without taking ownership of its writer. // Sessions cancel a group while its writer is in a frame. pub(crate) fn abort(&self, err: Error) -> Result<()> { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 07679e74e46..75f5e5c5a09 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -583,9 +583,9 @@ pub(crate) struct TrackState { // the readers riding them; see [`Self::board`]. Held weakly, so the last rider leaving // frees one, and pruned as they are looked up. flights: BTreeMap>, - // How many received copies wait on another copy of each sequence still arriving (see - // [`Hold`]), which a reader of that copy waits out when it fails; see [`Self::successor`]. - holds: BTreeMap, + // The copies of each sequence still arriving that another received copy waits on, once per + // [`Hold`], which a reader of that copy waits out when it fails; see [`successor`]. + holds: BTreeMap>, // Publisher-produced groups in arrival order as (sequence, stamp), walked by // subscriptions; an entry only resolves while its stamp matches the slot's. @@ -3793,7 +3793,12 @@ pub(super) fn successor( ) -> Poll> { let sequence = reader.sequence; let found = track.poll_ref(waiter, |state| { - if state.holds.contains_key(&sequence) { + // Only a hold on the copy read: one on a copy that took over from it waits on that one. + if state + .holds + .get(&sequence) + .is_some_and(|held| held.iter().any(|copy| copy.is_read_by(reader))) + { return Poll::Pending; } Poll::Ready( @@ -4055,8 +4060,10 @@ impl Drop for Hold { return; }; if let btree_map::Entry::Occupied(mut holds) = state.holds.entry(self.sequence) { - *holds.get_mut() -= 1; - if *holds.get() == 0 { + if let Some(at) = holds.get().iter().position(|held| held.is_copy(&self.arriving)) { + holds.get_mut().swap_remove(at); + } + if holds.get().is_empty() { holds.remove(); } } @@ -4220,7 +4227,7 @@ impl Producer { return Err(Error::Duplicate); } let arriving = holding.clone(); - *state.holds.entry(info.sequence).or_default() += 1; + state.holds.entry(info.sequence).or_default().push(arriving.clone()); return Ok(Received::Held(Hold { arriving, track: self.state.downgrade(), @@ -17171,8 +17178,9 @@ mod test { } /// A reader on a received copy that fails waits out a re-offer still held for its group, - /// then continues on it from its frame with nothing repeated or missed. A released hold - /// leaves the failure, as does a re-offer starting past the reader's frame. + /// then continues on it from its frame with nothing repeated or missed, even while another + /// duplicate waits on that re-offer. A released hold leaves the failure, as does a re-offer + /// starting past the reader's frame. #[kio::test] #[cfg(feature = "async")] async fn a_failed_copy_continues_on_its_held_reoffer() { @@ -17189,6 +17197,9 @@ mod test { let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { panic!("the re-offer waits on the copy still arriving"); }; + let Ok(Received::Held(second)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("a second duplicate waits too"); + }; first.abort(Error::Cancel).unwrap(); let waiter = kio::Waiter::noop(); assert!( @@ -17197,7 +17208,7 @@ mod test { ); if case == "released" { - drop(hold); + drop((hold, second)); assert!(reader.read_frame().await.is_err(), "{case}"); continue; } @@ -17206,6 +17217,16 @@ mod test { panic!("the re-offer takes the group"); }; drop(hold); + assert!( + reader.poll_read_frame(&waiter).is_pending(), + "{case}: the second still holds" + ); + // The second duplicate now waits on the re-offer, which holds nobody reading the + // failed copy. + let Ok(Received::Held(third)) = producer.create_group_at(group::Info { sequence: 3 }, start) else { + panic!("the second duplicate waits on the re-offer still arriving"); + }; + drop(second); if case == "past" { assert!(reader.read_frame().await.is_err(), "{case}: frame 1 is in no copy"); continue; @@ -17214,8 +17235,9 @@ mod test { assert!(reader.poll_read_frame(&waiter).is_pending()); again.write_frame(at(300), &b"a"[..]).unwrap(); again.write_frame(at(301), &b"b"[..]).unwrap(); - again.finish().unwrap(); assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + again.finish().unwrap(); + drop(third); assert!(reader.read_frame().await.unwrap().is_none()); } } From 74c043bf0ea8af41f5110c09eb5944faead71d28 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 02:09:31 -0800 Subject: [PATCH 06/21] fix(net): closing a reader waiting on a takeover keeps its buffered frames Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 24 ++++++++++++++++-------- js/net/src/track.test.ts | 19 +++++++++++++++---- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index f680a6c089d..810bb81e30a 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -941,6 +941,8 @@ export class Consumer { #expired?: Expired; // Closed by its reader, which ends it whatever is left unread. #released = false; + // Set once released, for a read waiting on a copy that may take the group over. + #leaving?: Once; // The arrival of the copy first read, kept once a replacement copy is adopted. #arrived?: Instant; // The last frame handed out to stream before it completed. Cut short, it is lost, so no @@ -1020,16 +1022,23 @@ export class Consumer { // Whether another copy in the track may take over the failed one being read: one is held, or took it. #succeeds(): boolean { const succession = this.#state.succession; - return succession !== undefined && (succession.holds.peek() > 0 || succession.next !== undefined); + return ( + !this.#released && + succession !== undefined && + (succession.holds.peek() > 0 || succession.next !== undefined) + ); } // Continue on the copy that took this group's sequence in its track once the one being read // failed, waiting out a duplicate still held for it. Returns whether the read should look again. async #succeed(): Promise { const succession = this.#state.succession; - if (!succession || this.#verdict.peek() || this.#torn()) return false; + if (!succession || this.#released || this.#verdict.peek() || this.#torn()) return false; while (succession.holds.peek() > 0) { - await Signal.race(succession.holds, this.#verdict); + this.#leaving ??= new Once(); + await Signal.race(succession.holds, this.#verdict, this.#leaving); + // A closed reader ends on the failure rather than continue elsewhere. + if (this.#released) return false; if (this.#verdict.peek()) return true; } const next = succession.next; @@ -1516,17 +1525,16 @@ export class Consumer { // Release this reader, failing it with `abort` if given. Every reader owns its mirrored // state, so closing that state drops its demand without touching the producer. #release(abort: Error | undefined): void { - // A copy failure recovery was continuing past ends with the abort instead, and one a copy - // that might take over left open ends with it, which also ends a read waiting on that copy. - const failed = this.#state.closed.peek(); - if (failed instanceof Error && (this.#succeeds() || (abort && this.#recover))) { - this.#fail(abort ?? failed); + // A copy failure recovery was continuing past ends with the abort instead. + if (abort && this.#recover && this.#state.closed.peek() instanceof Error) { + this.#fail(abort); } this.#abandon(); this.#recover?.close?.(); this.#recover = undefined; // Closing is terminal for this reader, even with frames still unread. this.#released = true; + this.#leaving?.set(true); if (this.#state.closed.peek() === undefined) this.#state.closed.set(abort ?? null); this.#closed?.refresh(); } diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index ad2c296494e..763d3d91804 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3404,9 +3404,9 @@ test("an upstream drop ends a group still arriving with its error", async () => // A reader on a received copy that fails waits out a duplicate still held for its group, then // continues on it from its frame with nothing repeated or missed. A released hold leaves the -// failure, as does a duplicate starting past the reader's frame, and a reader closed while it waits -// ends its read. -test.each(["reoffer", "readable", "released", "closed", "past"])( +// failure, as does a duplicate starting past the reader's frame. A reader closed while it waits ends +// its read, and one closed with frames still buffered reads them first. +test.each(["reoffer", "readable", "released", "leaving", "closed", "past"])( "a failed copy continues on its held re-offer (%p)", async (kind) => { const producer = new TrackProducer("video").accept({ maxAge: null }); @@ -3419,6 +3419,17 @@ test.each(["reoffer", "readable", "released", "closed", "past"])( const arriving = hooks.arrivingGroup(producer, 3n); expect(arriving).toBeDefined(); const failed = new Error("reset"); + if (kind === "closed") { + // A frame still buffered when the copy fails stays readable after a close. + first.writeFrame({ payload: enc.encode("c") }); + first.abort(failed); + reader.close(); + expect(await reader.readString()).toBe("c"); + await expect(reader.readString()).rejects.toBe(failed); + arriving?.release(); + producer.close(); + return; + } first.abort(failed); let read: string | Uint8Array | undefined | Error; // A readiness poll waits the same way, and adopts the re-offer rather than spinning on the @@ -3437,7 +3448,7 @@ test.each(["reoffer", "readable", "released", "closed", "past"])( expect(read).toBeUndefined(); expect(reader.closed.peek()).toBeUndefined(); - if (kind === "closed") { + if (kind === "leaving") { reader.close(); await reading; expect(read).toBe(failed); From 583b072c4f73f5a1700c89ffaa70932bc5fd97c3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 07:07:57 -0800 Subject: [PATCH 07/21] fix(net): closing a reader twice after a takeover wait does not throw Co-Authored-By: Claude Opus 5.5 --- js/net/src/track.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 763d3d91804..c084cc0a199 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3405,7 +3405,7 @@ test("an upstream drop ends a group still arriving with its error", async () => // A reader on a received copy that fails waits out a duplicate still held for its group, then // continues on it from its frame with nothing repeated or missed. A released hold leaves the // failure, as does a duplicate starting past the reader's frame. A reader closed while it waits ends -// its read, and one closed with frames still buffered reads them first. +// its read and can be closed again, and one closed with frames still buffered reads them first. test.each(["reoffer", "readable", "released", "leaving", "closed", "past"])( "a failed copy continues on its held re-offer (%p)", async (kind) => { @@ -3452,6 +3452,8 @@ test.each(["reoffer", "readable", "released", "leaving", "closed", "past"])( reader.close(); await reading; expect(read).toBe(failed); + // Closing is idempotent, even after the wait was cancelled. + reader.close(); arriving?.release(); producer.close(); return; From 925f45cc3e98e7d563e7aaae079abd4357e38375 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 07:25:50 -0800 Subject: [PATCH 08/21] fix(net): a sequence's copies share one succession, and closed observers see it change Re-offers of one sequence now share a single succession naming only the latest copy, so no chain of failed producers is retained, and a reader's closed handle re-checks when a hold is released or a copy takes over or fails. Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 93 +++++++++++++++++++++++++++++----------- js/net/src/track.test.ts | 39 +++++++++++++++-- 2 files changed, 104 insertions(+), 28 deletions(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 245b24d0fe6..b48c91749cb 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -130,7 +130,8 @@ class GroupState { // Tells the group this copy was mirrored from whether its reader will still read it, so a // copy buffered for a reader that passed it stops keeping the group wanted. reads?: (reads: boolean) => void; - // A received copy's, shared with every mirror: what may take the group over once it fails. + // A received copy's, shared with every mirror and incarnation: what may take the group over + // once it fails. succession?: Succession; constructor(sequence: bigint) { @@ -138,13 +139,26 @@ class GroupState { } } -// Another copy of a received group's sequence that may take the group over once this one fails, -// so its readers continue there (see `Consumer.#succeed`). A group's name fixes its content. +// The copies of a received group's sequence in one track, shared by each incarnation and its +// mirrors, so a reader of a failed copy continues on the latest (see `Consumer.#succeed`). A +// group's name fixes its content. Only the latest is kept, so re-offers retain no chain of copies. class Succession { - // Duplicates waiting on this copy while it arrives (see `hooks.holdGroup`). - readonly holds = new Signal(0); - // The copy that took the sequence after this one failed. + // Duplicates held while a copy arrives (see `hooks.holdGroup`), any of which may take over. + holds = 0; + // The latest copy, once one took over a failed one. next?: Producer; + // Bumped whenever a takeover may have become possible or impossible. + readonly changed = new Signal(0); + + bump(): void { + this.changed.update((n) => n + 1); + } + + // The latest copy, unless it failed too. + live(): Producer | undefined { + const next = this.next; + return next && !(next.closed.peek() instanceof Error) ? next : undefined; + } } // A written frame and when it landed. It keeps the writer's frame rather than copying it, so a @@ -312,20 +326,30 @@ export class Producer { }; hooks.groupOffset = (group) => group.#state.offset; hooks.receivedGroup = (group, failed) => { - group.#state.succession = new Succession(); - if (failed === undefined) return; - const succession = failed.#state.succession; - if (succession) succession.next = group; + if (failed === undefined) { + group.#state.succession = new Succession(); + return; + } + const succession = failed.#state.succession ?? new Succession(); + group.#state.succession = succession; + succession.next = group; + succession.bump(); + // A reader waiting on this copy gives up once it fails with nothing held. + group.#state.closed.changed((closed) => { + if (closed instanceof Error && succession.next === group) succession.bump(); + }); }; hooks.holdGroup = (group) => { - const holds = group.#state.succession?.holds; - if (!holds) return () => {}; - holds.update((n) => n + 1); + const succession = group.#state.succession; + if (!succession) return () => {}; + succession.holds++; + succession.bump(); let held = true; return () => { if (!held) return; held = false; - holds.update((n) => n - 1); + succession.holds--; + succession.bump(); }; }; hooks.startGroup = (group, start) => { @@ -842,16 +866,26 @@ class CombinedClosed implements GetPromise { #preferred: Once; #source: () => Once; #final: (value: Error | null) => boolean; + // What else may change `final`'s answer, watched like the source. + #wake: () => Signal | undefined; #value = new Once(); #watched?: Once; // The watcher on `#watched`, replaced when a recovery moves to another copy's state. #unwatch?: Dispose; + #woken?: Signal; + #unwake?: Dispose; #dispose: Dispose[] = []; - constructor(preferred: Once, source: () => Once, final: (value: Error | null) => boolean) { + constructor( + preferred: Once, + source: () => Once, + final: (value: Error | null) => boolean, + wake: () => Signal | undefined, + ) { this.#preferred = preferred; this.#source = source; this.#final = final; + this.#wake = wake; if (this.#sync()) return; this.#dispose.push(preferred.changed(() => this.#sync())); } @@ -869,6 +903,12 @@ class CombinedClosed implements GetPromise { this.#watched = source; this.#unwatch = source.changed(() => this.#sync()); } + const wake = this.#wake(); + if (this.#woken !== wake) { + this.#unwake?.(); + this.#woken = wake; + this.#unwake = wake?.subscribe(() => this.#sync()); + } return false; } @@ -878,6 +918,9 @@ class CombinedClosed implements GetPromise { this.#unwatch?.(); this.#unwatch = undefined; this.#watched = undefined; + this.#unwake?.(); + this.#unwake = undefined; + this.#woken = undefined; return true; } @@ -980,6 +1023,7 @@ export class Consumer { () => this.#state.closed, (value) => value instanceof Error ? !this.#recovers(value) : this.#released || this.#bufferedFrames().length === 0, + () => this.#state.succession?.changed, ); return this.#closed; } @@ -993,6 +1037,7 @@ export class Consumer { (value) => (!(value instanceof Error) || !this.#recovers(value)) && (this.#released || this.#bufferedFrames().length === 0), + () => this.#state.succession?.changed, ); return this.#finished; } @@ -1039,14 +1084,11 @@ export class Consumer { return !this.#verdict.peek() && !this.#torn() && (this.#succeeds() || this.#recover?.wants(failed) === true); } - // Whether another copy in the track may take over the failed one being read: one is held, or took it. + // Whether another copy in the track may take over the failed one being read: one took it and + // has not failed, or one is held. #succeeds(): boolean { const succession = this.#state.succession; - return ( - !this.#released && - succession !== undefined && - (succession.holds.peek() > 0 || succession.next !== undefined) - ); + return !this.#released && succession !== undefined && (succession.live() !== undefined || succession.holds > 0); } // Continue on the copy that took this group's sequence in its track once the one being read @@ -1054,15 +1096,16 @@ export class Consumer { async #succeed(): Promise { const succession = this.#state.succession; if (!succession || this.#released || this.#verdict.peek() || this.#torn()) return false; - while (succession.holds.peek() > 0) { + let next = succession.live(); + while (!next) { + if (succession.holds === 0) return false; this.#leaving ??= new Once(); - await Signal.race(succession.holds, this.#verdict, this.#leaving); + await Signal.race(succession.changed, this.#verdict, this.#leaving); // A closed reader ends on the failure rather than continue elsewhere. if (this.#released) return false; if (this.#verdict.peek()) return true; + next = succession.live(); } - const next = succession.next; - if (!next) return false; const group = next.mirror(); // It starts past the frame read next, which no copy holds now. if (group.#state.start > this.#index()) { diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index c084cc0a199..d50c4b37489 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3404,9 +3404,10 @@ test("an upstream drop ends a group still arriving with its error", async () => // A reader on a received copy that fails waits out a duplicate still held for its group, then // continues on it from its frame with nothing repeated or missed. A released hold leaves the -// failure, as does a duplicate starting past the reader's frame. A reader closed while it waits ends -// its read and can be closed again, and one closed with frames still buffered reads them first. -test.each(["reoffer", "readable", "released", "leaving", "closed", "past"])( +// failure, even to a reader only watching `closed`, as does a duplicate starting past the reader's +// frame. A re-offer that fails too hands over to the next. A reader closed while it waits ends its +// read and can be closed again, and one closed with frames still buffered reads them first. +test.each(["reoffer", "readable", "released", "observed", "twice", "leaving", "closed", "past"])( "a failed copy continues on its held re-offer (%p)", async (kind) => { const producer = new TrackProducer("video").accept({ maxAge: null }); @@ -3431,6 +3432,38 @@ test.each(["reoffer", "readable", "released", "leaving", "closed", "past"])( return; } first.abort(failed); + if (kind === "observed") { + let closed: Error | null | undefined; + reader.closed.then((value) => { + closed = value; + }); + await settle(); + expect(closed).toBeUndefined(); + arriving?.release(); + await settle(); + expect(closed).toBe(failed); + producer.close(); + return; + } + if (kind === "twice") { + // The reader reaches neither re-offer before the third copy takes over. + const second = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + arriving?.release(); + const held = hooks.arrivingGroup(producer, 3n); + second.abort(failed); + await settle(); + expect(reader.closed.peek()).toBeUndefined(); + const third = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + held?.release(); + third.writeFrame({ payload: enc.encode("a") }); + third.writeFrame({ payload: enc.encode("b") }); + third.finish(); + expect(await reader.readString()).toBe("b"); + expect(await reader.readString()).toBeUndefined(); + expect(await reader.closed).toBeNull(); + producer.close(); + return; + } let read: string | Uint8Array | undefined | Error; // A readiness poll waits the same way, and adopts the re-offer rather than spinning on the // failed copy. From 5555b0554b9fe4742bc2e28aa955209731c78697 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 07:39:38 -0800 Subject: [PATCH 09/21] fix(net): a reader rejects a successor that already shed its frame Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 5 +++-- js/net/src/track.test.ts | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index b48c91749cb..9b78605dc39 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -1107,8 +1107,9 @@ export class Consumer { next = succession.live(); } const group = next.mirror(); - // It starts past the frame read next, which no copy holds now. - if (group.#state.start > this.#index()) { + // It no longer holds the frame read next: it starts past it, or shed it, which fails the + // mirror. The mirror shares this succession, so adopting a failed one would loop. + if (group.#state.closed.peek() instanceof Error || group.#state.start > this.#index()) { group.close(); return false; } diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index d50c4b37489..c848a77378a 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3405,9 +3405,9 @@ test("an upstream drop ends a group still arriving with its error", async () => // A reader on a received copy that fails waits out a duplicate still held for its group, then // continues on it from its frame with nothing repeated or missed. A released hold leaves the // failure, even to a reader only watching `closed`, as does a duplicate starting past the reader's -// frame. A re-offer that fails too hands over to the next. A reader closed while it waits ends its +// frame or one that shed it. A re-offer that fails too hands over to the next. A reader closed while it waits ends its // read and can be closed again, and one closed with frames still buffered reads them first. -test.each(["reoffer", "readable", "released", "observed", "twice", "leaving", "closed", "past"])( +test.each(["reoffer", "readable", "released", "observed", "twice", "leaving", "closed", "past", "shed"])( "a failed copy continues on its held re-offer (%p)", async (kind) => { const producer = new TrackProducer("video").accept({ maxAge: null }); @@ -3501,6 +3501,17 @@ test.each(["reoffer", "readable", "released", "observed", "twice", "leaving", "c } const again = hooks.receiveGroup(producer, { sequence: 3n, start: kind === "past" ? 2 : 0 }); arriving?.release(); + if (kind === "shed") { + // The re-offer drops frames 0 and 1 to cache pressure before the reader reaches it. + again.writeFrame({ payload: enc.encode("a") }); + again.writeFrame({ payload: enc.encode("b") }); + again.writeFrame({ payload: enc.encode("c") }); + hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); + await reading; + expect(read).toBe(failed); + producer.close(); + return; + } if (kind === "past") { await reading; expect(read).toBe(failed); From 8203e3e01a93bf6de88c239ece8d55427d4b3339 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 08:19:38 -0800 Subject: [PATCH 10/21] fix(net): a terminal watch and the budget follow a failed copy's takeover A failed copy's `poll_finished` (Rust) now waits out a held re-offer and adopts the copy that takes over, as frame reads do, so a publisher's `poll_given_up` no longer resets a stream the re-offer continues. Waiting on a hold applies the reader's budget in both languages. Rust tries every covering copy for the successor. In JS, `closed` settles once the successor cannot serve the reader's frame and nothing is held, and a re-offer replacing a failed copy keeps a continuing subscriber reader's place instead of failing it as evicted. Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 60 +++++++++++++++--------- js/net/src/track.test.ts | 76 +++++++++++++++++++++++++++++- rs/moq-net/src/model/group.rs | 66 ++++++++++++++++---------- rs/moq-net/src/model/track.rs | 88 +++++++++++++++++++++++++++++++++-- 4 files changed, 238 insertions(+), 52 deletions(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 9b78605dc39..113f15e0f65 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -154,13 +154,17 @@ class Succession { this.changed.update((n) => n + 1); } - // The latest copy, unless it failed too. - live(): Producer | undefined { + // The latest copy, if it can continue a reader from frame `index`. + serving(index: number): Producer | undefined { const next = this.next; - return next && !(next.closed.peek() instanceof Error) ? next : undefined; + return next && serves(next, index) ? next : undefined; } } +// Whether `group` has not failed and still holds every frame from `index` on, so a mirror of it +// continues a reader there. +let serves: (group: Producer, index: number) => boolean; + // A written frame and when it landed. It keeps the writer's frame rather than copying it, so a // lazy payload is read only when a reader takes it. class Landed implements Frame { @@ -325,6 +329,15 @@ export class Producer { if (state.closed.peek() !== undefined) flate.finish(); }; hooks.groupOffset = (group) => group.#state.offset; + serves = (group, index) => { + const state = group.#state; + return ( + !(state.closed.peek() instanceof Error) && + state.start === state.offset && + state.start <= index && + !headShed(state.frames.peek().first) + ); + }; hooks.receivedGroup = (group, failed) => { if (failed === undefined) { group.#state.succession = new Succession(); @@ -1085,10 +1098,14 @@ export class Consumer { } // Whether another copy in the track may take over the failed one being read: one took it and - // has not failed, or one is held. + // can serve the frame read next, or one is held. #succeeds(): boolean { const succession = this.#state.succession; - return !this.#released && succession !== undefined && (succession.live() !== undefined || succession.holds > 0); + return ( + !this.#released && + succession !== undefined && + (succession.serving(this.#index()) !== undefined || succession.holds > 0) + ); } // Continue on the copy that took this group's sequence in its track once the one being read @@ -1096,24 +1113,19 @@ export class Consumer { async #succeed(): Promise { const succession = this.#state.succession; if (!succession || this.#released || this.#verdict.peek() || this.#torn()) return false; - let next = succession.live(); + let next = succession.serving(this.#index()); while (!next) { if (succession.holds === 0) return false; + // Waiting on a held copy is a stall the budget bounds. + if (this.#expire()) return true; this.#leaving ??= new Once(); - await Signal.race(succession.changed, this.#verdict, this.#leaving); + await Signal.race(succession.changed, this.#verdict, this.#leaving, ...(this.#expiry?.changed ?? [])); // A closed reader ends on the failure rather than continue elsewhere. if (this.#released) return false; if (this.#verdict.peek()) return true; - next = succession.live(); + next = succession.serving(this.#index()); } - const group = next.mirror(); - // It no longer holds the frame read next: it starts past it, or shed it, which fails the - // mirror. The mirror shares this succession, so adopting a failed one would loop. - if (group.#state.closed.peek() instanceof Error || group.#state.start > this.#index()) { - group.close(); - return false; - } - this.#switch(group, this.#expiry); + this.#switch(next.mirror(), this.#expiry); return true; } @@ -1181,7 +1193,8 @@ export class Consumer { // the group, so a clean end would pass the prefix off as the whole group. #expire(): boolean { if (this.#verdict.peek()) return false; - if (this.#state.closed.peek() instanceof Error) return false; + // A failed copy is no stall, unless the reader waits for another to take it over. + if (this.#state.closed.peek() instanceof Error && !this.#succeeds()) return false; if (!this.#expiry?.expired()) return false; this.#fail( @@ -1202,23 +1215,24 @@ export class Consumer { #evict(lost: Error): boolean { // A group read on from another copy is that copy's to reclaim. if (this.#state !== this.#mirrored) return false; + const closed = this.#state.closed.peek(); + // A failed copy that another copy or route may still continue keeps the reader's place, + // and that copy serves the frames dropped here. + const continues = closed instanceof Error && this.#recovers(closed); const frames = this.#bufferedFrames(); - if (frames.length > 0) { + if (frames.length > 0 && !continues) { // Unread content is going away either way. If the budget had already given up // on this group that is the more specific reason, so ask it first; otherwise // the cache reclaimed it before the reader got to it. this.#expire(); this.#fail(lost); - } - if (frames.length > 0) { this.#state.start += frames.length; } this.#state.cacheBytes = 0; this.#state.flate = undefined; this.#state.frames.set(new Queue()); - // A failed copy holds nothing more to reclaim, and its recovery may still continue the group. - const closed = this.#state.closed.peek(); - return !(closed instanceof Error && this.#recovers(closed)); + // A failed copy holds nothing more to reclaim. + return !continues; } // The next change a parked read waits for. diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index c848a77378a..b9c234cc7db 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3407,7 +3407,7 @@ test("an upstream drop ends a group still arriving with its error", async () => // failure, even to a reader only watching `closed`, as does a duplicate starting past the reader's // frame or one that shed it. A re-offer that fails too hands over to the next. A reader closed while it waits ends its // read and can be closed again, and one closed with frames still buffered reads them first. -test.each(["reoffer", "readable", "released", "observed", "twice", "leaving", "closed", "past", "shed"])( +test.each(["reoffer", "readable", "released", "observed", "unserved", "twice", "leaving", "closed", "past", "shed"])( "a failed copy continues on its held re-offer (%p)", async (kind) => { const producer = new TrackProducer("video").accept({ maxAge: null }); @@ -3464,6 +3464,16 @@ test.each(["reoffer", "readable", "released", "observed", "twice", "leaving", "c producer.close(); return; } + if (kind === "unserved") { + // A re-offer starting past the reader's frame cannot take over, so `closed` settles once + // nothing else is held. + hooks.receiveGroup(producer, { sequence: 3n, start: 2 }); + arriving?.release(); + await settle(); + expect(reader.closed.peek()).toBe(failed); + producer.close(); + return; + } let read: string | Uint8Array | undefined | Error; // A readiness poll waits the same way, and adopts the re-offer rather than spinning on the // failed copy. @@ -3531,6 +3541,70 @@ test.each(["reoffer", "readable", "released", "observed", "twice", "leaving", "c }, ); +// A subscriber's reader of a failed copy keeps its place when a re-offer replaces that copy in the +// track, continuing on it, and its budget still gives up on it while it waits on a held re-offer. +test.each(["buffered", "expires"])("a subscriber's failed copy waits on its re-offer (%p)", async (kind) => { + const clock = manualTime(0); + try { + await subscriberReoffer(kind); + } finally { + clock.installed.close(); + } +}); + +async function subscriberReoffer(kind: string): Promise { + const producer = new TrackProducer("video").accept({ timescale: Timescale.MILLI, maxAge: null }); + hooks.receiveTrack(producer); + const track = producer.subscribe({ delay: { max: Duration.millis(500) } }); + const first = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + first.writeFrame({ payload: enc.encode("a"), timestamp: Timestamp.fromMillis(0) }); + first.writeFrame({ payload: enc.encode("b"), timestamp: Timestamp.fromMillis(1) }); + const reader = await track.recvGroup(); + if (!reader) throw new Error("missing group"); + expect(await reader.readString()).toBe("a"); + + const arriving = hooks.arrivingGroup(producer, 3n); + const failed = new Error("reset"); + first.abort(failed); + if (kind === "expires") { + expect(await reader.readString()).toBe("b"); + let read: unknown; + reader.readString().then( + (frame) => { + read = frame; + }, + (err: unknown) => { + read = err; + }, + ); + // Group 4 bounds group 3's reach at 1s, and group 5 puts the edge a second past it. + for (const [sequence, millis] of [ + [4n, 1000], + [5n, 2000], + ] as const) { + hooks + .receiveGroup(producer, { sequence, start: 0 }) + .writeFrame({ payload: enc.encode("x"), timestamp: Timestamp.fromMillis(millis) }); + } + await settle(); + expect(read).toMatchObject({ code: StreamCode.Old }); + arriving?.release(); + producer.close(); + return; + } + // The re-offer replaces the failed copy in the track before the reader takes `b`. + const again = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + arriving?.release(); + again.writeFrame({ payload: enc.encode("a"), timestamp: Timestamp.fromMillis(0) }); + again.writeFrame({ payload: enc.encode("b"), timestamp: Timestamp.fromMillis(1) }); + again.writeFrame({ payload: enc.encode("c"), timestamp: Timestamp.fromMillis(2) }); + again.finish(); + expect(await reader.readString()).toBe("b"); + expect(await reader.readString()).toBe("c"); + expect(await reader.readString()).toBeUndefined(); + producer.close(); +} + test("a drop below the window is reported once the window lowers to it", () => { const producer = new TrackProducer("video").accept({}); const reader = producer diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index f3a4c4e0f4c..51079beb88e 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -4321,11 +4321,8 @@ impl Consumer { if !matches!(res, Poll::Ready(Err(_))) { return res; } - match self.poll_successor(waiter) { - Poll::Ready(true) => continue, - Poll::Ready(false) => {} - // Waiting on a re-offer is blocked like a read at the edge. - Poll::Pending => return self.poll_expired_if_blocked(waiter, Poll::Pending), + if ready!(self.poll_successor(waiter))? { + continue; } if !self.shed_next() { return res; @@ -4357,9 +4354,9 @@ impl Consumer { /// Continue on another copy of this group in the same track once the copy read failed, /// such as a re-offer held while that copy was still arriving (see [`track::successor`]). - /// Returns whether the reader moved to one. - fn poll_successor(&mut self, waiter: &kio::Waiter) -> Poll { - let (index, raw) = (self.cursor.index, self.cursor.raw); + /// Returns whether the reader moved to one, or [`Error::Old`] once its budget gives up + /// on the wait. + fn poll_successor(&mut self, waiter: &kio::Waiter) -> Poll> { { let state = self.cursor.state.read(); // Only a delivery failure: content that failed to inflate is the group's under its @@ -4368,31 +4365,39 @@ impl Consumer { if self.expired || self.carry.as_ref().is_some_and(|carry| carry.ride.is_some()) || state.outcome.error().is_none() - || (!raw && state.malformed()) - || state.committed() < index + || (!self.cursor.raw && state.malformed()) + || state.committed() < self.cursor.index || state.sparse() { - return Poll::Ready(false); + return Poll::Ready(Ok(false)); } } let Some(track) = self.cursor.cache.track() else { - return Poll::Ready(false); - }; - let Some(copy) = ready!(track::successor(&track, self, waiter)) else { - return Poll::Ready(false); + return Poll::Ready(Ok(false)); }; - { - let state = copy.cursor.state.read(); - // A plain read inflates from frame 0, and a copy that shed the frame can't serve it. - if state.sparse() - || (!raw && state.flate.is_some() && state.offset > 0) - || state.check_read(index, raw).is_err() - { - return Poll::Ready(false); + let copy = match track::successor(&track, self, waiter) { + Poll::Ready(copy) => copy, + // A stall the budget bounds, though the copy read can receive nothing more. + Poll::Pending => { + return match self.poll_expiry(waiter, true) { + true => Poll::Ready(Err(Error::Old)), + false => Poll::Pending, + }; } - } + }; + let Some(copy) = copy else { + return Poll::Ready(Ok(false)); + }; self.adopt_copy(copy); - Poll::Ready(true) + Poll::Ready(Ok(true)) + } + + /// Whether this copy can continue `reader` from its frame. + pub(super) fn continues(&self, reader: &Consumer) -> bool { + let (index, raw) = (reader.cursor.index, reader.cursor.raw); + let state = self.cursor.state.read(); + // A plain read inflates from frame 0, and a copy that shed the frame can't serve it. + !state.sparse() && (raw || state.flate.is_none() || state.offset == 0) && state.check_read(index, raw).is_ok() } fn recover(&self) -> Option<&super::resume::Recover> { @@ -4557,7 +4562,18 @@ impl Consumer { res } + /// Like a frame read, a failed copy's end waits out a re-offer held for the group and + /// reports the end of the copy that took it over (see [`Self::poll_successor`]). fn poll_finished_once(&mut self, waiter: &kio::Waiter) -> Poll> { + loop { + let res = self.poll_finished_copy(waiter); + if !matches!(res, Poll::Ready(Err(_))) || !ready!(self.poll_successor(waiter))? { + return res; + } + } + } + + fn poll_finished_copy(&mut self, waiter: &kio::Waiter) -> Poll> { if self.expired { return Poll::Ready(Err(Error::Old)); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 75f5e5c5a09..abd20eaac00 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3792,6 +3792,10 @@ pub(super) fn successor( waiter: &kio::Waiter, ) -> Poll> { let sequence = reader.sequence; + let frames = group::Bounds { + start: reader.index(), + end: None, + }; let found = track.poll_ref(waiter, |state| { // Only a hold on the copy read: one on a copy that took over from it waits on that one. if state @@ -3801,11 +3805,16 @@ pub(super) fn successor( { return Poll::Pending; } + // Every copy, since one that fails or cannot serve the frame may sit before one that can. Poll::Ready( state - .covering_group(sequence, reader.index()) - .filter(|copy| !copy.is_read_by(reader)) - .and_then(group::Handle::try_consume), + .lookup + .get(&sequence) + .into_iter() + .flat_map(Slot::copies) + .filter(|copy| copy.covers(frames) && !copy.is_read_by(reader)) + .filter_map(group::Handle::try_consume) + .find(|copy| copy.continues(reader)), ) }); Poll::Ready(match found { @@ -17242,6 +17251,79 @@ mod test { } } + /// A failed copy's end follows its frame reads: it waits out a held re-offer rather than + /// report the failure, so a publisher watching it keeps the stream the re-offer continues. + #[kio::test] + #[cfg(feature = "async")] + async fn a_failed_copy_finishes_on_its_held_reoffer() { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(300), &b"a"[..]).unwrap(); + let mut reader = first.consume(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let waiter = kio::Waiter::noop(); + assert!(reader.poll_finished(&waiter).is_pending(), "waits on the held re-offer"); + + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer takes the group"); + }; + drop(hold); + again.write_frame(at(300), &b"a"[..]).unwrap(); + again.write_frame(at(301), &b"b"[..]).unwrap(); + assert!(reader.poll_finished(&waiter).is_pending(), "the re-offer is still open"); + again.finish().unwrap(); + assert_eq!(reader.finished().await.unwrap(), 1); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + } + + /// A reader waiting out a held re-offer is stalled, so its budget still gives up on it. + #[kio::test] + #[cfg(feature = "async")] + async fn waiting_on_a_held_reoffer_expires() { + for finished in [false, true] { + let producer = received_producer(Info::timed()); + let mut subscriber = producer + .subscribe(Subscription::default().with_delay(Delay::default().with_max(Duration::from_millis(500)))); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(0), &b"a"[..]).unwrap(); + let mut reader = subscriber.recv_group().await.unwrap().expect("group 3"); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + + let Ok(Received::Held(_hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let waiter = kio::Waiter::noop(); + assert!(reader.poll_read_frame(&waiter).is_pending(), "{finished}"); + + // Group 4 bounds group 3's reach at 1s, and group 5 puts the edge a second past it. + let mut later = Vec::new(); + for (sequence, millis) in [(4, 1000), (5, 2000)] { + let Ok(Received::Group(mut next)) = producer.create_group_at(group::Info { sequence }, 0) else { + panic!("group {sequence} opens"); + }; + next.write_frame(at(millis), &b"x"[..]).unwrap(); + later.push(next); + } + let res = match finished { + true => reader.finished().await.map(|_| ()), + false => reader.read_frame().await.map(|_| ()), + }; + assert!(matches!(res, Err(Error::Old)), "{finished}: {res:?}"); + } + } + /// Create and finish group `sequence`, with one frame at `millis` on a timed track. #[cfg(feature = "async")] fn put(producer: &Producer, sequence: u64, millis: Option) { From 06528deaaaecf6abbcc4b285bed777c05206060f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 08:21:42 -0800 Subject: [PATCH 11/21] fix(net): a successor shedding a waiting reader's frame settles its closed Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 2 ++ js/net/src/track.test.ts | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 113f15e0f65..f84f771401a 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -715,6 +715,8 @@ export class Producer { if (state.cacheBytes === bytes) return; state.charge?.sub(bytes - state.cacheBytes); state.flate?.shed(state.start); + // A failed copy's readers waiting on this one to take over may no longer find their frame. + if (state.succession?.next === this) state.succession.bump(); const lost = headShed(frames.first); for (const mirror of this.#mirrors?.keys() ?? []) { // A mirror that has not taken the front frame can no longer read it from its start. diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index b9c234cc7db..cdd511919bf 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3407,7 +3407,7 @@ test("an upstream drop ends a group still arriving with its error", async () => // failure, even to a reader only watching `closed`, as does a duplicate starting past the reader's // frame or one that shed it. A re-offer that fails too hands over to the next. A reader closed while it waits ends its // read and can be closed again, and one closed with frames still buffered reads them first. -test.each(["reoffer", "readable", "released", "observed", "unserved", "twice", "leaving", "closed", "past", "shed"])( +test.each(["reoffer", "readable", "released", "observed", "shedObserved", "unserved", "twice", "leaving", "closed", "past", "shed"])( "a failed copy continues on its held re-offer (%p)", async (kind) => { const producer = new TrackProducer("video").accept({ maxAge: null }); @@ -3464,6 +3464,23 @@ test.each(["reoffer", "readable", "released", "observed", "unserved", "twice", " producer.close(); return; } + if (kind === "shedObserved") { + // A re-offer that could take over until it shed the reader's frame settles `closed` then. + const again = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + arriving?.release(); + for (const payload of ["a", "b", "c"]) again.writeFrame({ payload: enc.encode(payload) }); + let closed: Error | null | undefined; + reader.closed.then((value) => { + closed = value; + }); + await settle(); + expect(closed).toBeUndefined(); + hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); + await settle(); + expect(closed).toBe(failed); + producer.close(); + return; + } if (kind === "unserved") { // A re-offer starting past the reader's frame cannot take over, so `closed` settles once // nothing else is held. From 47d81e3b4797c0015760512b0968473f6ebaa9f5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 08:49:25 -0800 Subject: [PATCH 12/21] fix(net): a takeover drops a stale route attempt, and a capped reader takes a copy cut at its cap JS: switching copies abandons a replacement still asked for the copy left behind, whose refusal otherwise failed the reader on the copy that took over. Rust: `track::successor` asks coverage up to the reader's cap, so a copy cut short at the back no earlier than that cap continues it. Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 3 + js/net/src/track.test.ts | 319 +++++++++++++++++++--------------- rs/moq-net/src/model/group.rs | 8 + rs/moq-net/src/model/track.rs | 39 ++++- 4 files changed, 222 insertions(+), 147 deletions(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index f84f771401a..c589b3e072e 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -1325,6 +1325,9 @@ export class Consumer { // Read the rest of the group from `group`, which starts at or below the frame this one reached. #switch(group: Consumer, expiry: Expiry | undefined): void { const index = this.#index(); + // A replacement still asked for the copy left behind is moot: its refusal would fail + // the reader on this one. A stall here asks again. + this.#abandon(); // The copy left behind stops teeing into the state this consumer read. if (this.#state.closed.peek() === undefined) this.#state.closed.set(null); this.#arrived ??= this.#state.arrived; diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index cdd511919bf..66a8ed73d3d 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -1,6 +1,6 @@ import { expect, spyOn, test } from "bun:test"; import { Duration, Instant, Manual, Wall, yieldNow } from "@moq/clock"; -import { race } from "@moq/signals"; +import { race, Signal } from "@moq/signals"; import { DEFAULT_EXPIRY } from "./cache.ts"; import { ProtocolViolation, Skipped, StreamCode, StreamError, TimestampMismatch, TooFarBehind } from "./error.ts"; import type { Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; @@ -3406,157 +3406,190 @@ test("an upstream drop ends a group still arriving with its error", async () => // continues on it from its frame with nothing repeated or missed. A released hold leaves the // failure, even to a reader only watching `closed`, as does a duplicate starting past the reader's // frame or one that shed it. A re-offer that fails too hands over to the next. A reader closed while it waits ends its -// read and can be closed again, and one closed with frames still buffered reads them first. -test.each(["reoffer", "readable", "released", "observed", "shedObserved", "unserved", "twice", "leaving", "closed", "past", "shed"])( - "a failed copy continues on its held re-offer (%p)", - async (kind) => { - const producer = new TrackProducer("video").accept({ maxAge: null }); - hooks.receiveTrack(producer); - const first = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); - first.writeFrame({ payload: enc.encode("a") }); - const reader = first.consume(); - expect(await reader.readString()).toBe("a"); - - const arriving = hooks.arrivingGroup(producer, 3n); - expect(arriving).toBeDefined(); - const failed = new Error("reset"); - if (kind === "closed") { - // A frame still buffered when the copy fails stays readable after a close. - first.writeFrame({ payload: enc.encode("c") }); - first.abort(failed); - reader.close(); - expect(await reader.readString()).toBe("c"); - await expect(reader.readString()).rejects.toBe(failed); - arriving?.release(); - producer.close(); - return; - } - first.abort(failed); - if (kind === "observed") { - let closed: Error | null | undefined; - reader.closed.then((value) => { - closed = value; - }); - await settle(); - expect(closed).toBeUndefined(); - arriving?.release(); - await settle(); - expect(closed).toBe(failed); - producer.close(); - return; - } - if (kind === "twice") { - // The reader reaches neither re-offer before the third copy takes over. - const second = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); - arriving?.release(); - const held = hooks.arrivingGroup(producer, 3n); - second.abort(failed); - await settle(); - expect(reader.closed.peek()).toBeUndefined(); - const third = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); - held?.release(); - third.writeFrame({ payload: enc.encode("a") }); - third.writeFrame({ payload: enc.encode("b") }); - third.finish(); - expect(await reader.readString()).toBe("b"); - expect(await reader.readString()).toBeUndefined(); - expect(await reader.closed).toBeNull(); - producer.close(); - return; - } - if (kind === "shedObserved") { - // A re-offer that could take over until it shed the reader's frame settles `closed` then. - const again = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); - arriving?.release(); - for (const payload of ["a", "b", "c"]) again.writeFrame({ payload: enc.encode(payload) }); - let closed: Error | null | undefined; - reader.closed.then((value) => { - closed = value; - }); - await settle(); - expect(closed).toBeUndefined(); - hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); - await settle(); - expect(closed).toBe(failed); - producer.close(); - return; - } - if (kind === "unserved") { - // A re-offer starting past the reader's frame cannot take over, so `closed` settles once - // nothing else is held. - hooks.receiveGroup(producer, { sequence: 3n, start: 2 }); - arriving?.release(); - await settle(); - expect(reader.closed.peek()).toBe(failed); - producer.close(); - return; - } - let read: string | Uint8Array | undefined | Error; - // A readiness poll waits the same way, and adopts the re-offer rather than spinning on the - // failed copy. - const pending = - kind === "readable" ? reader.readable().then(() => reader.tryReadFrame()?.payload) : reader.readString(); - const reading = pending.then( - (frame) => { - read = frame; - }, - (err: Error) => { - read = err; +// read and can be closed again, and one closed with frames still buffered reads them first. A route +// replacement asked for while the copy stalled is moot once a re-offer takes over. +test.each([ + "reoffer", + "readable", + "released", + "observed", + "shedObserved", + "unserved", + "twice", + "leaving", + "closed", + "past", + "shed", + "stalled", +])("a failed copy continues on its held re-offer (%p)", async (kind) => { + const producer = new TrackProducer("video").accept({ maxAge: null }); + hooks.receiveTrack(producer); + const first = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + first.writeFrame({ payload: enc.encode("a") }); + const reader = first.consume(); + expect(await reader.readString()).toBe("a"); + + const arriving = hooks.arrivingGroup(producer, 3n); + expect(arriving).toBeDefined(); + const failed = new Error("reset"); + // A front whose route moved: a stalled read asks it for a replacement, held out until rejected. + const asked: PromiseWithResolvers[] = []; + if (kind === "stalled") { + hooks.recoverGroup(reader, { + changed: new Signal(0), + wants: () => true, + replace: () => { + const attempt = Promise.withResolvers(); + asked.push(attempt); + return attempt.promise; }, - ); + }); + } + if (kind === "closed") { + // A frame still buffered when the copy fails stays readable after a close. + first.writeFrame({ payload: enc.encode("c") }); + first.abort(failed); + reader.close(); + expect(await reader.readString()).toBe("c"); + await expect(reader.readString()).rejects.toBe(failed); + arriving?.release(); + producer.close(); + return; + } + if (kind !== "stalled") first.abort(failed); + if (kind === "observed") { + let closed: Error | null | undefined; + reader.closed.then((value) => { + closed = value; + }); + await settle(); + expect(closed).toBeUndefined(); + arriving?.release(); + await settle(); + expect(closed).toBe(failed); + producer.close(); + return; + } + if (kind === "twice") { + // The reader reaches neither re-offer before the third copy takes over. + const second = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + arriving?.release(); + const held = hooks.arrivingGroup(producer, 3n); + second.abort(failed); await settle(); - expect(read).toBeUndefined(); expect(reader.closed.peek()).toBeUndefined(); - - if (kind === "leaving") { - reader.close(); - await reading; - expect(read).toBe(failed); - // Closing is idempotent, even after the wait was cancelled. - reader.close(); - arriving?.release(); - producer.close(); - return; - } - if (kind === "released") { - arriving?.release(); - await reading; - expect(read).toBe(failed); - expect(await reader.closed).toBe(failed); - producer.close(); - return; - } - const again = hooks.receiveGroup(producer, { sequence: 3n, start: kind === "past" ? 2 : 0 }); + const third = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + held?.release(); + third.writeFrame({ payload: enc.encode("a") }); + third.writeFrame({ payload: enc.encode("b") }); + third.finish(); + expect(await reader.readString()).toBe("b"); + expect(await reader.readString()).toBeUndefined(); + expect(await reader.closed).toBeNull(); + producer.close(); + return; + } + if (kind === "shedObserved") { + // A re-offer that could take over until it shed the reader's frame settles `closed` then. + const again = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + arriving?.release(); + for (const payload of ["a", "b", "c"]) again.writeFrame({ payload: enc.encode(payload) }); + let closed: Error | null | undefined; + reader.closed.then((value) => { + closed = value; + }); + await settle(); + expect(closed).toBeUndefined(); + hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); + await settle(); + expect(closed).toBe(failed); + producer.close(); + return; + } + if (kind === "unserved") { + // A re-offer starting past the reader's frame cannot take over, so `closed` settles once + // nothing else is held. + hooks.receiveGroup(producer, { sequence: 3n, start: 2 }); arriving?.release(); - if (kind === "shed") { - // The re-offer drops frames 0 and 1 to cache pressure before the reader reaches it. - again.writeFrame({ payload: enc.encode("a") }); - again.writeFrame({ payload: enc.encode("b") }); - again.writeFrame({ payload: enc.encode("c") }); - hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); - await reading; - expect(read).toBe(failed); - producer.close(); - return; - } - if (kind === "past") { - await reading; - expect(read).toBe(failed); - producer.close(); - return; - } await settle(); - expect(read).toBeUndefined(); + expect(reader.closed.peek()).toBe(failed); + producer.close(); + return; + } + let read: string | Uint8Array | undefined | Error; + // A readiness poll waits the same way, and adopts the re-offer rather than spinning on the + // failed copy. + const pending = + kind === "readable" ? reader.readable().then(() => reader.tryReadFrame()?.payload) : reader.readString(); + const reading = pending.then( + (frame) => { + read = frame; + }, + (err: Error) => { + read = err; + }, + ); + await settle(); + expect(read).toBeUndefined(); + expect(reader.closed.peek()).toBeUndefined(); + + if (kind === "stalled") { + // The stall's attempt is out before the copy fails. + expect(asked.length).toBe(1); + first.abort(failed); + await settle(); + } + if (kind === "leaving") { + reader.close(); + await reading; + expect(read).toBe(failed); + // Closing is idempotent, even after the wait was cancelled. + reader.close(); + arriving?.release(); + producer.close(); + return; + } + if (kind === "released") { + arriving?.release(); + await reading; + expect(read).toBe(failed); + expect(await reader.closed).toBe(failed); + producer.close(); + return; + } + const again = hooks.receiveGroup(producer, { sequence: 3n, start: kind === "past" ? 2 : 0 }); + arriving?.release(); + if (kind === "shed") { + // The re-offer drops frames 0 and 1 to cache pressure before the reader reaches it. again.writeFrame({ payload: enc.encode("a") }); again.writeFrame({ payload: enc.encode("b") }); - again.finish(); + again.writeFrame({ payload: enc.encode("c") }); + hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); await reading; - expect(read).toEqual(kind === "readable" ? enc.encode("b") : "b"); - expect(await reader.readString()).toBeUndefined(); - expect(await reader.closed).toBeNull(); + expect(read).toBe(failed); producer.close(); - }, -); + return; + } + if (kind === "past") { + await reading; + expect(read).toBe(failed); + producer.close(); + return; + } + await settle(); + expect(read).toBeUndefined(); + asked[0]?.reject(new Error("route refused")); + await settle(); + expect(read).toBeUndefined(); + again.writeFrame({ payload: enc.encode("a") }); + again.writeFrame({ payload: enc.encode("b") }); + again.finish(); + await reading; + expect(read).toEqual(kind === "readable" ? enc.encode("b") : "b"); + expect(await reader.readString()).toBeUndefined(); + expect(await reader.closed).toBeNull(); + producer.close(); +}); // A subscriber's reader of a failed copy keeps its place when a re-offer replaces that copy in the // track, continuing on it, and its budget still gives up on it while it waits on a held re-offer. diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 51079beb88e..1a41224345f 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -4449,6 +4449,14 @@ impl Consumer { self.cursor.raw = raw; } + /// The frames this reader has yet to read: from its cursor to its cap. + pub(super) fn unread(&self) -> Bounds { + Bounds { + start: self.cursor.index as u64, + end: self.cursor.end.map(|end| end as u64), + } + } + /// One past the last frame the copy read committed. pub(super) fn committed(&self) -> usize { self.cursor.state.read().committed() diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index abd20eaac00..fcab9b9b5ca 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3792,10 +3792,8 @@ pub(super) fn successor( waiter: &kio::Waiter, ) -> Poll> { let sequence = reader.sequence; - let frames = group::Bounds { - start: reader.index(), - end: None, - }; + // A copy cut short at the back still continues a reader capped no later than the cut. + let frames = reader.unread(); let found = track.poll_ref(waiter, |state| { // Only a hold on the copy read: one on a copy that took over from it waits on that one. if state @@ -17284,6 +17282,39 @@ mod test { assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); } + /// A capped reader continues on a copy cut short at the back no earlier than its cap, which + /// holds every frame it still reads. + #[kio::test] + #[cfg(feature = "async")] + async fn a_capped_reader_continues_on_a_copy_cut_at_its_cap() { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + // Received from frame 1, so a fetch of the group's head can sit beside it. + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 1) else { + panic!("the first copy opens"); + }; + first.write_frame(at(301), &b"b"[..]).unwrap(); + let mut reader = first.consume().with_range(..3); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + + let mut head = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(3, group::Bounds { start: 0, end: Some(3) }, None) + .unwrap(); + for (millis, payload) in [(300, b"a"), (301, b"b"), (302, b"c")] { + head.write_frame(at(millis), &payload[..]).unwrap(); + } + head.finish().unwrap(); + first.abort(Error::Cancel).unwrap(); + // An uncapped reader needs frames past the head, which no copy holds. + assert!(reader.clone().with_end(None).read_frame().await.is_err()); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"c"); + assert!(reader.read_frame().await.unwrap().is_none()); + } + /// A reader waiting out a held re-offer is stalled, so its budget still gives up on it. #[kio::test] #[cfg(feature = "async")] From 41a142899ab4f652828e8443ad999b636333636c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 08:59:52 -0800 Subject: [PATCH 13/21] fix(net): a failure a JS reader's closed reported stands against a later re-offer Once `closed` or `done` settled with the copy's failure, nothing could take over then, so `#succeed` no longer continues on a re-offer arriving later and reads agree with what was reported. Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 2 ++ js/net/src/track.test.ts | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index c589b3e072e..9577b2a4f1b 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -1115,6 +1115,8 @@ export class Consumer { async #succeed(): Promise { const succession = this.#state.succession; if (!succession || this.#released || this.#verdict.peek() || this.#torn()) return false; + // A failure `closed` or `done` already reported, with nothing then able to take over, stands. + if (this.#closed?.peek() instanceof Error || this.#finished?.peek() instanceof Error) return false; let next = succession.serving(this.#index()); while (!next) { if (succession.holds === 0) return false; diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 66a8ed73d3d..4c87f279809 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3467,6 +3467,11 @@ test.each([ arriving?.release(); await settle(); expect(closed).toBe(failed); + // The failure it reported stands: a re-offer arriving later does not take over. + const late = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + late.writeFrame({ payload: enc.encode("a") }); + late.writeFrame({ payload: enc.encode("b") }); + await expect(reader.readString()).rejects.toBe(failed); producer.close(); return; } From 75aa85760ffc5cfc89cf5b9f0c6f8426ac7217d1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 12:19:58 -0800 Subject: [PATCH 14/21] feat(net): a held JS copy tees its successor into its readers; a raised Rust cap past a cut finds the rest JS: while a re-offer is held, a failed received copy keeps its mirrors open and the copy that takes over tees frames into each from where it stopped, so consumers never see the failure. Replaces the consumer-side Succession. Rust: a reader whose cap is raised past an adopted copy's back cut looks for a copy covering the rest instead of ending cleanly at the cut. Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 254 ++++++++++++---------------------- js/net/src/internal.ts | 15 +- js/net/src/queue.ts | 5 + js/net/src/track.test.ts | 133 +++++++++++++----- js/net/src/track.ts | 58 +++++--- rs/moq-net/src/model/group.rs | 22 ++- rs/moq-net/src/model/track.rs | 53 +++++++ 7 files changed, 318 insertions(+), 222 deletions(-) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 9577b2a4f1b..88b9f7f6ca9 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -130,41 +130,17 @@ class GroupState { // Tells the group this copy was mirrored from whether its reader will still read it, so a // copy buffered for a reader that passed it stops keeping the group wanted. reads?: (reads: boolean) => void; - // A received copy's, shared with every mirror and incarnation: what may take the group over - // once it fails. - succession?: Succession; + // Stops the group teeing into this mirror from watching it close. + unwatch?: () => void; + // The first frame the group teeing into this mirror wrote here. A copy that took over a failed + // one tees from where that one stopped, so the frames below came from the failed copy. + from = 0; constructor(sequence: bigint) { this.sequence = sequence; } } -// The copies of a received group's sequence in one track, shared by each incarnation and its -// mirrors, so a reader of a failed copy continues on the latest (see `Consumer.#succeed`). A -// group's name fixes its content. Only the latest is kept, so re-offers retain no chain of copies. -class Succession { - // Duplicates held while a copy arrives (see `hooks.holdGroup`), any of which may take over. - holds = 0; - // The latest copy, once one took over a failed one. - next?: Producer; - // Bumped whenever a takeover may have become possible or impossible. - readonly changed = new Signal(0); - - bump(): void { - this.changed.update((n) => n + 1); - } - - // The latest copy, if it can continue a reader from frame `index`. - serving(index: number): Producer | undefined { - const next = this.next; - return next && serves(next, index) ? next : undefined; - } -} - -// Whether `group` has not failed and still holds every frame from `index` on, so a mirror of it -// continues a reader there. -let serves: (group: Producer, index: number) => boolean; - // A written frame and when it landed. It keeps the writer's frame rather than copying it, so a // lazy payload is read only when a reader takes it. class Landed implements Frame { @@ -290,6 +266,10 @@ export class Producer { // Called as the group aborts, before any signal subscriber runs (see `hooks.onGroupAbort`). #aborted?: ((error: Error) => void)[]; + // Duplicates waiting on this received copy (see `hooks.holdGroup`). Failing while held, it + // keeps its mirrors open for the duplicate that takes over to tee into. + #holds = 0; + constructor(sequence: bigint) { if (checkSequence(sequence) >= RESERVED) throw new RangeError(`group sequence out of range: ${sequence}`); this.#state = new GroupState(sequence); @@ -329,42 +309,38 @@ export class Producer { if (state.closed.peek() !== undefined) flate.finish(); }; hooks.groupOffset = (group) => group.#state.offset; - serves = (group, index) => { - const state = group.#state; - return ( - !(state.closed.peek() instanceof Error) && - state.start === state.offset && - state.start <= index && - !headShed(state.frames.peek().first) - ); - }; - hooks.receivedGroup = (group, failed) => { - if (failed === undefined) { - group.#state.succession = new Succession(); - return; - } - const succession = failed.#state.succession ?? new Succession(); - group.#state.succession = succession; - succession.next = group; - succession.bump(); - // A reader waiting on this copy gives up once it fails with nothing held. - group.#state.closed.changed((closed) => { - if (closed instanceof Error && succession.next === group) succession.bump(); - }); - }; hooks.holdGroup = (group) => { - const succession = group.#state.succession; - if (!succession) return () => {}; - succession.holds++; - succession.bump(); + group.#holds++; let held = true; return () => { if (!held) return; held = false; - succession.holds--; - succession.bump(); + if (--group.#holds === 0) group.#failHeld(); }; }; + hooks.succeedGroup = (next, failed) => { + const error = failed.#state.closed.peek(); + if (!(error instanceof Error)) throw new Error("group has not failed"); + const state = next.#state; + for (const [mirror, reads] of failed.#mirrors ?? []) { + failed.#unmirror(mirror); + // The successor continues a mirror from the frame it reached, so it must hold that + // frame from its start. Compression binds a group to its prefix, so a compressed + // successor starts at 0. + if (mirror.closed.peek() !== undefined) continue; + const lost = state.start === mirror.total.peek() && headShed(state.frames.peek().first); + if (state.closed.peek() !== undefined || state.start > mirror.total.peek() || lost) { + mirror.closed.set(error); + continue; + } + mirror.from = mirror.total.peek(); + mirror.charge = state.charge; + if (state.compressed) mirror.flate = state.flate; + let index = state.start; + for (const frame of state.frames.peek()) if (index++ >= mirror.from) appendFrame(mirror, frame); + next.#attach(mirror, reads); + } + }; hooks.startGroup = (group, start) => { if (!Number.isSafeInteger(start) || start < 0) throw new RangeError("invalid group frame start"); const state = group.#state; @@ -455,7 +431,6 @@ export class Producer { dst.flate = this.#state.flate; dst.compressed = this.#state.compressed; dst.charge = this.#state.charge; - dst.succession = this.#state.succession; // A track restamps its own mirror with when the group joined it. dst.arrived = this.#state.arrived; if (this.#state.start === this.#state.offset && !headShed(this.#state.frames.peek().first)) { @@ -479,11 +454,17 @@ export class Producer { return makeConsumer(dst); } + this.#attach(dst, true); + return makeConsumer(dst); + } + + // Tee later writes into `dst`, counting its reader if it `reads`. + #attach(dst: GroupState, reads: boolean): void { this.#mirrors ??= new Map(); const mirrors = this.#mirrors; - mirrors.set(dst, true); - this.#readers++; - this.#wanted.set(true); + mirrors.set(dst, reads); + if (reads) this.#readers++; + this.#wanted.set(this.#readers > 0); dst.reads = (reads) => { const was = mirrors.get(dst); if (was === undefined || was === reads) return; @@ -494,13 +475,9 @@ export class Producer { // Track this mirror's close eagerly (not just lazily on the next write) so demand drops the // moment the last reader leaves, letting an abandoned fetch cancel even with no more frames. - const dispose = dst.closed.subscribe((c) => { - if (c === undefined) return; - this.#unmirror(dst); - dispose(); + dst.unwatch = dst.closed.subscribe((c) => { + if (c !== undefined) this.#unmirror(dst); }); - - return makeConsumer(dst); } /** @@ -519,6 +496,8 @@ export class Producer { this.#mirrors?.delete(mirror); // A consumer that outlives the group must not pin it, and its buffers, through this. mirror.reads = undefined; + mirror.unwatch?.(); + mirror.unwatch = undefined; if (reads) this.#readers--; this.#wanted.set(this.#readers > 0); } @@ -641,9 +620,11 @@ export class Producer { for (const commit of commits) commit(); } + const index = this.#state.total.peek() - 1; for (const mirror of this.#mirrors?.keys() ?? []) { if (mirror.closed.peek() !== undefined) this.#unmirror(mirror); - else appendFrame(mirror, frame); + // A mirror taken over from a failed copy already holds the frames that copy wrote. + else if (index >= mirror.total.peek()) appendFrame(mirror, frame); } this.#trim(); // Past the frame's own bookkeeping: paying the track's debt may evict other groups. @@ -715,13 +696,12 @@ export class Producer { if (state.cacheBytes === bytes) return; state.charge?.sub(bytes - state.cacheBytes); state.flate?.shed(state.start); - // A failed copy's readers waiting on this one to take over may no longer find their frame. - if (state.succession?.next === this) state.succession.bump(); const lost = headShed(frames.first); for (const mirror of this.#mirrors?.keys() ?? []) { // A mirror that has not taken the front frame can no longer read it from its start. if (mirror.closed.peek() !== undefined) continue; - if (mirror.start > state.start || (mirror.start === state.start && !lost)) continue; + const front = Math.max(mirror.start, mirror.from); + if (front > state.start || (front === state.start && !lost)) continue; mirror.frames.set(new Queue()); mirror.cacheBytes = 0; mirror.flate = undefined; @@ -755,21 +735,41 @@ export class Producer { #end(outcome: Error | null) { if (this.#state.closed.peek() !== undefined) return; // A frame cut short leaves the group incomplete, so even a clean end fails it. + let cut: Chunked | undefined; if (this.#open) { this.#open = undefined; outcome ??= new Error("group finished while a frame was being written"); const last = this.#state.frames.peek().last; - if (last instanceof Chunked && last.done.peek() === undefined) last.done.set(outcome); + if (last instanceof Chunked && last.done.peek() === undefined) cut = last; } + const held = outcome !== null && this.#holds > 0; + // A held copy's duplicate continues each mirror from the frame it reached, so a mirror + // drops the frame cut short before its reader takes it. One whose reader took it lost it. + if (held && cut) { + for (const mirror of this.#mirrors?.keys() ?? []) { + if (mirror.frames.peek().last === cut) { + mirror.frames.mutate((frames) => frames.pop()); + mirror.total.update((total) => total - 1); + } else if (mirror.closed.peek() === undefined) mirror.closed.set(outcome); + } + } + cut?.done.set(outcome); this.#state.flate?.finish(); this.#state.closed.set(outcome); + if (!held) this.#failHeld(); + if (outcome) for (const fn of this.#aborted ?? []) fn(outcome); + this.#aborted = undefined; + } + + // End the open mirrors, once this copy closed and no duplicate held for it can take over. + #failHeld(): void { + const outcome = this.#state.closed.peek(); + if (outcome === undefined) return; for (const mirror of this.#mirrors?.keys() ?? []) { if (mirror.closed.peek() === undefined) mirror.closed.set(outcome); this.#unmirror(mirror); } - if (outcome) for (const fn of this.#aborted ?? []) fn(outcome); - this.#aborted = undefined; } } @@ -881,26 +881,16 @@ class CombinedClosed implements GetPromise { #preferred: Once; #source: () => Once; #final: (value: Error | null) => boolean; - // What else may change `final`'s answer, watched like the source. - #wake: () => Signal | undefined; #value = new Once(); #watched?: Once; // The watcher on `#watched`, replaced when a recovery moves to another copy's state. #unwatch?: Dispose; - #woken?: Signal; - #unwake?: Dispose; #dispose: Dispose[] = []; - constructor( - preferred: Once, - source: () => Once, - final: (value: Error | null) => boolean, - wake: () => Signal | undefined, - ) { + constructor(preferred: Once, source: () => Once, final: (value: Error | null) => boolean) { this.#preferred = preferred; this.#source = source; this.#final = final; - this.#wake = wake; if (this.#sync()) return; this.#dispose.push(preferred.changed(() => this.#sync())); } @@ -918,12 +908,6 @@ class CombinedClosed implements GetPromise { this.#watched = source; this.#unwatch = source.changed(() => this.#sync()); } - const wake = this.#wake(); - if (this.#woken !== wake) { - this.#unwake?.(); - this.#woken = wake; - this.#unwake = wake?.subscribe(() => this.#sync()); - } return false; } @@ -933,9 +917,6 @@ class CombinedClosed implements GetPromise { this.#unwatch?.(); this.#unwatch = undefined; this.#watched = undefined; - this.#unwake?.(); - this.#unwake = undefined; - this.#woken = undefined; return true; } @@ -1001,8 +982,6 @@ export class Consumer { #expired?: Expired; // Closed by its reader, which ends it whatever is left unread. #released = false; - // Set once released, for a read waiting on a copy that may take the group over. - #leaving?: Once; // The arrival of the copy first read, kept once a replacement copy is adopted. #arrived?: Instant; // The last frame handed out to stream before it completed. Cut short, it is lost, so no @@ -1038,7 +1017,6 @@ export class Consumer { () => this.#state.closed, (value) => value instanceof Error ? !this.#recovers(value) : this.#released || this.#bufferedFrames().length === 0, - () => this.#state.succession?.changed, ); return this.#closed; } @@ -1052,7 +1030,6 @@ export class Consumer { (value) => (!(value instanceof Error) || !this.#recovers(value)) && (this.#released || this.#bufferedFrames().length === 0), - () => this.#state.succession?.changed, ); return this.#finished; } @@ -1094,43 +1071,9 @@ export class Consumer { return closed instanceof Error && !this.#recovers(closed) ? closed : undefined; } - // Whether a failure of the copy being read may still be continued from another copy. + // Whether a failure of the copy being read may still be continued from another route. #recovers(failed: Error): boolean { - return !this.#verdict.peek() && !this.#torn() && (this.#succeeds() || this.#recover?.wants(failed) === true); - } - - // Whether another copy in the track may take over the failed one being read: one took it and - // can serve the frame read next, or one is held. - #succeeds(): boolean { - const succession = this.#state.succession; - return ( - !this.#released && - succession !== undefined && - (succession.serving(this.#index()) !== undefined || succession.holds > 0) - ); - } - - // Continue on the copy that took this group's sequence in its track once the one being read - // failed, waiting out a duplicate still held for it. Returns whether the read should look again. - async #succeed(): Promise { - const succession = this.#state.succession; - if (!succession || this.#released || this.#verdict.peek() || this.#torn()) return false; - // A failure `closed` or `done` already reported, with nothing then able to take over, stands. - if (this.#closed?.peek() instanceof Error || this.#finished?.peek() instanceof Error) return false; - let next = succession.serving(this.#index()); - while (!next) { - if (succession.holds === 0) return false; - // Waiting on a held copy is a stall the budget bounds. - if (this.#expire()) return true; - this.#leaving ??= new Once(); - await Signal.race(succession.changed, this.#verdict, this.#leaving, ...(this.#expiry?.changed ?? [])); - // A closed reader ends on the failure rather than continue elsewhere. - if (this.#released) return false; - if (this.#verdict.peek()) return true; - next = succession.serving(this.#index()); - } - this.#switch(next.mirror(), this.#expiry); - return true; + return !this.#verdict.peek() && !this.#torn() && this.#recover?.wants(failed) === true; } // Whether a frame handed out to stream was cut short. @@ -1180,7 +1123,8 @@ export class Consumer { hooks.evictGroup = (group, lost) => group.#evict(lost); shedReader = (group, start) => { // A copy that took every shed frame keeps reading; one that had not lost them. - if (group.#state === group.#mirrored && group.#state.start < start) group.#evict(evicted()); + const state = group.#state; + if (state === group.#mirrored && Math.max(state.start, state.from) < start) group.#evict(evicted()); }; hooks.refreshGroup = (group) => group.#state.charge?.refresh(); hooks.onShedRead = (group, notify) => { @@ -1197,8 +1141,7 @@ export class Consumer { // the group, so a clean end would pass the prefix off as the whole group. #expire(): boolean { if (this.#verdict.peek()) return false; - // A failed copy is no stall, unless the reader waits for another to take it over. - if (this.#state.closed.peek() instanceof Error && !this.#succeeds()) return false; + if (this.#state.closed.peek() instanceof Error) return false; if (!this.#expiry?.expired()) return false; this.#fail( @@ -1219,24 +1162,23 @@ export class Consumer { #evict(lost: Error): boolean { // A group read on from another copy is that copy's to reclaim. if (this.#state !== this.#mirrored) return false; - const closed = this.#state.closed.peek(); - // A failed copy that another copy or route may still continue keeps the reader's place, - // and that copy serves the frames dropped here. - const continues = closed instanceof Error && this.#recovers(closed); const frames = this.#bufferedFrames(); - if (frames.length > 0 && !continues) { + if (frames.length > 0) { // Unread content is going away either way. If the budget had already given up // on this group that is the more specific reason, so ask it first; otherwise // the cache reclaimed it before the reader got to it. this.#expire(); this.#fail(lost); + } + if (frames.length > 0) { this.#state.start += frames.length; } this.#state.cacheBytes = 0; this.#state.flate = undefined; this.#state.frames.set(new Queue()); - // A failed copy holds nothing more to reclaim. - return !continues; + // A failed copy holds nothing more to reclaim, and its recovery may still continue the group. + const closed = this.#state.closed.peek(); + return !(closed instanceof Error && this.#recovers(closed)); } // The next change a parked read waits for. @@ -1321,20 +1263,11 @@ export class Consumer { return; } replacement.adopt(); - this.#switch(replacement.group, replacement.expiry); - } - - // Read the rest of the group from `group`, which starts at or below the frame this one reached. - #switch(group: Consumer, expiry: Expiry | undefined): void { - const index = this.#index(); - // A replacement still asked for the copy left behind is moot: its refusal would fail - // the reader on this one. A stall here asks again. - this.#abandon(); // The copy left behind stops teeing into the state this consumer read. if (this.#state.closed.peek() === undefined) this.#state.closed.set(null); this.#arrived ??= this.#state.arrived; - this.#state = group.#state; - this.#expiry = expiry; + this.#state = next; + this.#expiry = replacement.expiry; this.#skip = index; this.#refresh(); this.#expired?.refresh(); @@ -1462,11 +1395,9 @@ export class Consumer { } if (this.#ready()) return; const closed = this.#state.closed.peek(); - if (closed instanceof Error && ((await this.#succeed()) || (await this.#resume(closed)))) continue; + if (closed instanceof Error && (await this.#resume(closed))) continue; if (closed !== undefined) { this.#abandon(); - // Ends the reader, whose failure a copy that might have taken over left open. - if (closed instanceof Error) this.#fail(closed); this.#refresh(); return; } @@ -1529,11 +1460,9 @@ export class Consumer { const closed = this.#state.closed.peek(); if (closed instanceof Error) { - if (await this.#succeed()) continue; if (await this.#resume(closed)) continue; this.#abandon(); - // Ends the reader, whose failure a copy that might have taken over left open. - throw this.#fail(closed); + throw closed; } if (closed !== undefined) { // The copy finished the group: a replacement asked for meanwhile is moot. @@ -1620,7 +1549,6 @@ export class Consumer { this.#recover = undefined; // Closing is terminal for this reader, even with frames still unread. this.#released = true; - if (this.#leaving?.peek() === undefined) this.#leaving?.set(true); if (this.#state.closed.peek() === undefined) this.#state.closed.set(abort ?? null); this.#refresh(); } diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 7615697ea66..5e535bd0f51 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -631,7 +631,7 @@ export const hooks: { receiveGroup: (producer: Producer, group: { sequence: bigint; start: number; fill?: boolean }) => GroupProducer; /** * Hold the copy of `sequence` still arriving, if one is, for a duplicate to wait on: until - * released, a reader of that copy that fails waits for the duplicate to take the group. Assigned + * released, that copy's failure leaves its readers open for the duplicate to continue. Assigned * by `track.ts`. */ arrivingGroup: (producer: Producer, sequence: bigint) => Arriving | undefined; @@ -915,12 +915,15 @@ export const hooks: { */ recoverGroup: (group: GroupConsumer, recover: GroupRecover) => void; /** - * Mark `group` a received copy, which a later copy of its sequence may take over once it fails, - * and `failed`, if given, as the copy it took over from. Assigned by `group.ts`. + * Hold a received copy still arriving for a duplicate: if it fails while held, its readers wait + * for the duplicate to take over (see {@link Arriving}). Assigned by `group.ts`. */ - receivedGroup: (group: GroupProducer, failed?: GroupProducer) => void; - /** Hold a received copy still arriving for a duplicate: see {@link Arriving}. Assigned by `group.ts`. */ holdGroup: (group: GroupProducer) => () => void; + /** + * Hand `next` the readers `failed` kept open while held, teeing each from the frame it reached. + * Fails those `next` cannot serve. Assigned by `group.ts`. + */ + succeedGroup: (next: GroupProducer, failed: GroupProducer) => void; /** Read a payload chunk, releasing its wait when the output stream closes. */ readFrameChunk: (frame: FrameConsumer, closed: Promise) => Promise; /** Read the next frame, as its stored DEFLATE slice when `compressed`, so a lite-07 publisher forwards it without inflating. */ @@ -1192,7 +1195,7 @@ export const hooks: { arrivingGroup: () => { throw new Error("track.ts not loaded"); }, - receivedGroup: () => { + succeedGroup: () => { throw new Error("group.ts not loaded"); }, holdGroup: () => { diff --git a/js/net/src/queue.ts b/js/net/src/queue.ts index 5e1843bd9e4..b0524c961d2 100644 --- a/js/net/src/queue.ts +++ b/js/net/src/queue.ts @@ -28,6 +28,11 @@ export class Queue implements Iterable { this.#items.push(item); } + /** Remove the newest entry. */ + pop(): T | undefined { + return this.length === 0 ? undefined : this.#items.pop(); + } + shift(): T | undefined { if (this.length === 0) return undefined; const item = this.#items[this.#head]; diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 4c87f279809..a8cee7e5afb 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -1,6 +1,6 @@ import { expect, spyOn, test } from "bun:test"; import { Duration, Instant, Manual, Wall, yieldNow } from "@moq/clock"; -import { race, Signal } from "@moq/signals"; +import { race } from "@moq/signals"; import { DEFAULT_EXPIRY } from "./cache.ts"; import { ProtocolViolation, Skipped, StreamCode, StreamError, TimestampMismatch, TooFarBehind } from "./error.ts"; import type { Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; @@ -3405,9 +3405,9 @@ test("an upstream drop ends a group still arriving with its error", async () => // A reader on a received copy that fails waits out a duplicate still held for its group, then // continues on it from its frame with nothing repeated or missed. A released hold leaves the // failure, even to a reader only watching `closed`, as does a duplicate starting past the reader's -// frame or one that shed it. A re-offer that fails too hands over to the next. A reader closed while it waits ends its -// read and can be closed again, and one closed with frames still buffered reads them first. A route -// replacement asked for while the copy stalled is moot once a re-offer takes over. +// frame. One that sheds the reader's frame after taking over evicts it, as any copy does a reader +// behind it. A re-offer that fails too hands over to the next. A reader closed while it waits ends +// cleanly and can be closed again, and one closed with frames still buffered reads them first. test.each([ "reoffer", "readable", @@ -3420,7 +3420,6 @@ test.each([ "closed", "past", "shed", - "stalled", ])("a failed copy continues on its held re-offer (%p)", async (kind) => { const producer = new TrackProducer("video").accept({ maxAge: null }); hooks.receiveTrack(producer); @@ -3432,31 +3431,18 @@ test.each([ const arriving = hooks.arrivingGroup(producer, 3n); expect(arriving).toBeDefined(); const failed = new Error("reset"); - // A front whose route moved: a stalled read asks it for a replacement, held out until rejected. - const asked: PromiseWithResolvers[] = []; - if (kind === "stalled") { - hooks.recoverGroup(reader, { - changed: new Signal(0), - wants: () => true, - replace: () => { - const attempt = Promise.withResolvers(); - asked.push(attempt); - return attempt.promise; - }, - }); - } if (kind === "closed") { // A frame still buffered when the copy fails stays readable after a close. first.writeFrame({ payload: enc.encode("c") }); first.abort(failed); reader.close(); expect(await reader.readString()).toBe("c"); - await expect(reader.readString()).rejects.toBe(failed); + expect(await reader.readString()).toBeUndefined(); arriving?.release(); producer.close(); return; } - if (kind !== "stalled") first.abort(failed); + first.abort(failed); if (kind === "observed") { let closed: Error | null | undefined; reader.closed.then((value) => { @@ -3495,7 +3481,7 @@ test.each([ return; } if (kind === "shedObserved") { - // A re-offer that could take over until it shed the reader's frame settles `closed` then. + // A re-offer that took over and then shed the reader's frame settles `closed` then. const again = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); arriving?.release(); for (const payload of ["a", "b", "c"]) again.writeFrame({ payload: enc.encode(payload) }); @@ -3507,7 +3493,7 @@ test.each([ expect(closed).toBeUndefined(); hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); await settle(); - expect(closed).toBe(failed); + expect(closed).toMatchObject({ code: StreamCode.Evicted }); producer.close(); return; } @@ -3538,16 +3524,10 @@ test.each([ expect(read).toBeUndefined(); expect(reader.closed.peek()).toBeUndefined(); - if (kind === "stalled") { - // The stall's attempt is out before the copy fails. - expect(asked.length).toBe(1); - first.abort(failed); - await settle(); - } if (kind === "leaving") { reader.close(); await reading; - expect(read).toBe(failed); + expect(read).toBeUndefined(); // Closing is idempotent, even after the wait was cancelled. reader.close(); arriving?.release(); @@ -3571,7 +3551,7 @@ test.each([ again.writeFrame({ payload: enc.encode("c") }); hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); await reading; - expect(read).toBe(failed); + expect(read).toMatchObject({ code: StreamCode.Evicted }); producer.close(); return; } @@ -3583,9 +3563,6 @@ test.each([ } await settle(); expect(read).toBeUndefined(); - asked[0]?.reject(new Error("route refused")); - await settle(); - expect(read).toBeUndefined(); again.writeFrame({ payload: enc.encode("a") }); again.writeFrame({ payload: enc.encode("b") }); again.finish(); @@ -3596,6 +3573,96 @@ test.each([ producer.close(); }); +// While a duplicate is held, a failed copy keeps each mirror open and the copy that takes over tees +// into it from the frame it reached: a sink still buffering the group hands it out once, a frame cut +// short is read whole from the successor unless a reader already took part of it, readers at +// different frames each continue from their own, and with no takeover every mirror fails once. +test.each(["buffered", "handed", "partial", "positions", "once"])( + "a held copy's mirrors continue on its successor (%p)", + async (kind) => { + const producer = new TrackProducer("video").accept({ maxAge: null }); + hooks.receiveTrack(producer); + const track = producer.subscribe(); + const first = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + first.writeFrame({ payload: enc.encode("a") }); + const arriving = hooks.arrivingGroup(producer, 3n); + const failed = new Error("reset"); + const takeover = () => { + const again = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + arriving?.release(); + for (const payload of ["a", "b", "c"]) again.writeFrame({ payload: enc.encode(payload) }); + again.finish(); + }; + + if (kind === "buffered" || kind === "handed") { + // The sink hands the group out before or after its copy fails. + const handed = kind === "handed" ? await track.recvGroup() : undefined; + if (handed) expect(await handed.readString()).toBe("a"); + first.abort(failed); + takeover(); + const group = handed ?? (await track.recvGroup()); + expect(group?.sequence).toBe(3n); + for (const payload of handed ? ["b", "c"] : ["a", "b", "c"]) + expect(await group?.readString()).toBe(payload); + expect(await group?.readString()).toBeUndefined(); + // The sink held one copy of the group, so it hands out nothing more. + expect(await settled(track.recvGroup())).toBe(false); + } else if (kind === "partial") { + const ahead = first.consume(); + const behind = first.consume(); + expect(await ahead.readString()).toBe("a"); + const cut = first.createFrame({ size: 2 }); + cut.write(enc.encode("b")); + // `ahead` joins the frame as it arrives; `behind` has yet to reach it. + const joining = ahead.readString(); + await settle(); + first.abort(failed); + await expect(joining).rejects.toBe(failed); + takeover(); + for (const payload of ["a", "b", "c"]) expect(await behind.readString()).toBe(payload); + expect(await behind.readString()).toBeUndefined(); + } else if (kind === "positions") { + first.writeFrame({ payload: enc.encode("b") }); + const readers = [first.consume(), first.consume(), first.consume()]; + expect(await readers[1].readString()).toBe("a"); + expect(await readers[2].readString()).toBe("a"); + expect(await readers[2].readString()).toBe("b"); + first.abort(failed); + takeover(); + const rest = [["a", "b", "c"], ["b", "c"], ["c"]]; + for (const [i, reader] of readers.entries()) { + for (const payload of rest[i]) expect(await reader.readString()).toBe(payload); + expect(await reader.readString()).toBeUndefined(); + expect(await reader.closed).toBeNull(); + } + } else { + const group = await track.recvGroup(); + const reader = first.consume(); + first.writeFrame({ payload: enc.encode("b") }); + first.abort(failed); + await settle(); + expect(group?.closed.peek()).toBeUndefined(); + expect(reader.closed.peek()).toBeUndefined(); + arriving?.release(); + // A released hold ends each mirror with the copy's failure, once, after what it buffered. + arriving?.release(); + for (const mirror of [group, reader]) { + expect(await mirror?.readString()).toBe("a"); + expect(await mirror?.readString()).toBe("b"); + await expect(mirror?.readString()).rejects.toBe(failed); + expect(await mirror?.closed).toBe(failed); + } + // A re-offer arriving later serves new readers only. + const late = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + late.writeFrame({ payload: enc.encode("a") }); + await expect(reader.readString()).rejects.toBe(failed); + expect(await late.consume().readString()).toBe("a"); + } + track.close(); + producer.close(); + }, +); + // A subscriber's reader of a failed copy keeps its place when a re-offer replaces that copy in the // track, continuing on it, and its budget still gives up on it while it waits on a held re-offer. test.each(["buffered", "expires"])("a subscriber's failed copy waits on its re-offer (%p)", async (kind) => { diff --git a/js/net/src/track.ts b/js/net/src/track.ts index d6ad38820d4..a8fc6b4de4f 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -641,6 +641,16 @@ function intersect(runs: readonly Run[], start: bigint, end: bigint): Run[] { .filter((run) => run.start < run.end); } +// Take a group's mirror out of `sink`, so its subscriber can no longer receive it. +function unlinkMirror(sink: TrackState, mirror: GroupConsumer): void { + sink.groups.mutate((groups) => { + const i = groups.indexOf(mirror); + if (i >= 0) groups.splice(i, 1); + }); + const index = timelineIndex(sink.timeline, mirror.sequence); + if (sink.timeline[index] === mirror) sink.timeline.splice(index, 1); +} + // The index of the first timeline group at or after `sequence`. function timelineIndex(timeline: readonly { sequence: bigint }[], sequence: bigint): number { let lo = 0; @@ -1337,8 +1347,7 @@ export class Producer { hooks.receiveTrack = (producer, starts = {}) => { producer.#wire = { starts, served: producer.#wire?.served }; }; - hooks.receiveGroup = (producer, { sequence, start, fill }) => - producer.#createGroup(sequence, start, fill, true); + hooks.receiveGroup = (producer, { sequence, start, fill }) => producer.#createGroup(sequence, start, fill); hooks.arrivingGroup = (producer, sequence) => { const group = producer.#cached.get(sequence)?.group; if (!group || group.closed.peek() !== undefined) return undefined; @@ -1792,14 +1801,7 @@ export class Producer { // Take a cached group's mirrors out of every sink, so a subscriber can no longer // receive them. A reader already holding one keeps it as is. #unlink(entry: CachedGroup): void { - for (const [sink, mirror] of entry.mirrors) { - sink.groups.mutate((groups) => { - const i = groups.indexOf(mirror); - if (i >= 0) groups.splice(i, 1); - }); - const index = timelineIndex(sink.timeline, mirror.sequence); - if (sink.timeline[index] === mirror) sink.timeline.splice(index, 1); - } + for (const [sink, mirror] of entry.mirrors) unlinkMirror(sink, mirror); entry.charge.sub(entry.mirrors.size * cache.MIRROR_OVERHEAD); entry.mirrors.clear(); } @@ -1929,7 +1931,13 @@ export class Producer { // against this track when it lands. // `fetched` marks a group a fill answered, which the live feed never delivered: it accounts // for nothing the feed owes, as the Rust `insert` records arrivals only for visible groups. - #publish(group: GroupProducer, fetched = false): void { + // `succeeds` is a failed copy of the same sequence this one takes over: its mirrors held open + // for a duplicate continue on this one, and each sink in `kept` keeps its mirror. + #publish( + group: GroupProducer, + fetched = false, + succeeds?: { failed: GroupProducer; kept?: Map }, + ): void { // Before the group is cached, so the write creating it can never evict it. this.#chargeDebt(); this.#settle(); @@ -1941,6 +1949,7 @@ export class Producer { fetched: fetched || undefined, }; hooks.chargeGroup(group, entry.charge); + if (succeeds) hooks.succeedGroup(group, succeeds.failed); this.#undrop(group.sequence); // No reader is handed a failed fill answer (see #withhold), withheld as it aborts so a // read that follows at once cannot take it: its sequence is unknown again, owed while the @@ -1984,7 +1993,17 @@ export class Producer { this.#state.latest = max(this.#state.latest ?? 0n, group.sequence); if (edge && edge !== entry && !this.#protects(edge)) this.#account.demote(edge); this.#account.insert(entry); - for (const sink of this.#sinks) this.#mirror(entry, sink); + for (const sink of this.#sinks) { + const mirror = succeeds?.kept?.get(sink); + // Continued, it stays the sink's copy of the group, so the sink never holds two. + if (mirror && mirror.closed.peek() === undefined) { + entry.mirrors.set(sink, mirror); + entry.charge.bill(cache.MIRROR_OVERHEAD); + continue; + } + if (mirror) unlinkMirror(sink, mirror); + this.#mirror(entry, sink); + } // Follow the group's frames as they land, past the head `#advance` already named. const stop = hooks.groupProgress(group, (total) => { if (total > hooks.groupOffset(group)) this.#advanceObject({ group: group.sequence, frame: total - 1 }); @@ -2178,16 +2197,25 @@ export class Producer { return this.#createGroup(sequence, 0); } - #createGroup(sequence: bigint, start: number, fetched = false, received = false): GroupProducer { + #createGroup(sequence: bigint, start: number, fetched = false): GroupProducer { this.#writable(sequence); // A skip says the sequence will never be produced, so it is final. A drop is not: a // publisher may re-create a group it cut or aborted. this.#refuseSkipped(sequence); const existing = this.#cached.get(sequence); + let kept: Map | undefined; if (existing) { if (!(existing.group.closed.peek() instanceof Error)) { throw new Error(`duplicate group: sequence=${sequence}`); } + // A mirror the failed copy kept open, held for a duplicate, is the new copy's to continue. + for (const [sink, mirror] of existing.mirrors) { + if (mirror.closed.peek() !== undefined || !hooks.readsMirrored(mirror)) continue; + kept ??= new Map(); + kept.set(sink, mirror); + existing.mirrors.delete(sink); + existing.charge.sub(cache.MIRROR_OVERHEAD); + } // Subscribers may have seen the aborted incarnation's start, so it stays a watermark. this.#evict(existing); this.#uncache(existing, true); @@ -2196,11 +2224,9 @@ export class Producer { const group = new GroupProducer(sequence); // Mirrors copy the absolute indices, so set them before publishing to any sink. hooks.startGroup(group, start); - // Before any mirror, which shares it: the failed copy's readers continue on this one. - if (received) hooks.receivedGroup(group, existing?.group); this.#bind(group); this.#cap(group); - this.#publish(group, fetched); + this.#publish(group, fetched, existing && { failed: existing.group, kept }); return group; } diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 1a41224345f..71768cfbe0e 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -1222,7 +1222,13 @@ impl GroupState { /// (idle past the cache pool's expiry window, or evicted under its byte target). A reader /// that already consumed every frame is missing nothing, so it gets the clean end of /// group; one that fell short sees the abort rather than a silently truncated stream. + /// A copy cut short at the back holds nothing past its cut, which a reader reaches only + /// with its cap past it (one capped there ends before asking), so the frames it wants + /// are missing rather than the group ended. fn poll_terminal(&self, index: usize) -> Poll> { + if self.past_cut(index) { + return Poll::Ready(Err(Error::Evicted)); + } match &self.outcome { Outcome::Released(total, err) if index < *total => Poll::Ready(Err(err.clone())), Outcome::Finished(_) | Outcome::Released(..) => Poll::Ready(Ok(())), @@ -1238,6 +1244,12 @@ impl GroupState { } } + /// Whether this copy settled at the back cut its transport bound made, with `index` at + /// or past it. + fn past_cut(&self, index: usize) -> bool { + !matches!(self.outcome, Outcome::Open | Outcome::Aborted(_)) && self.range_end.is_some_and(|end| index >= end) + } + /// The relay state once the ingest has settled it, which it does before the first frame, /// the end object, or the close: a group is visible before its header is read. #[cfg(feature = "async")] @@ -4352,8 +4364,9 @@ impl Consumer { } } - /// Continue on another copy of this group in the same track once the copy read failed, - /// such as a re-offer held while that copy was still arriving (see [`track::successor`]). + /// Continue on another copy of this group in the same track once the copy read failed or + /// ended at a cut short of the reader's cap, such as a re-offer held while that copy was + /// still arriving (see [`track::successor`]). /// Returns whether the reader moved to one, or [`Error::Old`] once its budget gives up /// on the wait. fn poll_successor(&mut self, waiter: &kio::Waiter) -> Poll> { @@ -4361,10 +4374,11 @@ impl Consumer { let state = self.cursor.state.read(); // Only a delivery failure: content that failed to inflate is the group's under its // name, and a frame handed out partly is lost with the copy. Ordinals stand for - // frames only on a dense copy. + // frames only on a dense copy. A copy cut short at the back that the reader's + // raised cap passed lacks the rest, like a failed one. if self.expired || self.carry.as_ref().is_some_and(|carry| carry.ride.is_some()) - || state.outcome.error().is_none() + || (state.outcome.error().is_none() && !state.past_cut(self.cursor.index)) || (!self.cursor.raw && state.malformed()) || state.committed() < self.cursor.index || state.sparse() diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index fcab9b9b5ca..0e2be9f95ce 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -17315,6 +17315,59 @@ mod test { assert!(reader.read_frame().await.unwrap().is_none()); } + /// A capped reader that continued on a copy cut at its cap and then raises the cap reads + /// no clean end at the cut: the frames past it are missing from that copy, so it continues + /// on a copy that holds them, or fails without one. + #[kio::test] + #[cfg(feature = "async")] + async fn a_raised_cap_past_an_adopted_cut_finds_the_rest() { + for reoffer in [false, true] { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 1) else { + panic!("the first copy opens"); + }; + first.write_frame(at(301), &b"b"[..]).unwrap(); + let mut reader = first.consume().with_range(..3); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + + let mut head = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(3, group::Bounds { start: 0, end: Some(3) }, None) + .unwrap(); + for (millis, payload) in [(300, b"a"), (301, b"b"), (302, b"c")] { + head.write_frame(at(millis), &payload[..]).unwrap(); + } + head.finish().unwrap(); + first.abort(Error::Cancel).unwrap(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"c"); + + let mut reader = reader.with_end(None); + let mut finished = reader.clone(); + if !reoffer { + let res = reader.read_frame().await; + assert!(res.is_err(), "frame 3 is in no copy: {res:?}"); + let res = finished.finished().await; + assert!(res.is_err(), "nor is the group's end: {res:?}"); + continue; + } + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer takes the group"); + }; + for (millis, payload) in [(300, b"a"), (301, b"b"), (302, b"c"), (303, b"d")] { + again.write_frame(at(millis), &payload[..]).unwrap(); + } + again.finish().unwrap(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"d"); + assert!(reader.read_frame().await.unwrap().is_none()); + // Its end, from where that reader stood, on the copy holding the rest. + assert_eq!(finished.finished().await.unwrap(), 3); + } + } + /// A reader waiting out a held re-offer is stalled, so its budget still gives up on it. #[kio::test] #[cfg(feature = "async")] From d85fcb27d454a228f0ecb2c94dd150e4ff135059 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 14:08:00 -0800 Subject: [PATCH 15/21] fix(net): bill a continued reader's kept frames, tell a replay from a torn frame, finish past prefetched frames JS: the frames a continued mirror keeps from the failed copy are billed to the successor's cache entry, and a successor failing in a frame the mirror already holds no longer ends that mirror as torn. Rust: a frame counts as torn only when the reader took part of it from the failing copy, and finished() judges the successor past the reader's prefetched frames, as frame reads do. Co-Authored-By: Claude Opus 5.5 --- js/net/src/cache.test.ts | 24 +++++++ js/net/src/group.ts | 9 +++ js/net/src/internal.ts | 5 +- js/net/src/track.test.ts | 21 +++++- js/net/src/track.ts | 4 +- rs/moq-net/src/model/group.rs | 118 ++++++++++++++++++++++-------- rs/moq-net/src/model/track.rs | 132 ++++++++++++++++++++++++++++++++-- 7 files changed, 277 insertions(+), 36 deletions(-) diff --git a/js/net/src/cache.test.ts b/js/net/src/cache.test.ts index df22fc88c58..bda3a795203 100644 --- a/js/net/src/cache.test.ts +++ b/js/net/src/cache.test.ts @@ -7,6 +7,30 @@ import { RUN_OVERHEAD as ARRIVAL_OVERHEAD } from "./ledger.ts"; import { Tail } from "./tail.ts"; import { Producer as TrackProducer } from "./track.ts"; +// A reader continued on a held re-offer keeps the frames the failed copy wrote that it has yet to +// read, so the pool still counts them once that copy leaves the cache. +test("a re-offer bills the failed copy's frames its readers still hold", async () => { + await withClock(async () => { + const pool = new cache.Pool({ capacity: 100 * billed(10_000) }); + const producer = new TrackProducer("test"); + hooks.poolTrack(producer, pool); + const track = producer.accept({ maxAge: null }); + hooks.receiveTrack(track); + const first = hooks.receiveGroup(track, { sequence: 0n, start: 0 }); + first.writeFrame({ payload: new Uint8Array(10_000) }); + const reader = first.consume(); + const arriving = hooks.arrivingGroup(track, 0n); + first.abort(new Error("reset")); + // The re-offer starts past the frame the reader still holds. + hooks.receiveGroup(track, { sequence: 0n, start: 1 }); + arriving?.release(); + expect(pool.used).toBeGreaterThan(10_000); + expect((await reader.readFrame())?.payload.byteLength).toBe(10_000); + reader.close(); + track.close(); + }); +}); + // Run `fn` on a manual clock, stepped in milliseconds. async function withClock(fn: (clock: { advance: (ms: number) => Promise }) => Promise | void) { const clock = new Manual(); diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 88b9f7f6ca9..3634d6c4770 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -322,6 +322,8 @@ export class Producer { const error = failed.#state.closed.peek(); if (!(error instanceof Error)) throw new Error("group has not failed"); const state = next.#state; + // The failed copy's frames the continued mirrors hold, once each though mirrors share them. + const kept = new Set(); for (const [mirror, reads] of failed.#mirrors ?? []) { failed.#unmirror(mirror); // The successor continues a mirror from the frame it reached, so it must hold that @@ -333,6 +335,7 @@ export class Producer { mirror.closed.set(error); continue; } + for (const frame of mirror.frames.peek()) kept.add(frame); mirror.from = mirror.total.peek(); mirror.charge = state.charge; if (state.compressed) mirror.flate = state.flate; @@ -340,6 +343,9 @@ export class Producer { for (const frame of state.frames.peek()) if (index++ >= mirror.from) appendFrame(mirror, frame); next.#attach(mirror, reads); } + let bytes = 0; + for (const frame of kept) bytes += frameBytes(frame); + return bytes; }; hooks.startGroup = (group, start) => { if (!Number.isSafeInteger(start) || start < 0) throw new RangeError("invalid group frame start"); @@ -746,7 +752,10 @@ export class Producer { // A held copy's duplicate continues each mirror from the frame it reached, so a mirror // drops the frame cut short before its reader takes it. One whose reader took it lost it. if (held && cut) { + const index = this.#state.total.peek() - 1; for (const mirror of this.#mirrors?.keys() ?? []) { + // A mirror taken over past the cut frame never got it from this copy. + if (index < mirror.from) continue; if (mirror.frames.peek().last === cut) { mirror.frames.mutate((frames) => frames.pop()); mirror.total.update((total) => total - 1); diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 5e535bd0f51..bd5069f4913 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -921,9 +921,10 @@ export const hooks: { holdGroup: (group: GroupProducer) => () => void; /** * Hand `next` the readers `failed` kept open while held, teeing each from the frame it reached. - * Fails those `next` cannot serve. Assigned by `group.ts`. + * Fails those `next` cannot serve. Returns the bytes of `failed`'s frames they still hold. + * Assigned by `group.ts`. */ - succeedGroup: (next: GroupProducer, failed: GroupProducer) => void; + succeedGroup: (next: GroupProducer, failed: GroupProducer) => number; /** Read a payload chunk, releasing its wait when the output stream closes. */ readFrameChunk: (frame: FrameConsumer, closed: Promise) => Promise; /** Read the next frame, as its stored DEFLATE slice when `compressed`, so a lite-07 publisher forwards it without inflating. */ diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index a8cee7e5afb..bf5f0d34b37 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3577,7 +3577,7 @@ test.each([ // into it from the frame it reached: a sink still buffering the group hands it out once, a frame cut // short is read whole from the successor unless a reader already took part of it, readers at // different frames each continue from their own, and with no takeover every mirror fails once. -test.each(["buffered", "handed", "partial", "positions", "once"])( +test.each(["buffered", "handed", "partial", "replay", "positions", "once"])( "a held copy's mirrors continue on its successor (%p)", async (kind) => { const producer = new TrackProducer("video").accept({ maxAge: null }); @@ -3621,6 +3621,25 @@ test.each(["buffered", "handed", "partial", "positions", "once"])( takeover(); for (const payload of ["a", "b", "c"]) expect(await behind.readString()).toBe(payload); expect(await behind.readString()).toBeUndefined(); + } else if (kind === "replay") { + // The successor fails partway through a frame the reader already has: no loss to it, so a + // third copy held for the group still continues it. + const reader = first.consume(); + expect(await reader.readString()).toBe("a"); + first.abort(failed); + const second = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + arriving?.release(); + const held = hooks.arrivingGroup(producer, 3n); + second.createFrame({ size: 2 }).write(enc.encode("a")); + second.abort(failed); + await settle(); + expect(reader.closed.peek()).toBeUndefined(); + const third = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + held?.release(); + for (const payload of ["a", "b"]) third.writeFrame({ payload: enc.encode(payload) }); + third.finish(); + expect(await reader.readString()).toBe("b"); + expect(await reader.readString()).toBeUndefined(); } else if (kind === "positions") { first.writeFrame({ payload: enc.encode("b") }); const readers = [first.consume(), first.consume(), first.consume()]; diff --git a/js/net/src/track.ts b/js/net/src/track.ts index a8fc6b4de4f..4cd52ee9de0 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1949,7 +1949,9 @@ export class Producer { fetched: fetched || undefined, }; hooks.chargeGroup(group, entry.charge); - if (succeeds) hooks.succeedGroup(group, succeeds.failed); + // The failed copy's frames its continued readers still hold, billed until this entry leaves + // the cache, which evicts the sink mirrors holding them. + if (succeeds) entry.charge.bill(hooks.succeedGroup(group, succeeds.failed)); this.#undrop(group.sequence); // No reader is handed a failed fill answer (see #withhold), withheld as it aborts so a // read that follows at once cannot take it: its sequence is unknown again, owed while the diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 71768cfbe0e..6a838107322 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -3601,6 +3601,9 @@ struct Carry { recover: Option, // Boarding or riding a flight, once the copy read shed the frame read next. ride: Option, + // Where reads from the copy read began: frames below it came from a copy it replaced, so + // it cutting one of them short tore nothing this reader took. + began: usize, } impl Clone for Carry { @@ -3612,6 +3615,7 @@ impl Clone for Carry { boarding: None, ..*ride }), + began: self.began, } } } @@ -3944,6 +3948,7 @@ impl Consumer { self.cursor.index = index; self.cursor.end = end; self.cursor.raw = raw; + self.began_at(index); self.adopted = true; } } @@ -4370,45 +4375,48 @@ impl Consumer { /// Returns whether the reader moved to one, or [`Error::Old`] once its budget gives up /// on the wait. fn poll_successor(&mut self, waiter: &kio::Waiter) -> Poll> { + let Some(copy) = ready!(self.poll_successor_at(waiter, self.cursor.index))? else { + return Poll::Ready(Ok(false)); + }; + self.adopt_copy(copy); + Poll::Ready(Ok(true)) + } + + /// The copy that continues this reader from frame `index`, at or past its cursor, once + /// the copy read failed or ended at a cut short of its cap; see [`Self::poll_successor`]. + fn poll_successor_at(&mut self, waiter: &kio::Waiter, index: usize) -> Poll>> { { let state = self.cursor.state.read(); // Only a delivery failure: content that failed to inflate is the group's under its - // name, and a frame handed out partly is lost with the copy. Ordinals stand for - // frames only on a dense copy. A copy cut short at the back that the reader's - // raised cap passed lacks the rest, like a failed one. + // name, and a frame this reader took from the copy partly is lost with it. Ordinals + // stand for frames only on a dense copy. A copy cut short at the back that the + // reader's raised cap passed lacks the rest, like a failed one. if self.expired || self.carry.as_ref().is_some_and(|carry| carry.ride.is_some()) - || (state.outcome.error().is_none() && !state.past_cut(self.cursor.index)) + || (state.outcome.error().is_none() && !state.past_cut(index)) || (!self.cursor.raw && state.malformed()) - || state.committed() < self.cursor.index + || (state.committed() < self.cursor.index && self.began() < self.cursor.index) || state.sparse() { - return Poll::Ready(Ok(false)); + return Poll::Ready(Ok(None)); } } let Some(track) = self.cursor.cache.track() else { - return Poll::Ready(Ok(false)); + return Poll::Ready(Ok(None)); }; - let copy = match track::successor(&track, self, waiter) { - Poll::Ready(copy) => copy, + match track::successor(&track, self, index, waiter) { + Poll::Ready(copy) => Poll::Ready(Ok(copy)), // A stall the budget bounds, though the copy read can receive nothing more. - Poll::Pending => { - return match self.poll_expiry(waiter, true) { - true => Poll::Ready(Err(Error::Old)), - false => Poll::Pending, - }; - } - }; - let Some(copy) = copy else { - return Poll::Ready(Ok(false)); - }; - self.adopt_copy(copy); - Poll::Ready(Ok(true)) + Poll::Pending => match self.poll_expiry(waiter, true) { + true => Poll::Ready(Err(Error::Old)), + false => Poll::Pending, + }, + } } - /// Whether this copy can continue `reader` from its frame. - pub(super) fn continues(&self, reader: &Consumer) -> bool { - let (index, raw) = (reader.cursor.index, reader.cursor.raw); + /// Whether this copy can continue `reader` from frame `index`. + pub(super) fn continues(&self, reader: &Consumer, index: usize) -> bool { + let raw = reader.cursor.raw; let state = self.cursor.state.read(); // A plain read inflates from frame 0, and a copy that shed the frame can't serve it. !state.sparse() && (raw || state.flate.is_none() || state.offset == 0) && state.check_read(index, raw).is_ok() @@ -4433,6 +4441,7 @@ impl Consumer { self.carry = Some(Box::new(Carry { recover: None, ride: Some(ride), + began: 0, })) } (None, None) => {} @@ -4461,12 +4470,27 @@ impl Consumer { self.cursor.index = index; self.cursor.end = end; self.cursor.raw = raw; + self.began_at(index); } - /// The frames this reader has yet to read: from its cursor to its cap. - pub(super) fn unread(&self) -> Bounds { + /// Record that reads from the copy read began at `index`. + fn began_at(&mut self, index: usize) { + match &mut self.carry { + Some(carry) => carry.began = index, + None if index > 0 => self.carry.get_or_insert_default().began = index, + None => {} + } + } + + /// Where reads from the copy read began. + fn began(&self) -> usize { + self.carry.as_ref().map_or(0, |carry| carry.began) + } + + /// The frames this reader has yet to read from `index` on: up to its cap. + pub(super) fn unread(&self, index: usize) -> Bounds { Bounds { - start: self.cursor.index as u64, + start: index as u64, end: self.cursor.end.map(|end| end as u64), } } @@ -4589,9 +4613,47 @@ impl Consumer { fn poll_finished_once(&mut self, waiter: &kio::Waiter) -> Poll> { loop { let res = self.poll_finished_copy(waiter); - if !matches!(res, Poll::Ready(Err(_))) || !ready!(self.poll_successor(waiter))? { + if !matches!(res, Poll::Ready(Err(_))) { return res; } + // Frames this reader already prefetched are read before a successor is needed, + // so the end is judged past them, without leaving them behind. + let drained = self.cursor.index + self.cursor.prefetch.buffered().0 as usize; + if drained > self.cursor.index { + return self.poll_finished_past(waiter, drained, res); + } + if !ready!(self.poll_successor(waiter))? { + return res; + } + } + } + + /// This reader's end once it drained its prefetch up to `drained`: its cap's, if + /// that comes first, else the end of the copy that continues it from there, judged on a + /// copy of the reader so its cursor stays on the frames still to drain. `failed` is the + /// copy's own answer, which stands without one. + fn poll_finished_past( + &mut self, + waiter: &kio::Waiter, + drained: usize, + failed: Poll>, + ) -> Poll> { + let index = self.cursor.index as u64; + if self.cursor.end.is_some_and(|end| drained >= end) { + return Poll::Ready(Ok(index)); + } + let Some(copy) = ready!(self.poll_successor_at(waiter, drained))? else { + return failed; + }; + let mut next = self.clone(); + next.adopt_copy(copy); + next.cursor.index = drained; + match next.poll_finished_once(waiter) { + Poll::Ready(res) => Poll::Ready(res.map(|_| index)), + Poll::Pending => match self.poll_expiry(waiter, true) { + true => Poll::Ready(Err(Error::Old)), + false => Poll::Pending, + }, } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 0e2be9f95ce..8faf4a111b4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3783,17 +3783,18 @@ impl TrackState { } } -/// The copy of its group `reader` continues on once the copy it reads failed: one holding the -/// group from the reader's frame, once no re-offer of it is still held (see [`Hold`]). A +/// The copy of its group `reader` continues on from frame `index` once the copy it reads +/// failed: one holding the group from there, once no re-offer of it is still held (see [`Hold`]). A /// group's name fixes its content, so any such copy continues it. pub(super) fn successor( track: &kio::Producer, reader: &group::Consumer, + index: usize, waiter: &kio::Waiter, ) -> Poll> { let sequence = reader.sequence; // A copy cut short at the back still continues a reader capped no later than the cut. - let frames = reader.unread(); + let frames = reader.unread(index); let found = track.poll_ref(waiter, |state| { // Only a hold on the copy read: one on a copy that took over from it waits on that one. if state @@ -3812,7 +3813,7 @@ pub(super) fn successor( .flat_map(Slot::copies) .filter(|copy| copy.covers(frames) && !copy.is_read_by(reader)) .filter_map(group::Handle::try_consume) - .find(|copy| copy.continues(reader)), + .find(|copy| copy.continues(reader, index)), ) }); Poll::Ready(match found { @@ -17282,6 +17283,129 @@ mod test { assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); } + /// A re-offer that fails while still sending frames the reader took from an earlier copy + /// hands over to the next held re-offer: only a frame the reader took from the failing copy + /// itself is torn. One it was streaming from that copy is lost with it. + #[kio::test] + #[cfg(feature = "async")] + async fn a_reoffer_failing_in_its_replay_hands_over_again() { + for torn in [false, true] { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(300), &b"a"[..]).unwrap(); + let mut reader = first.consume(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a", "{torn}"); + + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer takes the group"); + }; + let Ok(Received::Held(next)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("another re-offer waits on the first"); + }; + drop(hold); + let waiter = kio::Waiter::noop(); + let handle = again.handle(); + let streaming = if torn { + // The reader streams part of frame 1 from the re-offer before it fails. + again.write_frame(at(300), &b"a"[..]).unwrap(); + let mut frame = again + .create_frame(frame::Info { + size: 2, + timestamp: Some(at(301)), + }) + .unwrap(); + frame.write(&b"b"[..]).unwrap(); + let mut part = reader.next_frame().await.unwrap().expect("frame 1 streams"); + assert_eq!(&part.read_chunk().await.unwrap().unwrap()[..], b"b"); + frame + } else { + // The re-offer fails partway through frame 0, which the reader already holds. + let mut frame = again + .create_frame(frame::Info { + size: 2, + timestamp: Some(at(300)), + }) + .unwrap(); + frame.write(&b"a"[..]).unwrap(); + assert!(reader.poll_read_frame(&waiter).is_pending()); + frame + }; + handle.abort(Error::Cancel).unwrap(); + drop(streaming); + if torn { + drop(next); + assert!(reader.read_frame().await.is_err(), "frame 1 was handed out partly"); + continue; + } + assert!( + reader.poll_read_frame(&waiter).is_pending(), + "waits on the next re-offer" + ); + let Ok(Received::Group(mut last)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the next re-offer takes the group"); + }; + drop(next); + last.write_frame(at(300), &b"a"[..]).unwrap(); + last.write_frame(at(301), &b"b"[..]).unwrap(); + last.finish().unwrap(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + assert!(reader.read_frame().await.unwrap().is_none()); + } + } + + /// A failed copy's end agrees with its frame reads when the reader still has frames it + /// prefetched from it: a re-offer starting past the reader's frame but not past those + /// continues it, and one starting past both does not. + #[kio::test] + #[cfg(feature = "async")] + async fn a_failed_copy_finishes_past_its_prefetched_frames() { + for start in [2, 3] { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(300), &b"a"[..]).unwrap(); + first.write_frame(at(301), &b"b"[..]).unwrap(); + let mut reader = first.consume(); + // Takes frame 1 into its prefetch with frame 0. + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let waiter = kio::Waiter::noop(); + assert!(reader.poll_finished(&waiter).is_pending(), "{start}: waits on the hold"); + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, start) else { + panic!("the re-offer takes the group"); + }; + drop(hold); + if start == 3 { + assert!(reader.finished().await.is_err(), "frame 2 is in no copy"); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + assert!(reader.read_frame().await.is_err(), "nor can a read find it"); + continue; + } + again.write_frame(at(302), &b"c"[..]).unwrap(); + let res = reader.poll_finished(&waiter); + assert!(res.is_pending(), "the re-offer is still open: {res:?}"); + again.finish().unwrap(); + // From where the reader stands, with frame 1 still to read. + assert_eq!(reader.finished().await.unwrap(), 1); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"c"); + assert!(reader.read_frame().await.unwrap().is_none()); + } + } + /// A capped reader continues on a copy cut short at the back no earlier than its cap, which /// holds every frame it still reads. #[kio::test] From 226867db17d3570dbafe450e149283e2d5c29fa0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 15:16:49 -0800 Subject: [PATCH 16/21] fix(net): a reader waits on a hold held for any failed copy of its group finished() judged a successor through a temporary clone, so once that successor failed while another re-offer was held for it, the hold did not count for the reader and finished() reported the first copy's failure. track::successor now waits on a hold on any failed copy of the sequence. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 68 +++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 8faf4a111b4..b715237b577 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3796,11 +3796,13 @@ pub(super) fn successor( // A copy cut short at the back still continues a reader capped no later than the cut. let frames = reader.unread(index); let found = track.poll_ref(waiter, |state| { - // Only a hold on the copy read: one on a copy that took over from it waits on that one. + // A hold on the copy read, or on any copy of the group that failed, promises a copy + // that takes the group over, which continues this reader too. One on a copy still + // arriving waits on that copy, which the reader takes instead. if state .holds .get(&sequence) - .is_some_and(|held| held.iter().any(|copy| copy.is_read_by(reader))) + .is_some_and(|held| held.iter().any(|copy| copy.is_read_by(reader) || copy.is_aborted())) { return Poll::Pending; } @@ -17360,6 +17362,68 @@ mod test { } } + /// A reader whose end was judged on a re-offer past its prefetched frames keeps waiting + /// when that re-offer fails while another is held for it, then reads its prefetched frame + /// and continues on whichever copy holds the rest. + #[kio::test] + #[cfg(feature = "async")] + async fn a_failed_copy_finishes_on_a_reoffer_of_a_failed_reoffer() { + for again_fails in [false, true] { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(300), &b"a"[..]).unwrap(); + first.write_frame(at(301), &b"b"[..]).unwrap(); + let mut reader = first.consume(); + // Takes frame 1 into its prefetch with frame 0. + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, 2) else { + panic!("the re-offer takes the group"); + }; + let Ok(Received::Held(next)) = producer.create_group_at(group::Info { sequence: 3 }, 2) else { + panic!("another re-offer waits on the first"); + }; + drop(hold); + let waiter = kio::Waiter::noop(); + assert!( + reader.poll_finished(&waiter).is_pending(), + "{again_fails}: the re-offer is open" + ); + if !again_fails { + drop(next); + again.write_frame(at(302), &b"c"[..]).unwrap(); + again.finish().unwrap(); + assert_eq!(reader.finished().await.unwrap(), 1); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"c"); + assert!(reader.read_frame().await.unwrap().is_none()); + continue; + } + again.abort(Error::Cancel).unwrap(); + let res = reader.poll_finished(&waiter); + assert!(res.is_pending(), "waits on the next re-offer: {res:?}"); + let Ok(Received::Group(mut last)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the next re-offer takes the group"); + }; + drop(next); + for (millis, payload) in [(300, b"a"), (301, b"b"), (302, b"c")] { + last.write_frame(at(millis), &payload[..]).unwrap(); + } + last.finish().unwrap(); + assert_eq!(reader.finished().await.unwrap(), 1); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"b"); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"c"); + assert!(reader.read_frame().await.unwrap().is_none()); + } + } + /// A failed copy's end agrees with its frame reads when the reader still has frames it /// prefetched from it: a re-offer starting past the reader's frame but not past those /// continues it, and one starting past both does not. From 9a433aa820be16d6fd77a4cd97a22f022f9100db Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 16:12:31 -0800 Subject: [PATCH 17/21] fix(net): judge a cut from where it settled, track torn frames directly, keep an end check's copy wanted - A bounded copy that finished short of its bound ends the group there, not at a cut; finished() judges a raised cap against the cut itself, even with frames left before it. - A frame counts as torn only when the reader was handed it still being written, so a skip past uncommitted frames continues on a re-offer. - finished() keeps the successor it waits on between polls, so a fetch does not abandon that copy as unwanted. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/group.rs | 134 ++++++++++++++++++++++++---------- rs/moq-net/src/model/track.rs | 76 +++++++++++++++++++ 2 files changed, 170 insertions(+), 40 deletions(-) diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 21bbe5cef97..a4490a06f60 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -1244,10 +1244,15 @@ impl GroupState { } } - /// Whether this copy settled at the back cut its transport bound made, with `index` at - /// or past it. + /// Where this copy settled at the back cut its transport bound made. One that finished + /// short of its bound holds the whole group, which ends there. + fn cut(&self) -> Option { + self.outcome.frames().filter(|&frames| self.range_end == Some(frames)) + } + + /// Whether this copy settled at its back cut, with `index` at or past it. fn past_cut(&self, index: usize) -> bool { - !matches!(self.outcome, Outcome::Open | Outcome::Aborted(_)) && self.range_end.is_some_and(|end| index >= end) + self.cut().is_some_and(|cut| index >= cut) } /// The relay state once the ingest has settled it, which it does before the first frame, @@ -1277,8 +1282,13 @@ impl GroupState { /// Resolve whether a reader at `index`, which already prefetched the frames below /// `unread`, can still make progress, answering the same question as a read without /// consuming anything. - fn poll_end(&self, index: usize, unread: usize, raw: bool) -> Poll> { + /// A cut short of the reader's cap `end` leaves the frames past it missing, whatever + /// the reader reads before it. + fn poll_end(&self, index: usize, unread: usize, raw: bool, end: Option) -> Poll> { self.check_read(unread, raw)?; + if self.cut().is_some_and(|cut| end.is_none_or(|end| end > cut)) { + return Poll::Ready(Err(Error::Evicted)); + } self.poll_terminal(index) } @@ -3138,6 +3148,7 @@ impl Handle { end: None, prefetch: Prefetch::default(), raw: false, + partial: false, compressed: self.track.compressed(), cache: self.cache.clone(), alive: self.alive.clone(), @@ -3614,6 +3625,13 @@ pub struct Consumer { #[cfg(target_pointer_width = "64")] const _: () = assert!(size_of::() <= 184); +/// Drop an end check's held copy once the reader moved to another copy itself. +fn drop_finishing(carry: &mut Option>) { + if let Some(carry) = carry { + carry.finishing = None; + } +} + /// What carries a read past the copy it reads; see [`Consumer::with_recover`] and /// [`track::board`]. #[derive(Default)] @@ -3622,9 +3640,9 @@ struct Carry { recover: Option, // Boarding or riding a flight, once the copy read shed the frame read next. ride: Option, - // Where reads from the copy read began: frames below it came from a copy it replaced, so - // it cutting one of them short tore nothing this reader took. - began: usize, + // The copy an end check waits on past the frames this reader prefetched, with the frame + // it reads that copy from: held between polls so the copy keeps this reader's demand. + finishing: Option<(usize, Consumer)>, } impl Clone for Carry { @@ -3636,7 +3654,7 @@ impl Clone for Carry { boarding: None, ..*ride }), - began: self.began, + finishing: None, } } } @@ -3715,6 +3733,10 @@ struct Cursor { // Read a compressed group's stored slices rather than its inflated copy. raw: bool, + // Whether the last frame handed out to stream from this copy was still being written. + // Left uncommitted when the copy fails, it is torn. + partial: bool, + // Whether the group is compressed, so a plain read inflates under the state's lock. compressed: bool, @@ -3736,6 +3758,7 @@ impl Clone for Cursor { end: self.end, prefetch: Prefetch::default(), raw: self.raw, + partial: self.partial, compressed: self.compressed, cache: self.cache.clone(), alive: self.alive.clone(), @@ -3969,8 +3992,8 @@ impl Consumer { self.cursor.index = index; self.cursor.end = end; self.cursor.raw = raw; - self.began_at(index); self.adopted = true; + drop_finishing(&mut self.carry); } } @@ -4416,7 +4439,7 @@ impl Consumer { || self.carry.as_ref().is_some_and(|carry| carry.ride.is_some()) || (state.outcome.error().is_none() && !state.past_cut(index)) || (!self.cursor.raw && state.malformed()) - || (state.committed() < self.cursor.index && self.began() < self.cursor.index) + || (self.cursor.partial && state.committed() < self.cursor.index) || state.sparse() { return Poll::Ready(Ok(None)); @@ -4462,7 +4485,7 @@ impl Consumer { self.carry = Some(Box::new(Carry { recover: None, ride: Some(ride), - began: 0, + finishing: None, })) } (None, None) => {} @@ -4491,21 +4514,7 @@ impl Consumer { self.cursor.index = index; self.cursor.end = end; self.cursor.raw = raw; - self.began_at(index); - } - - /// Record that reads from the copy read began at `index`. - fn began_at(&mut self, index: usize) { - match &mut self.carry { - Some(carry) => carry.began = index, - None if index > 0 => self.carry.get_or_insert_default().began = index, - None => {} - } - } - - /// Where reads from the copy read began. - fn began(&self) -> usize { - self.carry.as_ref().map_or(0, |carry| carry.began) + drop_finishing(&mut self.carry); } /// The frames this reader has yet to read from `index` on: up to its cap. @@ -4637,9 +4646,19 @@ impl Consumer { if !matches!(res, Poll::Ready(Err(_))) { return res; } - // Frames this reader already prefetched are read before a successor is needed, - // so the end is judged past them, without leaving them behind. - let drained = self.cursor.index + self.cursor.prefetch.buffered().0 as usize; + // Frames this reader already prefetched, and those a copy cut short of its cap + // still holds up to the cut, are read before a successor is needed, so the end is + // judged past them, without leaving them behind. + let mut drained = self.cursor.index + self.cursor.prefetch.buffered().0 as usize; + { + let state = self.cursor.state.read(); + if let Some(cut) = state.cut() + && self.cursor.end.is_none_or(|end| end > cut) + && state.check_read(drained, self.cursor.raw).is_ok() + { + drained = drained.max(cut); + } + } if drained > self.cursor.index { return self.poll_finished_past(waiter, drained, res); } @@ -4663,18 +4682,30 @@ impl Consumer { if self.cursor.end.is_some_and(|end| drained >= end) { return Poll::Ready(Ok(index)); } - let Some(copy) = ready!(self.poll_successor_at(waiter, drained))? else { - return failed; + let kept = self.carry.as_mut().and_then(|carry| carry.finishing.take()); + let mut next = match kept { + // It hands over to a later copy itself, should it fail too. + Some((at, next)) if at == drained => next, + _ => { + let Some(copy) = ready!(self.poll_successor_at(waiter, drained))? else { + return failed; + }; + let mut next = self.clone(); + next.adopt_copy(copy); + next.cursor.index = drained; + next + } }; - let mut next = self.clone(); - next.adopt_copy(copy); - next.cursor.index = drained; match next.poll_finished_once(waiter) { Poll::Ready(res) => Poll::Ready(res.map(|_| index)), - Poll::Pending => match self.poll_expiry(waiter, true) { - true => Poll::Ready(Err(Error::Old)), - false => Poll::Pending, - }, + Poll::Pending => { + // Dropped, it would leave the copy unwanted, and a fetch could abandon it. + self.carry.get_or_insert_default().finishing = Some((drained, next)); + match self.poll_expiry(waiter, true) { + true => Poll::Ready(Err(Error::Old)), + false => Poll::Pending, + } + } } } @@ -4687,13 +4718,13 @@ impl Consumer { if self.cursor.capped() { return Poll::Ready(Ok(index as u64)); } - let raw = self.cursor.raw; + let (raw, end) = (self.cursor.raw, self.cursor.end); // Frames already prefetched are this cursor's whatever the group sheds since, so // only the first one past them can be evicted. let unread = index + self.cursor.prefetch.buffered().0 as usize; let res = self .cursor - .poll(waiter, |state| state.poll_end(index, unread, raw)) + .poll(waiter, |state| state.poll_end(index, unread, raw, end)) .map(|res| res.map(|()| index as u64)); self.poll_expired_if_blocked(waiter, res) } @@ -4782,6 +4813,7 @@ impl Cursor { if let Some(frame) = self.prefetch.pop_cached() { self.refresh_if_stale(); self.index += 1; + self.partial = false; let tail = self.index.saturating_add(self.prefetch.buffered().0 as usize)..end; let info = frame::Info { size: frame.payload.len() as u64, @@ -4844,6 +4876,7 @@ impl Cursor { }; self.index += 1; + self.partial = matches!(source, frame::Source::Partial(_)); // A direct read (not prefetched): count the frame here; the frame::Consumer // counts its bytes per chunk as they're read out. stats.frames(1); @@ -7497,6 +7530,27 @@ mod test { assert!(matches!(whole.poll_finished(&waiter), Poll::Ready(Err(Error::Cancel)))); } + /// A copy its transport bound at frame 4 that finishes after frame 2 ends there: the group + /// holds two frames, so a reader past them reads a clean end rather than a cut. + #[test] + fn a_bounded_copy_finished_short_of_its_bound_ends_cleanly() { + let mut producer = Info { sequence: 0 }.produce(); + producer.bound(Bounds { start: 0, end: Some(4) }).unwrap(); + let mut reader = producer.consume(); + for payload in [b"a", b"b"] { + producer + .write_frame(crate::Timestamp::ZERO, Bytes::from_static(payload)) + .unwrap(); + } + producer.finish().unwrap(); + reader.skip_to(4); + let waiter = kio::Waiter::noop(); + let res = reader.poll_read_frame(&waiter); + assert!(matches!(res, Poll::Ready(Ok(None))), "{res:?}"); + let res = reader.poll_finished(&waiter); + assert!(matches!(res, Poll::Ready(Ok(4))), "{res:?}"); + } + /// A publisher that read every frame of a finished group still learns when the budget /// gives up on it, so it can abandon the stream it is still writing. #[test] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 397f8790d35..adcc414f4f8 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -17683,6 +17683,11 @@ mod test { again.write_frame(at(302), &b"c"[..]).unwrap(); let res = reader.poll_finished(&waiter); assert!(res.is_pending(), "the re-offer is still open: {res:?}"); + // The end check keeps reading the re-offer, so a fetch would not abandon it. + assert!( + again.handle().poll_unwanted(&waiter).is_pending(), + "the re-offer stays wanted" + ); again.finish().unwrap(); // From where the reader stands, with frame 1 still to read. assert_eq!(reader.finished().await.unwrap(), 1); @@ -17725,6 +17730,77 @@ mod test { assert!(reader.read_frame().await.unwrap().is_none()); } + /// A reader that raises its cap past a copy's cut before reaching it learns from its end + /// what its frame reads will: the frames past the cut are missing, whether the ones before + /// it are still in the copy or already prefetched. + #[kio::test] + #[cfg(feature = "async")] + async fn a_raised_cap_before_a_cut_ends_in_its_error() { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let mut head = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(3, group::Bounds { start: 0, end: Some(3) }, None) + .unwrap(); + let mut reader = head.consume().with_range(..3); + for (millis, payload) in [(300, b"a"), (301, b"b"), (302, b"c")] { + head.write_frame(at(millis), &payload[..]).unwrap(); + } + head.finish().unwrap(); + // Takes frames 1 and 2 into its prefetch with frame 0. + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + let mut reader = reader.with_end(None); + // A clone shares the cursor but not the prefetch, so frames 1 and 2 are the copy's. + let mut unfetched = reader.clone(); + let res = unfetched.finished().await; + assert!(res.is_err(), "frame 3 is in no copy: {res:?}"); + let res = reader.finished().await; + assert!(res.is_err(), "nor past the prefetch: {res:?}"); + for payload in [b"b", b"c"] { + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], payload); + } + assert!(reader.read_frame().await.is_err(), "the reads agree"); + } + + /// A reader that skipped ahead of the frames its copy committed tore none of them, so it + /// continues on a held re-offer from where it skipped to. + #[kio::test] + #[cfg(feature = "async")] + async fn a_skipped_reader_continues_on_a_held_reoffer() { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(300), &b"a"[..]).unwrap(); + let mut reader = first.consume(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + reader.skip_to(3); + + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let waiter = kio::Waiter::noop(); + assert!( + reader.poll_read_frame(&waiter).is_pending(), + "waits on the held re-offer" + ); + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer takes the group"); + }; + drop(hold); + for (millis, payload) in [(300, b"a"), (301, b"b"), (302, b"c"), (303, b"d")] { + again.write_frame(at(millis), &payload[..]).unwrap(); + } + again.finish().unwrap(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"d"); + assert!(reader.read_frame().await.unwrap().is_none()); + } + /// A capped reader that continued on a copy cut at its cap and then raises the cap reads /// no clean end at the cut: the frames past it are missing from that copy, so it continues /// on a copy that holds them, or fails without one. From 19be55cdfb3dbcbba6ff36528001cccb9f9e5297 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 12 Nov 2026 16:24:12 -0800 Subject: [PATCH 18/21] fix(net): an end check's kept copy follows the reader's cap finished() reused the copy it kept between polls even after the caller changed its cap, so a successor cut at the old cap reported a clean end. It now reuses the kept copy only while the cap and mode still match. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/group.rs | 9 +++++++-- rs/moq-net/src/model/track.rs | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index a4490a06f60..b6a0df1da34 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -4684,8 +4684,13 @@ impl Consumer { } let kept = self.carry.as_mut().and_then(|carry| carry.finishing.take()); let mut next = match kept { - // It hands over to a later copy itself, should it fail too. - Some((at, next)) if at == drained => next, + // It hands over to a later copy itself, should it fail too. A cap or mode changed + // since asks again: the copy kept may not cover the new range. + Some((at, next)) + if at == drained && next.cursor.end == self.cursor.end && next.cursor.raw == self.cursor.raw => + { + next + } _ => { let Some(copy) = ready!(self.poll_successor_at(waiter, drained))? else { return failed; diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index adcc414f4f8..952ae4f91d4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -17765,6 +17765,42 @@ mod test { assert!(reader.read_frame().await.is_err(), "the reads agree"); } + /// An end check waiting on a copy past the reader's prefetch follows the reader's cap: once + /// the cap is lifted, a successor cut at the old cap no longer ends the group cleanly. + #[kio::test] + #[cfg(feature = "async")] + async fn a_retained_end_check_follows_a_lifted_cap() { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(300), &b"a"[..]).unwrap(); + first.write_frame(at(301), &b"b"[..]).unwrap(); + let mut reader = first.consume().with_range(..3); + // Takes frame 1 into its prefetch with frame 0. + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let mut cut = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(3, group::Bounds { start: 2, end: Some(3) }, None) + .unwrap(); + drop(hold); + cut.write_frame(at(302), &b"c"[..]).unwrap(); + let waiter = kio::Waiter::noop(); + assert!(reader.poll_finished(&waiter).is_pending(), "the cut copy is still open"); + let mut reader = reader.with_range(..); + cut.finish().unwrap(); + let res = reader.finished().await; + assert!(res.is_err(), "frame 3 is in no copy: {res:?}"); + } + /// A reader that skipped ahead of the frames its copy committed tore none of them, so it /// continues on a held re-offer from where it skipped to. #[kio::test] From 8d7824550727936364105e5c38d313b23c8613e0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 01:33:07 -0800 Subject: [PATCH 19/21] fix(net): settle the review findings on a reader continuing on a re-offer - A skip past a frame handed out partly clears the torn mark, so the reader still continues on a held re-offer (Pullfrog). - A compressed successor whose slices since its inflated frames were shed continues no plain reader (CodeRabbit). - An end the successor answers releases the track demand (CodeRabbit). - The lite-07 duplicate test re-sends frames size first, as main now frames. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscriber.test.ts | 3 +- rs/moq-net/src/model/group.rs | 50 ++++++++++++-- rs/moq-net/src/model/track.rs | 107 +++++++++++++++++++++-------- 3 files changed, 128 insertions(+), 32 deletions(-) diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index b61ef3b844a..5c940f7eb7b 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -1691,8 +1691,9 @@ test.each([false, true])("a duplicate of an arriving copy waits for it to settle [0n, 7], [2n, 8], ] as const) { + // The size plus one first, as above. + await resend.u53(2); await resend.u62(timestamp); - await resend.u53(1); await resend.write(new Uint8Array([payload])); } const duplicate = new Reader( diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index bbf121dfd41..17a3d008b47 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -3739,8 +3739,9 @@ struct Cursor { // Read a compressed group's stored slices rather than its inflated copy. raw: bool, - // Whether the last frame handed out to stream from this copy was still being written. - // Left uncommitted when the copy fails, it is torn. + // Whether the last frame handed out to stream from this copy was still being written, + // until a skip past it. Left uncommitted when the copy fails, it is torn. A whole read + // after it follows its commit, so it can leave this set. partial: bool, // Whether the group is compressed, so a plain read inflates under the state's lock. @@ -4469,7 +4470,14 @@ impl Consumer { let raw = reader.cursor.raw; let state = self.cursor.state.read(); // A plain read inflates from frame 0, and a copy that shed the frame can't serve it. - !state.sparse() && (raw || state.flate.is_none() || state.offset == 0) && state.check_read(index, raw).is_ok() + !state.sparse() + && (raw || state.flate.is_none() || state.offset == 0) + && state.check_read(index, raw).is_ok() + // Inflating a frame past the inflated ones needs every slice since, so none shed. + && state.plain(raw).is_none_or(|plain| { + let len = plain.len.load(Ordering::Acquire); + len > index || len >= state.held() + }) } fn recover(&self) -> Option<&super::resume::Recover> { @@ -4708,7 +4716,13 @@ impl Consumer { } }; match next.poll_finished_once(waiter) { - Poll::Ready(res) => Poll::Ready(res.map(|_| index)), + Poll::Ready(res) => { + // The failed copy holds no end, so the copy that answered releases the demand. + if res.is_ok() { + next.release_if_finished(); + } + Poll::Ready(res.map(|_| index)) + } Poll::Pending => { // Dropped, it would leave the copy unwanted, and a fetch could abandon it. self.carry.get_or_insert_default().finishing = Some((drained, next)); @@ -4796,6 +4810,7 @@ impl Cursor { self.index = index; // The batch was drained from below the new cursor, so it can't be reused. self.prefetch = Prefetch::default(); + self.partial = false; } fn skip_to(&mut self, index: u64) { @@ -4805,6 +4820,8 @@ impl Cursor { } self.index = index; self.prefetch = Prefetch::default(); + // The frame handed out partly is given up below the skip, so a failure tears nothing. + self.partial = false; } fn poll_next_frame( @@ -6907,6 +6924,31 @@ mod test { assert!(matches!(raw.read_frame().now_or_never().unwrap(), Err(Error::Evicted))); } + /// A compressed copy that shed its front slices before any plain read inflated them + /// continues no plain reader, even past them: inflating its frame needs them. A raw + /// reader takes the stored slice. + #[test] + #[cfg_attr(miri, ignore = "fills the 32 MiB group tail, past Miri's 15-minute cap")] + #[cfg(feature = "async")] + fn a_copy_shed_before_inflating_continues_no_plain_reader() { + let pool = cache::Pool::unbounded(); + let mut group = compressed_group(&pool); + for frame in &mebibytes(40) { + group.write_frame(None, frame.clone()).unwrap(); + } + let held = group.handle.state.read().held(); + assert!(held > 1, "the slices were shed"); + let copy = group.consume(); + let mut plain = group.consume(); + plain.skip_to(held as u64); + assert!(!copy.continues(&plain, held)); + assert!( + matches!(read_plain(&mut plain), Err(Error::Evicted)), + "inflating needs them" + ); + assert!(copy.continues(&group.consume().compressed(), held)); + } + /// The inflated copy charges each frame's slot, so empty frames count toward its tail. #[test] #[cfg(feature = "async")] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index cfe4489ec3c..5744e2664cf 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -17727,6 +17727,39 @@ mod test { } } + /// A reader whose failed copy's end a re-offer answers past its prefetched frames reached + /// the group's end, so it no longer keeps the track wanted. + #[kio::test] + #[cfg(feature = "async")] + async fn a_failed_copy_finished_on_a_reoffer_releases_its_demand() { + let producer = received_producer(Info::timed()); + let mut subscriber = producer.subscribe(None); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + first.write_frame(at(300), &b"a"[..]).unwrap(); + first.write_frame(at(301), &b"b"[..]).unwrap(); + let mut reader = subscriber.assert_group(); + drop(subscriber); + // Takes frame 1 into its prefetch with frame 0. + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, 2) else { + panic!("the re-offer takes the group"); + }; + drop(hold); + again.write_frame(at(302), &b"c"[..]).unwrap(); + again.finish().unwrap(); + assert_eq!(producer.demand().wanted(), Some(0), "not read to its end yet"); + assert_eq!(reader.finished().await.unwrap(), 1); + assert_eq!(producer.demand().wanted(), None, "the re-offer answered the end"); + } + /// A capped reader continues on a copy cut short at the back no earlier than its cap, which /// holds every frame it still reads. #[kio::test] @@ -17836,35 +17869,55 @@ mod test { #[kio::test] #[cfg(feature = "async")] async fn a_skipped_reader_continues_on_a_held_reoffer() { - let producer = received_producer(Info::timed()); - let at = |millis| Timestamp::from_millis(millis).unwrap(); - let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { - panic!("the first copy opens"); - }; - first.write_frame(at(300), &b"a"[..]).unwrap(); - let mut reader = first.consume(); - assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); - reader.skip_to(3); + for chunked in [false, true] { + let producer = received_producer(Info::timed()); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let Ok(Received::Group(mut first)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the first copy opens"); + }; + let mut reader = first.consume(); + if chunked { + // Frame 0 streams to the reader while still being written, then commits. + let mut frame = first + .create_frame(frame::Info { + size: 1, + timestamp: Some(at(300)), + }) + .unwrap(); + let mut part = reader.next_frame().await.unwrap().expect("frame 0 streams"); + frame.write(&b"a"[..]).unwrap(); + assert_eq!(&part.read_chunk().await.unwrap().unwrap()[..], b"a"); + first = frame.finish().unwrap(); + } else { + first.write_frame(at(300), &b"a"[..]).unwrap(); + assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"a"); + } + reader.skip_to(3); - let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { - panic!("the re-offer waits on the copy still arriving"); - }; - first.abort(Error::Cancel).unwrap(); - let waiter = kio::Waiter::noop(); - assert!( - reader.poll_read_frame(&waiter).is_pending(), - "waits on the held re-offer" - ); - let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { - panic!("the re-offer takes the group"); - }; - drop(hold); - for (millis, payload) in [(300, b"a"), (301, b"b"), (302, b"c"), (303, b"d")] { - again.write_frame(at(millis), &payload[..]).unwrap(); + let Ok(Received::Held(hold)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer waits on the copy still arriving"); + }; + first.abort(Error::Cancel).unwrap(); + let waiter = kio::Waiter::noop(); + assert!( + reader.poll_read_frame(&waiter).is_pending(), + "waits on the held re-offer" + ); + let Ok(Received::Group(mut again)) = producer.create_group_at(group::Info { sequence: 3 }, 0) else { + panic!("the re-offer takes the group"); + }; + drop(hold); + for (millis, payload) in [(300, b"a"), (301, b"b"), (302, b"c"), (303, b"d")] { + again.write_frame(at(millis), &payload[..]).unwrap(); + } + again.finish().unwrap(); + assert_eq!( + &reader.read_frame().await.unwrap().unwrap().payload[..], + b"d", + "chunked: {chunked}" + ); + assert!(reader.read_frame().await.unwrap().is_none()); } - again.finish().unwrap(); - assert_eq!(&reader.read_frame().await.unwrap().unwrap().payload[..], b"d"); - assert!(reader.read_frame().await.unwrap().is_none()); } /// A capped reader that continued on a copy cut at its cap and then raises the cap reads From a503b94126eb92bc688060c40afd0bfc719b8ac3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 02:41:58 -0800 Subject: [PATCH 20/21] fix(net): a takeover counts a mirror's unread frames against the tail A stalled reader continued across a chain of takeovers, each copy writing one frame, kept every inherited frame: the successor's trim sheds only its own. A mirror whose unread frames already pass the tail is behind it, so the takeover ends it as evicted, bounding it to two tails over any chain (Codex). Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.ts | 11 +++++++++++ js/net/src/track.test.ts | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 3634d6c4770..bafcb9a61a7 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -335,6 +335,17 @@ export class Producer { mirror.closed.set(error); continue; } + // Its unread frames count against the tail it continues in, as they would on one copy, so a + // chain of takeovers cannot grow a stalled reader's buffer: past the tail, it is behind it. + let unread = 0; + for (const frame of mirror.frames.peek()) unread += frameBytes(frame); + if (unread > TAIL_BYTES) { + mirror.frames.set(new Queue()); + mirror.cacheBytes = 0; + mirror.flate = undefined; + mirror.closed.set(evicted()); + continue; + } for (const frame of mirror.frames.peek()) kept.add(frame); mirror.from = mirror.total.peek(); mirror.charge = state.charge; diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index bf5f0d34b37..51d1cb4c65d 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3573,6 +3573,27 @@ test.each([ producer.close(); }); +// A stalled reader continued across a chain of takeovers, each copy writing one frame, keeps no more +// than the tail: its unread frames count against each successor's, so it ends evicted past it. +test("a chain of takeovers keeps a stalled reader within the tail", async () => { + const producer = new TrackProducer("video").accept({ maxAge: null }); + hooks.receiveTrack(producer); + const failed = new Error("reset"); + const payload = new Uint8Array(9 * 1024 * 1024); + let copy = hooks.receiveGroup(producer, { sequence: 3n, start: 0 }); + const reader = copy.consume(); + for (let index = 0; index < 8; index++) { + copy.writeFrame({ payload }); + const held = hooks.arrivingGroup(producer, 3n); + copy.abort(failed); + copy = hooks.receiveGroup(producer, { sequence: 3n, start: index + 1 }); + held?.release(); + } + copy.finish(); + expect(await reader.closed).toMatchObject({ code: StreamCode.Evicted }); + producer.close(); +}); + // While a duplicate is held, a failed copy keeps each mirror open and the copy that takes over tees // into it from the frame it reached: a sink still buffering the group hands it out once, a frame cut // short is read whole from the successor unless a reader already took part of it, readers at From fead46179c03ddde9a6ecf43779f8c30d81519ef Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 05:45:14 -0800 Subject: [PATCH 21/21] chore(quest): drop the memlock follow-up, which #8641 did Co-Authored-By: Claude Opus 5.5 --- quest/m0/test-flakes-2/uring-memlock.md | 30 ------------------------- 1 file changed, 30 deletions(-) delete mode 100644 quest/m0/test-flakes-2/uring-memlock.md diff --git a/quest/m0/test-flakes-2/uring-memlock.md b/quest/m0/test-flakes-2/uring-memlock.md deleted file mode 100644 index 6d41de195b0..00000000000 --- a/quest/m0/test-flakes-2/uring-memlock.md +++ /dev/null @@ -1,30 +0,0 @@ -# [S] The relay's io_uring memlock test holds on every kernel - -## Goal - -`moq-relay`'s `runtime_uring::uring_startup_reports_the_memlock_limit` passes -on every Linux host `just check` runs on, or is skipped where the kernel never -charges a ring to `RLIMIT_MEMLOCK`. The relay keeps reporting the memlock hint -where the limit does refuse a worker. - -## Plan - -Found 2026-11-12 while checking -[moq-dev/moq#8546](https://github.com/moq-dev/moq/pull/8546). On a Linux -7.3.3 host, `cargo nextest run -p moq-relay --all-features -E -'test(uring_startup_reports_the_memlock_limit)'` fails the same way on -`origin/main` and on that PR's branch: `a worker needs locked memory: ()` -(`rs/moq-relay/tests/integration/runtime_uring.rs:1328`). The test sets -`RLIMIT_MEMLOCK` to 0 and expects the first io_uring worker to fail. On this -kernel the ring starts anyway, so the relay never refuses. - -Newer kernels charge io_uring memory to the memory cgroup rather than to -`RLIMIT_MEMLOCK`. Find which ring setups still hit the limit, then either -make the test force a setup that does, or detect at startup whether the limit -applies and skip the test where it cannot fail. Never loosen the assertion on -the message the relay prints. If no kernel the relay supports still charges -the limit, the startup hint and this test may go. - -## Hosts - -- linux - io_uring