fix(browser): fold loopback spellings into one attach admission key (#1604)

A CDP endpoint serves every loopback stack on one port, but the attach
admission key was built from the endpoint origin string, so scenarios
attaching through http://127.0.0.1:PORT and http://[::1]:PORT produced
two lease keys, passed admission in parallel and the browser rejected
the second client. Collapse literal loopback hostnames to one identity
in the lease key (port and target still partition it) without touching
endpoint validation.

Validation: npx vitest run src/browser/PlaywrightScenarioBrowser.test.ts (9 passed; the two new loopback tests failed before the fix), npx vitest run src/browser src/domain (1745 passed, 2 failed, both failing on pristine main), npm run test:fast (3896 passed, 12 failed, 7 skipped; the failure set is identical on pristine main at 3894 passed), plus typecheck, oxlint, oxfmt and resolved module-boundary verification.
This commit is contained in:
akushonkamen
2026-10-11 06:22:59 +08:00
committed by GitHub
parent 6edd5cf045
commit 3e931a7e35
2 changed files with 94 additions and 2 deletions
@@ -19,6 +19,18 @@ it("locks one attached browser for spellings that discover the same endpoint", (
);
});
it("locks one attached browser across loopback spellings of one endpoint", () => {
expect(attachedScenarioLeaseKey("http://127.0.0.1:9222", "page-1")).toBe(
attachedScenarioLeaseKey("http://[::1]:9222", "page-1"),
);
expect(attachedScenarioLeaseKey("http://[::1]:9222", "page-1")).not.toBe(
attachedScenarioLeaseKey("http://[::1]:9222", "page-2"),
);
expect(attachedScenarioLeaseKey("http://127.0.0.1:9222", "page-1")).not.toBe(
attachedScenarioLeaseKey("http://127.0.0.1:9223", "page-1"),
);
});
const deferred = <Value>() => {
let resolve: (value: Value | PromiseLike<Value>) => void = () => undefined;
const promise = new Promise<Value>((resolvePromise) => {
@@ -302,3 +314,76 @@ it("cancels same-target queued connection before acquiring a browser", async ()
).rejects.toThrow("connection failed");
expect(connectOverCDP).toHaveBeenCalledTimes(3);
});
it("serializes concurrent attach across loopback spellings of one browser", async () => {
const makeScenario = (endpoint: string) =>
browserScenarioSchema.parse({
browser: { mode: "connect", cdp_endpoint: endpoint, target_id: "page-1" },
start_url: { url: "https://example.test" },
actions: [
{
step_id: "wait",
action: "wait_for_timeout",
duration_ms: 1,
},
],
});
const firstConnection =
deferred<Pick<Browser, "contexts" | "close" | "version">>();
let rejectFirstConnection: ((cause: Error) => void) | undefined;
const failingConnection = new Promise<never>((_resolve, reject) => {
rejectFirstConnection = reject;
});
const firstStarted = deferred<void>();
const endpoints: string[] = [];
const connectOverCDP = vi.fn((endpoint: string) => {
endpoints.push(endpoint);
if (endpoints.length === 1) {
firstStarted.resolve(undefined);
return firstConnection.promise;
}
return failingConnection;
});
const launcher = {
connectOverCDP,
launchPersistentContext: async () => {
throw new Error("Unexpected launch");
},
};
const first = openPlaywrightScenarioBrowser(
makeScenario("http://127.0.0.1:9222"),
{},
{ launcher },
);
await firstStarted.promise;
const second = openPlaywrightScenarioBrowser(
makeScenario("http://[::1]:9222"),
{},
{ launcher },
);
const race = await Promise.race([
second.then(
() => "settled" as const,
() => "settled" as const,
),
new Promise((resolve) => setTimeout(() => resolve("queued"), 100)),
]);
// The loopback spelling is the same browser, so the second attach must wait
// for the lease instead of dialing in parallel and being rejected.
expect(race).toBe("queued");
expect(connectOverCDP).toHaveBeenCalledTimes(1);
firstConnection.resolve({
close: vi.fn(async () => undefined),
contexts: vi.fn(() => []),
version: () => "fixture",
});
await expect(first).rejects.toThrow();
if (rejectFirstConnection === undefined)
throw new Error("second connection did not begin");
rejectFirstConnection(new Error("second connection failed"));
await expect(second).rejects.toThrow("second connection failed");
expect(endpoints).toEqual(["http://127.0.0.1:9222", "http://[::1]:9222"]);
});
+9 -2
View File
@@ -11,6 +11,7 @@ import {
import type { BrowserScenario } from "../domain/browserScenario.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
import { isLiteralLoopbackHostname } from "../domain/browserObservation.js";
import { BrowserObservationError } from "../domain/browserObservationError.js";
import { withPlaywrightExecutionBoundary } from "./PlaywrightExecutionBoundary.js";
@@ -189,8 +190,14 @@ const createAttachedScenarioRelease = (
export const attachedScenarioLeaseKey = (
endpoint: string,
targetId: string,
): string =>
JSON.stringify([new URL("/json/version", endpoint).href, targetId]);
): string => {
const discovery = new URL("/json/version", endpoint);
// One CDP browser serves every loopback stack on one port, so spellings that
// reach it through 127.0.0.1 or ::1 must share a single admission.
if (isLiteralLoopbackHostname(discovery.hostname))
discovery.hostname = "127.0.0.1";
return JSON.stringify([discovery.href, targetId]);
};
const acquireAttachedScenario = (
scenario: Extract<BrowserScenario["browser"], { readonly mode: "connect" }>,