Skip to content

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

Merged
morluto merged 1 commit into
morluto:mainfrom
akushonkamen:fix/1497-loopback-lock-key
Oct 10, 2026
Merged

morluto merged 1 commit into
morluto:mainfrom
akushonkamen:fix/1497-loopback-lock-key

Conversation

@akushonkamen

Copy link
Copy Markdown
Contributor

Summary

Fixes #1497. A single CDP browser serves every loopback stack on one port, but the attach admission lock was keyed by the endpoint origin string, so two concurrent connect-mode captures naming the same browser as http://127.0.0.1:9222 and http://[::1]:9222 took two admissions and the browser rejected the second client.

Note on issue state: #1497 was closed as resolved by b4db35f (#1513). That change keyed admission on the normalized /json/version discovery URL, which fixed trailing-slash spellings but kept the origin string as the basis, so the bracketed-IPv6 spelling still produced a second key. The regression tests below fail on current main for exactly that spelling and pass with this change.

Problem and expected behavior

  • Smallest trigger: two connect scenarios with cdp_endpoint http://127.0.0.1:9222 and http://[::1]:9222, same target_id, started concurrently.
  • Violated invariant: one CDP browser → one admission and one cleanup owner. browserEndpointSchema normalizes the endpoint to url.origin (src/domain/browserObservation.ts:36-38), and attachedScenarioLeaseKey serialized that origin (src/browser/PlaywrightScenarioBrowser.ts:190), so the two spellings yielded two lease keys and passed admission in parallel — matching @CassieFox23's reproduction where the browser refuses the second CDP client.

Change and scope

attachedScenarioLeaseKey (src/browser/PlaywrightScenarioBrowser.ts:190-201) folds a literal loopback hostname to 127.0.0.1 before serializing the key, using the existing domain helper isLiteralLoopbackHostname (src/domain/browserObservation.ts:42-43). The port and target_id remain partition keys, so distinct ports and targets still get distinct admissions. Endpoint schema validation and rejection behavior are unchanged; no cleanup semantics change beyond both spellings now sharing a single admission owner.

Contract and boundary impact

  • Semantic owner and earliest changed stage: browser scenario attach admission (adapters, PlaywrightScenarioBrowser).
  • CLI and MCP/tool-catalog contract: none.
  • Provider, bridge, target-format, or platform compatibility: none.
  • Evidence, artifact, provenance, or reconstruction contract: none.
  • Process execution, authorization, cleanup, or containment impact: attached scenarios reaching one loopback browser through different loopback spellings now share one admission and one cleanup owner instead of opening two CDP connections.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none.

Evidence and regression coverage

  • Tests added or updated:
    • "locks one attached browser across loopback spellings of one endpoint" — attachedScenarioLeaseKey("http://127.0.0.1:9222", "page-1") equals attachedScenarioLeaseKey("http://[::1]:9222", "page-1"), while different targets and different ports still differ.
    • "serializes concurrent attach across loopback spellings of one browser" — drives the public openPlaywrightScenarioBrowser entry twice (127.0.0.1 then [::1]) with a suspended first connection; asserts the second attach stays queued (does not settle within 100 ms) and connectOverCDP was dialed once, then verifies both dials carry the original spellings. No real browser needed.
  • Base reproduction or other evidence: without the fix the two new tests fail on the affected base revision (2 failed | 7 passed, red cause as in the issue: connectOverCDP called twice / keys unequal) — observed on 6d8f95e3 and re-observed on the rebased base 603d04ee (which contains b4db35f). With the fix: 9 passed (9).
  • User-visible CLI/MCP output: not applicable (no output change).
  • Remaining proof gaps: no real-browser lane run; concurrency is covered through the launcher stub.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • timeout 300 npx vitest run src/browser/PlaywrightScenarioBrowser.test.ts — 9 passed (9); on base 603d04ee without the fix: 2 failed | 7 passed.
  • timeout 300 npx vitest run src/browser src/domain --reporter=dot — 1745 passed | 2 failed; both failures (javascriptSemanticAnalysis.member_depth, PlaywrightBrowserScenarioProvider cleanup retry) also fail on pristine 603d04ee.
  • timeout 600 npm run test:fast — 3896 passed | 12 failed | 7 skipped; pristine 603d04ee baseline: 3894 passed | 12 failed | 7 skipped; the 12-item failure set is identical (JS semantic analysis / process capture / Electron / Hopper conformance, unrelated to the attach lock; +2 passed = the new tests).
  • npm run typecheck (tsc --noEmit) — passes.
  • npx oxlint — 0 errors (the 1 warning in the touched files is the pre-existing max-params warning on openAttachedScenarioBrowser).
  • npm run format:check (oxfmt) — passes.
  • npm run verify:module-boundaries — 1409 files checked.
  • git diff --check — clean.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: no real-browser run; attach serialization covered through the stubbed launcher (launcher-independent, no browser install required).
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: removes a duplicate-connection and duplicate-cleanup-owner path on one loopback browser; no new processes, privileges, or endpoints admitted — loopback-only endpoint validation is untouched.

