From f6825226ebffc59bbdc3eb9fbd1ae936ac854032 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 16:05:07 -0800 Subject: [PATCH 1/3] chore(quest): claim quest/m0/test-flakes-2/fingerprint-host-clock.md Quest-Claim: b665c07f18618dbb From 6b2d476c15a3c67648419d0fba9dfcc079d2ea88 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 16:56:01 -0800 Subject: [PATCH 2/3] fix(net): arm the WebTransport hedge after the http:// fingerprint resolves A fingerprint fetch slower than the 500 ms hedge fired the hedge timer on the host clock and started a second fetch and dial. Resolve the dial target once, then hedge only the dial itself. Co-Authored-By: Claude Opus 5.5 --- js/net/src/connection/connect.test.ts | 36 +++++++++++++++++++ js/net/src/connection/connect.ts | 25 ++++++++++--- quest/m0/test-flakes-2/README.md | 1 - .../test-flakes-2/fingerprint-host-clock.md | 30 ---------------- 4 files changed, 56 insertions(+), 36 deletions(-) delete mode 100644 quest/m0/test-flakes-2/fingerprint-host-clock.md diff --git a/js/net/src/connection/connect.test.ts b/js/net/src/connection/connect.test.ts index 0f516a9ed4f..307a24a7ee4 100644 --- a/js/net/src/connection/connect.test.ts +++ b/js/net/src/connection/connect.test.ts @@ -173,6 +173,42 @@ test("an http:// URL pins the fetched fingerprint and dials https", async () => ]); }); +test("a fingerprint fetch slower than the hedge starts no second dial", async () => { + using clock = manual(); + // Hold the fingerprint until the clock has passed the 500 ms hedge: the hedge arms only once + // the fingerprint resolves, so a slow fetch neither fires it nor fetches or dials twice. + const requested = Promise.withResolvers(); + const release = Promise.withResolvers(); + const server = fingerprintServer(async () => { + requested.resolve(); + await release.promise; + return new Response("ab".repeat(32)); + }); + const pair = createMockTransportPair(ALPN_06); + const original = globalThis.WebTransport; + let dials = 0; + function StubWebTransport(this: unknown) { + dials++; + return pair.client; + } + globalThis.WebTransport = StubWebTransport as unknown as typeof WebTransport; + + try { + const connecting = connect(server.url, { websocket: { enabled: false } }); + await requested.promise; + await clock.advance(Nano.fromMilli(Milli(1_000))); + release.resolve(); + const connection = await connecting; + connection.abort(); + } finally { + globalThis.WebTransport = original; + server.stop(); + } + + expect(dials).toBe(1); + expect(server.requests()).toBe(1); +}); + test("an http:// fingerprint longer than a SHA-256 is refused without reading the rest", async () => { // A chunked body with no Content-Length that never ends: only a bounded read can refuse it. const chunk = new TextEncoder().encode("a".repeat(1024)); diff --git a/js/net/src/connection/connect.ts b/js/net/src/connection/connect.ts index 3e32c75efc3..364e540ffe2 100644 --- a/js/net/src/connection/connect.ts +++ b/js/net/src/connection/connect.ts @@ -711,7 +711,12 @@ async function hedgeWebTransport( alpns: string[], options?: WebTransportProps, ): Promise { - const first = connectWebTransport(url, cancel, alpns, options); + // Resolve pins (and fetch an http:// fingerprint) once, before the hedge delay starts, so a slow + // fingerprint fetch can't start a second dial and both dials share the one fetch. + const target = await resolveWebTransport(url, cancel, alpns, options); + if (!target) return undefined; + + const first = dialWebTransport(target, cancel); // The first dial opening, failing, or being cancelled settles this before the delay does. const hedge = await timeout(Nano.fromMilli(WEBTRANSPORT_HEDGE), first).then( @@ -721,7 +726,7 @@ async function hedgeWebTransport( if (!hedge) return first; console.debug(redact(url), `WebTransport not open after ${WEBTRANSPORT_HEDGE}ms; dialing a second`); - const dials = [first, connectWebTransport(url, cancel, alpns, options)]; + const dials = [first, dialWebTransport(target, cancel)]; try { const winner = await Promise.any(dials); closeLosers(dials, winner); @@ -733,12 +738,18 @@ async function hedgeWebTransport( } } -async function connectWebTransport( +interface WebTransportTarget { + url: URL; + options: WebTransportOptions; +} + +/** Resolve the URL and options to dial, or `undefined` once `cancel` resolves. */ +async function resolveWebTransport( url: URL, cancel: Promise, alpns: string[], options?: WebTransportProps, -): Promise { +): Promise { let finalUrl = url; // Our custom pinning fields are normalized separately; the rest are DOM options. @@ -781,7 +792,11 @@ async function connectWebTransport( finalOptions.serverCertificateHashes = hashes; } - const quic = new WebTransport(finalUrl, finalOptions); + return { url: finalUrl, options: finalOptions }; +} + +async function dialWebTransport(target: WebTransportTarget, cancel: Promise): Promise { + const quic = new WebTransport(target.url, target.options); // Both .ready and .closed reject on failure; catch .closed to avoid an unhandled rejection. quic.closed.catch(() => {}); diff --git a/quest/m0/test-flakes-2/README.md b/quest/m0/test-flakes-2/README.md index b035a89b3be..b917c1a9bda 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -180,6 +180,5 @@ Public API: none. Wire: none. ## Required - [Fallback playback](/quest/m0/test-flakes-2/ietf22-fallback.md) - a browser viewer follows a fallback within the media lane's bound without dropping its session -- [The http:// fingerprint tests survive a slow fetch](/quest/m0/test-flakes-2/fingerprint-host-clock.md) - a fingerprint fetch slower than the 500 ms hedge no longer trips the host-clock guard - [A lite-07 relative start resolves to the publisher's start every time](/quest/m0/test-flakes-2/lite07-relative-start.md) - `js/net`'s frameless-newest-group integration test stops reading a start of 0, and its `test.skip` is removed - [A lite-07 reply FINs instead of resetting when the subscriber FINs mid-write](/quest/m0/test-flakes-2/lite07-reply-fin.md) - `js/net`'s empty-window reply test passes under `--rerun-each 200`, fixed in the publisher diff --git a/quest/m0/test-flakes-2/fingerprint-host-clock.md b/quest/m0/test-flakes-2/fingerprint-host-clock.md deleted file mode 100644 index 88b998b367d..00000000000 --- a/quest/m0/test-flakes-2/fingerprint-host-clock.md +++ /dev/null @@ -1,30 +0,0 @@ -# [XS] The http:// fingerprint tests survive a slow fetch - -## Goal - -`js/net/src/connection/connect.test.ts`'s http:// fingerprint tests pass -under load: a fingerprint fetch slower than `hedgeWebTransport`'s 500 ms -hedge no longer fires a host-clock timer that the `bun test` guard fails. - -## Plan - -Planned 2026-11-07 (maintainer, /quest-complete), a follow-up of -[moq-dev/moq#8163](https://github.com/moq-dev/moq/pull/8163), whose CI run -[42402778685](https://github.com/moq-dev/moq/actions/runs/42402778685/job/128493969468) -failed `test js` on a PR that doesn't touch `js/net`. The failing test, -"an http:// fingerprint longer than a SHA-256 is refused without reading -the rest" (`connect.test.ts:176` at the time), took 894 ms: its connect -arms the hedge timer before `connectWebTransport` fetches the fingerprint -from a real `Bun.serve` server, the fetch outlasted 500 ms, and the timer -fired on the host clock inside that same test. `timeout` already disarms -when its dial settles (`js/clock/src/timer.ts`), so no timer outlives its -test. - -The three http:// fingerprint tests at `:147`, `:176`, and `:208` talk to a -real HTTP server but lack the `hostTime(); // a real HTTP server` opt-out -their sibling at `:230` carries, which the mock-clock epic's rules allow for -real I/O. Decide in the PR between adding that opt-out and arming the hedge -only after the fingerprint resolves, so a slow fetch can't start a second -dial. Prove it by delaying the fingerprint response past the hedge. - -Public API: none. Wire: none. From 660230b91dcf4c23907a01530aa7e6451e060887 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 21:54:36 -0800 Subject: [PATCH 3/3] test(net): pin the hedge fix on its timer log, and stop the ticker test's leaked loop The slow-fingerprint test now asserts no 'dialing a second' line before the fingerprint is released; the dial/request counts raced connect() cancelling the hedge. Fails 15/15 with the fix reverted, passes 20/20 here. ticker.test.ts left its last subscriber running on a closed Manual clock, so bandwidth.test.ts's sampler joined that dead loop when it ran later. Co-Authored-By: Claude Opus 5.5 --- js/net/src/connection/connect.test.ts | 5 +++++ js/net/src/connection/ticker.test.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/js/net/src/connection/connect.test.ts b/js/net/src/connection/connect.test.ts index 307a24a7ee4..4b6e1fed94b 100644 --- a/js/net/src/connection/connect.test.ts +++ b/js/net/src/connection/connect.test.ts @@ -192,15 +192,20 @@ test("a fingerprint fetch slower than the hedge starts no second dial", async () return pair.client; } globalThis.WebTransport = StubWebTransport as unknown as typeof WebTransport; + const captured = captureConsole(); try { const connecting = connect(server.url, { websocket: { enabled: false } }); await requested.promise; await clock.advance(Nano.fromMilli(Milli(1_000))); + // The hedge logs inside its timer callback, so this catches it as deterministically as the + // clock; the dial/request counts below race connect() cancelling the hedge once one dial wins. + expect(captured.lines.filter((line) => line.includes("dialing a second"))).toEqual([]); release.resolve(); const connection = await connecting; connection.abort(); } finally { + captured.restore(); globalThis.WebTransport = original; server.stop(); } diff --git a/js/net/src/connection/ticker.test.ts b/js/net/src/connection/ticker.test.ts index c87600b4f76..876d345461d 100644 --- a/js/net/src/connection/ticker.test.ts +++ b/js/net/src/connection/ticker.test.ts @@ -25,9 +25,11 @@ test("subscribers share one tick and the loop stops with the last", async () => expect(ticks).toEqual(["a", "b", "b"]); // A stopped loop restarts for a later subscriber, rather than leaving the page unsampled. - tick(period, () => ticks.push("c")); + const c = tick(period, () => ticks.push("c")); await clock.advance(period); expect(ticks).toEqual(["a", "b", "b", "c"]); + // Stop it, or the shared loop outlives this clock and a later file's ticker joins it. + c(); } finally { installed.close(); }