diff --git a/js/net/bench/qmux.ts b/js/net/bench/qmux.ts index f9b08490b63..2c1b92bdca5 100644 --- a/js/net/bench/qmux.ts +++ b/js/net/bench/qmux.ts @@ -1,9 +1,17 @@ /** Compare eager and cold lazy fallback startup with real WebSocket negotiation. */ import { basename } from "node:path"; import { gzipSync } from "node:zlib"; -import { selectSubprotocol } from "@moq/qmux"; +import { Session, selectSubprotocol, type WebSocketStreamCloseEvent, type WebSocketStreamCloseInfo } from "@moq/qmux"; import { type Browser, chromium } from "playwright"; +/** Per-connection state from the upgrade; `open` attaches the frame feed. */ +interface Peer { + url: string; + protocol: string; + closed: PromiseWithResolvers; + incoming?: ReadableStreamDefaultController; +} + const build = await Bun.build({ entrypoints: [`${import.meta.dir}/qmux-eager.ts`, `${import.meta.dir}/qmux-lazy.ts`], target: "browser", @@ -16,14 +24,15 @@ const files = new Map( build.outputs.map(async (file) => [basename(file.path), gzipSync(await file.arrayBuffer())] as const), ), ); -const server = Bun.serve({ +const server = Bun.serve({ port: 0, fetch(request, server) { const path = new URL(request.url).pathname; if (path === "/qmux") { const protocol = selectSubprotocol(request.headers.get("Sec-WebSocket-Protocol"), {}); if (!protocol) return new Response("unsupported QMux protocol", { status: 400 }); - if (server.upgrade(request, { headers: { "Sec-WebSocket-Protocol": protocol } })) return; + const data: Peer = { url: request.url, protocol, closed: Promise.withResolvers() }; + if (server.upgrade(request, { data, headers: { "Sec-WebSocket-Protocol": protocol } })) return; return new Response("upgrade required", { status: 400 }); } const code = files.get(path.slice(1)); @@ -34,7 +43,46 @@ const server = Bun.serve({ headers: { "Content-Type": "text/html" }, }); }, - websocket: { message() {} }, + websocket: { + // Accept the upgraded socket like a real server, so Session sends its transport parameters first. + open(ws) { + const peer = ws.data; + const session = Session.accept({ + url: peer.url, + opened: Promise.resolve({ + protocol: peer.protocol, + extensions: "", + readable: new ReadableStream({ + start: (controller) => { + peer.incoming = controller; + }, + }), + writable: new WritableStream({ + write: (bytes) => { + ws.send(bytes); + }, + }), + }), + closed: peer.closed.promise, + close: (info?: WebSocketStreamCloseInfo) => ws.close(info?.closeCode, info?.reason), + }); + // The client only sees the socket close, so log the server-side cause, such as a handshake timeout. + session.closed.catch((err: unknown) => console.error("qmux fake peer:", err)); + }, + message(ws, message) { + // QMux is binary-only. Close rather than throw, so the session ends with the close. + if (typeof message === "string") { + ws.close(1003, "QMux frames are binary"); + return; + } + if (!ws.data.incoming) throw new Error("QMux frame before the socket opened"); + ws.data.incoming.enqueue(message); + }, + close(ws, code, reason) { + ws.data.incoming?.close(); + ws.data.closed.resolve({ closeCode: code, reason }); + }, + }, }); let browser: Browser | undefined; try { diff --git a/quest/m0/nightly/README.md b/quest/m0/nightly/README.md index 3a82a9d0589..2bfe1c3e8ae 100644 --- a/quest/m0/nightly/README.md +++ b/quest/m0/nightly/README.md @@ -51,7 +51,6 @@ near the paired threshold (re-check if one repeats), a one-off ## Required - [Nightly's macOS platform job passes](/quest/m0/nightly/macos.md) - `moq-audio`'s tests compile on macOS again and the websocket version test's timeout is fixed if it recurs -- [The qmux startup benchmark's fake peer sends transport parameters](/quest/m0/nightly/qmux-bench-peer.md) - Audio quality's WebSocket startup step stops timing out - [The moq-net session bench completes moq-transport-22 at 16 broadcasts](/quest/m0/nightly/session-bench-handshake.md) - the bench workload stops panicking on a handshake timeout - [Bisect the moq-audio bus and moq-mux parameter_sets regressions](/quest/m0/nightly/bench-regressions.md) - two real benchmark regressions are explained and fixed or accepted - [Moxygen conformance cells pass again](/quest/m0/nightly/moxygen-conformance.md) - the first-object signal and integer extensions survive, or moxygen's divergence is skipped with an upstream link diff --git a/quest/m0/nightly/qmux-bench-peer.md b/quest/m0/nightly/qmux-bench-peer.md deleted file mode 100644 index e77df5d870d..00000000000 --- a/quest/m0/nightly/qmux-bench-peer.md +++ /dev/null @@ -1,23 +0,0 @@ -# [XS] The qmux startup benchmark's fake peer sends transport parameters - -## Goal - -Nightly's Audio quality job passes its "WebSocket fallback startup benchmark" -step: `just js qmux-bench` completes instead of failing with -`error: evaluate: Error: handshake timeout`. - -## Plan - -Planned 2026-11-09 (maintainer, /quest-complete from the triage of Nightly -runs [42377867523](https://github.com/moq-dev/moq/actions/runs/42377867523) -and [42487375545](https://github.com/moq-dev/moq/actions/runs/42487375545)). -Failed both nights. - -The Bun server in `js/net/bench/qmux.ts` never sends QMux transport -parameters, and the JS session now waits for them before it opens. -[moq-dev/moq#8290](https://github.com/moq-dev/moq/pull/8290) fixes the same -fake-peer handshake in `js/qmux/bench/datagrams.ts`, but Nightly runs the -`js/net` copy. Make the fake peer send them the way the real server does, -ideally through the same helper as `datagrams.ts` so the two cannot drift. - -Public API: none. Wire: none.