Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions js/net/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ export class Consumer {
};
hooks.groupArrived = (group) => group.#state.arrived;
hooks.groupStarted = (group) => group.#state.total.peek() > 0 || group.#state.closed.peek() !== undefined;
hooks.groupFate = (group) => {
const state = group.#state;
return () => {
const closed = state.closed.peek();
return closed instanceof Error ? closed : undefined;
};
};
hooks.arriveGroup = (group, at) => {
group.#state.arrived = at;
};
Expand Down
90 changes: 88 additions & 2 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,7 @@ test("a group stream whose FIN never completes is reset when the group expires",

write(0);
await closeStarted;
// The newest group is never stale, however long its FIN takes. Kept inside the cache's
// idle window: a group evicted once superseded is no longer judged.
// The newest group is never stale, however long its FIN takes.
await clock.advance(Nano.fromMilli(Milli(budget * 2)));
expect(reset).toBe(false);

Expand All @@ -806,6 +805,93 @@ test("a group stream whose FIN never completes is reset when the group expires",
}
});

// Under a budget past the cache window, the cache evicts a stalled group and its successor
// before the budget convicts the group. The verdict still waits only for the successor's
// arrival plus the budget, not for whichever newer group the cache holds, and PUBLISH_DONE
// follows the reset.
test("a group stream whose FIN never completes is reset after the cache evicts it", async () => {
const clock = new Manual();
const installed = clock.install();
const pair = createMockTransportPair(ALPN.DRAFT_19);

let closing!: () => void;
const closeStarted = new Promise<void>((resolve) => {
closing = resolve;
});
let reset = false;
let resetting!: () => void;
const streamReset = new Promise<void>((resolve) => {
resetting = resolve;
});
const stalled = new WritableStream<Uint8Array>({
start(controller) {
controller.signal.addEventListener("abort", () => {
reset = true;
resetting();
});
},
close() {
closing();
return new Promise<void>(() => {});
},
});
spyOn(pair.server, "createUnidirectionalStream").mockImplementationOnce(async () => stalled);

// Twice the cache's idle window.
const budget = Milli(60_000);
const { pub, origin } = publisher(pair.server);
const broadcast = publish(origin, Path.from("test"));
const track = broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: budget });
const client = await Stream.open(pair.client, { version: VERSION });
const server = await Stream.accept(pair.server, VERSION);
if (!server) throw new Error("publisher never accepted the subscribe stream");

try {
const running = pub.runSubscribe(
new Subscribe({
requestId: 0n,
trackNamespace: Path.from("test"),
trackName: "video",
subscriberPriority: 0,
}),
server,
);
expect(await client.reader.u53()).toBe(SubscribeOk.id);
await SubscribeOk.decode(client.reader, VERSION);

// Frozen timestamps, so only the wall clock since group 1 arrived convicts group 0.
const write = (sequence: number) => {
const group = new GroupProducer(sequence);
group.writeFrame({ payload: new TextEncoder().encode("frame"), timestamp: Timestamp.fromMillis(0) });
group.close();
track.writeGroup(group);
};

write(0);
await closeStarted;
write(1);
// A later group, so a verdict that forgot group 1 would wait for this one's deadline.
const later = Milli(10_000);
await clock.advance(Nano.fromMilli(later));
write(2);
track.close();

await clock.advance(Nano.fromMilli(Milli(budget - later - 1)));
expect(reset).toBe(false);
await clock.advance(Nano.fromMilli(Milli(1)));
await streamReset;

await running;
expect(await client.reader.u53()).toBe(PublishDone.id);
await PublishDone.decode(client.reader, VERSION);
} finally {
client.close();
broadcast.close();
origin.close();
installed.close();
}
});

