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
53 changes: 49 additions & 4 deletions js/net/src/lite/subscriber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
StreamError,
TimestampMismatch,
} from "../error.ts";
import type { Consumer as GroupConsumer } from "../group.ts";
import { HopSchema, isAnonymous, MAX_HOPS, Route, UNKNOWN_HOP } from "../hop.ts";
import { hooks } from "../internal.ts";
import { GRACE } from "../ledger.ts";
Expand Down Expand Up @@ -1309,6 +1310,48 @@ test("an already-aborted fetch rejects without opening a stream", async () => {
expect(streams.length).toBe(0);
});

// A fetch-only reader's FETCH goes out alongside the TRACK stream, not a round trip after it,
// and its response is read only once TRACK_INFO says the timescale.
test("the first FETCH goes out before TRACK_INFO answers", async () => {
const { quic, streams, opened } = fakeSession();
const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n));

let fetched: GroupConsumer | undefined;
const fetch = subscriber.fetchGroup(Path.from("room"), "video", 0).then((group) => {
fetched = group;
return group;
});
await opened(0);
await opened(1);
await streams[1].reading;

// An empty-group FIN accepts the fetch, but nothing is handed out before the info.
streams[1].inbound.close();
for (let i = 0; i < 64; i++) await Promise.resolve();
expect(fetched).toBeUndefined();

await answerTrackInfo(streams[0]);
expect(await (await fetch).readFrame()).toBeUndefined();

subscriber.close();
});

test("a failed TRACK_INFO fails the FETCH sent alongside it", async () => {
const { quic, streams, opened } = fakeSession();
const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n));

const fetch = subscriber.fetchGroup(Path.from("room"), "video", 0).catch((err: unknown) => err);
await opened(0);
await opened(1);
await streams[1].reading;

streams[0].inbound.error(new StreamError(StreamCode.NotFound, { message: "no such track" }));
expect(await fetch).toBeInstanceOf(Error);
await streams[1].aborted;

subscriber.close();
});

