Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c9f011a
chore(quest): claim quest/m1/copy-recover.md
kixelated Nov 12, 2026
9fbb39e
feat(net): a reader on a failed copy continues on the held re-offer
kixelated Nov 12, 2026
45b5692
quest: finish copy-recover
kixelated Nov 12, 2026
ac0042d
quest: follow up the io_uring memlock test that fails on newer kernels
kixelated Nov 12, 2026
0a4ba81
fix(net): held re-offers wait per copy, and JS readiness and close en…
kixelated Nov 12, 2026
74c043b
fix(net): closing a reader waiting on a takeover keeps its buffered f…
kixelated Nov 12, 2026
2445676
Merge origin/main into quest/m1/copy-recover
kixelated Nov 12, 2026
583b072
fix(net): closing a reader twice after a takeover wait does not throw
kixelated Nov 12, 2026
925f45c
fix(net): a sequence's copies share one succession, and closed observ…
kixelated Nov 12, 2026
5555b05
fix(net): a reader rejects a successor that already shed its frame
kixelated Nov 12, 2026
8203e3e
fix(net): a terminal watch and the budget follow a failed copy's take…
kixelated Nov 12, 2026
06528de
fix(net): a successor shedding a waiting reader's frame settles its c…
kixelated Nov 12, 2026
47d81e3
fix(net): a takeover drops a stale route attempt, and a capped reader…
kixelated Nov 12, 2026
41a1428
fix(net): a failure a JS reader's closed reported stands against a la…
kixelated Nov 12, 2026
75aa857
feat(net): a held JS copy tees its successor into its readers; a rais…
kixelated Nov 12, 2026
d85fcb2
fix(net): bill a continued reader's kept frames, tell a replay from a…
kixelated Nov 12, 2026
226867d
fix(net): a reader waits on a hold held for any failed copy of its group
kixelated Nov 12, 2026
9be0a79
Merge remote-tracking branch 'origin/main' into HEAD
kixelated Nov 12, 2026
9a433aa
fix(net): judge a cut from where it settled, track torn frames direct…
kixelated Nov 13, 2026
19be55c
fix(net): an end check's kept copy follows the reader's cap
kixelated Nov 13, 2026
2ee3c88
Merge remote-tracking branch 'origin/main' into quest/m1/copy-recover
kixelated Nov 13, 2026
8d78245
fix(net): settle the review findings on a reader continuing on a re-o…
kixelated Nov 13, 2026
a503b94
fix(net): a takeover counts a mirror's unread frames against the tail
kixelated Nov 13, 2026
fead461
chore(quest): drop the memlock follow-up, which #8641 did
kixelated Nov 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions js/net/src/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> }) => Promise<void> | void) {
const clock = new Manual();
Expand Down
119 changes: 104 additions & 15 deletions js/net/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ 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;
// 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;
Expand Down Expand Up @@ -261,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);
Expand Down Expand Up @@ -300,6 +309,55 @@ export class Producer {
if (state.closed.peek() !== undefined) flate.finish();
};
hooks.groupOffset = (group) => group.#state.offset;
hooks.holdGroup = (group) => {
group.#holds++;
let held = true;
return () => {
if (!held) return;
held = false;
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;
// The failed copy's frames the continued mirrors hold, once each though mirrors share them.
const kept = new Set<Frame>();
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;
}
// 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;
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);
}
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");
const state = group.#state;
Expand Down Expand Up @@ -413,11 +471,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;
Expand All @@ -428,13 +492,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);
}

/**
Expand All @@ -453,6 +513,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);
}
Expand Down Expand Up @@ -575,9 +637,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.
Expand Down Expand Up @@ -653,7 +717,8 @@ export class Producer {
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;
Expand Down Expand Up @@ -687,21 +752,44 @@ 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) {
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);
} 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;
}
}

Expand Down Expand Up @@ -1055,7 +1143,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) => {
Expand Down
33 changes: 31 additions & 2 deletions js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ export interface GroupRecover {
replace(index: number, failed: Error | undefined, signal: AbortSignal): Promise<Replacement>;
}

/** 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<Error | null>;
/** 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;
Expand Down Expand Up @@ -647,8 +655,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<Error | null> | undefined;
/**
* Hold the copy of `sequence` still arriving, if one is, for a duplicate to wait on: until
* 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;
/** The producer's largest reported location and its subsequent changes. */
producerLargest: (producer: Producer) => Getter<Position | undefined>;

Expand Down Expand Up @@ -935,6 +947,17 @@ export const hooks: {
* out, so its `closed` never settles on a failure the recovery continues past.
*/
recoverGroup: (group: GroupConsumer, recover: GroupRecover) => void;
/**
* 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`.
*/
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. Returns the bytes of `failed`'s frames they still hold.
* Assigned by `group.ts`.
*/
succeedGroup: (next: GroupProducer, failed: GroupProducer) => number;
/** Read a payload chunk, releasing its wait when the output stream closes. */
readFrameChunk: (frame: FrameConsumer, closed: Promise<void>) => Promise<Uint8Array | undefined>;
/** Read the next frame, as its stored DEFLATE slice when `compressed`, so a lite-07 publisher forwards it without inflating. */
Expand Down Expand Up @@ -1206,6 +1229,12 @@ export const hooks: {
arrivingGroup: () => {
throw new Error("track.ts not loaded");
},
succeedGroup: () => {
throw new Error("group.ts not loaded");
},
holdGroup: () => {
throw new Error("group.ts not loaded");
},
groupOffset: () => {
throw new Error("group.ts not loaded");
},
Expand Down
40 changes: 35 additions & 5 deletions js/net/src/lite/subscriber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,8 @@ test("a lite-06 group reset with YIELDED ends", 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();
Expand Down Expand Up @@ -1681,7 +1682,30 @@ 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<Uint8Array>({ write: (chunk) => void resent.push(new Uint8Array(chunk)) }),
version,
);
for (const [timestamp, payload] of [
[0n, 7],
[2n, 8],
] as const) {
// The size plus one first, as above.
await resend.u53(2);
await resend.u62(timestamp);
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(() => {
Expand All @@ -1700,16 +1724,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();
}
Expand Down
Loading