diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 04392b27e70..f38d06dd5b9 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"; @@ -1881,13 +1881,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 0dbfefe4f72..9abf37f99b3 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 efe2fe9198d..a7b0a8103a0 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -116,16 +116,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. */ @@ -531,11 +524,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 }); @@ -833,9 +822,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. @@ -851,31 +840,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; } @@ -1209,11 +1185,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 @@ -1241,11 +1213,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 d850520405a..dca96941585 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1522,8 +1522,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 2a07d272e32..b1e6cf03ca7 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 { Elapsed, Milli, Nano, timeout } from "@moq/clock"; +import { SessionCode } from "../error.ts"; import { randomHop } from "../hop.ts"; import { createMockTransportPair } from "../mock.ts"; import { Producer as OriginProducer } from "../origin.ts"; @@ -149,6 +150,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 5e14312fd7b..cc2d21028d9 100644 --- a/js/net/src/util/deadline.ts +++ b/js/net/src/util/deadline.ts @@ -16,15 +16,28 @@ 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 { /** Creates still waiting on credit: a fetch opens its TRACK stream alongside. */ #opening = 0; - #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++; let stream: Stream; try { @@ -35,18 +48,19 @@ export class Deadline { } finally { this.#opening--; } + 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(); } @@ -68,7 +82,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; } diff --git a/quest/m2/README.md b/quest/m2/README.md index 6df26604fe1..4db684c3c1a 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -197,7 +197,6 @@ lite-07; bitrate enforcement is not part of this release. - [Auth serve bound address](/quest/m2/auth-serve-bound-addr.md) - `moq auth serve` logs the port it actually bound - [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 - [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.