Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions js/net/src/connection/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();
const release = Promise.withResolvers<void>();
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);
Comment thread
kixelated marked this conversation as resolved.
Resolved
});

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));
Expand Down
25 changes: 20 additions & 5 deletions js/net/src/connection/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,12 @@ async function hedgeWebTransport(
alpns: string[],
options?: WebTransportProps,
): Promise<WebTransport | undefined> {
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(
Expand All @@ -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);
Expand All @@ -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<void>,
alpns: string[],
options?: WebTransportProps,
): Promise<WebTransport | undefined> {
): Promise<WebTransportTarget | undefined> {
let finalUrl = url;

// Our custom pinning fields are normalized separately; the rest are DOM options.
Expand Down Expand Up @@ -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<void>): Promise<WebTransport | undefined> {
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(() => {});
Expand Down
1 change: 0 additions & 1 deletion quest/m0/test-flakes-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 0 additions & 30 deletions quest/m0/test-flakes-2/fingerprint-host-clock.md

This file was deleted.

Loading