Review checklist

  • This PR addresses a concrete problem or an agreed enhancement.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

Claim and root-cause analysis were posted on the issue before this PR; no competing PR touched the attach admission key at submission time. AI-assisted (Claude Code).

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.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@CassieFox23

Copy link
Copy Markdown
Contributor

TL;DR: With this PR, 127.0.0.1 and [::1] now share one lock key. Two providers in one process now take turns: 5 of 5 pass on the PR, and 5 of 5 fail on main. The plain MCP and CLI repro already passes on main because of the queue.

I tested the head 3361ad116 against main 11e3fae69. My #1497 retest note found that the queue in 2a66ec909 hides the bug on the MCP and CLI path. So the repro that shows the bug is two providers in one process, or the key itself.

Case main 11e3fae69 PR 3361ad116
Lease key for 127.0.0.1 and [::1] two keys one key
Same, with [0:0:0:0:0:0:0:1] two keys one key
Other port, or other target id other key other key
Two providers, one tab, 5 runs 5 of 5 fail: Another locale override is already in effect (my retest note saw 3 of 3 on 603d04ee3) 5 of 5 pass, one after the other (2.1 s, then 4.4 s)
MCP server, one tab, 3 runs 3 of 3 pass, one after the other same as main
One provider in the library, one tab, 3 runs 3 of 3 pass, one after the other same as main
Two tabs, one browser, 3 runs each for MCP and library one after the other same as main
Two browsers, 3 runs both pass, run together, right pages same as main
The PR's two new tests, Node 22, 24 and 26 2 of 9 fail (Node 24) 9 of 9 pass on each of the three
npm run check:fast not run passes

Two different browsers

I started two Chromium browsers and put a proxy on [::1] that points at the second one. Then I sent one capture to each, from two providers in one process, each with its own page. The two captures ran together on both trees, in 2.1 s and 2.3 s. Each browser ended on its own page. None came from the wrong browser. The two target ids always differed, so the two keys differed.

If the two browsers shared one target id, the PR would give both one key, and one would wait for the other. I could not test that. Target ids are random, and in six pairs they always differed. The lease only gates when a connection starts. Each attach still dials its own endpoint (I read openAttachedScenarioBrowser). I expect a wait, not a wrong capture. That is a code reading, not a run.

Does one port serve both stacks?

The PR body says one browser serves every loopback stack on one port. For headless Chromium 154 I could not confirm that. It listened on 127.0.0.1 only. A curl to /json/version on 127.0.0.1, port P, answered with HTTP 200. The same curl -g to [::1], port P, failed with exit 7 (connection refused). The result was the same with --remote-debugging-address=:: and with 0.0.0.0: still 127.0.0.1 only. ss -ltnp showed one listener each time.

The new tests

On main, both new tests fail for the right reason: the keys differ, and connectOverCDP ran twice, not once. The 100 ms "queued" check in that test passes on main too, because the stub leaves the second dial hanging. So the call count is the check that matters.

