From 3e931a7e357a3c8af671f39aa54952d6a6019d1f Mon Sep 17 00:00:00 2001 From: akushonkamen <74654766+akushonkamen@users.noreply.github.com> Date: Sun, 11 Oct 2026 06:22:59 +0800 Subject: [PATCH] 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. --- src/browser/PlaywrightScenarioBrowser.test.ts | 85 +++++++++++++++++++ src/browser/PlaywrightScenarioBrowser.ts | 11 ++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/browser/PlaywrightScenarioBrowser.test.ts b/src/browser/PlaywrightScenarioBrowser.test.ts index cb3c0e862..40d5896a3 100644 --- a/src/browser/PlaywrightScenarioBrowser.test.ts +++ b/src/browser/PlaywrightScenarioBrowser.test.ts @@ -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 = () => { let resolve: (value: Value | PromiseLike) => void = () => undefined; const promise = new Promise((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>(); + let rejectFirstConnection: ((cause: Error) => void) | undefined; + const failingConnection = new Promise((_resolve, reject) => { + rejectFirstConnection = reject; + }); + const firstStarted = deferred(); + 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"]); +}); diff --git a/src/browser/PlaywrightScenarioBrowser.ts b/src/browser/PlaywrightScenarioBrowser.ts index 36089a788..4cac279b6 100644 --- a/src/browser/PlaywrightScenarioBrowser.ts +++ b/src/browser/PlaywrightScenarioBrowser.ts @@ -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,