Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
acbe2e9
chore(quest): claim quest/m2/pipeline-requests/subscribe.md
kixelated Oct 30, 2026
bd26003
feat(net): send SUBSCRIBE alongside TRACK in Rust
kixelated Oct 30, 2026
2447b09
feat(js/net): send SUBSCRIBE alongside TRACK
kixelated Oct 30, 2026
2edc150
test(net): cover each TRACK and SUBSCRIBE arrival order
kixelated Oct 30, 2026
3a72747
test(relay): let an undeafened subscription see the latest group first
kixelated Oct 30, 2026
ccc77d1
chore(quest): finish SUBSCRIBE goes out with TRACK
kixelated Oct 30, 2026
8ce1d4e
Merge remote-tracking branch 'origin/main' into quest/m2/pipeline-req…
kixelated Oct 30, 2026
a1f3749
fix(js/net): read a datagram with the entry it was decoded with
kixelated Oct 30, 2026
59d15b1
fix(js/net): join at the latest group and hold the answered TRACK
kixelated Oct 31, 2026
37e7466
fix(js/net): move the floor only when the resolved track changes it
kixelated Oct 31, 2026
8ed41a9
fix(net): bound the session tail's grace by the track's max age
kixelated Oct 31, 2026
83c9317
Merge remote-tracking branch 'origin/main' into quest/m2/pipeline-req…
kixelated Oct 31, 2026
eefbf7d
fix(js/net): floor an unresolved track at its newest group on every d…
kixelated Oct 31, 2026
f191195
Merge base into PR 7328 and reconcile subscriber conflicts
Nov 1, 2026
bab713a
Merge origin/main into quest/m2/pipeline-requests/subscribe
kixelated Nov 1, 2026
c5916e7
fix(net): deliver parked groups before a failed pipelined SUBSCRIBE g…
kixelated Nov 1, 2026
ff6aaed
style: format the merge resolution
kixelated Nov 1, 2026
1400eb5
Merge origin/main into quest/m2/pipeline-requests/subscribe
kixelated Nov 1, 2026
2b9b38f
fix(net): read every accepted group stream before a failed SUBSCRIBE …
kixelated Nov 1, 2026
23c135c
fix(net): release a failed subscription's copy once nobody holds it
kixelated Nov 1, 2026
e25e960
Merge remote-tracking branch 'origin/main' into iterate-7328
kixelated Nov 1, 2026
f1e690d
style: format the failed-drain fix
kixelated Nov 1, 2026
40fb7a2
quest land: merge tested base
Nov 1, 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
8 changes: 8 additions & 0 deletions js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ export const hooks: {
* so it names no object; assigned by `track.ts`.
*/
reportedGroup: (producer: Producer, group: number) => void;
/**
* How long a session's tail waits for a late group: the subscribers' max delay, bounded by
* the track's max age; assigned by `track.ts`.
*/
tailGrace: (producer: Producer) => Milli;
/** Snapshot the latest group sequence for wire positioning; assigned by `track.ts`. */
latestSequence: (subscriber: Subscriber) => number | undefined;
/**
Expand Down Expand Up @@ -671,6 +676,9 @@ export const hooks: {
reportedGroup: () => {
throw new Error("track.ts not loaded");
},
tailGrace: () => {
throw new Error("track.ts not loaded");
},
latestSequence: () => {
throw new Error("track.ts not loaded");
},
Expand Down
45 changes: 42 additions & 3 deletions js/net/src/lite/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Front } from "../resume.ts";
import { Reader, Stream, Writer } from "../stream.ts";
import { Timescale, Timestamp } from "../time.ts";
import { Subscriber as TrackSubscriber } from "../track.ts";
import { wireOf } from "../wire.ts";
import { type AnnounceBroadcast, AnnounceOk, AnnounceRequest, decodeAnnounceBroadcast } from "./announce.ts";
import { Datagram as DatagramMessage } from "./datagram.ts";
import { Fetch } from "./fetch.ts";
Expand Down Expand Up @@ -696,6 +697,8 @@ async function servedSubscription(
// Answer the subscription with a front's broadcast, as an application forwarding a
// routed one does, so the track reads through the front's serving route.
routed?: boolean;
// Leave the track unresolved until `accept`, as an on-demand track the SUBSCRIBE beats.
pending?: boolean;
} = {},
) {
const version = options.version ?? Version.DRAFT_05;
Expand All @@ -720,7 +723,10 @@ async function servedSubscription(
} else {
broadcast = publish(origin, Path.from("test"));
}
const track = broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: options.maxAge });
const info = { timescale: Timescale.MILLI, maxAge: options.maxAge };
let track = options.pending ? undefined : broadcast.createTrack("video", info);
// Pulled before the SUBSCRIBE arrives: a broadcast nobody pulls refuses an unknown track.
const request = options.pending ? wireOf(broadcast).requested() : undefined;

const client = await Stream.open(pair.client, { version: version });

Expand Down Expand Up @@ -752,16 +758,29 @@ async function servedSubscription(

const opened = pair.client.incomingUnidirectionalStreams.getReader();

const resolved = () => {
if (!track) throw new Error("the track is still pending");
return track;
};

return {
client,
datagrams: pair.client.datagrams.readable,
track,
get track() {
return resolved();
},
serving,
parked: gate.parked,
release: gate.release,
// Resolve a pending track with the publisher's request for it.
async accept() {
const pulled = await request;
if (!pulled) throw new Error("the publisher never requested the track");
track = pulled.accept(info);
},
// Stamped by sequence, so a group served out of order still starts in order.
serve(sequence: number) {
const group = track.createGroup(sequence);
const group = resolved().createGroup(sequence);
for (const frame of frames) group.writeFrame(textFrame(frame, Timestamp.fromMillis(sequence)));
group.finish();
},
Expand Down Expand Up @@ -1330,6 +1349,26 @@ test("lite draft-05: an update dropping the floor re-pins to the latest group",
}
});

// A SUBSCRIBE can beat its track resolving, as one a relay serves from a copy still fetching
// upstream. Groups the track holds once it does are older than the join, so an absent Group
// Start is still the latest group.
test.each([Version.DRAFT_01, Version.DRAFT_03, Version.DRAFT_05])(
"lite %s: a subscribe that beats its track resolving joins at the latest group",
async (version) => {
const sub = await servedSubscription({ version, pending: true });
try {
await sub.accept();
sub.serve(0);
sub.serve(1);
sub.serve(2);
expect(await sub.servedHeader()).toBe(2);
expect(await sub.servedSequence()).toBeUndefined();
} finally {
await sub.close();
}
},
);

// On an empty track the re-pin has no latest group to name, so the first group the
// track produces is served, even below the dropped floor.
test("lite draft-05: an update dropping the floor on an empty track serves the next group", async () => {
Expand Down
15 changes: 14 additions & 1 deletion js/net/src/lite/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,8 @@ export class Publisher {
},
});
serving(track);
const floor = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track));
const latest = hooks.latestSequence(track);
let floor = cursorFloor(this.version, msg.startGroup, latest);
track.withRange({
start: { included: floor },
end: endGroup === undefined ? undefined : { excluded: endGroup },
Expand Down Expand Up @@ -926,6 +927,18 @@ export class Publisher {
// priority.
const info = await untilAborted(track.info(), left.signal);
const publisher = info.priority;
// A SUBSCRIBE can arrive before its track resolves, such as one a relay serves from a
// copy still fetching upstream, or on lite-05 one sent alongside its TRACK, when no
// group is known yet: the newest once it resolves stands in, as it would for one
// sent after.
const resolved = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track));
if (latest === undefined && resolved !== floor) {
floor = resolved;
track.withRange({
start: { included: floor },
end: endGroup === undefined ? undefined : { excluded: endGroup },
});
}
// Older drafts carry no timestamps.
let stamp: Stamp | undefined;
let compress = false;
Expand Down
149 changes: 143 additions & 6 deletions js/net/src/lite/subscriber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Probe } from "./probe.ts";
import { StreamId } from "./stream.ts";
import {
encodeSubscribeResponse,
Subscribe,
SubscribeDrop,
SubscribeEnd,
SubscribeOk,
Expand Down Expand Up @@ -1162,7 +1163,7 @@ test.each([
});

// A setup that outlived its deadline is over: its TRACK stream is reset, even one still waiting for a
// slot, and the subscription is neither registered again nor sent as a SUBSCRIBE.
// slot, along with the SUBSCRIBE sent beside it, and the subscription is not registered again.
test.each([
["lite-05 subscribe waiting on the TRACK_INFO", Version.DRAFT_05, false],
["lite-06 subscribe waiting on the TRACK_INFO", Version.DRAFT_06, false],
Expand All @@ -1184,7 +1185,8 @@ test.each([

if (parked) streams[0].release();
await streams[0].aborted;
expect(streams.length).toBe(1);
await streams[1].aborted;
expect(streams.length).toBe(2);

// A GROUP for a forgotten id is ignored without touching its stream.
const touched: PropertyKey[] = [];
Expand Down Expand Up @@ -1258,6 +1260,104 @@ test("a lite subscribe whose SUBSCRIBE write stalls past the deadline closes its
}
});

// On lite-05 and later a SUBSCRIBE goes out beside its TRACK, before the TRACK_INFO, and accepting
// the info sends no SUBSCRIBE_UPDATE even when the publisher keeps groups for less than the
// subscriber waits.
test.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])(
"lite %s: a subscribe goes out with its track",
async (version) => {
const { quic, streams, opened } = fakeSession();
const subscriber = new Subscriber(quic, version, HopSchema.parse(1n));
const track = subscriber
.consume(Path.from("room"))
.track("video")
.subscribe({ priority: 3, maxDelay: Milli(10_000) });
try {
await opened(0);
await streams[0].reading;
await (await opened(1)).writing;
const sent = new Reader(undefined, new Uint8Array(Buffer.concat(streams[0].written)), version);
expect(await sent.u53()).toBe(StreamId.Track);
await drainReactions();
const wire = () => new Reader(undefined, new Uint8Array(Buffer.concat(streams[1].written)), version);
let subscribe = wire();
expect(await subscribe.u53()).toBe(StreamId.Subscribe);
const msg = await Subscribe.decode(subscribe, version);
expect([msg.id, msg.track, msg.priority, msg.maxDelay]).toEqual([0n, "video", 3, 10_000]);

const chunks: Uint8Array[] = [];
const writer = new Writer(
new WritableStream<Uint8Array>({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }),
version,
);
await new TrackInfo({ timescale: 1000, maxAge: 1000 }).encode(writer, version);
for (const chunk of chunks) streams[0].inbound.enqueue(chunk);
await track.info();
await drainReactions();
subscribe = wire();
await subscribe.u53();
await Subscribe.decode(subscribe, version);
expect(await subscribe.done()).toBe(true);
} finally {
track.close();
subscriber.close();
}
},
);

// A GROUP that beats its subscription's TRACK_INFO waits unread for it, then is read with it.
test("a group that beats its track info waits for it", async () => {
const version = Version.DRAFT_07;
const { quic, streams, opened } = fakeSession();
const subscriber = new Subscriber(quic, version, HopSchema.parse(1n));
const track = subscriber.consume(Path.from("room")).track("video").subscribe();
try {
await opened(0);
await (await opened(1)).writing;
const touched: PropertyKey[] = [];
const stream = await groupStream(0, version);
const reader = new Proxy(stream, {
get: (target, key) => {
touched.push(key);
const value = Reflect.get(target, key, target);
return typeof value === "function" ? value.bind(target) : value;
},
});
const receiving = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0 }), reader);
await drainReactions();
expect(touched).toEqual([]);

await answerTrackInfo(streams[0], version, 1000);
await receiving;
const group = await track.recvGroup();
expect(group?.sequence).toBe(0);
expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([0]));
} finally {
track.close();
subscriber.close();
}
});

// A TRACK reset while its SUBSCRIBE is live fails the subscription, so the origin can try another
// route, and resets the SUBSCRIBE.
test("a reset track rejects the subscription and resets its subscribe", async () => {
const warn = spyOn(console, "warn").mockImplementation(() => {});
const { quic, streams, opened } = fakeSession();
const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n));
const track = subscriber.consume(Path.from("room")).track("video").subscribe();
try {
await opened(0);
await (await opened(1)).writing;
streams[0].inbound.error(new Error("peer reset"));
expect(await track.closed).toBeInstanceOf(Error);
await streams[1].aborted;
} finally {
track.close();
subscriber.close();
warn.mockRestore();
}
});

test("a fetch started after the subscriber closes rejects without opening a stream", async () => {
const { quic, streams } = fakeSession();
const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n));
Expand All @@ -1269,7 +1369,7 @@ test("a fetch started after the subscriber closes rejects without opening a stre
});

// The grant watch is armed before the first check, so a shrink while TRACK_INFO is still in
// flight refuses the subscription rather than opening a SUBSCRIBE the grant no longer covers.
// flight refuses the subscription and resets the SUBSCRIBE sent beside its TRACK.
test("a grant that shrinks while the subscription sets up refuses it", async () => {
const { quic, streams, opened } = fakeSession();
const scoped = (prefix: string): Grant => ({
Expand Down Expand Up @@ -1298,8 +1398,8 @@ test("a grant that shrinks while the subscription sets up refuses it", async ()
const err = await next;
expect(err).toBeInstanceOf(StreamError);
expect((err as StreamError).code).toBe(StreamCode.Unauthorized);
// Nothing past the TRACK stream reached the wire.
expect(streams.length).toBe(1);
await streams[1].aborted;
expect(streams.length).toBe(2);

track.close();
});
Expand Down Expand Up @@ -1805,6 +1905,42 @@ test.each([
}
});

// A TRACK answered while its SUBSCRIBE still waits for a stream belongs to the subscription, so
// a withdrawal then closes it rather than leaving it to a create that may never open.
test("a withdrawal while the subscribe opens closes the answered track", async () => {
const version = Version.DRAFT_07;
const warn = spyOn(console, "warn").mockImplementation(() => {});
const { quic, streams, opened } = fakeSession([2]);
const subscriber = new Subscriber(quic, version, HopSchema.parse(1n));
const announced = subscriber.announced();
try {
await opened(0);
const pool = await epochlessPool(version, streams, announced);
const held = subscriber.consume(Path.from("pool/job"));
const track = held.track("video").subscribe();
const info = await opened(1);
await opened(2);
await info.reading;
await answerTrackInfo(info, version);
await drainReactions();

let finished = false;
void info.finished.then(() => {
finished = true;
});
await pool.withdraw();
expect(await track.closed).toBeInstanceOf(Error);
await drainReactions();
expect(finished).toBe(true);
track.close();
held.close();
} finally {
announced.close();
subscriber.close();
warn.mockRestore();
}
});

// A TRACKS request without an epoch lists whichever instance won the path when it arrived, so
// another instance taking the path refuses one still unanswered.
test("a listing of an epochless handle replaced before its answer is refused", async () => {
Expand Down Expand Up @@ -2684,8 +2820,9 @@ test("closing a broadcast while TRACK_INFO is pending leaves no unhandled reject
broadcast.close();
track.close();
await streams[0].aborted;
await streams[1].aborted;
await drainReactions();
expect(streams).toHaveLength(1);
expect(streams).toHaveLength(2);
expect(unhandled).toEqual([]);
} finally {
process.off("unhandledRejection", onUnhandled);
Expand Down
Loading