Not run

  • A browser that listens on [::1] by itself. I used a proxy.
  • Two browsers with one shared target id.
  • Two separate processes (two CLI runs or two MCP servers).
  • The PR merged onto newest main. The PR sits on 603d04ee3. Main 11e3fae69 has one more commit, which changes cleanup code. Newest main 0d8997833 adds an Android change. I read the diffs, and neither touches the key. I did not merge them.
  • Other Chromium versions, and the full test:fast suite.

Thanks, akushonkamen, for the fix and for tests that fail on main for the right reason.

Commands
# trees: git archive <sha> | tar -x, then npm ci && npm run build (Node 24.18.0)
# browser, headless: chromium-browser --headless=new --remote-debugging-port=P \
#   --remote-allow-origins='*' --password-store=basic --user-data-dir=<fresh dir>
# proxy: Node TCP server on [::1]:P piped to 127.0.0.1:P (or to a second browser's port)
# key:        node key.mjs <tree> P Q
# 2 providers: createBrowserScenarioProvider(process.env) once per capture, Promise.all
# MCP:        two capture_browser_scenario calls over stdio, Promise.all
# tests:      npx vitest run src/browser/PlaywrightScenarioBrowser.test.ts
# main run:   the PR's test file copied over main's
# lint/types: npm run check:fast (fresh clone, git checkout --detach 3361ad116)
# each run: env -i, fake HOME, no D-Bus, ulimit -c 0, nice -n 10
Raw output
main, two providers, 5 runs: B failed at 174 to 227 ms, A ok at 2,084 to 2,096 ms
  cause: Emulation.setLocaleOverride: Another locale override is already in effect
PR, two providers, 5 runs: A ok at 2,075 to 2,080 ms, B ok at 4,354 to 4,361 ms

main tests with the PR test file: Tests 2 failed | 7 passed (9)
  - Expected "http://[::1]:9222/json/version"  Received "http://127.0.0.1:9222/json/version"
  - expected "vi.fn()" to be called 1 times, but got 2 times
PR tests: Node 22.23.1, 24.18.0, 26.11.1 each: Tests 9 passed (9)

two browsers, main: A 2,084 to 2,088 ms, B 2,305 to 2,310 ms
two browsers, PR:   A 2,090 to 2,093 ms, B 2,306 to 2,313 ms
  each browser's tab ended on its own page (checked with /json/list on both)

key on PR (port P, other port Q):
  http://127.0.0.1:P, http://127.0.0.1:P/, http://[::1]:P,
  http://[0:0:0:0:0:0:0:1]:P, http://127.1:P
    -> ["http://127.0.0.1:P/json/version","T"]
  http://127.0.0.1:Q  -> ...:Q/json/version
  http://127.0.0.1:P with target "U" -> ["...","U"]
  localhost, 127.0.0.2, 0.0.0.0, [::], [::ffff:127.0.0.1], 192.168.1.5,
  example.com -> rejected by the scenario schema
key on main: [::1] and [0:0:0:0:0:0:0:1] -> ["http://[::1]:P/json/version","T"]

Chromium 154 listeners (ss -ltnp): 127.0.0.1:P only, in all three launches
Test setup
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.9
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • RAM: 32 GB DDR4-3600
  • Node: 22.23.1, 24.18.0, 26.11.1 (tests); 24.18.0 (builds and captures)
  • Head / base: PR 3361ad116 (based on 603d04ee3) / main 11e3fae69
  • Browser: Chromium in headless mode
  • Version: 154.0.8037.57
  • Licence: BSD-3-Clause
  • Load: other jobs ran at the same time. The 1-minute load average (tasks waiting to run, out of 24 threads) was 3.3 to 6.0.

🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊

@morluto
morluto merged commit 3e931a7 into morluto:main Oct 10, 2026
@morluto

morluto commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Welcome to REA, @akushonkamen! Thanks for making loopback spellings share the attach admission key and testing concurrent connections. Requests for the same target now queue together when one uses IPv4 and the other IPv6 loopback. Squash-merged.

@morluto morluto added bug Something isn't working area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
@akushonkamen
akushonkamen deleted the fix/1497-loopback-lock-key branch October 25, 2026 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(browser): lock an attached CDP scenario by the URL the connection uses

3 participants