// Coalesced fetches share one FETCH stream. An abort releases only that caller's share; the
// stream is cancelled once the last sharer leaves, before its FETCH is sent if it can be.
test("one of two fetch sharers aborting leaves the other's fetch", async () => {
Expand Down Expand Up @@ -1367,10 +1410,12 @@ test.each([
expect(((await b.catch((err: unknown) => err)) as Error).message).toBe("second");

if (stage === "track") {
// The abandoned TRACK exchange is reset promptly; no FETCH is sent.
const err = fromTransport(await streams[0].aborted) as StreamError;
expect(err.code).toBe(StreamCode.Cancel);
expect(streams.length).toBe(1);
// The abandoned TRACK exchange is reset promptly, with the FETCH sent alongside it.
for (const stream of streams) {
const err = fromTransport(await stream.aborted) as StreamError;
expect(err.code).toBe(StreamCode.Cancel);
}
expect(streams.length).toBe(2);
} else {
const err = fromTransport(await streams[1].aborted) as StreamError;
expect(err.code).toBe(StreamCode.Cancel);
Expand Down
61 changes: 37 additions & 24 deletions js/net/src/lite/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1361,8 +1361,9 @@ export class Subscriber {
}
}

// Resolve the track's timescale, then open the FETCH stream and wait for it to be accepted.
// Closing the group during that wait resets the stream.
// Open the FETCH stream alongside the TRACK stream that says the track's timescale, and wait
// for both: the response waits unread in its stream until the timescale lands. Closing the
// group during that wait resets the stream, and a failed TRACK_INFO fails the fetch.
async #fetchSetup(
broadcast: Path.Valid,
consumed: Consumed,
Expand All @@ -1381,29 +1382,41 @@ export class Subscriber {
// Only the properties are needed, so the TRACK stream closes however the wait ends.
answered.then(
({ stream }) => stream.close(),
() => {},
);
const { info } = await untilClosed(group, answered);
return this.#exchange(
{ sendOrder: sendOrder({ priority }), version: this.version },
async (stream) => {
await stream.writer.u53(StreamId.Fetch);
await new FetchMessage({
broadcast,
epoch,
track,
priority,
maxDelay: hasFetchMaxDelay(this.version) ? maxDelay : undefined,
group: sequence,
}).encode(stream.writer, this.version);
// A byte or an empty-group FIN accepts the fetch; a reset rejects it.
// done() buffers that byte so the response pump can decode it normally.
await untilClosed(group, stream.reader.done());
return { stream, info };
},
signal,
deadline,
(err: unknown) => cancel.abort(error(err)),
);
let stream: Stream;
try {
stream = await this.#exchange(
{ sendOrder: sendOrder({ priority }), version: this.version },
async (stream) => {
await stream.writer.u53(StreamId.Fetch);
await new FetchMessage({
broadcast,
epoch,
track,
priority,
maxDelay: hasFetchMaxDelay(this.version) ? maxDelay : undefined,
group: sequence,
}).encode(stream.writer, this.version);
// A byte or an empty-group FIN accepts the fetch; a reset rejects it.
// done() buffers that byte so the response pump can decode it normally.
await untilClosed(group, stream.reader.done());
return stream;
},
signal,
deadline,
);
} catch (err: unknown) {
cancel.abort(error(err));
throw err;
}
try {
const { info } = await untilClosed(group, answered);
return { stream, info };
} catch (err: unknown) {
stream.abort(error(err));
throw err;
}
}

// Read the FETCH response (bare frames, zigzag-delta-timestamped unless `timescale` is 0)
Expand Down
17 changes: 11 additions & 6 deletions js/net/src/request-deadline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,29 @@ import * as Path from "./path.ts";
// Zero stream credit can be rejected immediately or queued by a transport that ignores
// waitUntilAvailable. Once a slot returns, observe both resets and any unwanted bytes.
function limited(mode: "refuse" | "queue" | "open") {
const slot = Promise.withResolvers<WebTransportBidirectionalStream>();
const slot = Promise.withResolvers<void>();
const created = Promise.withResolvers<void>();
const aborted = Promise.withResolvers<unknown>();
const cancelled = Promise.withResolvers<unknown>();
const written: Uint8Array[] = [];
const options: unknown[] = [];
const stream = {
// Each create gets its own stream: a fetch opens its TRACK stream alongside.
const stream = () => ({
readable: new ReadableStream<Uint8Array>({ cancel: cancelled.resolve }),
writable: new WritableStream<Uint8Array>({
abort: aborted.resolve,
write: (bytes) => {
written.push(new Uint8Array(bytes));
},
}),
};
});
const quic = {
closed: new Promise(() => {}),
createBidirectionalStream: (opts: unknown) => {
options.push(opts);
created.resolve();
if (mode === "refuse") return Promise.reject(new DOMException("stream limit reached", "NetworkError"));
return mode === "open" ? Promise.resolve(stream) : slot.promise;
return mode === "open" ? Promise.resolve(stream()) : slot.promise.then(stream);
},
} as unknown as WebTransport;
return {
Expand All @@ -43,7 +44,7 @@ function limited(mode: "refuse" | "queue" | "open") {
cancelled: cancelled.promise,
written,
options,
release: () => slot.resolve(stream),
release: () => slot.resolve(),
};
}

Expand Down Expand Up @@ -125,7 +126,11 @@ for (const operation of ["info", "fetch"] as const) {
await transport.cancelled;
expect(transport.written).toEqual([]);
}
expect(transport.options).toEqual([{ sendOrder: undefined, waitUntilAvailable: false }]);
// A fetch opens its FETCH stream alongside the TRACK stream, under the same deadline.
const opened = transport.options as { sendOrder?: number; waitUntilAvailable: boolean }[];
expect(opened).toHaveLength(operation === "info" ? 1 : 2);
expect(opened[0]).toEqual({ sendOrder: undefined, waitUntilAvailable: false });
for (const open of opened) expect(open.waitUntilAvailable).toBe(false);
} finally {
subscriber.close();
installed.close();
Expand Down
37 changes: 37 additions & 0 deletions js/net/src/resume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,43 @@ test("aborting a fetch stops it while its route's properties are still being che
b.close();
});

test("a fetch asks its route alongside that route's properties", async () => {
const front = new Front();
const a = new BroadcastProducer();
// a serves tracks on demand but never answers, so its properties never arrive.
void wireOf(a).requested();
const { route, asked } = counted(a);
front.serve(route);
const fetching = front.consumer.track("seq").fetchGroup(0);
void fetching.catch(() => {});
await settle();
expect(asked).toEqual([0]);

front.consumer.close();
a.close();
});

test("a fetch never hands out a group from a route refused for its properties", async () => {
const front = new Front();
const a = new BroadcastProducer();
const b = new BroadcastProducer();
a.createTrack("seq", { priority: 1 });
const tb = b.createTrack("seq", { priority: 2 });
const group = tb.createGroup(0);
group.writeFrame({ payload: enc.encode("b") });
group.finish();
front.serve(a.consume());
await front.consumer.track("seq").info();
const { route, asked } = counted(b);
front.serve(route);
await expect(front.consumer.track("seq").fetchGroup(0)).rejects.toThrow("changed its properties");
expect(asked).toEqual([0]);

front.consumer.close();
a.close();
b.close();
});

test("a refused route is not served again by a later compatible source", async () => {
const front = new Front();
const a = new BroadcastProducer();
Expand Down
23 changes: 20 additions & 3 deletions js/net/src/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1935,10 +1935,27 @@ export class Front {
const retired = this.#retire.signal;
const signal = options?.signal ? AbortSignal.any([options.signal, retired]) : retired;
try {
await until(this.#verify(name, source), signal);
// Asked alongside the check that the source claims the track's recorded properties,
// rather than a round trip after it: only handing the group out waits for the check.
const fetchGroup = source.wire.fetchGroup;
if (!fetchGroup) throw new Error("fetch group is not supported by this session's version");
const group = await fetchGroup(name, sequence, { ...options, signal });
const ahead = new AbortController();
const fetching = fetchGroup?.(name, sequence, {
...options,
signal: AbortSignal.any([signal, ahead.signal]),
});
fetching?.catch(() => {});
try {
await until(this.#verify(name, source), signal);
} catch (err) {
ahead.abort(err);
void fetching?.then(
(group) => group.close(),
() => {},
);
throw err;
}
if (!fetching) throw new Error("fetch group is not supported by this session's version");
const group = await fetching;
// A copy that already failed continues nothing: that is the route failing again.
const failed = group.closed.peek();
if (failed instanceof Error) {
Expand Down
9 changes: 5 additions & 4 deletions js/net/src/util/deadline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ function sessionFailure(err: unknown): boolean {

/** One fatal deadline for a request, including every stream its setup creates. @internal */
export class Deadline {
#opening = false;
/** Creates still waiting on credit: a fetch opens its TRACK stream alongside. */
#opening = 0;
#streams: Stream[] = [];
#failure?: Error;

/** Record a create so deadline failures reset its stream even if it opens late. */
async open(opening: Stream | Promise<Stream>): Promise<Stream> {
this.#opening = true;
this.#opening++;
let stream: Stream;
try {
stream = await opening;
} catch (cause) {
if (sessionFailure(cause)) throw cause;
throw new NoStreamCredit({ cause });
} finally {
this.#opening = false;
this.#opening--;
}
if (this.#failure) {
stream.abort(this.#failure);
Expand Down Expand Up @@ -60,7 +61,7 @@ export class Deadline {
} catch (cause) {
const failure =
cause instanceof TimeoutError
? this.#opening
? this.#opening > 0
? new NoStreamCredit({ cause })
: controlTimeout(cause)
: error(cause);
Expand Down
1 change: 0 additions & 1 deletion quest/m2/pipeline-requests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ and two relay hops, before and after the line.
## Required

- [SUBSCRIBE goes out with TRACK](/quest/m2/pipeline-requests/subscribe.md) - subscribers open TRACK and SUBSCRIBE together in Rust and JS
- [Pipelined first FETCH](/quest/m2/pipeline-requests/fetch.md) - a fetch-only reader's first FETCH goes out with the info request, on lite and moq-transport
61 changes: 0 additions & 61 deletions quest/m2/pipeline-requests/fetch.md

This file was deleted.

1 change: 0 additions & 1 deletion quest/m2/pipeline-requests/subscribe.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,4 @@ blocks the Related quests below; whichever lands second rebases.

## Related

- [Pipelined first FETCH](/quest/m2/pipeline-requests/fetch.md) - the same change for fetch-only readers
- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - replaces the SUBSCRIBE floor with ranges
Loading