diff --git a/js/net/src/connection/connect.test.ts b/js/net/src/connection/connect.test.ts index 6d6d4ad93ed..491c14cc76c 100644 --- a/js/net/src/connection/connect.test.ts +++ b/js/net/src/connection/connect.test.ts @@ -173,6 +173,47 @@ 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; + 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(); + } + + 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 5f1734fb39a..1b6baa32a72 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 3e995aae68e..d6542004e4b 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -204,7 +204,6 @@ 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 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 - [moq-relay's forged-dial test waits for bob's endpoint](/quest/m0/test-flakes-2/forged-dial-announce.md) - `a_forged_dial_never_connects` requests bob's endpoint only after its announce reaches mallory, and its `#[ignore]` is removed - [moq-uring's qlog accounting test waits on the accounting](/quest/m0/test-flakes-2/qlog-staging-spin.md) - `an_open_trace_accounts_for_staging_and_close` waits for the accounting, not 10,000 spins 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.