Repository navigation
fix(launcher): cross-process startup-window race — starting marker prevents double-spawn (#707) - #709
Conversation
…events double-spawn (#707)
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-709Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr709.tgz
npm install -g packageThis comment is automatically updated on each push. |
[bot] 🏷 Reviewing PR #709 (fixes #707) now — checking diff cleanliness, the O_EXCL marker mechanics in |
…OCTOU and deadline-boundary sliver (#707 review) Two residual double-spawn windows in the starting-marker protocol, found in review: 1. The pre-spawn readStartingMarker() was treated as the coordination decision while the O_EXCL claim result was ignored: two processes that both read 'no marker' before either claimed would each spawn (the loser's EEXIST was dropped on the floor). The default port-0 path widens the window with await pickEphemeralPort() between read and claim. Now a claim loser re-reads the marker: active holder → wait for it (same probe as attach); stale/garbage file → remove and re-claim once; still losing → degrade to pre-#707 spawn. This also self-heals an unreadable marker file (crash mid-write), which previously blocked every future claim forever. 2. The wait loop's deadline coincides with the starter's own poll deadline: a starter succeeding in its last ~200ms could be missed by the waiter's final poll, which then fell through to spawning a duplicate. The success path clears the marker, so the miss is indistinguishable from a failure bail without one more look — waitForStarterInstance now ends with a final probe after the loop exits. Tests: garbage-marker self-heal (fails on the pre-review code), deadline-boundary attach instead of double-spawn (fails on the pre-review code), hung-starter bounded-wait pin. Preflight: typecheck clean, 1379 pass / 0 fail / 2 skipped, build ok.
[bot] 🏷 Review: LGTM after two fixes I pushed directly to the branch (commit Diff cleanliness first pass: clean — 5 files, all on-topic ( Two residual double-spawn windows remained, both in the exact invariant this PR establishes — fixed in 1. Read→claim TOCTOU (the real one). The pre-spawn 2. Deadline-boundary sliver. The waiter's deadline coincides with the starter's own poll deadline. A starter succeeding in its last ~200 ms could be missed by the waiter's final poll (200 ms interval + probe latency), which then fell through to spawning a duplicate over a healthy instance. Subtle because the success path clears the marker — to the waiter it looks identical to a failure bail. Tests added for both (plus a bounded-wait pin for the hung-starter worst case): garbage-marker self-heal, deadline-boundary attach-instead-of-double-spawn, hung starter waits max twice then spawns. I verified the first two fail against the pre-review code (ran them against Preflight on the updated branch: 中文摘要:审查发现并直接修复了两处残留的双开窗口——(1) 读标记与 O_EXCL 抢占之间的 TOCTOU(抢占失败被忽略,仍会再 spawn 一个,默认端口 0 路径因 pickEphemeralPort 异步操作窗口更大),现改为以抢占本身为仲裁者,失败方重查后等待或清除重抢,顺带让崩溃残留的垃圾标记文件可自愈;(2) 等待截止时间与启动方自身轮询截止重合,启动方在最后 ~200ms 成功时等待方可能漏探而重复 spawn,现循环结束后补一次终探。两个新回归测试均验证过在修复前代码上失败、修复后通过;全量测试 1379 通过,可以合并。 |
What
Fixes #707. Two concurrent
ensureProxyRunningcalls inside the spawn→healthy window each spawned their own proxy child: two live proxies over one sessions dir (double writers), last-writer-wins instance file, and the orphaned extra proxy living until ITS OWN spawner exited.Root cause: cross-launch coordination happened only through
<state>/proxy-origin, which the child writes after its first successful bind (announceListening, src/server.ts). Between the launcher'sspawnImpland the child's first listen there was zero on-disk evidence of an in-flight bring-up, so concurrent probes found nothing and both spawned. Ephemeral ports (#446) let both children bind different ports cleanly; an explicit port just made the second retry port+1 (#407) — either way, two writers.Fix
The starter publishes a "starting" marker before spawning:
<state>/proxy-startingfile, claimed with O_EXCL (fs.openSync(path, "wx")) — atomic across processes; a rename-based write would leave the same race at the marker level. Content:{token, pid, host, port, startedAt}.SPAWN_WAIT_MS + 30s) waits for the starter's instance using the same probe as attach (pid alive +/__bili/health+ instanceId match), bounded bySPAWN_WAIT_MS, then attaches. Marker disappearance (starter gave up / crashed) breaks the wait early so the caller spawns immediately instead of burning the full window.try/finallyaround spawn→poll→throw). A hard crash leaves a stale marker that the dead-owner/TTL checks render inert.bili startbypasses the marker entirely (user action, not auto-bootstrap). In-process dedup (singleFlight, feat(pi): native plugin mode — self-bootstrapping proxy + fetch interception #706) untouched — this is the cross-process half.Verification
PI_BIN=<fake-client> node dist/index.js pi --port 0sharing one isolatedXDG_STATE_HOME: 2 proxy children, twostarted proxylog lines, two registry markers, instance file flapping between origins.another bili launch is bringing up a proxy — waiting for it instead of spawning a second→attaching to running proxy at http://127.0.0.1:44551 (pid …); exactly one registry marker; no leftover marker file.tests/launcher.test.tsnow isolatesXDG_STATE_HOMEper run so marker I/O stays hermetic under parallelnode --test.npm run typecheckclean ·npm test1376 pass / 0 fail (2 skipped = gated e2e) ·npm run buildok.e2e-codexsuite needs a Responses-compatible upstream (defaulthttp://127.0.0.1:8199/v1), which is not reachable from this environment; the change touches only the launcher bootstrap path (src/launcher.ts+src/instance.ts), not the request pipeline, and is covered by the unit tests + the empirical concurrent-launch repro above.中文摘要: 修复了跨进程启动窗口竞态——启动方在 spawn 前用 O_EXCL 发布
proxy-starting标记,并发的启动会等待该实例就绪后 attach,而不是再 spawn 一个代理(消除同一 sessions 目录的双写者),可以合并。