// A group can go stale while its stream is still opening. Serving it must abandon the group
// without starting a write: an abandoned write rejects once the stream resets, and nothing
// would handle it (Node exits on the first unhandled rejection).
Expand Down
5 changes: 5 additions & 0 deletions js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ export const hooks: {
groupArrived: (group: GroupConsumer) => Instant;
/** Whether a group counts as content: its first frame landed, or it ended. */
groupStarted: (group: GroupConsumer) => boolean;
/** Watch the error a group's copy closes with, ignoring readers' verdicts, without holding the consumer. */
groupFate: (group: GroupConsumer) => () => Error | undefined;
/** Record when a track's mirror of a group joined that track at this hop. */
arriveGroup: (group: GroupConsumer, at: Instant) => void;
/** Keep applying a subscription's drift policy after it hands a group out. */
Expand Down Expand Up @@ -715,6 +717,9 @@ export const hooks: {
groupStarted: () => {
throw new Error("group.ts not loaded");
},
groupFate: () => {
throw new Error("group.ts not loaded");
},
expireGroup: () => {
throw new Error("group.ts not loaded");
},
Expand Down
89 changes: 89 additions & 0 deletions js/net/src/track.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,95 @@ test("a parked read wakes at its wall deadline", async () => {
}
});

// Evicting a recorded successor nobody read leaves its mirror a reader-side gap, which is not
// a failure: the held group is still judged by that successor's arrival, not the next one's.
test("an evicted successor with unread frames still starts the wall clock", async () => {
const clock = manualTime(10_000);
const producer = new TrackProducer("test").accept({ timescale: Timescale.MILLI });
try {
const budget = 2 * CACHE_WINDOW_MS;
const track = producer.subscribe({ maxDelay: Milli(budget) });
const write = () => {
const group = producer.appendGroup();
group.writeFrame({ payload: enc.encode("x"), timestamp: Timestamp.fromMillis(0) });
group.close();
};
write();
const group = track.tryRecvGroup();
if (!group) throw new Error("missing group");
expect(await group.readString()).toBe("x");

let stale = false;
const guarded = hooks
.guardGroup(group, () => new Promise<never>(() => {}))
.catch((err: unknown) => {
stale = true;
return err;
});
write(); // the successor, left unread
await settle();
// A later live edge, so the successor ages out of the cache.
await clock.advance(20_000);
write();

await clock.advance(10_000 + budget - 1);
expect(stale).toBe(false);
await clock.advance(10_000 + budget);
expect(stale).toBe(true);
expect(await guarded).toMatchObject({ code: StreamCode.Old });
} finally {
producer.close();
clock.installed.close();
}
});

// A held group's recorded successor that failed hands the wall clock on to the next live
// group, even when the write that brings that group in also evicts the failed one before the
// guard looks again, rather than convicting by the failed one's arrival.
test("a failed successor hands the wall clock on after the cache evicts it", async () => {
const clock = manualTime(10_000);
const producer = new TrackProducer("test").accept({ timescale: Timescale.MILLI });
try {
const budget = 2 * CACHE_WINDOW_MS;
const track = producer.subscribe({ maxDelay: Milli(budget) });
const held = producer.appendGroup();
held.writeFrame({ payload: enc.encode("x"), timestamp: Timestamp.fromMillis(0) });
held.close();
const group = track.tryRecvGroup();
if (!group) throw new Error("missing group");
expect(await group.readString()).toBe("x");

let stale = false;
const guarded = hooks
.guardGroup(group, () => new Promise<never>(() => {}))
.catch((err: unknown) => {
stale = true;
return err;
});
const failed = producer.appendGroup();
await settle();

// Past the cache window, so the next write evicts the failed successor at once.
const replacement = 10_000 + CACHE_WINDOW_MS + 10_000;
await clock.advance(replacement);
failed.close(new Error("upstream failed"));
const live = producer.appendGroup();
live.writeFrame({ payload: enc.encode("y"), timestamp: Timestamp.fromMillis(0) });
live.close();

await clock.advance(10_000 + budget + 1);
expect(stale).toBe(false);
await clock.advance(replacement + budget - 1);
expect(stale).toBe(false);
await clock.advance(replacement + budget);
expect(stale).toBe(true);
expect(await guarded).toMatchObject({ code: StreamCode.Old });
} finally {
producer.close();
clock.installed.close();
}
});

// Empty groups are handed out like any other and retired by the wall clock like an untimed
// one, whether they finished without a frame or are still open with none. A publisher
// flooding a run of them cannot pin the group before the run either.
Expand Down
Loading
Loading