From a5b1aac3b95ec1824b31904218daef6d40c5eebd Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 16 Oct 2026 10:25:45 -0700 Subject: [PATCH 1/5] chore: claim JS request deadline quest Co-Authored-By: GPT-6 From 7e24af214387ef3554fb2da79ec48e7aeb2aea1b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 16 Oct 2026 10:35:55 -0700 Subject: [PATCH 2/5] fix(net): bound requests with one fatal deadline Co-Authored-By: GPT-6 --- js/net/src/error.ts | 12 ++ js/net/src/errors.ts | 1 + js/net/src/ietf/adapter.ts | 14 +- js/net/src/ietf/publisher.test.ts | 130 +------------- js/net/src/ietf/publisher.ts | 43 +++-- js/net/src/ietf/subscriber.test.ts | 2 +- js/net/src/ietf/subscriber.ts | 47 +++-- js/net/src/index.ts | 2 +- js/net/src/lite/subscriber.test.ts | 37 +++- js/net/src/lite/subscriber.ts | 75 ++++---- js/net/src/request-deadline.test.ts | 263 ++++++++++++++++++++++++++++ js/net/src/stream.ts | 15 +- js/net/src/util/request.ts | 58 ++++++ quest/m1/README.md | 1 - quest/m1/js-request-deadline.md | 73 -------- quest/m1/qmux-no-wait.md | 4 +- quest/m1/rs-request-credit.md | 5 +- 17 files changed, 473 insertions(+), 309 deletions(-) create mode 100644 js/net/src/request-deadline.test.ts create mode 100644 js/net/src/util/request.ts delete mode 100644 quest/m1/js-request-deadline.md diff --git a/js/net/src/error.ts b/js/net/src/error.ts index d0b1dc5acd5..41dba873aba 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -378,6 +378,18 @@ function localStreamCode(err: unknown): StreamCode { return StreamCode.Internal; } +/** + * The transport refused a request stream or failed to grant stream credit before its deadline. + * + * This local failure is terminal and has no wire code. The request is not retried. + */ +export class NoStreamCredit extends Error { + constructor(options?: ErrorOptions) { + super("no stream credit", options); + this.name = "NoStreamCredit"; + } +} + /** * The {@link StreamCode.ControlTimeout} error for a control request the peer never answered. * diff --git a/js/net/src/errors.ts b/js/net/src/errors.ts index 09ffa12816c..ec33cc0ab88 100644 --- a/js/net/src/errors.ts +++ b/js/net/src/errors.ts @@ -6,6 +6,7 @@ export { FrameTooLarge, GroupTooLarge, + NoStreamCredit, NotFound, ProtocolViolation, RefusedRedirect, diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index 5e25fa584f7..50323425b0d 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -2,7 +2,7 @@ import { Once } from "@moq/signals"; import { Mutex } from "async-mutex"; import type { Drain } from "../connection/goaway.ts"; import { error, ProtocolViolation, SessionCode, SessionError } from "../error.ts"; -import { Reader, Stream, type Writer } from "../stream.ts"; +import { type OpenOptions, Reader, Stream, type Writer } from "../stream.ts"; import { untilAborted } from "../util/abort.ts"; import * as Varint from "../varint.ts"; import { GoAway } from "./goaway.ts"; @@ -26,8 +26,8 @@ export class RequestWindowError extends Error { * Implemented by both ControlStreamAdapter (v14-v16) and NativeSession (v17). */ export interface Session { - openBi(): Stream | Promise; - openNativeBi?(): Promise; + openBi(options?: Partial): Stream | Promise; + openNativeBi?(options?: Partial): Promise; acceptBi(): Promise; nextRequestId(signal?: AbortSignal): Promise; /** Spend a request ID the peer used without holding a request open, closing the session if it cannot. */ @@ -69,8 +69,8 @@ export class NativeSession implements Session { } } - async openBi(): Promise { - return Stream.open(this.#quic, { version: this.version }); + async openBi(options?: Partial): Promise { + return Stream.open(this.#quic, { ...options, version: this.version }); } async acceptBi(): Promise { @@ -406,8 +406,8 @@ export class ControlStreamAdapter implements Session { /** * Open a real WebTransport bidi stream (for v16 SubscribeNamespace). */ - async openNativeBi(): Promise { - return Stream.open(this.#quic, { version: this.version }); + async openNativeBi(options?: Partial): Promise { + return Stream.open(this.#quic, { ...options, version: this.version }); } /** diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 11c639c3162..f6e8643b553 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -783,134 +783,6 @@ test("a declined advertisement is retried on the next change", async () => { origin.close(); }); -/** - * A peer out of stream credit rejects the open. That has to cost the namespace a turn, - * not the session its discovery: the announce loop is never restarted, so unwinding it - * would lose every future publish too. - */ -test("a failed stream open does not kill the announce loop", async () => { - const pair = createMockTransportPair(ALPN.DRAFT_19); - const inner = new NativeSession(pair.server, VERSION, true); - - let failures = 1; - const session: Session = { - version: inner.version, - client: inner.client, - acceptBi: () => inner.acceptBi(), - nextRequestId: () => inner.nextRequestId(), - spendRequest: (id) => inner.spendRequest(id), - close: () => inner.close(), - openBi: () => { - if (failures-- > 0) throw new Error("no stream credit"); - return inner.openBi(); - }, - }; - - const { pub, origin } = publisher(pair.server, { session }); - publish(origin, Path.from("first")); - - void pub.runPublishNamespaces(); - await new Promise((resolve) => setTimeout(resolve, SETTLE)); - - // The refused open cost "first" its turn; the next change has to bring it back along - // with the newcomer. - publish(origin, Path.from("second")); - - const seen = new Set(); - for (let i = 0; i < 2; i++) { - const stream = await nextStream(pair.client); - if (!stream) break; - seen.add(await readPublishNamespace(stream)); - await acceptPublishNamespace(stream); - } - - expect(seen).toContain(Path.from("first")); - expect(seen).toContain(Path.from("second")); - - origin.close(); -}); - -/** - * Capacity coming back raises no signal of its own: no broadcast is published, closed, or - * changed. The loop has to come back and ask again on its own, or a namespace refused - * once stays undiscoverable for the session. - */ -test("a namespace refused once is retried without anything else changing", async () => { - const pair = createMockTransportPair(ALPN.DRAFT_19); - const inner = new NativeSession(pair.server, VERSION, true); - - let failures = 1; - const session: Session = { - version: inner.version, - client: inner.client, - acceptBi: () => inner.acceptBi(), - nextRequestId: () => inner.nextRequestId(), - spendRequest: (id) => inner.spendRequest(id), - close: () => inner.close(), - openBi: () => { - if (failures-- > 0) throw new Error("no stream credit"); - return inner.openBi(); - }, - }; - - const { pub, origin } = publisher(pair.server, { session }); - publish(origin, Path.from("lonely")); - - void pub.runPublishNamespaces(); - - // Nothing else happens: no second publish, no close. Only the retry can save it. - const stream = await nextStream(pair.client); - if (!stream) throw new Error("the refused namespace was never retried"); - expect(await readPublishNamespace(stream)).toBe(Path.from("lonely")); - await acceptPublishNamespace(stream); - - origin.close(); -}); - -/** - * The solicited legacy path advertises with PUBLISH_NAMESPACE requests too, so a declined - * one needs the same retry the unsolicited loop has. Without it, a namespace refused once - * stays undiscoverable for the life of the subscription, since the peer starting to - * answer raises no signal the loop is watching. - */ -test("a solicited legacy advertisement refused once is retried", async () => { - const pair = createMockTransportPair(ALPN.DRAFT_19); - const inner = new NativeSession(pair.server, Version.DRAFT_15, true); - - let failures = 1; - const session: Session = { - version: inner.version, - client: inner.client, - acceptBi: () => inner.acceptBi(), - nextRequestId: () => inner.nextRequestId(), - spendRequest: (id) => inner.spendRequest(id), - close: () => inner.close(), - openBi: () => { - if (failures-- > 0) throw new Error("no stream credit"); - return inner.openBi(); - }, - }; - - // The peer declared that advertisements to it must be solicited, so this is the loop - // that answers its SUBSCRIBE_NAMESPACE. - const { pub, origin } = publisher(pair.server, { requiresSolicitation: true, session }); - publish(origin, Path.from("lonely")); - - const subscription = await Stream.open(pair.client, { version: Version.DRAFT_15 }); - const accepted = await Stream.accept(pair.server, Version.DRAFT_15); - if (!accepted) throw new Error("the subscription stream was never accepted"); - void pub.runSubscribeNamespace(new SubscribeNamespace({ requestId: 0n, namespace: Path.empty() }), accepted); - - // Nothing else happens: no second publish, no close. Only the retry can save it. - const stream = await nextStream(pair.client); - if (!stream) throw new Error("the refused namespace was never retried"); - expect(await readPublishNamespace(stream)).toBe(Path.from("lonely")); - await acceptPublishNamespace(stream); - - subscription.close(); - origin.close(); -}); - /** * A SUBSCRIBE_NAMESPACE below an advertised route still hears that route: it serves the * requested prefix, so it lands as the empty suffix, then paths beneath the prefix follow @@ -1675,7 +1547,7 @@ test("an unanswered update drops the request and re-offers the namespace fresh", await readUpdate(stream); const reset = watch(stream.reader.u53()); - await advance(4_900); + await advance(9_900); expect(reset.state).toBe("pending"); expect(streams).toHaveLength(0); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 63652590f6d..9214f528b09 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -8,6 +8,7 @@ import * as DatagramStream from "../datagram_stream.ts"; import { controlTimeout, error, + NoStreamCredit, NotFound, ProtocolViolation, reason, @@ -26,6 +27,7 @@ import { Timescale } from "../time.ts"; import type { Location as TrackLocation, Subscriber as TrackSubscriber } from "../track.ts"; import { untilAborted } from "../util/abort.ts"; import { LIMITS, type Limits, Slots } from "../util/limits.ts"; +import { REQUEST_TIMEOUT_MS, Request, requestOptions } from "../util/request.ts"; import { TimeoutError, withTimeout } from "../util/timeout.ts"; import * as Varint from "../varint.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; @@ -114,11 +116,11 @@ function sameHops(a: Hop[], b: Hop[]): boolean { } /** - * How long one advertisement may take to be answered. Matches the Rust publisher, and the - * peer accepting the stream is only half the exchange: one it never answers on holds the + * How long one advertisement may take to be answered. Accepting the stream is only half + * the exchange: one it never answers on holds the * loop just as effectively as one it never grants. */ -const ADVERTISE_TIMEOUT_MS = 5000; +const ADVERTISE_TIMEOUT_MS = REQUEST_TIMEOUT_MS; const ADVERTISE_TIMEOUT = Nano.fromMilli(Milli(ADVERTISE_TIMEOUT_MS)); /** Sleep `delay`, jittered, so a relay's namespaces don't all retry on the same tick. */ @@ -127,12 +129,12 @@ function retryAfter(delay: number): Promise { } /** - * What a refusal said about re-offering the namespace. + * What prevents re-offering the namespace. * * A peer answers a request it declines with a retry interval, and ignoring it is how a * permanent refusal (unauthorized, uninterested) turns into a request every few seconds - * for the life of the session. `"never"` is an interval of 0; a number is the epoch - * milliseconds before which we must not come back. + * for the life of the session. `"never"` is an interval of 0 or a terminal local failure; + * a number is the epoch milliseconds before which we must not come back. */ type Refused = "never" | number; @@ -1493,9 +1495,8 @@ export class Publisher { ns.active = held; - // Whatever we wanted up and could not get up. Stream credit freeing, a transient - // failure clearing, or the peer starting to answer raises no signal of its own, so - // the only way back is to ask again on a timer. + // Keep the backoff only for a peer refusal that permits another offer. Local + // credit failures and unanswered requests are terminal for this broadcast instance. return [...updated].some( ([key, snap]) => change(this.#advert, held.get(key), snap) !== "same" && this.#pending(key, ns.refused), ); @@ -1519,7 +1520,7 @@ export class Publisher { * Distinct from {@link offerable}, and the difference is what arms the retry: a * namespace waiting out a minimum is not offerable yet but is still pending, and * gating the timer on offerable instead would disarm it for exactly the wait it is - * supposed to be counting. Only a refusal that forbids retrying ends it. + * supposed to be counting. A terminal failure or a refusal that forbids retrying ends it. */ #pending(key: Path.Valid, refused: Map): boolean { return refused.get(key) !== "never"; @@ -1556,18 +1557,14 @@ export class Publisher { // Opening is inside the try because it fails like any other advertisement does: // a peer out of stream credit rejects here, and letting that escape would unwind // the announce loop and take discovery down for the whole session, rather than - // costing this one namespace a turn. + // leaving this one namespace unadvertised. let request: Stream | undefined; try { - request = await this.#session.openBi(); - const stream = request; - - // Bounded, like the subscriber's own request: the loop is single-threaded over - // advertisements, so a peer that takes the stream and answers nothing strands - // every publish and withdrawal behind it. Timing out costs this namespace a - // turn and leaves it outstanding, which the retry above re-offers. - const answer = await withTimeout( + const deadline = new Request(); + const answer = await deadline.wait( (async (): Promise => { + request = await deadline.open(this.#session.openBi(requestOptions)); + const stream = request; await stream.writer.u53(PublishNamespace.id); const msg = new PublishNamespace({ requestId, trackNamespace: path, cluster }); await msg.encode(stream.writer, this.#session.version); @@ -1597,18 +1594,18 @@ export class Publisher { } return "held"; })(), - ADVERTISE_TIMEOUT, - `advertisement timed out after ${ADVERTISE_TIMEOUT_MS}ms waiting for the peer's answer`, ); - if (answer === "held") requests.set(path, { path, requestId, stream }); + if (answer === "held" && request) requests.set(path, { path, requestId, stream: request }); return answer; } catch (err: unknown) { // The peer never answered the advertisement: a control timeout, not late content. const e = err instanceof TimeoutError ? controlTimeout(err) : error(err); console.warn(`announce failed: broadcast=${path} error=${reason(e)}`); request?.abort(e); - return "dropped"; + return e instanceof NoStreamCredit || (e instanceof StreamError && e.code === StreamCode.ControlTimeout) + ? "never" + : "dropped"; } } diff --git a/js/net/src/ietf/subscriber.test.ts b/js/net/src/ietf/subscriber.test.ts index 908d65063ca..8d57fff13c2 100644 --- a/js/net/src/ietf/subscriber.test.ts +++ b/js/net/src/ietf/subscriber.test.ts @@ -1235,7 +1235,7 @@ test.each(["acceptance", "timeout"])("returning demand preserves pending setup u } else { jest.advanceTimersByTime(4_000); expect(await info).toBeInstanceOf(Error); - expect(((await info) as Error).message).toContain("subscribe timed out after 10000ms"); + expect(((await info) as Error).message).toContain("request timed out after 10000ms"); } expect(opening).toHaveBeenCalledTimes(1); }); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index ca4827ed8b9..87363562ecb 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -1,4 +1,4 @@ -import { Milli, Nano } from "@moq/clock"; +import { Milli } from "@moq/clock"; import { type Dispose, type GetPromise, type Getter, race, Signal } from "@moq/signals"; import * as announce from "../announced.ts"; import type { Grant } from "../auth.ts"; @@ -8,7 +8,6 @@ import { BroadcastCache } from "../consume.ts"; import * as DatagramStream from "../datagram_stream.ts"; import { closeError, - controlTimeout, error, ProtocolViolation, reason, @@ -27,7 +26,7 @@ import type { Timescale, Timestamp } from "../time.ts"; import type * as track from "../track.ts"; import { untilAborted } from "../util/abort.ts"; import { LIMITS, type Limits, SlotMap, Slots } from "../util/limits.ts"; -import { TimeoutError, withTimeout } from "../util/timeout.ts"; +import { Request, requestOptions } from "../util/request.ts"; import { overrideBroadcastWire, wireOf } from "../wire.ts"; import type { Session } from "./adapter.ts"; import { DuplicateTrackAlias, RetiredTrackAlias, TrackAliases } from "./aliases.ts"; @@ -68,11 +67,6 @@ import { import { MalformedAuthToken } from "./token.ts"; import { Version } from "./version.ts"; -// Bound on how long stream-open plus SUBSCRIBE_OK may take. Browsers cap -// concurrent QUIC streams (Chrome ~100); past the cap openBi() silently -// blocks. The timeout turns that into a clear error. -const SUBSCRIBE_OK_TIMEOUT_MS = 10_000; - // Wire ceiling (2^62-1). A draining session stamps it on every live route so any other // candidate outranks it, while the route stays selectable as the last path. Matches Rust // Cost::DRAIN: cost is the whole mechanism, not a separate state. @@ -443,10 +437,14 @@ export class Subscriber { try { // v16: use a real bidi stream (not virtual control stream) - const stream = - version === Version.DRAFT_16 && this.#session.openNativeBi - ? await this.#session.openNativeBi() - : await this.#session.openBi(); + const deadline = new Request(); + const stream = await deadline.wait( + deadline.open( + version === Version.DRAFT_16 && this.#session.openNativeBi + ? this.#session.openNativeBi(requestOptions) + : this.#session.openBi(requestOptions), + ), + ); try { // Draft-18+ uses SUBSCRIBE_NAMESPACE (0x50); earlier drafts use the @@ -671,7 +669,8 @@ export class Subscriber { // flows back via `state` so the timeout path can clean up the stream // and any registration if setup eventually finishes. const state: SubscribeSetupState = {}; - const setup = this.#openSubscribe(state, broadcast, request, subscription, requestId); + const deadline = new Request(); + const setup = this.#openSubscribe(state, broadcast, request, subscription, requestId, deadline); // The publisher can be serving before it answers, so waiting only on the response // would miss the local side going away and leave it serving a track nobody reads. @@ -694,11 +693,7 @@ export class Subscriber { const abandoned = new Error("subscribe abandoned before it was accepted"); try { // Returning demand keeps the same setup and its original deadline. - const accepted = withTimeout( - setup, - Nano.fromMilli(Milli(SUBSCRIBE_OK_TIMEOUT_MS)), - `subscribe timed out after ${SUBSCRIBE_OK_TIMEOUT_MS}ms waiting for SUBSCRIBE_OK (browser stream limit reached?)`, - ); + const accepted = deadline.wait(setup); for (;;) { const result = await untilAborted(race([accepted, waitAbandoned()]), valid); if (result !== null) { @@ -713,12 +708,7 @@ export class Subscriber { } catch (err) { // A control request that timed out is not late content, so it carries its own code. // Local abandonment must commit without yielding after the demand check. - const e = - err === abandoned - ? abandoned - : err instanceof TimeoutError - ? controlTimeout(err) - : await sessionCause(this.#quic, err); + const e = err === abandoned ? abandoned : await sessionCause(this.#quic, err); request.reject(e); console.warn( `subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`, @@ -895,10 +885,16 @@ export class Subscriber { request: track.Request, subscription: Subscription, requestId: bigint, + deadline: Request, ): Promise<{ stream: Stream; alias: bigint }> { const version = this.#session.version; - state.stream = await this.#session.openBi(); + const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16; + // Virtual streams use no transport credit. Their owner must send UNSUBSCRIBE before + // resetting them, including on a deadline, so they stay out of the real-stream guard. + state.stream = legacy + ? await this.#session.openBi() + : await deadline.open(this.#session.openBi(requestOptions)); // 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 @@ -906,7 +902,6 @@ export class Subscriber { // A legacy ID is already spent and cannot be skipped. Its virtual open is local, // so finish this request and let cleanup send UNSUBSCRIBE, even if retirement // won the await above. No response data is released from the canceled setup. - const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16; if (state.cancelled && !legacy) throw new Error("subscribe cancelled before it was sent"); await state.stream.writer.u53(Subscribe.id); diff --git a/js/net/src/index.ts b/js/net/src/index.ts index 2801de422d1..fb683346ecf 100644 --- a/js/net/src/index.ts +++ b/js/net/src/index.ts @@ -20,7 +20,7 @@ export { Connection } from "./connection/index.ts"; /** Publisher instance identities, carried in broadcast paths as UUIDv7 epochs. */ export * as Epoch from "./epoch.ts"; export { SessionCode, StreamCode } from "./error.ts"; -/** Session and stream errors, each carrying a code from its own registry. */ +/** Session, stream, and local request errors. */ export * as Error from "./errors.ts"; /** Group role handles and frame helpers. */ export * as Group from "./group.ts"; diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 24e7affa1b2..e29d0ce987a 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -1,5 +1,5 @@ import { expect, jest, spyOn, test } from "bun:test"; -import { Milli } from "@moq/clock"; +import { Manual, Milli, Nano } from "@moq/clock"; import { Signal } from "@moq/signals"; import type { Grant } from "../auth.ts"; import type { Probe as ProbeStats } from "../connection/stats.ts"; @@ -1076,7 +1076,7 @@ test("a lite subscribe whose SUBSCRIBE write stalls past the deadline closes its jest.advanceTimersByTime(SUBSCRIBE_SETUP_TIMEOUT_MS); await track.closed; - await streams[0].finished; + await streams[0].aborted; } finally { subscriber.close(); warn.mockRestore(); @@ -1742,3 +1742,36 @@ test("closing a broadcast while TRACK_INFO is pending leaves no unhandled reject subscriber.close(); } }); + +test.each(["answer", "credit"] as const)("FETCH shares the TRACK deadline while waiting for %s", async (stage) => { + const clock = new Manual(); + const restore = clock.install(); + const { quic, streams, opened } = fakeSession(stage === "credit" ? [1] : []); + const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); + let failure: unknown; + const fetch = subscriber.fetchGroup(Path.from("room"), "video", 0).catch((err: unknown) => { + failure = err; + }); + try { + await opened(0); + await streams[0].reading; + await clock.advanceAsync(Nano.fromMilli(Milli(5_000))); + await answerTrackInfo(streams[0], Version.DRAFT_07); + await opened(1); + if (stage === "answer") await streams[1].reading; + await clock.advanceAsync(Nano.fromMilli(Milli(4_999))); + expect(failure).toBeUndefined(); + await clock.advanceAsync(Nano.fromMilli(Milli(1))); + await fetch; + if (stage === "answer") expect((failure as StreamError).code).toBe(StreamCode.ControlTimeout); + else { + expect((failure as Error).name).toBe("NoStreamCredit"); + streams[1].release(); + await streams[1].aborted; + expect(streams[1].written).toEqual([]); + } + } finally { + subscriber.close(); + restore(); + } +}); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 797b686627f..935d3cf686e 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1,4 +1,4 @@ -import { Milli, Nano } from "@moq/clock"; +import { Milli } from "@moq/clock"; import { type Dispose, Effect, type GetPromise, type Getter, race, Signal } from "@moq/signals"; import * as announce from "../announced.ts"; import type { Grant } from "../auth.ts"; @@ -8,16 +8,7 @@ import type { Probe as ProbeStats } from "../connection/stats.ts"; import { BroadcastCache } from "../consume.ts"; import * as DatagramStream from "../datagram_stream.ts"; import type * as Epoch from "../epoch.ts"; -import { - controlTimeout, - error, - ProtocolViolation, - reason, - StreamCode, - StreamError, - sessionCause, - unauthorized, -} from "../error.ts"; +import { error, ProtocolViolation, reason, StreamCode, StreamError, sessionCause, unauthorized } from "../error.ts"; import * as netGroup from "../group.ts"; import { Cost, type Hop, MAX_HOPS, type Route, routesEqual, UNKNOWN_HOP } from "../hop.ts"; import { groupBounds, hiddenBelow, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts"; @@ -29,7 +20,7 @@ import * as Time from "../time.ts"; import type * as track from "../track.ts"; import { untilAborted } from "../util/abort.ts"; import { LIMITS, type Limits, SlotMap, Slots } from "../util/limits.ts"; -import { TimeoutError, withTimeout } from "../util/timeout.ts"; +import { REQUEST_TIMEOUT_MS, Request, requestOptions } from "../util/request.ts"; import { overrideBroadcastWire, wireOf } from "../wire.ts"; import { AnnounceHistory, @@ -70,11 +61,8 @@ import { waitsForSubscriberFin, } from "./version.ts"; -// Bound on how long stream-open plus the first response (SUBSCRIBE_OK on older -// drafts, or TRACK_INFO on lite-05+) may take. Browsers cap concurrent QUIC streams -// (Chrome ~100) and we open with waitUntilAvailable, so past the cap the open blocks -// until the peer frees a slot. The timeout turns a stall into a clear error. -export const SUBSCRIBE_SETUP_TIMEOUT_MS = 10_000; +// Request streams refuse exhausted peer credit, with one deadline covering create and answer. +export const SUBSCRIBE_SETUP_TIMEOUT_MS = REQUEST_TIMEOUT_MS; // Wire ceiling (2^62-1). A draining session stamps it on every live route so any other // candidate outranks it, while the route stays selectable as the last path. Matches Rust @@ -279,7 +267,10 @@ export class Subscriber { // to reset the stream, not just close our side of it. let stream: Stream; try { - stream = await Stream.open(this.#quic, { version: this.version }); + const deadline = new Request(); + stream = await deadline.wait( + deadline.open(Stream.open(this.#quic, { version: this.version, ...requestOptions })), + ); } catch (err: unknown) { announced.close(error(err)); return; @@ -702,23 +693,18 @@ export class Subscriber { }; valid?.addEventListener("abort", retire); if (valid?.aborted) retire(); - const setup = this.#openSubscribe(state, msg, request, id, timescale); + const deadline = new Request(); + const setup = this.#openSubscribe(state, msg, request, id, timescale, deadline); let opened: { stream: Stream; entry: SubscribeEntry }; try { - opened = await withTimeout( - untilAborted(setup, valid), - Nano.fromMilli(Milli(SUBSCRIBE_SETUP_TIMEOUT_MS)), - `subscribe timed out after ${SUBSCRIBE_SETUP_TIMEOUT_MS}ms waiting for the first response (browser stream limit reached?)`, - ); + opened = await deadline.wait(untilAborted(setup, valid)); console.debug(`subscribe ok: id=${id} broadcast=${broadcast} track=${request.name}`); } catch (err) { closeViolation(this.#quic, err); // The setup outlived its deadline waiting for the first response: a control // timeout, not content that arrived late. A revocation says so instead. - const e = - state.revoked ?? - (err instanceof TimeoutError ? controlTimeout(err) : await sessionCause(this.#quic, err)); + const e = state.revoked ?? (await sessionCause(this.#quic, err)); state.cancel.abort(e); valid?.removeEventListener("abort", retire); request.reject(e); @@ -818,6 +804,7 @@ export class Subscriber { request: track.Request, id: bigint, timescale: Signal, + deadline: Request, ): Promise<{ stream: Stream; entry: SubscribeEntry }> { state.cancel.signal.throwIfAborted(); let producer: track.Producer; @@ -825,7 +812,13 @@ export class Subscriber { if (supportsTrackStream(this.version)) { // Fetch the immutable properties once via the TRACK stream. - const { info, stream } = await this.#trackInfo(msg.broadcast, msg.epoch, msg.track, state.cancel.signal); + const { info, stream } = await this.#trackInfo( + msg.broadcast, + msg.epoch, + msg.track, + state.cancel.signal, + deadline, + ); state.track = stream; // The deadline passed as TRACK_INFO landed: the request is already rejected, so don't // register it again or send its SUBSCRIBE. @@ -858,7 +851,7 @@ export class Subscriber { this.#subscribes.set(id, entry); state.producer = producer; - state.stream = await Stream.open(this.#quic, { version: this.version }); + state.stream = await deadline.open(Stream.open(this.#quic, { version: this.version, ...requestOptions })); // The deadline passed while the open waited: the late-setup handler resets the stream, so // don't send a SUBSCRIBE on it first. state.cancel.signal.throwIfAborted(); @@ -883,6 +876,7 @@ export class Subscriber { epoch: Epoch.Valid | undefined, track: string, signal?: AbortSignal, + deadline?: Request, ): Promise<{ info: TrackInfo; stream: Stream }> { return this.#exchange( { version: this.version }, @@ -893,16 +887,26 @@ export class Subscriber { return { info, stream }; }, signal, + deadline, ); } // Opens a stream and runs a request/response exchange on it, resetting the stream if `run` // fails. Subscriber.close() or `signal` also resets it while `run` is pending, so a peer that // never answers cannot hold it open, and a stream that opens after either is reset at once. - async #exchange(options: OpenOptions, run: (stream: Stream) => Promise, signal?: AbortSignal): Promise { + async #exchange( + options: OpenOptions, + run: (stream: Stream) => Promise, + signal?: AbortSignal, + deadline?: Request, + ): Promise { + if (!deadline) { + const request = new Request(); + return request.wait(this.#exchange(options, run, signal, request)); + } const closed = signal ? AbortSignal.any([this.#closed.signal, signal]) : this.#closed.signal; closed.throwIfAborted(); - const opening = Stream.open(this.#quic, options); + const opening = deadline.open(Stream.open(this.#quic, { ...options, ...requestOptions })); let stream: Stream; try { stream = await untilAborted(opening, closed); @@ -1043,7 +1047,10 @@ export class Subscriber { // Lite has no FETCH_OK, so a publisher that never answers would hold the setup forever. // Subscriber.close() closing the group releases every caller at any stage, and resets // the streams the setup opened. - const setup = this.#fetchSetup(broadcast, consumed, track, sequence, priority, group, maxDelay); + const deadline = new Request(); + const setup = deadline.wait( + this.#fetchSetup(broadcast, consumed, track, sequence, priority, group, deadline, maxDelay), + ); let accepted: { stream: Stream; info: TrackInfo }; try { accepted = await untilAbandoned(group, setup); @@ -1072,13 +1079,14 @@ export class Subscriber { sequence: number, priority: number, group: netGroup.Producer, + deadline: Request, maxDelay?: Milli, ): Promise<{ stream: Stream; info: TrackInfo }> { const { epoch, valid } = consumed; const cancel = new AbortController(); void group.closed.then((err) => cancel.abort(err ?? new StreamError(StreamCode.Cancel))); const signal = valid ? AbortSignal.any([valid, cancel.signal]) : cancel.signal; - const answered = this.#trackInfo(broadcast, epoch, track, signal); + const answered = this.#trackInfo(broadcast, epoch, track, signal, deadline); // Only the properties are needed, so the TRACK stream closes however the wait ends. answered.then( ({ stream }) => stream.close(), @@ -1103,6 +1111,7 @@ export class Subscriber { return { stream, info }; }, signal, + deadline, ); } diff --git a/js/net/src/request-deadline.test.ts b/js/net/src/request-deadline.test.ts new file mode 100644 index 00000000000..59eefd96f7a --- /dev/null +++ b/js/net/src/request-deadline.test.ts @@ -0,0 +1,263 @@ +import { expect, test } from "bun:test"; +import { Manual, Milli, Nano } from "@moq/clock"; +import { StreamCode, StreamError } from "./error.ts"; +import { HopSchema } from "./hop.ts"; +import { NativeSession } from "./ietf/adapter.ts"; +import { Publisher } from "./ietf/publisher.ts"; +import { SubscribeNamespace } from "./ietf/subscribe_namespace.ts"; +import { Subscriber as IetfSubscriber } from "./ietf/subscriber.ts"; +import { Version as Ietf } from "./ietf/version.ts"; +import { Subscriber as LiteSubscriber } from "./lite/subscriber.ts"; +import { Version as Lite } from "./lite/version.ts"; +import { Producer as Origin } from "./origin.ts"; +import * as Path from "./path.ts"; +import { Stream } from "./stream.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(); + const created = Promise.withResolvers(); + const aborted = Promise.withResolvers(); + const cancelled = Promise.withResolvers(); + const written: Uint8Array[] = []; + const options: unknown[] = []; + const stream = { + readable: new ReadableStream({ cancel: cancelled.resolve }), + writable: new WritableStream({ + 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; + }, + } as unknown as WebTransport; + return { + quic, + created: created.promise, + aborted: aborted.promise, + cancelled: cancelled.promise, + written, + options, + release: () => slot.resolve(stream), + }; +} + +for (const protocol of ["lite", "ietf-17"] as const) { + for (const mode of ["refuse", "queue", "open"] as const) { + test(`${protocol}: ${mode} has one fatal request deadline`, async () => { + const clock = new Manual(); + const restore = clock.install(); + const transport = limited(mode); + const subscriber = + protocol === "lite" + ? new LiteSubscriber(transport.quic, Lite.DRAFT_07, HopSchema.parse(1n)) + : new IetfSubscriber({ session: new NativeSession(transport.quic, Ietf.DRAFT_17, true) }); + const broadcast = subscriber.consume(Path.from("room")); + const track = broadcast.track("video").subscribe(); + try { + await transport.created; + await clock.advanceAsync(Nano.zero); + if (mode === "refuse") expect(track.closed.peek()?.name).toBe("NoStreamCredit"); + await clock.advanceAsync(Nano.fromMilli(Milli(9_999))); + if (mode !== "refuse") expect(track.closed.peek()).toBeUndefined(); + await clock.advanceAsync(Nano.fromMilli(Milli(1))); + const failure = await track.closed; + if (mode === "open") { + expect(failure).toBeInstanceOf(StreamError); + expect((failure as StreamError).code).toBe(StreamCode.ControlTimeout); + } else { + expect(failure?.name).toBe("NoStreamCredit"); + } + expect(transport.options).toEqual([{ sendOrder: undefined, waitUntilAvailable: false }]); + if (mode === "queue") { + transport.release(); + await transport.aborted; + await transport.cancelled; + expect(transport.written).toEqual([]); + } + if (mode === "open") { + await transport.aborted; + await transport.cancelled; + } + await clock.advanceAsync(Nano.fromMilli(Milli(30_000))); + expect(transport.options).toHaveLength(1); + } finally { + track.close(); + broadcast.close(); + subscriber.close(); + restore(); + } + }); + } +} + +for (const legacy of [false, true]) { + for (const mode of ["refuse", "queue", "open"] as const) { + test(`${legacy ? "solicited draft-15" : "unsolicited draft-17"} advertisement: ${mode} is terminal`, async () => { + const clock = new Manual(); + const restore = clock.install(); + const transport = limited(mode); + const version = legacy ? Ietf.DRAFT_15 : Ietf.DRAFT_17; + const origin = new Origin(); + const publisher = new Publisher({ + quic: transport.quic, + session: new NativeSession(transport.quic, version, true), + publish: origin.consume(), + requiresSolicitation: legacy, + }); + const subscription = new Stream({ + readable: new ReadableStream(), + writable: new WritableStream(), + version, + }); + origin.createBroadcast(Path.from("room")).announce(); + const loop = legacy + ? publisher.runSubscribeNamespace( + new SubscribeNamespace({ requestId: 0n, namespace: Path.empty() }), + subscription, + ) + : publisher.runPublishNamespaces(); + try { + await transport.created; + await clock.advanceAsync(Nano.fromMilli(Milli(9_999))); + let aborted = false; + void transport.aborted.then(() => { + aborted = true; + }); + expect(aborted).toBe(false); + await clock.advanceAsync(Nano.fromMilli(Milli(1))); + if (mode === "queue") transport.release(); + if (mode !== "refuse") { + const reset = await transport.aborted; + await transport.cancelled; + if (mode === "open") expect((reset as Error).message).toContain("request timed out after 10000ms"); + else expect(transport.written).toEqual([]); + } + await clock.advanceAsync(Nano.fromMilli(Milli(30_000))); + // An unrelated change wakes reconciliation, but cannot revive the failed offer. + origin.createBroadcast(Path.from("another")).announce(); + await clock.advanceAsync(Nano.zero); + expect(transport.options).toEqual([ + { sendOrder: undefined, waitUntilAvailable: false }, + { sendOrder: undefined, waitUntilAvailable: false }, + ]); + } finally { + subscription.close(); + origin.close(); + await loop; + publisher.close(); + restore(); + } + }); + } +} + +for (const operation of ["info", "fetch"] as const) { + for (const mode of ["refuse", "queue", "open"] as const) { + test(`lite standalone ${operation}: ${mode} has a fatal deadline`, async () => { + const clock = new Manual(); + const restore = clock.install(); + const transport = limited(mode); + const subscriber = new LiteSubscriber(transport.quic, Lite.DRAFT_07, HopSchema.parse(1n)); + let failure: unknown; + const request = ( + operation === "info" + ? subscriber.resolveTrackInfo(Path.from("room"), "video") + : subscriber.fetchGroup(Path.from("room"), "video", 0) + ).catch((err: unknown) => { + failure = err; + }); + try { + await transport.created; + await clock.advanceAsync(Nano.zero); + if (mode === "refuse") expect((failure as Error).name).toBe("NoStreamCredit"); + await clock.advanceAsync(Nano.fromMilli(Milli(10_000))); + await request; + if (mode === "open") expect((failure as StreamError).code).toBe(StreamCode.ControlTimeout); + else expect((failure as Error).name).toBe("NoStreamCredit"); + if (mode === "queue") { + transport.release(); + await transport.aborted; + await transport.cancelled; + expect(transport.written).toEqual([]); + } + expect(transport.options).toEqual([{ sendOrder: undefined, waitUntilAvailable: false }]); + } finally { + subscriber.close(); + restore(); + } + }); + } +} + +for (const protocol of ["lite", "ietf-17"] as const) { + for (const mode of ["refuse", "queue", "open"] as const) { + test(`${protocol} discovery: ${mode} bounds only the create`, async () => { + const clock = new Manual(); + const restore = clock.install(); + const transport = limited(mode); + const subscriber = + protocol === "lite" + ? new LiteSubscriber(transport.quic, Lite.DRAFT_07, HopSchema.parse(1n)) + : new IetfSubscriber({ session: new NativeSession(transport.quic, Ietf.DRAFT_17, true) }); + const announced = subscriber.announced(); + try { + await transport.created; + await clock.advanceAsync(Nano.zero); + await clock.advanceAsync(Nano.fromMilli(Milli(30_000))); + if (mode === "open") expect(announced.closed.peek()).toBeUndefined(); + else { + expect((await announced.closed)?.name).toBe("NoStreamCredit"); + if (mode === "queue") { + transport.release(); + await transport.aborted; + await transport.cancelled; + expect(transport.written).toEqual([]); + } + } + expect(transport.options).toEqual([{ sendOrder: undefined, waitUntilAvailable: false }]); + } finally { + announced.close(); + subscriber.close(); + restore(); + } + }); + } +} + +for (const protocol of ["lite", "ietf-17"] as const) { + test(`${protocol}: credit granted in the deadline task cannot send a late request`, async () => { + const clock = new Manual(); + const restore = clock.install(); + const transport = limited("queue"); + const subscriber = + protocol === "lite" + ? new LiteSubscriber(transport.quic, Lite.DRAFT_07, HopSchema.parse(1n)) + : new IetfSubscriber({ session: new NativeSession(transport.quic, Ietf.DRAFT_17, true) }); + const broadcast = subscriber.consume(Path.from("room")); + const track = broadcast.track("video").subscribe(); + try { + await transport.created; + clock.advance(Nano.fromMilli(Milli(10_000))); + transport.release(); + await clock.advanceAsync(Nano.zero); + expect((await track.closed)?.name).toBe("NoStreamCredit"); + await transport.aborted; + expect(transport.written).toEqual([]); + } finally { + track.close(); + broadcast.close(); + subscriber.close(); + restore(); + } + }); +} diff --git a/js/net/src/stream.ts b/js/net/src/stream.ts index 455469e0e85..c9c7c8f53d2 100644 --- a/js/net/src/stream.ts +++ b/js/net/src/stream.ts @@ -52,7 +52,7 @@ const MAX_READ_SIZE = 1024 * 1024 * 64; // don't allocate more than 64MB for a m * Options handed to the transport for one outgoing stream. * * `waitUntilAvailable` waits for the peer's concurrent stream limit to free a slot instead - * of rejecting with a `QuotaExceededError`, which is what we want for the streams a session + * of rejecting the create, which is what we want for the streams a session * opens occasionally. `@types/web` doesn't declare it yet, hence the intersection. */ export function sendOptions(options?: OpenOptions): WebTransportSendStreamOptions & { waitUntilAvailable?: boolean } { @@ -62,7 +62,6 @@ export function sendOptions(options?: OpenOptions): WebTransportSendStreamOption // How long any open may wait for the peer to free a stream slot. Every open needs this, // whether or not it asked to wait: an implementation may park an over-limit open rather // than rejecting it, and a peer can advertise a stream limit of zero and never raise it. -// Matches the subscribe budget. const OPEN_TIMEOUT_MS = 10_000; /** @@ -70,7 +69,8 @@ const OPEN_TIMEOUT_MS = 10_000; * the pre-waitUntilAvailable failure after a grace period. A stream arriving after the * deadline is discarded, since nobody is waiting for it any more. */ -async function openWithin(opening: Promise, timeout: number, discard: (stream: T) => void): Promise { +async function openWithin(opening: Promise, timeout: number | null, discard: (stream: T) => void): Promise { + if (timeout === null) return opening; try { return await withTimeout( opening, @@ -163,10 +163,9 @@ export interface OpenOptions { /** * Reject if the peer hasn't freed a stream slot within this many milliseconds. Defaults - * to 10s. There is no way to wait indefinitely: a peer can advertise a stream limit of - * zero and never raise it, so every open needs a way out. + * to 10s. Set null only when the caller owns the create deadline. */ - timeout?: number; + timeout?: number | null; /** * Ask the transport to wait for a stream slot rather than failing when the peer's @@ -237,7 +236,7 @@ export class Stream { static async open(quic: WebTransport, options: OpenOptions): Promise { const { readable, writable } = await openWithin( quic.createBidirectionalStream(sendOptions(options)), - options?.timeout ?? OPEN_TIMEOUT_MS, + options.timeout === undefined ? OPEN_TIMEOUT_MS : options.timeout, (stream) => { void stream.writable.abort().catch(() => void 0); void stream.readable.cancel().catch(() => void 0); @@ -818,7 +817,7 @@ export class Writer { static async open(quic: WebTransport, options: OpenOptions): Promise { const writable = await openWithin( quic.createUnidirectionalStream(sendOptions(options)) as Promise>, - options?.timeout ?? OPEN_TIMEOUT_MS, + options.timeout === undefined ? OPEN_TIMEOUT_MS : options.timeout, (stream) => void stream.abort().catch(() => void 0), ); diff --git a/js/net/src/util/request.ts b/js/net/src/util/request.ts new file mode 100644 index 00000000000..28b673960b5 --- /dev/null +++ b/js/net/src/util/request.ts @@ -0,0 +1,58 @@ +import { Milli, Nano } from "@moq/clock"; +import { controlTimeout, error, NoStreamCredit } from "../error.ts"; +import type { OpenOptions, Stream } from "../stream.ts"; +import { TimeoutError, withTimeout } from "./timeout.ts"; + +/** Request opens defer their deadline to the create-to-answer budget. @internal */ +export const requestOptions = { timeout: null, waitUntilAvailable: false } satisfies Partial; +/** Milliseconds allowed from a request create to its first answer. @internal */ +export const REQUEST_TIMEOUT_MS = 10_000; + +/** One fatal deadline for a request, including every stream its setup creates. @internal */ +export class Request { + #opening = false; + #streams: Stream[] = []; + #failure?: Error; + + /** Record a create so deadline failures reset its stream even if it opens late. */ + async open(opening: Stream | Promise): Promise { + this.#opening = true; + let stream: Stream; + try { + stream = await opening; + } catch (cause) { + throw new NoStreamCredit({ cause }); + } finally { + this.#opening = false; + } + if (this.#failure) { + stream.abort(this.#failure); + throw this.#failure; + } + this.#streams.push(stream); + return stream; + } + + /** Wait for the first answer under the request's single deadline. */ + async wait(answer: Promise): Promise { + try { + return await withTimeout( + answer, + Nano.fromMilli(Milli(REQUEST_TIMEOUT_MS)), + "request timed out after 10000ms", + ); + } catch (cause) { + this.#failure = + cause instanceof TimeoutError + ? this.#opening + ? new NoStreamCredit({ cause }) + : controlTimeout(cause) + : error(cause); + // Other failures keep the caller's cancellation ordering, including legacy UNSUBSCRIBE. + if (cause instanceof TimeoutError || cause instanceof NoStreamCredit) { + for (const stream of this.#streams) stream.abort(this.#failure); + } + throw this.#failure; + } + } +} diff --git a/quest/m1/README.md b/quest/m1/README.md index 1d8c10f5e0a..6067053140c 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -101,7 +101,6 @@ lands before the cut. - [Audio jitter target](/quest/m1/audio-jitter-target/README.md) - the playout target is default in both languages; a manual browser proof and a native trace replay remain - [A/V clock](/quest/m1/av-clock.md) - the audio playhead drives Sync.reference while audio plays, through per-track sync handles - [Publish channel count](/quest/m1/publish-audio-channel-count.md) - forcing a channel count on an Audio.Capture stops costing the subscriber gaps of silence, and the media fixture's cold-start audio-tone flake is fixed at its cause -- [JS request deadline](/quest/m1/js-request-deadline.md) - each JS request, PUBLISH_NAMESPACE included, has one fatal 10 s timer from create to answer and fails fast without stream credit, so nothing queues or retries - [Handshake deadline covers admission](/quest/m1/handshake-admission-deadline.md) - `listen.timeout` bounds a connection through auth admission on both listeners, replacing the hardcoded admission timeout - [Drain cleanup](/quest/m1/drain-cleanup.md) - sessions force-closed at the drain deadline record their lease end and bytes before the relay or CLI exits, while unfinished handshakes stay bounded - [Container reads drop backfill](/quest/m1/read-backfill.md) - the JS container consumer drops groups below its read cursor like Rust diff --git a/quest/m1/js-request-deadline.md b/quest/m1/js-request-deadline.md deleted file mode 100644 index d7e98d72ec9..00000000000 --- a/quest/m1/js-request-deadline.md +++ /dev/null @@ -1,73 +0,0 @@ -# [M] JS requests: one 10 s deadline from create to answer - -## Goal - -Every `@moq/net` request that waits for an answer runs under one fatal 10 s -timer, from creating its stream to the peer's first answer. That covers -subscribe, standalone track info, and fetch on lite and moq-transport, plus the -IETF publisher's PUBLISH_NAMESPACE. When it fires, the request fails, and any -stream it created is reset, including one that opens later. A create the -transport refuses, or one still pending when the timer fires, fails with a -local "no stream credit" error. A request the peer opened but never answered -fails with `ControlTimeout`. Announce interest and SubscribeNamespace bound -their create the same way but have no answer deadline, since they are -long-lived. - -Nothing retries either failure. A request without credit is refused, which -sheds load instead of queueing work the caller can't see, and a timeout is -terminal. - -## Plan - -Decided 2026-10-07, re-planning the external -[#4999](https://github.com/moq-dev/moq/pull/4999) and -[#5001](https://github.com/moq-dev/moq/pull/5001): - -- Fail fast without credit. Request opens pass `waitUntilAvailable: false`. - Chrome already rejects an over-limit create at once with a `NetworkError` - (crbug.com/487117768), and stays terminal. Firefox ignores the flag and - queues, so the one timer bounds it; a stream that opens after that is reset. - `@moq/qmux` gets the same fix upstream. -- One timer, not two. `OPEN_TIMEOUT_MS` (`openWithin` in - `js/net/src/stream.ts`) stays the default for `Stream.open` and - `Writer.open`, which SETUP, probe, and group streams inherit. Request sites - opt out of it, and their setup timer (`SUBSCRIBE_SETUP_TIMEOUT_MS` in lite, - `SUBSCRIBE_OK_TIMEOUT_MS` in IETF, and PUBLISH_NAMESPACE's - `ADVERTISE_TIMEOUT_MS`, which moves from 5 s to the shared 10 s) covers the - create as well as the answer. -- PUBLISH_NAMESPACE is in scope with no retry: both loops that advertise - through `#advertise` (`runPublishNamespaces` and the draft-14/15 solicited - `#runSubscribeNamespace`, `js/net/src/ietf/publisher.ts`) stop re-offering - after either failure above. Both failures return `"dropped"` today, the - answer that keeps the backoff running, so they need their own terminal - answer. The namespace then stays unadvertised through that peer until it is - republished or the session reconnects. Each loop still re-offers when the - peer's REQUEST_ERROR names a retry interval, and keeps its backoff for a - draft-14/15 refusal, which can't carry one (decided 2026-10-07 from review: - the peer asked for those). Test both loops. -- The credit error is local, with no wire code. Propose its name in the PR; - it is a new public `@moq/net` error. -- `@moq/watch` keeps ending a track on either failure. Rejected: #4999's - resubscribe on a local timeout, since retrying adds a create per attempt and - the timeout is meant to be fatal. Rejected: #5001's unbounded wait for a - slot. -- Keep [#5002](https://github.com/moq-dev/moq/pull/5002)'s handling of a timer - that fires mid-setup: the TRACK stream is reset, and a SUBSCRIBE stream that - opens afterwards is reset with nothing written. -- Fix the stale text: "Chrome ~100" and "silently blocks" (IETF) or "the open - blocks" (lite) in the subscribers' timeout comments (the cap is the peer's - MAX_STREAMS; the Rust relay grants 10,000), `stream.ts`'s - `QuotaExceededError` claim and its "matches the subscribe budget" note, and - the "(browser stream limit reached?)" hint in the timeout messages. - -Test with mocked time: a transport with a stream limit, covering an immediate -rejection, a queued create that times out with the credit error and is reset -when it opens, and an opened request with no answer that fails with -`ControlTimeout`. Cover lite and IETF draft 17. - -Public API: one new error. Wire: none. - -## Related - -- [Rust request credit](/quest/m1/rs-request-credit.md) - the same rule in moq-net -- [qmux no-wait opens](/quest/m1/qmux-no-wait.md) - the WebSocket fallback stops queueing over-limit creates diff --git a/quest/m1/qmux-no-wait.md b/quest/m1/qmux-no-wait.md index 8bf7364474a..da270030d60 100644 --- a/quest/m1/qmux-no-wait.md +++ b/quest/m1/qmux-no-wait.md @@ -10,6 +10,6 @@ moq-dev/web-transport and bumped in `js/net/package.json`. ## Plan -Decided 2026-10-07 with [JS requests](/quest/m1/js-request-deadline.md), -which pass the flag and otherwise rely on their 10 s timer to bound the queue. +Decided 2026-10-07: JS requests pass the flag and otherwise rely on their +10 s timer to bound the queue. Match the rejection Chrome raises so `@moq/net` handles both the same way. diff --git a/quest/m1/rs-request-credit.md b/quest/m1/rs-request-credit.md index 9feada84238..77ba5612e0d 100644 --- a/quest/m1/rs-request-credit.md +++ b/quest/m1/rs-request-credit.md @@ -10,9 +10,8 @@ out of scope. ## Plan -Decided 2026-10-07 with [JS requests](/quest/m1/js-request-deadline.md): a -request without credit is refused, which sheds load rather than parking work -the caller can't see. Accepted: a relay asking a peer that grants few streams +Decided 2026-10-07: a request without credit is refused, which sheds load +rather than parking work the caller can't see. Accepted: a relay asking a peer that grants few streams gets hard failures in bursts where it used to wait, and nothing retries them. Rust gets no answer timer: a request lives until it is answered, its demand leaves, or the session closes, and a relay shouldn't kill a subscribe on a From 44e97058a7fd386ea51021d39a10f6f2e116ed97 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 21:13:29 -0700 Subject: [PATCH 3/5] fix(net): report a closed session, not missing credit, on a failed create A create the session fails (session-sourced error or InvalidStateError) now passes through the request deadline, so callers see the session's close code instead of NoStreamCredit. Rename the internal Request helper to Deadline so it no longer shadows the fetch global next to track.Request, and make the lite exchange take its deadline explicitly instead of recursing to create one. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/publisher.ts | 4 +-- js/net/src/ietf/subscriber.ts | 8 +++--- js/net/src/lite/subscriber.ts | 27 +++++++++---------- js/net/src/request-deadline.test.ts | 29 ++++++++++++++++++++- js/net/src/util/{request.ts => deadline.ts} | 13 +++++++-- 5 files changed, 57 insertions(+), 24 deletions(-) rename js/net/src/util/{request.ts => deadline.ts} (76%) diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 9214f528b09..615b5ca4f0a 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -26,8 +26,8 @@ import { type Stream, Writer } from "../stream.ts"; import { Timescale } from "../time.ts"; import type { Location as TrackLocation, Subscriber as TrackSubscriber } from "../track.ts"; import { untilAborted } from "../util/abort.ts"; +import { Deadline, REQUEST_TIMEOUT_MS, requestOptions } from "../util/deadline.ts"; import { LIMITS, type Limits, Slots } from "../util/limits.ts"; -import { REQUEST_TIMEOUT_MS, Request, requestOptions } from "../util/request.ts"; import { TimeoutError, withTimeout } from "../util/timeout.ts"; import * as Varint from "../varint.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; @@ -1560,7 +1560,7 @@ export class Publisher { // leaving this one namespace unadvertised. let request: Stream | undefined; try { - const deadline = new Request(); + const deadline = new Deadline(); const answer = await deadline.wait( (async (): Promise => { request = await deadline.open(this.#session.openBi(requestOptions)); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 87363562ecb..ddfd992f0fa 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -25,8 +25,8 @@ import { Tail } from "../tail.ts"; import type { Timescale, Timestamp } from "../time.ts"; import type * as track from "../track.ts"; import { untilAborted } from "../util/abort.ts"; +import { Deadline, requestOptions } from "../util/deadline.ts"; import { LIMITS, type Limits, SlotMap, Slots } from "../util/limits.ts"; -import { Request, requestOptions } from "../util/request.ts"; import { overrideBroadcastWire, wireOf } from "../wire.ts"; import type { Session } from "./adapter.ts"; import { DuplicateTrackAlias, RetiredTrackAlias, TrackAliases } from "./aliases.ts"; @@ -437,7 +437,7 @@ export class Subscriber { try { // v16: use a real bidi stream (not virtual control stream) - const deadline = new Request(); + const deadline = new Deadline(); const stream = await deadline.wait( deadline.open( version === Version.DRAFT_16 && this.#session.openNativeBi @@ -669,7 +669,7 @@ export class Subscriber { // flows back via `state` so the timeout path can clean up the stream // and any registration if setup eventually finishes. const state: SubscribeSetupState = {}; - const deadline = new Request(); + const deadline = new Deadline(); const setup = this.#openSubscribe(state, broadcast, request, subscription, requestId, deadline); // The publisher can be serving before it answers, so waiting only on the response @@ -885,7 +885,7 @@ export class Subscriber { request: track.Request, subscription: Subscription, requestId: bigint, - deadline: Request, + deadline: Deadline, ): Promise<{ stream: Stream; alias: bigint }> { const version = this.#session.version; diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 935d3cf686e..754476def7b 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -19,8 +19,8 @@ import { TAIL_GRACE_MS, Tail } from "../tail.ts"; import * as Time from "../time.ts"; import type * as track from "../track.ts"; import { untilAborted } from "../util/abort.ts"; +import { Deadline, REQUEST_TIMEOUT_MS, requestOptions } from "../util/deadline.ts"; import { LIMITS, type Limits, SlotMap, Slots } from "../util/limits.ts"; -import { REQUEST_TIMEOUT_MS, Request, requestOptions } from "../util/request.ts"; import { overrideBroadcastWire, wireOf } from "../wire.ts"; import { AnnounceHistory, @@ -267,7 +267,7 @@ export class Subscriber { // to reset the stream, not just close our side of it. let stream: Stream; try { - const deadline = new Request(); + const deadline = new Deadline(); stream = await deadline.wait( deadline.open(Stream.open(this.#quic, { version: this.version, ...requestOptions })), ); @@ -693,7 +693,7 @@ export class Subscriber { }; valid?.addEventListener("abort", retire); if (valid?.aborted) retire(); - const deadline = new Request(); + const deadline = new Deadline(); const setup = this.#openSubscribe(state, msg, request, id, timescale, deadline); let opened: { stream: Stream; entry: SubscribeEntry }; @@ -804,7 +804,7 @@ export class Subscriber { request: track.Request, id: bigint, timescale: Signal, - deadline: Request, + deadline: Deadline, ): Promise<{ stream: Stream; entry: SubscribeEntry }> { state.cancel.signal.throwIfAborted(); let producer: track.Producer; @@ -875,8 +875,8 @@ export class Subscriber { broadcast: Path.Valid, epoch: Epoch.Valid | undefined, track: string, - signal?: AbortSignal, - deadline?: Request, + signal: AbortSignal | undefined, + deadline: Deadline, ): Promise<{ info: TrackInfo; stream: Stream }> { return this.#exchange( { version: this.version }, @@ -897,13 +897,9 @@ export class Subscriber { async #exchange( options: OpenOptions, run: (stream: Stream) => Promise, - signal?: AbortSignal, - deadline?: Request, + signal: AbortSignal | undefined, + deadline: Deadline, ): Promise { - if (!deadline) { - const request = new Request(); - return request.wait(this.#exchange(options, run, signal, request)); - } const closed = signal ? AbortSignal.any([this.#closed.signal, signal]) : this.#closed.signal; closed.throwIfAborted(); const opening = deadline.open(Stream.open(this.#quic, { ...options, ...requestOptions })); @@ -956,7 +952,8 @@ export class Subscriber { if (!supportsTrackStream(this.version)) { throw new Error("track info requires moq-lite-05 or newer"); } - const { info, stream } = await this.#trackInfo(broadcast, epoch, track, hold); + const deadline = new Deadline(); + const { info, stream } = await deadline.wait(this.#trackInfo(broadcast, epoch, track, hold, deadline)); if (hold && !hold.aborted) hold.addEventListener("abort", () => stream.close(), { once: true }); else stream.close(); return this.#toModelInfo(info); @@ -1047,7 +1044,7 @@ export class Subscriber { // Lite has no FETCH_OK, so a publisher that never answers would hold the setup forever. // Subscriber.close() closing the group releases every caller at any stage, and resets // the streams the setup opened. - const deadline = new Request(); + const deadline = new Deadline(); const setup = deadline.wait( this.#fetchSetup(broadcast, consumed, track, sequence, priority, group, deadline, maxDelay), ); @@ -1079,7 +1076,7 @@ export class Subscriber { sequence: number, priority: number, group: netGroup.Producer, - deadline: Request, + deadline: Deadline, maxDelay?: Milli, ): Promise<{ stream: Stream; info: TrackInfo }> { const { epoch, valid } = consumed; diff --git a/js/net/src/request-deadline.test.ts b/js/net/src/request-deadline.test.ts index 59eefd96f7a..64f02df52ac 100644 --- a/js/net/src/request-deadline.test.ts +++ b/js/net/src/request-deadline.test.ts @@ -1,6 +1,6 @@ import { expect, test } from "bun:test"; import { Manual, Milli, Nano } from "@moq/clock"; -import { StreamCode, StreamError } from "./error.ts"; +import { SessionCode, SessionError, StreamCode, StreamError } from "./error.ts"; import { HopSchema } from "./hop.ts"; import { NativeSession } from "./ietf/adapter.ts"; import { Publisher } from "./ietf/publisher.ts"; @@ -261,3 +261,30 @@ for (const protocol of ["lite", "ietf-17"] as const) { } }); } + +for (const protocol of ["lite", "ietf-17"] as const) { + test(`${protocol}: a create the closed session fails reports the session`, async () => { + const quic = { + closed: Promise.resolve({ closeCode: SessionCode.Unauthorized, reason: "bye" }), + createBidirectionalStream: () => + Promise.reject( + Object.assign(new Error("session closed"), { source: "session", streamErrorCode: null }), + ), + } as unknown as WebTransport; + const subscriber = + protocol === "lite" + ? new LiteSubscriber(quic, Lite.DRAFT_07, HopSchema.parse(1n)) + : new IetfSubscriber({ quic, session: new NativeSession(quic, Ietf.DRAFT_17, true) }); + const broadcast = subscriber.consume(Path.from("room")); + const track = broadcast.track("video").subscribe(); + try { + const failure = await track.closed; + expect(failure).toBeInstanceOf(SessionError); + expect((failure as SessionError).code).toBe(SessionCode.Unauthorized); + } finally { + track.close(); + broadcast.close(); + subscriber.close(); + } + }); +} diff --git a/js/net/src/util/request.ts b/js/net/src/util/deadline.ts similarity index 76% rename from js/net/src/util/request.ts rename to js/net/src/util/deadline.ts index 28b673960b5..8c074820fc3 100644 --- a/js/net/src/util/request.ts +++ b/js/net/src/util/deadline.ts @@ -8,8 +8,16 @@ export const requestOptions = { timeout: null, waitUntilAvailable: false } satis /** Milliseconds allowed from a request create to its first answer. @internal */ export const REQUEST_TIMEOUT_MS = 10_000; +// A closed session fails its creates with a session-sourced WebTransportError, or with an +// InvalidStateError once it has closed. Those report the session's failure, not missing credit. +function sessionFailure(err: unknown): boolean { + if (typeof err !== "object" || err === null) return false; + const { source, name } = err as { source?: unknown; name?: unknown }; + return source === "session" || name === "InvalidStateError"; +} + /** One fatal deadline for a request, including every stream its setup creates. @internal */ -export class Request { +export class Deadline { #opening = false; #streams: Stream[] = []; #failure?: Error; @@ -21,6 +29,7 @@ export class Request { try { stream = await opening; } catch (cause) { + if (sessionFailure(cause)) throw cause; throw new NoStreamCredit({ cause }); } finally { this.#opening = false; @@ -39,7 +48,7 @@ export class Request { return await withTimeout( answer, Nano.fromMilli(Milli(REQUEST_TIMEOUT_MS)), - "request timed out after 10000ms", + `request timed out after ${REQUEST_TIMEOUT_MS}ms`, ); } catch (cause) { this.#failure = From 28953f1076d14075a0d426621592c17a6f4ffdb5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 03:50:06 -0800 Subject: [PATCH 4/5] chore(quest): claim quest/m1/js-request-deadline.md Quest-Claim: 099fb62afa5196fb From b1e3c705c7af0d16dec0d8789478c55264d77ac6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 03:54:32 -0800 Subject: [PATCH 5/5] test(net): cover request streams opening after their deadline Co-Authored-By: GPT-6.1 Sol --- js/net/src/ietf/request_stream.test.ts | 61 +++++++++++++++++++++++++- quest/m1/README.md | 1 - quest/m1/js-request-deadline.md | 29 ------------ 3 files changed, 59 insertions(+), 32 deletions(-) delete mode 100644 quest/m1/js-request-deadline.md diff --git a/js/net/src/ietf/request_stream.test.ts b/js/net/src/ietf/request_stream.test.ts index 9abf37f99b3..6d5f85fb36e 100644 --- a/js/net/src/ietf/request_stream.test.ts +++ b/js/net/src/ietf/request_stream.test.ts @@ -1,6 +1,6 @@ -import { expect, spyOn, test } from "bun:test"; +import { expect, mock, spyOn, test } from "bun:test"; import { Manual, Milli, Nano } from "@moq/clock"; -import { ProtocolViolation, SessionCode, StreamCode, StreamError } from "../error.ts"; +import { NoStreamCredit, 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"; @@ -196,3 +196,60 @@ test("a request's deadline ends it through the request, so later ends do nothing installed.close(); } }); + +test("a request opening after its deadline is aborted through its owner once and never sends", async () => { + const clock = new Manual(); + const installed = clock.install(); + const owners: RequestStream[] = []; + const abort = RequestStream.prototype.abort; + const ownerAborts = spyOn(RequestStream.prototype, "abort").mockImplementation(function ( + this: RequestStream, + err: unknown, + ) { + owners.push(this); + abort.call(this, err); + }); + try { + const write = mock((_chunk: Uint8Array) => {}); + const stream = new Stream({ + readable: new ReadableStream(), + writable: new WritableStream({ write }), + version: Version.DRAFT_18, + }); + const aborts = spyOn(stream, "abort"); + const opening = Promise.withResolvers(); + const session = { + version: Version.DRAFT_18, + client: true, + openBi: () => opening.promise, + } as unknown as Session; + const deadline = new Deadline(); + const send = mock((request: RequestStream) => request.writer.u8(1)); + const sending = RequestStream.open(session, deadline).then(send); + const answer = deadline.wait(sending).catch((e: unknown) => e); + await clock.advance(Nano.fromMilli(Milli(REQUEST_TIMEOUT_MS))); + const err = await answer; + expect(err).toBeInstanceOf(NoStreamCredit); + expect(ownerAborts).not.toHaveBeenCalled(); + expect(aborts).not.toHaveBeenCalled(); + + opening.resolve(stream); + await expect(sending).rejects.toBe(err); + expect(ownerAborts).toHaveBeenCalledTimes(1); + expect(ownerAborts.mock.calls[0]?.[0]).toBe(err); + const [request] = owners; + expect(request).toBeInstanceOf(RequestStream); + await request.ended; + + // The caller's cleanup cannot reset the late stream a second time. + request.abort(new Error("abandoned")); + request.close(); + expect(aborts).toHaveBeenCalledTimes(1); + expect(aborts.mock.calls[0]?.[0]).toBe(err as NoStreamCredit); + expect(send).not.toHaveBeenCalled(); + expect(write).not.toHaveBeenCalled(); + } finally { + ownerAborts.mockRestore(); + installed.close(); + } +}); diff --git a/quest/m1/README.md b/quest/m1/README.md index 360826fa74b..f569dcddaab 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -193,7 +193,6 @@ per-frame CPU growth since #5926. - [A cancelled MoqTrackDemand wait never loses the next change](/quest/m1/demand-cancel-race.md) - cancelling a `MoqTrackDemand::wanted_changed` call, from any binding, never consumes a demand change - [moq-ffi reports an unsupported media configuration as a typed error](/quest/m1/ffi-unsupported-config.md) - `MoqAudioConsumer::subscribe` and the matching video subscribe fail with a typed "unsupported configuration" error - [A live keyframe interval change never outruns the declared HLS duration](/quest/m1/js-hls-keyframe-duration.md) - raising a live keyframe interval, even to infinite, never pushes a segment past the declared HLS target duration, and the browser interop cell asserts the catalog `hls` field -- [A request stream that opens after its deadline is aborted once and never sends](/quest/m1/js-request-deadline.md) - A `@moq/net` IETF request whose stream finishes opening only after its deadline fired is aborted exactly once, through its `RequestStream`, and never writes its request - [One Ed25519 public key type with one z-base-32 codec](/quest/m1/mesh-key-codec.md) - `moq_auth::mesh::EndpointId` is merged into `moq_sign::PublicKey` - [moqsink reports track demand only once the track is in the catalog](/quest/m1/moqsink-demand-catalog.md) - A `moqsink` request pad's `track-demand` stays null until its track is listed in the published catalog, and only then reports `wanted` - [A discontinuity before the first frame keeps the init's parameter sets](/quest/m1/mux-discontinuity-headers.md) - an avc3, hev1, or av01 track that marks a discontinuity before its first frame still publishes that first keyframe with its init's parameter sets diff --git a/quest/m1/js-request-deadline.md b/quest/m1/js-request-deadline.md deleted file mode 100644 index 8c52aff30e0..00000000000 --- a/quest/m1/js-request-deadline.md +++ /dev/null @@ -1,29 +0,0 @@ -# [XS] A request stream that opens after its deadline is aborted once and never sends - -## Goal - -A `@moq/net` IETF request whose stream finishes opening only after its -deadline fired is aborted exactly once, through its `RequestStream`, and -never writes its request. A test proves it. - -## Plan - -Decided 2026-10-31 (user, /quest-plan of the follow-ups from the 2026-10-31 -/quest-complete run), from -[moq-dev/moq#7355](https://github.com/moq-dev/moq/pull/7355). - -Milestone: ✅ m1 (moved from m2). Not chosen: m2. - -- [moq-dev/moq#7355](https://github.com/moq-dev/moq/pull/7355) (merged) routes a deadline failure through the owning `RequestStream` - (`js/net/src/ietf/request_stream.ts`, `js/net/src/util/deadline.ts`), so a - timed-out request is ended once. Its online review suggested one more test, - left as this follow-up: resolve the opening stream only after the deadline - expires, then assert the owner aborts it exactly once and the request bytes - are never written. -- Add it beside `request_stream.test.ts`'s "a request's deadline ends it - through the request" case. Fix the code if it fails. -- The deleted quest of the same name (one 10 s deadline from create to - answer) planned this reset; this quest only proves it through the new - owner. - -Public API: none. Wire: none.