From 3230d2113c02c3a0ae3451db57b1e4d62da7e600 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:11:54 -0700 Subject: [PATCH 1/3] chore(quest): claim quest/m2/js-request-stream.md Quest-Claim: 768d992a7bb5df38 From b2c2e82a806058bce4d74e0ab6ad8e443a149676 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:16:57 -0700 Subject: [PATCH 2/3] fix(js/net): end IETF requests once through RequestStream, and close the session on data after TRACK A request's Deadline now resets the RequestStream that owns its stream rather than the raw stream, so a timeout ends the request through the same closer as abandonment and errors, and the caller's later cleanup is a no-op. SUBSCRIBE's cleanup drops the sent/torn guards left from the draft 14-16 UNSUBSCRIBE path. Bytes after a lite TRACK (0x6) request now close the session with PROTOCOL_VIOLATION, as in Rust. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/publisher.ts | 7 ++-- js/net/src/ietf/request_stream.test.ts | 36 ++++++++++++++++- js/net/src/ietf/request_stream.ts | 9 +++++ js/net/src/ietf/subscriber.ts | 54 ++++++-------------------- js/net/src/lite/publisher.ts | 7 +++- js/net/src/lite/track_stream.test.ts | 26 ++++++++++++- js/net/src/util/deadline.ts | 32 ++++++++++----- 7 files changed, 111 insertions(+), 60 deletions(-) diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index fe8eaa09b9a..0efcc5c76ac 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -32,7 +32,7 @@ import { Timescale } from "../time.ts"; import type { Groups, Info, Position as TrackPosition, Subscriber as TrackSubscriber } from "../track.ts"; import { untilAborted } from "../util/abort.ts"; import { nextChange } from "../util/change.ts"; -import { Deadline, REQUEST_TIMEOUT_MS, requestOptions } from "../util/deadline.ts"; +import { Deadline, REQUEST_TIMEOUT_MS } from "../util/deadline.ts"; import { untilGivenUp } from "../util/given_up.ts"; import { TimeoutError, withTimeout } from "../util/timeout.ts"; import * as Varint from "../varint.ts"; @@ -1970,13 +1970,12 @@ export class Publisher { const deadline = new Deadline(); const answer = await deadline.wait( (async (): Promise => { - const stream = await deadline.open(this.#session.openBi(requestOptions)); - const opened = new RequestStream(stream, this.#session, this.#quic); + const opened = await RequestStream.open(this.#session, deadline, this.#quic); request = opened; const version = this.#session.version; const token = this.#auth?.carried(auth); const msg = new PublishNamespace({ requestId, trackNamespace: path, params, token }); - await deadline.send(stream, async () => { + await deadline.send(opened, async () => { await Message.write(opened.writer, PublishNamespace.id, msg, version); }); diff --git a/js/net/src/ietf/request_stream.test.ts b/js/net/src/ietf/request_stream.test.ts index c334d0917fb..d8fe680e638 100644 --- a/js/net/src/ietf/request_stream.test.ts +++ b/js/net/src/ietf/request_stream.test.ts @@ -1,6 +1,8 @@ -import { expect, test } from "bun:test"; -import { ProtocolViolation, SessionCode } from "../error.ts"; +import { expect, spyOn, test } from "bun:test"; +import { Manual, Milli, Nano } from "@moq/clock"; +import { ProtocolViolation, SessionCode, StreamCode, StreamError } from "../error.ts"; import { Reader, Stream } from "../stream.ts"; +import { Deadline, REQUEST_TIMEOUT_MS } from "../util/deadline.ts"; import * as Varint from "../varint.ts"; import { RequestError } from "./request.ts"; import { namespaceUpdate, nextMessage, RequestStream, refuse } from "./request_stream.ts"; @@ -164,3 +166,33 @@ test("a refusal carries its reason", async () => { expect(err.reasonPhrase).toBe("no"); } }); + +test("a request's deadline ends it through the request, so later ends do nothing", async () => { + const clock = new Manual(); + const installed = clock.install(); + try { + const stream = new Stream({ + readable: new ReadableStream(), + writable: new WritableStream(), + version: Version.DRAFT_18, + }); + const aborts = spyOn(stream, "abort"); + const session = { version: Version.DRAFT_18, client: true, openBi: () => stream } as unknown as Session; + const deadline = new Deadline(); + const request = await RequestStream.open(session, deadline); + const answer = deadline.wait(new Promise(() => {})).catch((e: unknown) => e); + await clock.advance(Nano.fromMilli(Milli(REQUEST_TIMEOUT_MS))); + const err = await answer; + expect(err).toBeInstanceOf(StreamError); + expect((err as StreamError).code).toBe(StreamCode.ControlTimeout); + await request.ended; + + // The caller's own cleanup after the deadline. + request.abort(new Error("abandoned")); + request.close(); + expect(aborts).toHaveBeenCalledTimes(1); + expect(aborts.mock.calls[0]?.[0]).toBe(err as StreamError); + } finally { + installed.close(); + } +}); diff --git a/js/net/src/ietf/request_stream.ts b/js/net/src/ietf/request_stream.ts index 0732883c18b..c1472a46a97 100644 --- a/js/net/src/ietf/request_stream.ts +++ b/js/net/src/ietf/request_stream.ts @@ -1,6 +1,7 @@ import { error, ProtocolViolation, StreamCode, StreamError } from "../error.ts"; import type * as Path from "../path.ts"; import { Reader, type Stream, type Writer } from "../stream.ts"; +import { type Deadline, requestOptions } from "../util/deadline.ts"; import { type RequestCondition, type RequestKind, toRequestCode } from "./error.ts"; import { GoAway } from "./goaway.ts"; import * as Message from "./message.ts"; @@ -165,6 +166,14 @@ export class RequestStream { }); } + /** + * Open a request's stream under `deadline`, whose failure then resets the request through + * {@link abort} like any other end, so the request ends once whatever ends it. + */ + static open(session: Session, deadline: Deadline, quic?: WebTransport): Promise { + return deadline.open(session.openBi(requestOptions), (stream) => new RequestStream(stream, session, quic)); + } + /** * Settles once the peer ends the request, by FIN where that cancels or by resetting either * direction, or once it closes here or over a violation. Never rejects. diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 02e467391d2..95c4730b34c 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -115,16 +115,9 @@ type SubscribeSetupState = { * gone quiet, and the cleanup that fired first saw no stream to tear down. */ cancelled?: boolean; - /** - * Whether the SUBSCRIBE reached the wire. From that moment the publisher may be serving, - * even before it answers, so abandoning owes it a cancellation regardless of whether we - * ever saw the SUBSCRIBE_OK. - */ - sent?: boolean; /** * Whether the publisher answered with an error. It has torn the request down already, so - * there is nothing left to cancel and naming the dead request id at it risks being read - * as a protocol violation. + * there is nothing left to cancel and ours ends cleanly. */ rejected?: boolean; /** Hands a draft-22 fill stream that overtook SUBSCRIBE_OK the subscription it heads, or nothing. */ @@ -548,11 +541,7 @@ export class Subscriber { try { const deadline = new Deadline(); - const request = new RequestStream( - await deadline.wait(deadline.open(this.#session.openBi(requestOptions))), - this.#session, - this.#quic, - ); + const request = await deadline.wait(RequestStream.open(this.#session, deadline, this.#quic)); const run = async () => { const msg = new SubscribeNamespace({ namespace: prefix, requestId, hidden }); @@ -862,9 +851,9 @@ export class Subscriber { ); // Runs now for whatever is already open, and again if `setup` settles late. // Retirement is repeated because a late SUBSCRIBE_OK can register an alias after - // the first pass; the stream is torn down once. - let torn = false; - const cleanup = async (afterSetup: boolean) => { + // the first pass. The request ends once: a deadline failure already reset it through + // the same stream, so a second end here does nothing. + const cleanup = () => { state.cancelled = true; // A fill stream waiting on SUBSCRIBE_OK gets nothing, and a head already started goes. @@ -880,31 +869,18 @@ export class Subscriber { this.#timescales.delete(state.registeredAlias); } - if (!state.stream || torn) return; - - // A SUBSCRIBE still being written must not be torn down from here. Aborting a - // Web Stream waits for the in-flight write, so the request can still reach the - // peer while the abort destroys the stream we would have cancelled on. Setup - // unwinds on the cancelled flag once that write lands, and its pass below has - // the finished picture. - if (!state.sent && !afterSetup) return; - torn = true; - // A refusal already ended the request, so end ours cleanly: a reset would tell the // publisher we failed. - if (state.rejected) state.stream.close(); - else state.stream.abort(e); + if (state.rejected) state.stream?.close(); + else state.stream?.abort(e); }; // Tear down what is already open rather than waiting on `setup`. A peer that // opened the stream and then went quiet never settles it, and deferring would // leave the request outstanding for the life of the session -- which is the // timeout case, not a rare one. - void cleanup(false); - setup.then( - () => cleanup(true), - () => cleanup(true), - ); + cleanup(); + setup.then(cleanup, cleanup); return; } @@ -1239,11 +1215,7 @@ export class Subscriber { }> { const version = this.#session.version; - state.stream = new RequestStream( - await deadline.open(this.#session.openBi(requestOptions)), - this.#session, - this.#quic, - ); + state.stream = await RequestStream.open(this.#session, deadline, this.#quic); // The timeout can fire while the open is still in flight, in which case cleanup ran // with nothing to release. Bail now that the stream is recorded, so settling this @@ -1272,11 +1244,9 @@ export class Subscriber { state.fill = accepted.resolve; } await Message.write(state.stream.writer, Subscribe.id, msg, version); - state.sent = true; - // The caller may have given up while that write was in flight. It deliberately left - // the stream alone, so unwind here and let its cleanup send the cancellation now that - // the SUBSCRIBE has actually reached the peer. + // The caller may have given up while that write was in flight, resetting the request + // under it. if (state.cancelled) throw new Error("subscribe cancelled while it was being sent"); console.debug(`subscribe written: id=${requestId} broadcast=${broadcast} track=${request.name}`); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 88b7a1a6df5..5e9163111b6 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1499,8 +1499,11 @@ export class Publisher { void stream.reader.done().then( (fin) => { hold.abort(); - // TRACK is the requester's only message. - if (!fin) stream.abort(new ProtocolViolation("data after TRACK")); + if (fin) return; + // TRACK is the requester's only message, so more breaks the session, as in Rust. + const violation = new ProtocolViolation("data after TRACK"); + closeViolation(this.#quic, violation); + stream.abort(violation); }, () => hold.abort(), ); diff --git a/js/net/src/lite/track_stream.test.ts b/js/net/src/lite/track_stream.test.ts index 1fc625b7ef8..523a40ce191 100644 --- a/js/net/src/lite/track_stream.test.ts +++ b/js/net/src/lite/track_stream.test.ts @@ -1,8 +1,9 @@ // An open Track Stream is interest in the track: the subscriber keeps it open until its // SUBSCRIBE is answered, and the publisher keeps the track wanted until the subscriber closes it. -import { describe, expect, test } from "bun:test"; +import { describe, expect, spyOn, test } from "bun:test"; import { Milli } from "@moq/clock"; +import { SessionCode } from "../error.ts"; import { randomHop } from "../hop.ts"; import { Version as IetfVersion } from "../ietf/version.ts"; import { createMockTransportPair } from "../mock.ts"; @@ -150,6 +151,29 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver }, ); + test("data after a TRACK request closes the session", async () => { + const pair = createMockTransportPair(ALPN_05); + const closing = spyOn(pair.server, "close"); + const origin = new OriginProducer(); + const publisher = new Publisher(pair.server, version, randomHop(), origin.consume()); + const broadcastPublication = origin.createBroadcast(Path.from("room")); + const broadcast = broadcastPublication.broadcast; + broadcastPublication.announce(); + + const client = await Stream.open(pair.client, { version }); + const server = await Stream.accept(pair.server, version); + if (!server) throw new Error("the publisher never accepted the TRACK stream"); + void publisher.runTrackInfo(new TrackMessage(Path.from("room"), "video"), server); + + await client.writer.u53(0); + await expect(client.reader.done()).rejects.toThrow(); + expect(closing).toHaveBeenCalledWith(expect.objectContaining({ closeCode: SessionCode.ProtocolViolation })); + + publisher.close(); + broadcast.close(); + origin.close(); + }); + test.each(["FIN", "reset"] as const)( "a requester's %s while TRACK_INFO is blocked lets the track go", async (close) => { diff --git a/js/net/src/util/deadline.ts b/js/net/src/util/deadline.ts index 68c5f37373a..f4101f1867d 100644 --- a/js/net/src/util/deadline.ts +++ b/js/net/src/util/deadline.ts @@ -16,14 +16,27 @@ function sessionFailure(err: unknown): boolean { return source === "session" || name === "InvalidStateError"; } +/** What a deadline failure resets: a stream, or whatever owns one. @internal */ +export interface Abortable { + abort(err: Error): void; +} + /** One fatal deadline for a request, including every stream its setup creates. @internal */ export class Deadline { #opening = false; - #streams: Stream[] = []; + #owners: Abortable[] = []; #failure?: Error; - /** Record a create so deadline failures reset its stream even if it opens late. */ - async open(opening: Stream | Promise): Promise { + /** + * Record a create so deadline failures reset its stream even if it opens late. `own` wraps + * the opened stream in its owner, which the failure then resets instead. + */ + open(opening: Stream | Promise): Promise; + open(opening: Stream | Promise, own: (stream: Stream) => T): Promise; + async open( + opening: Stream | Promise, + own: (stream: Stream) => Abortable = (stream) => stream, + ): Promise { this.#opening = true; let stream: Stream; try { @@ -34,18 +47,19 @@ export class Deadline { } finally { this.#opening = false; } + const owner = own(stream); if (this.#failure) { - stream.abort(this.#failure); + owner.abort(this.#failure); throw this.#failure; } - this.#streams.push(stream); - return stream; + this.#owners.push(owner); + return owner; } /** Write the request on an opened stream, which a deadline failure after resets. */ - async send(stream: Stream, write: () => Promise): Promise { + async send(stream: Abortable, write: () => Promise): Promise { if (this.#failure) throw this.#failure; - if (!this.#streams.includes(stream)) throw new Error("request sent on a stream the deadline did not open"); + if (!this.#owners.includes(stream)) throw new Error("request sent on a stream the deadline did not open"); await write(); } @@ -67,7 +81,7 @@ export class Deadline { this.#failure = failure; // Other failures keep the caller's cancellation ordering. if (cause instanceof TimeoutError || cause instanceof NoStreamCredit) { - for (const stream of this.#streams) stream.abort(failure); + for (const owner of this.#owners) owner.abort(failure); } throw failure; } From 10eba9335dd05255a390e79d64ce2d44629949a4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:22:54 -0700 Subject: [PATCH 3/3] quest: finish m2/js-request-stream Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 1 - quest/m2/js-request-stream.md | 21 --------------------- 2 files changed, 22 deletions(-) delete mode 100644 quest/m2/js-request-stream.md diff --git a/quest/m2/README.md b/quest/m2/README.md index 4f4f2452b0a..94344ff2650 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -230,7 +230,6 @@ lite-07; bitrate enforcement is not part of this release. - [H.264 parsing with maintained crates](/quest/m2/h264-reader.md) - moq-mux parses SPS VUI with h264-reader and shares one bit reader across codec config parsers - [avcC and hvcC through mp4-atom](/quest/m2/mp4-atom-config.md) - moq-mux stops hand-writing the decoder config records its mp4-atom dependency already parses - [Relay metrics through prometheus-client](/quest/m2/relay-prometheus-client.md) - `/metrics` renders through the maintained client with the same series -- [JS IETF request streams](/quest/m2/js-request-stream.md) - `RequestStream` owns deadlines and cancellation, and TRACK trailing bytes close the session like Rust - [moq-transport finish interop](/quest/m2/ietf-finish.md) - the interop `finish` case runs on every even moq-transport draft instead of being skipped - [Join after a quick finish](/quest/m2/ietf-join-after-finish.md) - a pre-draft-22 joining FETCH that arrives after its subscription finished is still answered, within a bound the peer already negotiates - [JS joining FETCH](/quest/m2/ietf-js-joining-fetch.md) - the JS publisher answers a joining FETCH that arrives after a quick finish, mirroring the Rust bound diff --git a/quest/m2/js-request-stream.md b/quest/m2/js-request-stream.md deleted file mode 100644 index 7ffeb84c579..00000000000 --- a/quest/m2/js-request-stream.md +++ /dev/null @@ -1,21 +0,0 @@ -# [S] JS IETF requests own their stream - -## Goal - -js/net's IETF request cleanup lives in one place: `RequestStream` owns the -request's deadline and its cancellation, and a malformed TRACK stream ends -the session as it does in Rust. - -## Plan - -- **One owner.** `Deadline` and SUBSCRIBE's `#cancelSubscribe` each clean up - part of a request. - [moq-dev/moq#6133](https://github.com/moq-dev/moq/pull/6133) kept them - apart because routing only SUBSCRIBE's timeout through `Deadline` would - send UNSUBSCRIBE twice. Fold both into `RequestStream`, so each request - sends its withdrawal once on timeout, abandonment, or error. -- **TRACK trailing bytes.** JS resets a TRACK (0x6) stream that carries - bytes after its request, while Rust closes the session - ([moq-dev/moq#6158](https://github.com/moq-dev/moq/pull/6158)). Match Rust. - -Public API: none (`Deadline` and `RequestStream` are internal). Wire: none.