From e43d870ee4f1ed73507774290f7aba3e8589049f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 26 Oct 2026 18:33:20 -0700 Subject: [PATCH 1/5] chore(quest): claim quest/m1/cut-audit/js-net-cleanups.md Quest-Claim: ddea8f979760a421 From 10b9add033fed40f4e1eaa0d2f2db584fde4da44 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 26 Oct 2026 18:59:03 -0700 Subject: [PATCH 2/5] fix(net): align small transport and reader behaviors Co-Authored-By: GPT-6 (Codex) --- js/net/api/index.api.md | 2 + js/net/bench/compression-fanout.ts | 55 ++++ js/net/bench/compression.ts | 48 +--- js/net/src/connection/accept.ts | 48 +++- js/net/src/connection/connect.ts | 5 +- js/net/src/connection/handshake.ts | 4 + js/net/src/connection/reload.test.ts | 32 +-- js/net/src/connection/reload.ts | 48 +--- js/net/src/connection/stats.test.ts | 6 +- js/net/src/group.ts | 10 +- js/net/src/internal.ts | 5 + js/net/src/lite/subscriber.ts | 58 ++--- js/net/src/net-cleanups.test.ts | 241 ++++++++++++++++++ js/net/src/track.ts | 4 +- js/net/src/zod.test.ts | 2 + quest/m1/cut-audit/README.md | 1 - quest/m1/cut-audit/js-net-cleanups.md | 49 ---- .../clients/js/net-cleanups.browser.ts | 32 +++ .../clients/js/net-cleanups/index.html | 3 + test/interop/clients/js/net-cleanups/main.ts | 59 +++++ test/interop/clients/js/tsconfig.json | 2 + test/justfile | 1 + 22 files changed, 502 insertions(+), 213 deletions(-) create mode 100644 js/net/bench/compression-fanout.ts create mode 100644 js/net/src/net-cleanups.test.ts delete mode 100644 quest/m1/cut-audit/js-net-cleanups.md create mode 100644 test/interop/clients/js/net-cleanups.browser.ts create mode 100644 test/interop/clients/js/net-cleanups/index.html create mode 100644 test/interop/clients/js/net-cleanups/main.ts diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index 8450e592e75..32ad3dfbe1b 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -23,6 +23,8 @@ interface AcceptProps { consume?: Producer_4; implementation?: string; publish?: Consumer_5; + signal?: AbortSignal; + timeout?: Milli; transport: WebTransport; url: URL; version?: number; diff --git a/js/net/bench/compression-fanout.ts b/js/net/bench/compression-fanout.ts new file mode 100644 index 00000000000..5fedfd27b91 --- /dev/null +++ b/js/net/bench/compression-fanout.ts @@ -0,0 +1,55 @@ +import type { Producer as Group } from "../src/group.ts"; +import { hooks } from "../src/internal.ts"; +import { Producer as Track } from "../src/track.ts"; + +/** Sweep compressed publishers and public group readers, sharing one inflated copy per group. */ +export function fanout() { + const payload = new TextEncoder().encode('{"value":123,"status":"unchanged"}\n'.repeat(64)); + const frames = 32; + const rounds = 10; + const rows: { publishers: number; readers: number; phase: string; us: number; size_bytes: number }[] = []; + for (const publishers of [1, 4, 16]) { + for (const readers of [1, 4, 16]) { + let publish = 0; + let read = 0; + let stored = 0; + let inflated = 0; + for (let round = 0; round < rounds; round++) { + const tracks = Array.from({ length: publishers }, () => new Track("bench").accept({ compress: true })); + const groups: Group[] = tracks.map((track) => track.appendGroup()); + const consumers = groups.map((group) => Array.from({ length: readers }, () => group.consume())); + let started = performance.now(); + for (const group of groups) { + for (let index = 0; index < frames; index++) group.writeFrame({ payload }); + group.close(); + } + publish += performance.now() - started; + stored = groups.reduce((bytes, group) => bytes + hooks.groupCacheBytes(group), 0); + started = performance.now(); + for (const subscribers of consumers) { + let shared: Uint8Array | undefined; + for (const consumer of subscribers) { + for (let index = 0; index < frames; index++) { + const frame = consumer.tryReadFrame(); + if (!frame || frame.payload.length !== payload.length) throw new Error("missing payload"); + if (index === 0) { + if (shared && shared !== frame.payload) throw new Error("fanout duplicated inflation"); + shared = frame.payload; + } + } + consumer.close(); + } + } + read += performance.now() - started; + inflated = groups.reduce((bytes, group) => bytes + hooks.groupCacheBytes(group), 0) - stored; + if (inflated !== publishers * frames * payload.length) + throw new Error("inflated bytes scale with readers"); + for (const track of tracks) track.close(); + } + rows.push({ publishers, readers, phase: "publish", us: (publish * 1000) / rounds, size_bytes: stored }); + rows.push({ publishers, readers, phase: "read", us: (read * 1000) / rounds, size_bytes: inflated }); + } + } + + return rows; +} diff --git a/js/net/bench/compression.ts b/js/net/bench/compression.ts index 8c5cbe91ea4..ce62fa82a6b 100644 --- a/js/net/bench/compression.ts +++ b/js/net/bench/compression.ts @@ -1,55 +1,13 @@ /** Sweep compressed publishers x plain readers, reporting one shared inflated copy per group, then compressed datagram retention. */ import { gzipSync } from "node:zlib"; import { Encoder } from "../src/flate/encoder.ts"; -import type { Producer as Group } from "../src/group.ts"; import { hooks } from "../src/internal.ts"; import { Producer as Track } from "../src/track.ts"; +import { fanout } from "./compression-fanout.ts"; -const payload = new TextEncoder().encode('{"value":123,"status":"unchanged"}\n'.repeat(64)); -const frames = 32; -const rounds = 10; console.log("publishers,readers,phase,us,size_bytes"); -for (const publishers of [1, 4, 16]) { - for (const readers of [1, 4, 16]) { - let publish = 0; - let read = 0; - let stored = 0; - let inflated = 0; - for (let round = 0; round < rounds; round++) { - const tracks = Array.from({ length: publishers }, () => new Track("bench").accept({ compress: true })); - const groups: Group[] = tracks.map((track) => track.appendGroup()); - const consumers = groups.map((group) => Array.from({ length: readers }, () => group.mirror())); - let started = performance.now(); - for (const group of groups) { - for (let index = 0; index < frames; index++) group.writeFrame({ payload }); - group.close(); - } - publish += performance.now() - started; - stored = groups.reduce((bytes, group) => bytes + hooks.groupCacheBytes(group), 0); - started = performance.now(); - for (const subscribers of consumers) { - let shared: Uint8Array | undefined; - for (const consumer of subscribers) { - for (let index = 0; index < frames; index++) { - const frame = consumer.tryReadFrame(); - if (!frame || frame.payload.length !== payload.length) throw new Error("missing payload"); - if (index === 0) { - if (shared && shared !== frame.payload) throw new Error("fanout duplicated inflation"); - shared = frame.payload; - } - } - consumer.close(); - } - } - read += performance.now() - started; - inflated = groups.reduce((bytes, group) => bytes + hooks.groupCacheBytes(group), 0) - stored; - if (inflated !== publishers * frames * payload.length) throw new Error("inflated bytes scale with readers"); - for (const track of tracks) track.close(); - } - console.log([publishers, readers, "publish", ((publish * 1000) / rounds).toFixed(1), stored].join(",")); - console.log([publishers, readers, "read", ((read * 1000) / rounds).toFixed(1), inflated].join(",")); - } -} +for (const row of fanout()) + console.log([row.publishers, row.readers, row.phase, row.us.toFixed(1), row.size_bytes].join(",")); // Received compressed datagrams of zeros, each inflating to the largest payload, swept over // tracks x queued datagrams. A fast reader takes each one while a slow reader never reads, so diff --git a/js/net/src/connection/accept.ts b/js/net/src/connection/accept.ts index 9f3d7d6cf2f..168ec215775 100644 --- a/js/net/src/connection/accept.ts +++ b/js/net/src/connection/accept.ts @@ -1,13 +1,18 @@ +import { Milli, Nano } from "@moq/clock"; +import { race } from "@moq/signals"; +import { ConnectTimeout, error, fromClose, ProtocolViolation } from "../error.ts"; import * as Ietf from "../ietf/index.ts"; import * as Message from "../ietf/message.ts"; import { IMPLEMENTATION } from "../implementation.ts"; import * as Lite from "../lite/index.ts"; import type { Consumer as OriginConsumer, Producer as OriginProducer } from "../origin.ts"; import { Stream } from "../stream.ts"; +import { untilAborted } from "../util/abort.ts"; +import { after } from "../util/timer.ts"; import type { Established } from "./established.ts"; import * as Extensions from "./extensions.ts"; import { DISCOVERY_REMOVED, forwardAnnounced } from "./forward.ts"; -import { exchangeSetup, handsOff, legacyPeerImplementation } from "./handshake.ts"; +import { DEFAULT_TIMEOUT, exchangeSetup, handsOff, legacyPeerImplementation } from "./handshake.ts"; /** Options for {@link accept}. */ export interface AcceptProps { @@ -16,6 +21,11 @@ export interface AcceptProps { /** The request URL associated with the transport. */ url: URL; + /** Bound SETUP in milliseconds (default: 30000; zero waits until aborted). Throws ConnectTimeout. */ + timeout?: Milli; + /** Abort the handshake and release the transport; ignored after acceptance completes. */ + signal?: AbortSignal; + /** Version to select during SETUP negotiation (for non-ALPN paths). */ version?: number; @@ -63,9 +73,39 @@ type SessionProps = { */ export async function accept({ transport, url, ...props }: AcceptProps): Promise { if ("discovery" in props) throw new TypeError(DISCOVERY_REMOVED); - const connection = await acceptInner(transport, url, props); - if (props.consume) forwardAnnounced(connection, props.consume); - return connection; + props.signal?.throwIfAborted(); + const limit = props.timeout ?? DEFAULT_TIMEOUT; + const duration = Nano.fromMilli(Milli(limit)); + const deadline = new AbortController(); + const signal = props.signal ? AbortSignal.any([props.signal, deadline.signal]) : deadline.signal; + const disarm = limit > 0 ? after(duration, () => deadline.abort(new ConnectTimeout(limit))) : () => {}; + const pending = acceptInner(transport, url, props); + // As on connect, a dead transport must end a SETUP whose peer sends nothing more. + const terminal = transport.closed.then(fromClose, error).then((cause) => { + throw cause ?? new Error("session closed during SETUP"); + }); + try { + const connection = await untilAborted(race([pending, terminal]), signal); + if (signal.aborted) throw signal.reason; + if (props.consume) forwardAnnounced(connection, props.consume); + return connection; + } catch (failure) { + try { + transport.close( + failure instanceof ProtocolViolation ? { closeCode: failure.code, reason: failure.message } : undefined, + ); + } catch { + /* Already closed. */ + } + // A handshake that resolves after cancellation must release its connection too. + void pending.then( + (connection) => connection.abort(), + () => {}, + ); + throw props.signal?.aborted ? props.signal.reason : failure; + } finally { + disarm(); + } } async function acceptInner( diff --git a/js/net/src/connection/connect.ts b/js/net/src/connection/connect.ts index 842c17d70a4..4abe77ceaf0 100644 --- a/js/net/src/connection/connect.ts +++ b/js/net/src/connection/connect.ts @@ -17,15 +17,12 @@ import { isWebTransportSupported } from "./browser.ts"; import type { Established } from "./established.ts"; import * as Extensions from "./extensions.ts"; import { DISCOVERY_REMOVED, forwardAnnounced } from "./forward.ts"; -import { exchangeSetup, handsOff, legacyPeerImplementation } from "./handshake.ts"; +import { DEFAULT_TIMEOUT, exchangeSetup, handsOff, legacyPeerImplementation } from "./handshake.ts"; import { type Credential, credential, fallback, hold } from "./tokens.ts"; // Default head start for WebTransport before attempting the WebSocket fallback. const DEFAULT_WEBSOCKET_DELAY_MS = Milli(500); -// Default bound on one connect, the dial and the MoQ handshake; matches the Rust client. -const DEFAULT_TIMEOUT = Milli(30_000); - // How long the first WebTransport dial may stay unopened before a second one dials alongside // it. The same moment as the WebSocket's default head start, so a fresh WebTransport flow // races the fallback rather than losing to it, which would leave the session on WebSocket for diff --git a/js/net/src/connection/handshake.ts b/js/net/src/connection/handshake.ts index 57503c9d69c..40a4b279ab2 100644 --- a/js/net/src/connection/handshake.ts +++ b/js/net/src/connection/handshake.ts @@ -1,3 +1,4 @@ +import { Milli } from "@moq/clock"; import { closeReason, ProtocolViolation, SessionCode, StreamError } from "../error.ts"; import { type Hop, randomHop } from "../hop.ts"; import * as Ietf from "../ietf/index.ts"; @@ -6,6 +7,9 @@ import * as Lite from "../lite/index.ts"; import { Reader, Stream, UnexpectedEnd, Writer } from "../stream.ts"; import * as Extensions from "./extensions.ts"; +/** Default bound on a connection attempt, including its SETUP exchange. @internal */ +export const DEFAULT_TIMEOUT = Milli(30_000); + /** * Draft-17+ SETUP exchange. Each side opens a uni stream, writes its Setup * message, and reads the peer's Setup off an incoming uni stream. The two diff --git a/js/net/src/connection/reload.test.ts b/js/net/src/connection/reload.test.ts index 247def4228f..c05c329b1ae 100644 --- a/js/net/src/connection/reload.test.ts +++ b/js/net/src/connection/reload.test.ts @@ -1008,13 +1008,10 @@ test("closing the loop keeps the tokens until the session drains", async () => { test("closing an announce consumer during upstream teardown does not append retractions", async () => { const { Producer } = await import("../announced.ts"); const upstream = new Producer(); - const reload = new Reload({ enabled: false }); - reload.established.set({ - probe: new Signal(undefined), - announced: () => upstream.consume(), - stats: async () => undefined, - } as unknown as import("./established.ts").Established); - const consumer = reload.announced(); + const producer = new Producer(); + const pump = new Effect((effect) => pumpAnnounced(effect, upstream.consume(), producer)); + const consumer = producer.consume(); + void consumer.closed.then(() => pump.close()); const errors = spyOn(console, "error").mockImplementation(() => {}); try { upstream.append({ @@ -1032,18 +1029,19 @@ test("closing an announce consumer during upstream teardown does not append retr expect(errors.mock.calls).toEqual([]); } finally { consumer.close(); - reload.close(); + pump.close(); + producer.close(); errors.mockRestore(); } }); test("announced accepts a non-prefix scope before any session exists", () => { - const reload = new Reload({ enabled: false }); + const connection = new Connection({ enabled: false }); try { - const announced = reload.announced(Path.Pattern.parse("room/*")); + const announced = connection.announced(Path.Pattern.parse("room/*")); announced.close(); } finally { - reload.close(); + connection.close(); } }); @@ -1076,14 +1074,15 @@ test("a watcher syncs with the server's initial set, and again after a reconnect globalThis.WebTransport = stub as unknown as typeof WebTransport; const clientOrigin = new OriginProducer(); - const reload = new Reload({ + const props = { enabled: true, url, websocket: { enabled: false }, delay: { initial: Milli(10), multiplier: 1, max: Milli(10) }, - consume: viaOrigin ? clientOrigin : undefined, - }); - const watching = viaOrigin ? clientOrigin.announced() : reload.announced(); + }; + const reload = viaOrigin ? new Reload({ ...props, consume: clientOrigin }) : undefined; + const connection = viaOrigin ? undefined : new Connection({ ...props, share: false }); + const watching = connection ? connection.announced() : clientOrigin.announced(); const seen = new Set(); void (async () => { for await (const event of watching) { @@ -1104,7 +1103,8 @@ test("a watcher syncs with the server's initial set, and again after a reconnect expect([...seen].sort()).toEqual(["a", "b"]); } finally { watching.close(); - reload.close(); + reload?.close(); + connection?.close(); clientOrigin.close(); for (const broadcast of published) broadcast.close(); serverOrigin.close(); diff --git a/js/net/src/connection/reload.ts b/js/net/src/connection/reload.ts index ab40e3c824c..6e03d02292d 100644 --- a/js/net/src/connection/reload.ts +++ b/js/net/src/connection/reload.ts @@ -1,6 +1,6 @@ import { Instant, Milli, Nano, now, sleep } from "@moq/clock"; import { Effect, type GetPromise, type Getter, Once, Signal } from "@moq/signals"; -import * as Announce from "../announced.ts"; +import type * as Announce from "../announced.ts"; import { Handle as AuthHandle } from "../auth.ts"; import { ended } from "../auth_session.ts"; import { Allocator } from "../bandwidth.ts"; @@ -19,7 +19,7 @@ import { import type { Established } from "./established.ts"; import { DISCOVERY_REMOVED } from "./forward.ts"; import { DEFAULT_HANDOVER, type Drain, dialed, type GoawayProps, handover, pinnedTransport, target } from "./goaway.ts"; -import type { Probe, Stats } from "./stats.ts"; +import type { Probe } from "./stats.ts"; import { heldOf } from "./tokens.ts"; function sameTokens(a: readonly string[], b: readonly string[]): boolean { @@ -823,50 +823,6 @@ export class Reload { this.#retire(); } - /** - * Subscribe to broadcast announcements matching `scope`, spanning reconnects. - * - * The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active - * is retracted (an `end` event) whenever the connection drops and re-announced on - * reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect. - */ - announced(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer { - // With a consume origin the table already spans reconnects (the forwarder retracts - // a dead session's entries), so its stream is the same thing with less machinery. - if (this.consume) return this.consume.announced(scope, options); - - const producer = new Announce.Producer(); - const consumer = producer.consume(); - - const pump = new Effect(); - pump.run((effect) => { - const conn = effect.get(this.established); - if (!conn) { - // The same hold as a consume origin's: behind while enabled with no session. - if (effect.get(this.enabled)) producer.behind(); - else producer.synced(); - return; - } - - // A dropped connection retracts what it announced, so a per-broadcast watcher tears - // down instead of clinging to the dead route. - pumpAnnounced(effect, conn.announced(scope, options), producer); - }); - - this.#signals.cleanup(() => pump.close()); - void consumer.closed.then(() => pump.close()); - - return consumer; - } - - /** - * Snapshot the live connection's transport counters, or undefined while disconnected. - * See {@link Established.stats}. - */ - async stats(): Promise { - return this.established.peek()?.stats(); - } - /** Stop reconnecting, drain and close the current connection, and settle {@link Reload.closed}. Idempotent. */ close(abort?: Error) { this.#signals.close(); diff --git a/js/net/src/connection/stats.test.ts b/js/net/src/connection/stats.test.ts index 3853255e6d7..6e7b1155eff 100644 --- a/js/net/src/connection/stats.test.ts +++ b/js/net/src/connection/stats.test.ts @@ -103,7 +103,7 @@ test("probe starts empty and stays empty without PROBE support", async () => { } }); -test("Reload reports stats and probe only while connected", async () => { +test("Reload exposes its established session stats and probe only while connected", async () => { const original = globalThis.WebTransport; const pair = createMockTransportPair(Lite.ALPN_06, { stats: { bytesSent: 99 } }); // `new` on a function returning an object yields that object, handing connect() the mock. @@ -118,7 +118,7 @@ test("Reload reports stats and probe only while connected", async () => { while (!reload.established.peek()) { await new Promise((resolve) => setTimeout(resolve, 0)); } - expect((await reload.stats())?.bytesSent).toBe(99); + expect((await reload.established.peek()?.stats())?.bytesSent).toBe(99); expect(reload.probe.peek()).toEqual({}); server.abort(); @@ -126,7 +126,7 @@ test("Reload reports stats and probe only while connected", async () => { await new Promise((resolve) => setTimeout(resolve, 0)); } expect(reload.status.peek()).toBe("disconnected"); - expect(await reload.stats()).toBeUndefined(); + expect(await reload.established.peek()?.stats()).toBeUndefined(); await new Promise((resolve) => setTimeout(resolve, 0)); expect(reload.probe.peek()).toBeUndefined(); } finally { diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 08b525a37c8..d44e1943d48 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -215,6 +215,7 @@ export class Producer { }; hooks.writeGroupFrame = (group, frame, inflated) => group.#writeFrame(frame, inflated); hooks.groupCacheBytes = (group) => group.#state.flate?.bytes ?? group.#state.cacheBytes; + hooks.groupFrameCount = (group) => group.#state.total.peek(); hooks.groupProgress = (group, fn) => { const state = group.#state; fn(state.total.peek()); @@ -271,7 +272,7 @@ export class Producer { /** A read handle for this group. */ consume(): Consumer { - return makeConsumer(this.#state); + return this.mirror(); } /** @@ -1000,13 +1001,16 @@ export class Consumer { /** Reads the next frame and parses its payload as JSON. */ async readJson(): Promise { const frame = await this.readString(); - return frame ? JSON.parse(frame) : undefined; + return frame !== undefined ? JSON.parse(frame) : undefined; } /** Reads the next frame and decodes its payload as a one-byte boolean. */ async readBool(): Promise { const frame = await this.readFrame(); - return frame ? frame.payload[0] === 1 : undefined; + if (!frame) return undefined; + const payload = frame.payload; + if (payload.byteLength !== 1 || !(payload[0] === 0 || payload[0] === 1)) throw new Error("invalid bool frame"); + return payload[0] === 1; } /** Closes the group, optionally with an error to abort readers. Idempotent. */ diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 12b4658f76b..633163a404a 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -487,6 +487,8 @@ export const hooks: { writeGroupFrame: (group: GroupProducer, frame: Frame, inflated?: number) => void; /** Payload footprint including a shared inflated copy, for measurements. */ groupCacheBytes: (group: GroupProducer) => number; + /** Frame count without creating a read handle. */ + groupFrameCount: (group: GroupProducer) => number; /** * Cap a group at `frames` for a track end inside it, so later writes past it throw. * Throws `ProtocolViolation` if a frame at or past the cap already exists. @@ -666,6 +668,9 @@ export const hooks: { groupCacheBytes: () => { throw new Error("group.ts not loaded"); }, + groupFrameCount: () => { + throw new Error("group.ts not loaded"); + }, bindGroupTimed: () => { throw new Error("group.ts not loaded"); }, diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 84f8ef5441a..ed82e6d7c9d 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -73,6 +73,7 @@ import { hasProbeRtt, hasRanges, hasRefresh, + hasSetupStream, hasSynced, hasTrackStream, hasTracks, @@ -122,12 +123,8 @@ interface SubscribeEntry { // the matching track.Subscriber it got from broadcast.Consumer.subscribe. track: track.Producer; compress: boolean; - // Per-frame timestamp scale (0 = none). undefined until it's known (from TRACK_INFO - // on lite-05+, or implicit defaults on older drafts). A non-zero value means each - // frame on the group stream is prefixed with a zigzag-delta timestamp varint that - // runGroup must consume to stay in sync; group streams block on it before decoding, - // since a group's QUIC stream can race ahead of the subscribe stream. - timescale: Signal; + // Per-frame timestamp scale (0 = none), resolved before this entry is registered. + timescale: number; // The group streams received, so the subscription can wait for the ones still owed // after the publisher ends it. tail: Tail; @@ -163,9 +160,8 @@ export class Subscriber { // Shared with the Publisher so reflected announces can be dropped on receipt. readonly hop: Hop; - // Our subscribed tracks. `timescale` resolves once known (from TRACK_INFO on - // lite-05+, or implicit defaults on older drafts); group streams block on it - // before decoding any frame, since a group's QUIC stream can race ahead. + // Registered only after immutable TRACK_INFO or the older drafts' defaults resolve, + // so every incoming group and datagram already knows its timestamp framing. #subscribes = new Map(); #subscribeNext = 0n; @@ -868,10 +864,6 @@ export class Subscriber { const { epoch, valid } = consumed; const subscription = request.subscription; - // `timescale` stays undefined until TRACK_INFO (or, on older drafts, - // implicit defaults) resolves it; runGroup blocks on it before decoding. - const timescale = new Signal(undefined); - console.debug(`subscribe start: id=${id} broadcast=${broadcast} track=${request.name}`); const bounds = groupBounds(subscription.groups); @@ -899,7 +891,7 @@ export class Subscriber { valid?.addEventListener("abort", retire); if (valid?.aborted) retire(); const deadline = new Deadline(); - const setup = this.#openSubscribe(state, msg, request, id, timescale, deadline); + const setup = this.#openSubscribe(state, msg, request, id, deadline); let opened: { stream: Stream; entry: SubscribeEntry }; try { @@ -1011,13 +1003,13 @@ export class Subscriber { msg: Subscribe, request: track.Request, id: bigint, - timescale: Signal, deadline: Deadline, ): Promise<{ stream: Stream; entry: SubscribeEntry }> { state.cancel.signal.throwIfAborted(); let producer: track.Producer; let drainOk = false; let compress = false; + let timescale = 0; if (hasTrackStream(this.version)) { // Fetch the immutable properties once via the TRACK stream. @@ -1034,11 +1026,10 @@ export class Subscriber { state.cancel.signal.throwIfAborted(); producer = request.accept(this.#toModelInfo(info)); compress = info.compress; - timescale.set(info.timescale ?? 0); + timescale = info.timescale ?? 0; } else { // Older drafts negotiate nothing per-track: verbatim frames with no timeline. producer = request.accept(); - timescale.set(0); drainOk = true; } // Lite-06+ resolves where the feed starts only with SUBSCRIBE_START, so a relay serving @@ -1642,21 +1633,11 @@ export class Subscriber { } track.writeGroup(producer); - // Block until the timescale is known; the group's stream can arrive before - // TRACK_INFO (or implicit defaults) resolves it on the subscribe stream. - let scale = timescale.peek(); - while (scale === undefined) { - if (track.closed.peek() !== undefined) { - // Subscription ended before the scale resolved; nothing to decode. - producer.close(); - stream.stop(StreamCode.Cancel); - return; - } - await Signal.race(timescale, track.closed); - scale = timescale.peek(); - } - - await readFrames(stream, producer, { scale, timed: hasMediaTime(this.version), compress: entry.compress }); + await readFrames(stream, producer, { + scale: timescale, + timed: hasMediaTime(this.version), + compress: entry.compress, + }); producer.close(); stream.stop(StreamCode.Cancel); @@ -1721,21 +1702,18 @@ export class Subscriber { } // Decode one datagram body and hand it to the matching subscription's producer. Drops the - // datagram (best-effort) if the subscription is unknown/closed or its timescale isn't resolved. + // datagram (best-effort) if the subscription is unknown or closed. async #routeDatagram(payload: Uint8Array): Promise { - // An unknown or unresolved subscription's datagram is dropped below, however it parses. + // An unknown subscription's datagram is dropped below, however it parses. const dg = await DatagramMessage.decode(payload, this.version, (id) => ({ - timed: !!this.#subscribes.get(id)?.timescale.peek(), + timed: !!this.#subscribes.get(id)?.timescale, compressed: this.#subscribes.get(id)?.compress ?? false, })); const entry = this.#subscribes.get(dg.subscribe); if (!entry) return; // Unknown or already-closed subscription. - // If the timescale hasn't resolved yet (the datagram raced ahead of TRACK_INFO), drop - // rather than guess. Zero means the track carries no timestamps. - const scale = entry.timescale.peek(); - if (scale === undefined) return; + const scale = entry.timescale; // Dropped where it may be a send time, like a frame's. const timestamp = @@ -1782,7 +1760,7 @@ export class Subscriber { // input ends the session. Drop the estimates so consumers know they're stale. try { // Lite-05+ gates PROBE on the peer advertising Probe >= Report in its SETUP. - if (this.#peerSetup) { + if (hasSetupStream(this.version) && this.#peerSetup) { const level = await this.#peerProbeLevel(this.#peerSetup, scope); if (level === undefined || level < ProbeLevel.Report) return; } diff --git a/js/net/src/net-cleanups.test.ts b/js/net/src/net-cleanups.test.ts new file mode 100644 index 00000000000..95b636cb5e6 --- /dev/null +++ b/js/net/src/net-cleanups.test.ts @@ -0,0 +1,241 @@ +import { expect, spyOn, test } from "bun:test"; +import { Manual, Milli, Nano } from "@moq/clock"; +import { accept } from "./connection/accept.ts"; +import { connect } from "./connection/connect.ts"; +import { ConnectTimeout } from "./error.ts"; +import { Producer as GroupProducer } from "./group.ts"; +import { Connection as LiteConnection } from "./lite/connection.ts"; +import { ALPN_03, ALPN_04, Version } from "./lite/version.ts"; +import { createMockTransportPair } from "./mock.ts"; +import { Producer as TrackProducer } from "./track.ts"; + +test("group read handles independently replay buffered and future frames", async () => { + const group = new GroupProducer(0); + const first = group.consume(); + const payload = (text: string) => ({ payload: new TextEncoder().encode(text) }); + try { + group.writeFrame(payload("a")); + expect(await first.readString()).toBe("a"); + const second = group.consume(); + try { + expect(second.tryReadFrame()?.payload).toEqual(payload("a").payload); + group.writeFrame(payload("b")); + expect(await second.readString()).toBe("b"); + expect(await first.readString()).toBe("b"); + first.close(); + group.writeFrame(payload("c")); + expect(await second.readString()).toBe("c"); + group.close(); + expect(await second.readFrame()).toBeUndefined(); + } finally { + second.close(); + } + } finally { + first.close(); + group.close(); + } +}); + +test.each(["group", "track"] as const)("%s JSON rejects an empty frame without treating it as EOF", async (kind) => { + const group = new GroupProducer(0); + const producer = new TrackProducer("json"); + const reader = kind === "group" ? group.consume() : producer.subscribe().ordered(); + if (kind === "track") producer.writeGroup(group); + group.writeFrame({ payload: new Uint8Array() }); + for (const value of [null, false, 0, ""]) + group.writeFrame({ payload: new TextEncoder().encode(JSON.stringify(value)) }); + group.close(); + producer.close(); + try { + await expect(reader.readJson()).rejects.toThrow(SyntaxError); + for (const value of [null, false, 0, ""]) expect(await reader.readJson()).toBe(value); + expect(await reader.readJson()).toBeUndefined(); + } finally { + reader.close(); + } +}); + +test("group booleans refuse every shape except one byte of zero or one", async () => { + const group = new GroupProducer(0); + const reader = group.consume(); + for (const bytes of [[], [2], [1, 0], [0], [1]]) group.writeFrame({ payload: new Uint8Array(bytes) }); + group.close(); + try { + for (let i = 0; i < 3; i++) await expect(reader.readBool()).rejects.toThrow("invalid bool frame"); + expect(await reader.readBool()).toBe(false); + expect(await reader.readBool()).toBe(true); + expect(await reader.readBool()).toBeUndefined(); + } finally { + reader.close(); + } +}); + +for (const [version, alpn] of [ + [Version.DRAFT_03, ALPN_03], + [Version.DRAFT_04, ALPN_04], +] as const) { + test(`${alpn} two-Connection peers receive PROBE without waiting for nonexistent SETUP`, async () => { + const clock = new Manual(); + const installed = clock.install(); + const pair = createMockTransportPair(alpn, { stats: { estimatedSendRate: 8_000_000, smoothedRtt: 12 } }); + const props = { url: new URL("https://example.com"), version }; + const client = new LiteConnection({ ...props, quic: pair.client }); + const server = new LiteConnection({ ...props, quic: pair.server }); + const dispose = client.probe.subscribe(() => {}); + try { + await clock.advance(Nano.zero); + // The publisher samples on its existing 100ms PROBE cadence. + await clock.advance(Nano.fromMilli(Milli(100))); + expect(client.probe.peek().estimatedRecvRate).toBe(8_000_000); + expect(client.probe.peek().rtt).toBe(version === Version.DRAFT_03 ? undefined : Milli(12)); + } finally { + dispose(); + client.abort(); + server.abort(); + installed.close(); + } + }); +} + +test("accept applies its default handshake deadline and releases a silent transport", async () => { + const clock = new Manual(); + const installed = clock.install(); + const pair = createMockTransportPair("moqt-18"); + let outcome: unknown; + const pending = accept({ transport: pair.server, url: new URL("https://example.com") }).then( + (value) => { + outcome = value; + }, + (error) => { + outcome = error; + }, + ); + try { + await clock.advance(Nano.fromMilli(Milli(29_999))); + expect(outcome).toBeUndefined(); + await clock.advance(Nano.fromMilli(Milli(1))); + expect(outcome).toBeInstanceOf(ConnectTimeout); + expect((await pair.client.closed).closeCode).toBe(0); + } finally { + pair.client.close(); + pair.server.close(); + await pending; + installed.close(); + } +}); + +test("accept releases the transport when protocol selection fails", async () => { + const pair = createMockTransportPair("unsupported"); + const closing = spyOn(pair.server, "close"); + try { + await expect(accept({ transport: pair.server, url: new URL("https://example.com") })).rejects.toThrow( + "unsupported WebTransport protocol", + ); + expect(closing).toHaveBeenCalledTimes(1); + } finally { + pair.client.close(); + pair.server.close(); + closing.mockRestore(); + } +}); + +test("malformed SETUP closes the accepted transport", async () => { + const pair = createMockTransportPair(""); + const pending = accept({ transport: pair.server, url: new URL("https://example.com") }).catch( + (error: unknown) => error, + ); + try { + const stream = await pair.client.createBidirectionalStream(); + const writer = stream.writable.getWriter(); + try { + await writer.write(new Uint8Array([0])); + expect(await pending).toEqual(new Error("unexpected client message type: 0x0")); + expect((await pair.client.closed).closeCode).toBe(0); + } finally { + writer.releaseLock(); + } + } finally { + pair.client.close(); + pair.server.close(); + } +}); + +test("zero accept timeout waits for caller cancellation and preserves its reason", async () => { + const clock = new Manual(); + const installed = clock.install(); + const pair = createMockTransportPair("moqt-18"); + const controller = new AbortController(); + const reason = new Error("caller cancelled SETUP"); + let outcome: unknown; + const pending = accept({ + transport: pair.server, + url: new URL("https://example.com"), + timeout: Milli(0), + signal: controller.signal, + }).then( + (value) => { + outcome = value; + }, + (error) => { + outcome = error; + }, + ); + try { + await clock.advance(Nano.fromMilli(Milli(60_000))); + expect(outcome).toBeUndefined(); + controller.abort(reason); + await pending; + expect(outcome).toBe(reason); + expect((await pair.client.closed).closeCode).toBe(0); + } finally { + pair.client.close(); + pair.server.close(); + await pending; + installed.close(); + } +}); + +test("invalid or pre-aborted accept options never take ownership of the transport", async () => { + const pair = createMockTransportPair("moqt-18"); + const closing = spyOn(pair.server, "close"); + try { + for (const timeout of [-1, Number.NaN, Number.POSITIVE_INFINITY]) { + await expect( + accept({ transport: pair.server, url: new URL("https://example.com"), timeout: timeout as Milli }), + ).rejects.toThrow("invalid time"); + } + const reason = new Error("already cancelled"); + await expect( + accept({ transport: pair.server, url: new URL("https://example.com"), signal: AbortSignal.abort(reason) }), + ).rejects.toBe(reason); + expect(closing).not.toHaveBeenCalled(); + } finally { + closing.mockRestore(); + pair.client.close(); + pair.server.close(); + } +}); + +test.each([ALPN_04, "moqt-18"])("accepted %s sessions outlive their handshake timeout and signal", async (alpn) => { + const clock = new Manual(); + const installed = clock.install(); + const pair = createMockTransportPair(alpn); + const controller = new AbortController(); + const closing = spyOn(pair.server, "close"); + const url = new URL("https://example.com"); + const [client, server] = await Promise.all([ + connect({ url, transport: pair.client }), + accept({ url, transport: pair.server, timeout: Milli(1_000), signal: controller.signal }), + ]); + try { + controller.abort(); + await clock.advance(Nano.fromMilli(Milli(5_000))); + expect(closing).not.toHaveBeenCalled(); + expect(await server.stats()).toEqual({}); + } finally { + closing.mockRestore(); + client.abort(); + server.abort(); + installed.close(); + } +}); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 098d71aad96..5107e2a0305 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1171,7 +1171,7 @@ export class Producer { let largest: Position | undefined; for (const { group } of this.#cache) { - const count = group.consume().frameCount; + const count = hooks.groupFrameCount(group); if (count > 0 && (largest === undefined || group.sequence > largest.group)) { largest = { group: group.sequence, frame: count - 1 }; } @@ -2806,7 +2806,7 @@ export class Subscriber { /** Reads the next frame and parses it as JSON. */ async #readJson(): Promise { const next = await this.#readString(); - if (!next) return undefined; + if (next === undefined) return undefined; return JSON.parse(next); } diff --git a/js/net/src/zod.test.ts b/js/net/src/zod.test.ts index ddc8cee2798..370fb556035 100644 --- a/js/net/src/zod.test.ts +++ b/js/net/src/zod.test.ts @@ -7,11 +7,13 @@ import * as Zod from "./zod.ts"; test("read validates each frame and ends only at the end of the group", async () => { const producer = new Producer(0); const consumer = producer.consume(); + producer.writeFrame({ payload: new Uint8Array() }); // Falsy values are frames like any other, not the end of the stream. for (const json of ["0", "false", "null", '"ok"']) producer.writeFrame(textFrame(json)); producer.close(); const schema = z.union([z.number(), z.boolean(), z.null(), z.string()]); + await expect(Zod.read(consumer, schema)).rejects.toThrow(SyntaxError); expect(await Zod.read(consumer, schema)).toBe(0); expect(await Zod.read(consumer, schema)).toBe(false); expect(await Zod.read(consumer, schema)).toBe(null); diff --git a/quest/m1/cut-audit/README.md b/quest/m1/cut-audit/README.md index a84b26c68c0..dfc0b249e33 100644 --- a/quest/m1/cut-audit/README.md +++ b/quest/m1/cut-audit/README.md @@ -80,7 +80,6 @@ leaving JS-only redesigns of `js/net` to [rs2ts](/quest/m2/rs2ts/README.md). - [The Unix-socket allowlist is checked first](/quest/m1/cut-audit/uds-allowlist-first.md) - refused local users never reach the qmux parser - [A route's terminal state is one phase](/quest/m1/cut-audit/route-phase.md) - three resume flags collapse into one check - [Lite's version gates, refusals, and draft text agree](/quest/m1/cut-audit/lite-wire-nits.md) - small wire and draft mismatches fixed -- [Small @moq/net divergences from Rust are fixed](/quest/m1/cut-audit/js-net-cleanups.md) - lite-04 probes, group consumers, accept deadlines, empty JSON - [Signals and the encoded preview keep their promises](/quest/m1/cut-audit/js-media-cleanups.md) - `Computed.close()` and the encoded preview behave as documented ## Related diff --git a/quest/m1/cut-audit/js-net-cleanups.md b/quest/m1/cut-audit/js-net-cleanups.md deleted file mode 100644 index 5e92b416741..00000000000 --- a/quest/m1/cut-audit/js-net-cleanups.md +++ /dev/null @@ -1,49 +0,0 @@ -# [S] Small @moq/net divergences from Rust are fixed - -## Goal - -`@moq/net` matches Rust on four small behaviors the pre-cut audit found: -lite-03/04 sessions get probe estimates, a group's `consume()` starts at -frame 0, `Connection.accept()` has the same handshake deadline and -close-on-failure as `connect()`, and an empty JSON frame fails instead of -reading as EOF. Dead code found alongside is gone. - -## Plan - -Found by the 2026-10-24 pre-cut audit at 151a529670. - -- **No PROBE on lite-03/04** (reproduced, inherited): - `js/net/src/lite/subscriber.ts:1753` waits on `#peerProbeLevel`, but - lite-03/04 have no SETUP stream, so it never resolves and ABR gets no - estimates on moq-lite-04, which is in the default list. Gate on - `hasSetupStream(version)` like Rust (`rs/moq-net/src/lite/subscriber.rs:1453`). - Add a two-`Connection` test; the unit tests build `Subscriber` without - `peerSetup`, which hid this. -- **`group.Producer.consume()` shares a cursor** (reproduced, inherited): - `group.ts:269` returns `makeConsumer(this.#state)`, so a later reader - starts where an earlier one left off. Return an independent handle, as - `mirror()` does; `#cachedLargest` (`track.ts:1262`) needs only - `frameCount`. -- **`Connection.accept()` has no deadline** (source-inferred, inherited): - `connection/accept.ts` waits forever for SETUP and leaves the transport - open on a malformed one. Reuse `connectTransport`'s race-and-close, with - additive `timeout`/`signal` options, matching Rust's `server.rs` - `Error::Timeout`. -- **Empty JSON frame reads as EOF** (reproduced, inherited): `group.ts:993` - and `track.ts:2821` treat an empty frame as end of stream, so `zod.read` - stops silently; `group.Consumer.readBool` skips validation. Fail loud. -- **Dead code:** the `timescale` Signal wait in `lite/subscriber.ts:1615` - and `:1705` can never block (it is set before the entry registers); make - it a plain number. `Reload.announced()` and `stats()` - (`connection/reload.ts:660`) appeared to have only test consumers (the - public `Connection` in `pool.ts` has its own), and `announced()` registers - a root cleanup per call that is never released, the pile-up `pool.ts:341` - already fixed. Confirm no other consumer, then delete both; otherwise fix - the leak. - -Public API: `accept()` gains optional `timeout`/`signal`; `Reload.announced()` -and `stats()` are removed if `Reload` is exported. Wire: none. - -## Related - -- [moq-dev/moq#6756](https://github.com/moq-dev/moq/pull/6756) - the other `Reload` change before the cut, now landed diff --git a/test/interop/clients/js/net-cleanups.browser.ts b/test/interop/clients/js/net-cleanups.browser.ts new file mode 100644 index 00000000000..f64a1624376 --- /dev/null +++ b/test/interop/clients/js/net-cleanups.browser.ts @@ -0,0 +1,32 @@ +import { expect, test } from "bun:test"; +import { createServer } from "vite"; +import { launch } from "./harness"; + +test("public net consumers and lite probes work in native browser streams", async () => { + const server = await createServer({ + configFile: false, + root: `${import.meta.dir}/net-cleanups`, + server: { host: "127.0.0.1", port: 0 }, + }); + await server.listen(); + const browser = await launch(); + try { + const page = await browser.newPage(); + const errors: string[] = []; + page.on("pageerror", (error) => errors.push(error.message)); + await page.goto(server.resolvedUrls?.local[0] ?? ""); + const result = await page.evaluate(async () => (await import(`${location.origin}/main.ts`)).run()); + expect(result.cursors).toEqual(["first", "first", "second"]); + expect(result.malformed).toBe(true); + expect(result.value).toBe(false); + expect(result.probes).toEqual([{ rate: 8_000_000 }, { rate: 8_000_000, rtt: 12 }]); + expect(result.rows).toHaveLength(18); + // Timings are evidence, not a noise-sensitive acceptance threshold. The shared sweep + // itself checks complete delivery, shared payload identity and reader-independent bytes. + console.log("browser compressed group fanout", JSON.stringify(result.rows)); + expect(errors).toEqual([]); + } finally { + await browser.close(); + await server.close(); + } +}, 30_000); diff --git a/test/interop/clients/js/net-cleanups/index.html b/test/interop/clients/js/net-cleanups/index.html new file mode 100644 index 00000000000..4850a647bd8 --- /dev/null +++ b/test/interop/clients/js/net-cleanups/index.html @@ -0,0 +1,3 @@ + + +Net consumer lifetimes diff --git a/test/interop/clients/js/net-cleanups/main.ts b/test/interop/clients/js/net-cleanups/main.ts new file mode 100644 index 00000000000..6c1388df200 --- /dev/null +++ b/test/interop/clients/js/net-cleanups/main.ts @@ -0,0 +1,59 @@ +/** Public net consumers and native browser streams, plus the shared fan-out sweep. @module */ + +import { Manual, Milli, Nano } from "@moq/clock"; +import * as Net from "@moq/net"; +import { fanout } from "../../../../../js/net/bench/compression-fanout"; +import { createMockTransportPair } from "../../../../../js/net/src/mock"; + +/** Exercise public readers and accepted connections in the browser. */ +export async function run() { + const group = new Net.Group.Producer(0); + group.writeFrame({ payload: new TextEncoder().encode("first") }); + const first = group.consume(); + const a = await first.readString(); + const second = group.consume(); + const b = await second.readString(); + first.close(); + group.writeFrame({ payload: new TextEncoder().encode("second") }); + const c = await second.readString(); + group.close(); + second.close(); + const json = new Net.Group.Producer(0); + const reader = json.consume(); + json.writeFrame({ payload: new Uint8Array() }); + json.writeFrame({ payload: new TextEncoder().encode("false") }); + json.close(); + let malformed = false; + try { + await reader.readJson(); + } catch (error) { + malformed = error instanceof SyntaxError; + } + const value = await reader.readJson(); + reader.close(); + const probes: { rate?: number; rtt?: number }[] = []; + for (const alpn of ["moq-lite-03", "moq-lite-04"]) { + const clock = new Manual(); + const installed = clock.install(); + const pair = createMockTransportPair(alpn, { stats: { estimatedSendRate: 8_000_000, smoothedRtt: 12 } }); + const controller = new AbortController(); + const url = new URL("https://example.com"); + const [client, server] = await Promise.all([ + Net.Connection.connect({ url, transport: pair.client }), + Net.Connection.accept({ url, transport: pair.server, timeout: Milli(1_000), signal: controller.signal }), + ]); + const dispose = client.probe.subscribe(() => {}); + try { + controller.abort(); // Acceptance no longer owns the session's lifetime. + await clock.advance(Nano.zero); + await clock.advance(Nano.fromMilli(Milli(100))); + probes.push({ rate: client.probe.peek().estimatedRecvRate, rtt: client.probe.peek().rtt }); + } finally { + dispose(); + client.abort(); + server.abort(); + installed.close(); + } + } + return { cursors: [a, b, c], malformed, value, probes, rows: fanout() }; +} diff --git a/test/interop/clients/js/tsconfig.json b/test/interop/clients/js/tsconfig.json index 0105b8e4c5e..e899217cdab 100644 --- a/test/interop/clients/js/tsconfig.json +++ b/test/interop/clients/js/tsconfig.json @@ -4,6 +4,8 @@ "types": ["bun", "vite/client"] }, "include": [ + "net-cleanups.browser.ts", + "net-cleanups", "close.ts", "driver.ts", "harness.ts", diff --git a/test/justfile b/test/justfile index e0a35035d65..1d2cb67172b 100644 --- a/test/justfile +++ b/test/justfile @@ -18,6 +18,7 @@ qmux_examples := absolute_path(env("CARGO_TARGET_DIR", source_directory() / ".." harness_browser_tests := ''' ./capture-channel-count.browser.ts + ./net-cleanups.browser.ts ./harness.browser.ts ./open-gop.browser.ts ./publish-ui.browser.ts From df824275c84184d064840277febf448eb8a280a0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 26 Oct 2026 23:21:41 -0700 Subject: [PATCH 3/5] test(net): drive the PROBE test until the estimate lands The test asserted right after advancing past the 100ms PROBE cadence, but the shared write budget can yield a host task mid-write once 4ms of real time has passed, so advance returned before the PROBE reached the client. Drive the clock until the probe signal reports the estimate instead. Co-Authored-By: Claude Opus 5.5 --- js/net/src/net-cleanups.test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/js/net/src/net-cleanups.test.ts b/js/net/src/net-cleanups.test.ts index 95b636cb5e6..1a4b91bb020 100644 --- a/js/net/src/net-cleanups.test.ts +++ b/js/net/src/net-cleanups.test.ts @@ -83,9 +83,13 @@ for (const [version, alpn] of [ const server = new LiteConnection({ ...props, quic: pair.server }); const dispose = client.probe.subscribe(() => {}); try { - await clock.advance(Nano.zero); - // The publisher samples on its existing 100ms PROBE cadence. - await clock.advance(Nano.fromMilli(Milli(100))); + // The publisher samples on its 100ms PROBE cadence, and a shared write budget may + // yield a host task mid-delivery, so drive time until the estimate actually lands. + await clock.drive( + (async () => { + while (client.probe.peek().estimatedRecvRate === undefined) await client.probe.changed(); + })(), + ); expect(client.probe.peek().estimatedRecvRate).toBe(8_000_000); expect(client.probe.peek().rtt).toBe(version === Version.DRAFT_03 ? undefined : Milli(12)); } finally { From e0ce52f9ab64aac856fc0eb732316978daeebc32 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 02:29:14 -0700 Subject: [PATCH 4/5] test(net): adopt createGroup and mirror production lifecycles Merge origin/main brought #6751, which renamed Track.Producer.writeGroup to createGroup. The interop probe fixture now drives the clock until the estimate lands, and the announce teardown test uses closeUnwanted like Connection.announced. Co-Authored-By: Claude Opus 5.5 --- js/net/src/connection/reload.test.ts | 6 +++++- js/net/src/net-cleanups.test.ts | 6 +++--- test/interop/clients/js/net-cleanups/main.ts | 10 +++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/js/net/src/connection/reload.test.ts b/js/net/src/connection/reload.test.ts index 35f9189aab7..6cca856ef05 100644 --- a/js/net/src/connection/reload.test.ts +++ b/js/net/src/connection/reload.test.ts @@ -12,6 +12,7 @@ import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; import { Stream } from "../stream.ts"; import { Timescale } from "../time.ts"; +import { closeUnwanted } from "../util/announce.ts"; import { nextTask } from "../util/yield.ts"; import { wireOf } from "../wire.ts"; import type { Established } from "./established.ts"; @@ -1015,7 +1016,9 @@ test("closing an announce consumer during upstream teardown does not append retr const producer = new Producer(); const pump = new Effect((effect) => pumpAnnounced(effect, upstream.consume(), producer)); const consumer = producer.consume(); - void consumer.closed.then(() => pump.close()); + // The same lifecycle as Connection.announced: the last consumer closing closes the producer and its pump. + closeUnwanted(producer); + void producer.closed.then(() => pump.close()); const errors = spyOn(console, "error").mockImplementation(() => {}); try { upstream.append({ @@ -1030,6 +1033,7 @@ test("closing an announce consumer during upstream teardown does not append retr await Promise.resolve(); consumer.close(); await settle(); + expect(producer.closed.peek()).not.toBeUndefined(); expect(errors.mock.calls).toEqual([]); } finally { consumer.close(); diff --git a/js/net/src/net-cleanups.test.ts b/js/net/src/net-cleanups.test.ts index 960a73deeca..1f210bc1515 100644 --- a/js/net/src/net-cleanups.test.ts +++ b/js/net/src/net-cleanups.test.ts @@ -37,10 +37,10 @@ test("group read handles independently replay buffered and future frames", async }); test.each(["group", "track"] as const)("%s JSON rejects an empty frame without treating it as EOF", async (kind) => { - const group = new GroupProducer(0); const producer = new TrackProducer("json"); - const reader = kind === "group" ? group.consume() : producer.subscribe().ordered(); - if (kind === "track") producer.writeGroup(group); + const subscriber = kind === "track" ? producer.subscribe().ordered() : undefined; + const group = kind === "track" ? producer.createGroup(0) : new GroupProducer(0); + const reader = subscriber ?? group.consume(); group.writeFrame({ payload: new Uint8Array() }); for (const value of [null, false, 0, ""]) group.writeFrame({ payload: new TextEncoder().encode(JSON.stringify(value)) }); diff --git a/test/interop/clients/js/net-cleanups/main.ts b/test/interop/clients/js/net-cleanups/main.ts index 5dc4b291230..8f1a3428168 100644 --- a/test/interop/clients/js/net-cleanups/main.ts +++ b/test/interop/clients/js/net-cleanups/main.ts @@ -1,6 +1,6 @@ /** Public net consumers and native browser streams, plus the shared fan-out sweep. @module */ -import { Manual, Milli, Nano } from "@moq/clock"; +import { Manual, Milli } from "@moq/clock"; import * as Net from "@moq/net"; import { fanout } from "../../../../../js/net/bench/compression-fanout"; import { createMockTransportPair } from "../../../../../js/net/src/mock"; @@ -45,8 +45,12 @@ export async function run() { const dispose = client.probe.subscribe(() => {}); try { controller.abort(); // Acceptance no longer owns the session's lifetime. - await clock.advance(Nano.zero); - await clock.advance(Nano.fromMilli(Milli(100))); + // A shared write budget may yield a host task mid-delivery, so drive time until the estimate lands. + await clock.drive( + (async () => { + while (client.probe.peek().estimatedRecvRate === undefined) await client.probe.changed(); + })(), + ); probes.push({ rate: client.probe.peek().estimatedRecvRate, rtt: client.probe.peek().rtt }); } finally { dispose(); From d3063b0f425a0ed0d4bf9a728902a9dd5233dfe2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 02:33:48 -0700 Subject: [PATCH 5/5] test(net): group PTS test reads per handle #6910's test assumed consume() shared the producer's queue, so reading it trimmed what a later mirror replays. Every reader is now an independent mirror, so the later mirror replays all frames and still keeps the high PTS. Co-Authored-By: Claude Opus 5.5 --- js/net/src/group.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/js/net/src/group.test.ts b/js/net/src/group.test.ts index 62faf87a33b..cb258019da4 100644 --- a/js/net/src/group.test.ts +++ b/js/net/src/group.test.ts @@ -20,12 +20,14 @@ test("reordered frames keep the highest group PTS in live and later mirrors", () expect(hooks.groupTimestamp(shared)?.asMillis()).toBe(0); expect(hooks.groupLatest(shared)?.asMillis()).toBe(80); expect(hooks.groupLatest(live)?.asMillis()).toBe(80); - // A later mirror only replays the remaining B-frame, but keeps the known high PTS. + // A reader left with only the B-frame keeps the high PTS it already read past. shared.tryReadFrame(); shared.tryReadFrame(); + expect(hooks.groupLatest(shared)?.asMillis()).toBe(80); + // Reads are per handle, so a later mirror still replays every frame. const later = producer.mirror(); expect(hooks.groupLatest(later)?.asMillis()).toBe(80); - expect(later.tryReadFrame()?.timestamp?.asMillis()).toBe(40); + expect(later.tryReadFrame()?.timestamp?.asMillis()).toBe(0); for (const reader of [shared, live, later]) reader.close(); producer.finish(); } finally {