From 40968e87861becaf8091f1712fd917be14ecd68e Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 15:25:53 +0800 Subject: [PATCH 1/3] fix: refuse concurrent launches on a live shared overlay instead of clobbering it (#1952) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two bili launches sharing one real home both target the fixed `${home}-bili` overlay. The second launch detected the live first holder (.bili-launch.pid) but only warned, then rewrote the first launcher's generated .env (proxy routing) and MCP config.toml — last writer won. The detection was advisory: no ownership, and even two FIRST launches racing had a check-then-write window. Replace the advisory check with an exclusive lease: - acquireOverlayLease(): atomic mkdir of /.bili-launch.lock/ plus an owner record (pid + random token). A live foreign holder throws OverlayBusyError BEFORE any refresh/config write; a dead holder's lease is reclaimed (crash residue); a partially written owner record reads BUSY (manual unlock = delete the lock dir). The legacy .bili-launch.pid marker still acts as a liveness hint, so this build also protects overlays held by pre-#1952 launchers. - runLaunch aborts cleanly on busy: logs the reason, stops THIS launch's own proxy child (a plain throw would leak it — prepare runs after ensureProxyRunning), exits 1. The lease is released in the same finally that stops the proxy. - Sits in the shared refreshOverlayHome, so every overlay lane gets the guard (codex + dsh) — one fix at the mechanism. Behavior change: a second concurrent launch of the same home now hard-refuses (exit 1, zero writes) instead of warning and clobbering the first launcher's config. Sequential relaunches are unaffected (lease released on exit; stale leases auto-reclaimed). Tests: tests/overlay-busy-lock.test.ts — OP repro via legacy marker, foreign live lease, partial owner record, stale reclaim (lease + marker), same-process roundtrip, token-verified release, six-process race (exactly one winner), dsh lane parity, runLaunch integration (proxy stopped, client not spawned, exit 1). --- src/launcher.ts | 208 ++++++++++++++++--- tests/overlay-busy-lock.test.ts | 344 ++++++++++++++++++++++++++++++++ 2 files changed, 527 insertions(+), 25 deletions(-) create mode 100644 tests/overlay-busy-lock.test.ts diff --git a/src/launcher.ts b/src/launcher.ts index 1d8a9033e9..ecef6d17a7 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -1327,7 +1327,9 @@ export function buildClaudeSettingsArg(platform: NodeJS.Platform, override: stri * route — in direct-URL mode the model traffic does not reach the proxy and * the binding cannot happen). When the overlay cannot be built the injection * degrades to nothing (wire mode still compresses server-side) with a - * warning. */ + * warning. OverlayBusyError (#1952: another live launch owns the overlay) is + * PROPAGATED, not degraded — runLaunch aborts the launch, because degrading + * to the real home would silently drop the MCP tools and the .env protection. */ export function prepareCodexMcpInjection(opts: { codexHome: string; origin: string; @@ -1377,13 +1379,37 @@ export function prepareCodexMcpInjection(opts: { * real-home entry are merged into the real home (recursively; mtime-newer-wins * for files, losers preserved as `.bili-conflict`) and only removed from * the overlay when the merge fully succeeded; entries the real home lacks are - * kept as-is. A `.bili-launch.pid` marker warns when two launches share the - * overlay (each launch rewrites the generated file with its own proxy origin). + * kept as-is. + * + * Ownership (#1952): the overlay is EXCLUSIVE per live launch. Before any + * merge/write, refresh takes an atomic lease (`/.bili-launch.lock/` + * dir + owner record with pid + token); a second live launch refuses with + * OverlayBusyError instead of clobbering the first one's generated .env / MCP + * config / pid marker (the old warn-and-continue behavior let the last writer + * win the routing). Stale leases from crashed launches are reclaimed + * automatically (dead owner pid); a partially written owner record reads as + * BUSY — conservative, manual unlock is deleting the lock dir. The legacy + * `.bili-launch.pid` marker is still honored as a liveness hint so a pre-#1952 + * launch (which creates no lock dir) keeps its overlay too. runLaunch releases + * the lease in its finally, after the client exits and the proxy is stopped. */ function overlayLockPath(overlay: string): string { return path.join(overlay, ".bili-launch.pid"); } +// Conservative liveness for ownership decisions (unlike instance.ts' +// isPidAlive): any kill error other than ESRCH reads as ALIVE, so a lease is +// never reclaimed from an owner we cannot verify. +function holderPidAlive(pid: number): boolean { + if (!Number.isInteger(pid) || pid <= 0) return false; + try { + process.kill(pid, 0); + return true; + } catch (err) { + return (err as NodeJS.ErrnoException).code !== "ESRCH"; + } +} + function livePidHoldsOverlay(overlay: string): number | undefined { let raw: string | undefined; try { @@ -1392,13 +1418,122 @@ function livePidHoldsOverlay(overlay: string): number | undefined { return undefined; } const pid = Number.parseInt(raw.trim(), 10); - if (!Number.isInteger(pid) || pid <= 0 || pid === process.pid) return undefined; + if (pid === process.pid) return undefined; + return holderPidAlive(pid) ? pid : undefined; +} + +/** #1952: thrown when another LIVE bili launch owns the shared `-bili` + * overlay. The refused launch made no writes — the holder's generated files + * stay byte-identical. Manual unlock (crashed holder whose pid was reused, or + * any stuck state): delete `/.bili-launch.lock/`. */ +export class OverlayBusyError extends Error { + readonly code = "BILI_OVERLAY_BUSY"; + constructor(readonly overlay: string, readonly holderPid?: number) { + super( + (holderPid !== undefined + ? `another bili launch (pid ${holderPid}) is using the shared overlay ${overlay} — only one bili launch per home runs at a time; wait for it to exit` + : `the shared overlay ${overlay} is held by a launch whose owner record is incomplete or unverifiable — if no bili launch of this home is running, remove ${overlayLeaseDir(overlay)} to unlock`) + + " (this launch made no changes)", + ); + this.name = "OverlayBusyError"; + } +} + +function overlayLeaseDir(overlay: string): string { + return path.join(overlay, ".bili-launch.lock"); +} + +function overlayOwnerPath(overlay: string): string { + return path.join(overlayLeaseDir(overlay), "owner.json"); +} + +interface OverlayLeaseRecord { + dir: string; + ownerFile: string; + token: string; +} + +let activeOverlayLease: OverlayLeaseRecord | undefined; + +function writeOwner(ownerFile: string, token: string): void { + fs.writeFileSync(ownerFile, JSON.stringify({ pid: process.pid, token, ts: Date.now() }, null, 2)); +} + +function readOwner(ownerFile: string): { pid?: number; token?: string } { try { - process.kill(pid, 0); - } catch (err) { - if ((err as NodeJS.ErrnoException).code === "ESRCH") return undefined; + const rec = JSON.parse(fs.readFileSync(ownerFile, "utf8")) as Record; + return { + ...(typeof rec.pid === "number" ? { pid: rec.pid } : {}), + ...(typeof rec.token === "string" ? { token: rec.token } : {}), + }; + } catch { + return {}; + } +} + +/** #1952: atomically claim exclusive ownership of the overlay before any + * refresh/write (see the module doc above). Reentrant within one process — + * sequential prepares in the same launch (or test suite) just refresh the + * record. Throws OverlayBusyError when a foreign live launch holds it. */ +export function acquireOverlayLease(overlay: string): void { + // Cross-version guard first: pre-#1952 launches leave only the legacy + // marker. A live foreign pid in it means an older bili owns the overlay — + // refuse rather than clobber its generated files. + const legacyHolder = livePidHoldsOverlay(overlay); + if (legacyHolder !== undefined) throw new OverlayBusyError(overlay, legacyHolder); + const dir = overlayLeaseDir(overlay); + const ownerFile = overlayOwnerPath(overlay); + const take = (): boolean => { + const token = randomUUID(); + try { + fs.mkdirSync(dir); + } catch (err) { + if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err; + return false; + } + try { + writeOwner(ownerFile, token); + } catch { + // Crash window between mkdir and the owner write: the bare dir + // stays — an incomplete record reads as busy (manual unlock). + } + activeOverlayLease = { dir, ownerFile, token }; + return true; + }; + if (take()) return; + const owner = readOwner(ownerFile); + if (owner.pid === process.pid) { + try { + const token = randomUUID(); + writeOwner(ownerFile, token); + activeOverlayLease = { dir, ownerFile, token }; + } catch { + activeOverlayLease = undefined; + } + return; + } + if (owner.pid !== undefined && !holderPidAlive(owner.pid)) { + // Stale lease from a crashed launch: reclaim once. The atomic mkdir in + // take() stays the arbiter if another reclaim runs in parallel. + try { + fs.rmSync(dir, { recursive: true, force: true }); + } catch {} + if (take()) return; } - return pid; + throw new OverlayBusyError(overlay, owner.pid); +} + +/** Release the lease taken by acquireOverlayLease (runLaunch's finally). + * Token-verified: a record rewritten out from under us (reclaimed) is left + * untouched. Idempotent. */ +export function releaseOverlayLease(): void { + const lease = activeOverlayLease; + activeOverlayLease = undefined; + if (lease === undefined) return; + try { + if (readOwner(lease.ownerFile).token !== lease.token) return; + fs.rmSync(lease.dir, { recursive: true, force: true }); + } catch {} } /** @@ -1815,12 +1950,10 @@ export function refreshOverlayHome(realHome: string, overlay: string, generatedF } catch { return false; } - const holder = livePidHoldsOverlay(overlay); - if (holder !== undefined) { - console.error( - `bili: another bili launch (pid ${holder}) is using ${overlay} — concurrent launches share this overlay and the last one's proxy port wins in the generated config.`, - ); - } + // #1952: exclusive ownership BEFORE any merge/write — a foreign live + // launch holding this overlay gets a hard refusal (OverlayBusyError), not + // the old warn-and-clobber. The marker below stays informational. + acquireOverlayLease(overlay); try { fs.writeFileSync(overlayLockPath(overlay), `${process.pid}\n`); } catch {} @@ -2481,9 +2614,10 @@ export function renderCodexDotEnv(userText: string | undefined, values: { origin * The overlay's .env is refresh-protected on EVERY launch, so a stale * generated copy can never merge back into the real home even when a later * launch does not manage it (#1802 review). - * Returns the overlay dir to point CODEX_HOME at, or undefined when it cannot - * be built (caller degrades: wire-injected compression still works, native - * MCP tools / the .env protection do not). */ + * Returns the overlay dir to point CODEX_HOME at, or undefined when it cannot + * be built (caller degrades: wire-injected compression still works, native + * MCP tools / the .env protection do not). Throws OverlayBusyError BEFORE any + * write when another live launch owns the overlay (#1952) — callers abort. */ export function prepareCodexHome(opts: { codexHome: string; origin: string; @@ -3703,6 +3837,16 @@ export async function stopProxyGuarded( stopProxy(handle); } +/** #1952: the shared overlay is owned by another live launch — stop THIS + * launch's proxy (plainly throwing here would leak the detached proxy child, + * since prepare runs after ensureProxyRunning) and exit non-zero. The client + * is never spawned on this path. */ +async function abortLaunchOnBusyOverlay(handle: ProxyHandle, err: OverlayBusyError, deps: LauncherDeps): Promise { + console.error(`bili: ${err.message}`); + await stopProxyGuarded(handle, deps.fetchHealthInfo ?? fetchHealthInfoDefault); + process.exit(1); +} + /** #679: quote one token for cmd.exe's line parser. Only whitespace-bearing * tokens get wrapped in double quotes, so a space-free launch produces a * byte-identical line to the old shell:true form. A token containing an @@ -4141,7 +4285,13 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} // overrides the env fallback). env.PI_CACHE_RETENTION = "long"; const dshHomeDir = resolveDshHome(process.env); - dshOverlayHome = routes.httpRewrites.length > 0 ? prepareDshHome(dshHomeDir, origin, routes.httpRewrites) : undefined; + try { + dshOverlayHome = routes.httpRewrites.length > 0 ? prepareDshHome(dshHomeDir, origin, routes.httpRewrites) : undefined; + } catch (err) { + if (!(err instanceof OverlayBusyError)) throw err; + await abortLaunchOnBusyOverlay(handle, err, deps); + return; + } if (dshOverlayHome) { env.DSH_HOME = dshOverlayHome; } else if (routes.httpRewrites.length > 0) { @@ -4381,13 +4531,20 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} // $CODEX_HOME/.env (load_dotenv overrides spawn env after start); // direct-URL launches only need it to carry the MCP server block. if (!directUrl || (injectMcp && codexConversationId !== undefined)) { - const inj = prepareCodexMcpInjection({ - codexHome: resolveCodexHome(process.env), - origin, - caPath: codexCaPath, - conversationId: codexConversationId, - manageRouting: !directUrl, - }); + let inj: ReturnType; + try { + inj = prepareCodexMcpInjection({ + codexHome: resolveCodexHome(process.env), + origin, + caPath: codexCaPath, + conversationId: codexConversationId, + manageRouting: !directUrl, + }); + } catch (err) { + if (!(err instanceof OverlayBusyError)) throw err; + await abortLaunchOnBusyOverlay(handle, err, deps); + return; + } if (inj.clientArgs.length > 0) clientArgs = [...inj.clientArgs, ...clientArgs]; Object.assign(env, inj.envPatch); if (inj.warning) console.error(`bili: ${inj.warning}`); @@ -4463,6 +4620,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} code = 1; } finally { await stopProxyGuarded(handle, deps.fetchHealthInfo ?? fetchHealthInfoDefault); + releaseOverlayLease(); if (gooseOverlay) { try { finalizeGooseHome(gooseOverlay); diff --git a/tests/overlay-busy-lock.test.ts b/tests/overlay-busy-lock.test.ts new file mode 100644 index 0000000000..deb1c2527b --- /dev/null +++ b/tests/overlay-busy-lock.test.ts @@ -0,0 +1,344 @@ +/** #1952: the shared `-bili` overlay is exclusive per live launch. A + * second live launch must refuse (zero writes) instead of clobbering the + * first launch's generated .env / MCP config / pid marker; crashed holders' + * leases are reclaimed; normal relaunch after exit keeps working. */ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { spawn, spawnSync, type ChildProcess } from "node:child_process"; +import { + OverlayBusyError, + acquireOverlayLease, + prepareCodexHome, + prepareDshHome, + releaseOverlayLease, + runLaunch, + type LauncherDeps, + type SpawnChild, +} from "../src/launcher.ts"; + +function mkdtemp(prefix: string): string { + return fs.mkdtempSync(path.join(os.tmpdir(), prefix)); +} + +function cleanup(...dirs: string[]): void { + for (const d of dirs) fs.rmSync(d, { recursive: true, force: true }); +} + +/** A live foreign process whose pid can be planted as the overlay owner. */ +async function spawnHolder(): Promise<{ pid: number; stop(): void }> { + const child = spawn(process.execPath, ["-e", "setTimeout(() => {}, 60000)"], { stdio: "ignore" }); + await new Promise((resolve) => child.once("spawn", () => resolve())); + return { pid: child.pid ?? -1, stop: () => { try { child.kill(); } catch {} } }; +} + +function deadPid(): number { + const res = spawnSync(process.execPath, ["-e", ""]); + if (res.pid === undefined || res.pid <= 0) throw new Error("spawnSync returned no pid"); + return res.pid; +} + +function makeChild(pid: number, onKill?: () => void): SpawnChild { + return { pid, unref() {}, kill() { onKill?.(); return true; }, on() {} }; +} + +function plantLease(overlay: string, pid: number, token?: string): void { + const dir = path.join(overlay, ".bili-launch.lock"); + fs.mkdirSync(dir, { recursive: true }); + if (token !== undefined) { + fs.writeFileSync(path.join(dir, "owner.json"), JSON.stringify({ pid, token, ts: Date.now() }, null, 2)); + } +} + +const CODEX_OPTS_BASE = { caPath: "/ca.pem", manageRouting: true } as const; + +test("second live launch refuses instead of clobbering the first launch's overlay (#1952)", async () => { + const home = mkdtemp("bili-1952-home-"); + const overlay = `${home}-bili`; + fs.writeFileSync(path.join(home, "config.toml"), 'model = "audit-model"\n'); + const holder = await spawnHolder(); + try { + const o1 = prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:11111", conversationId: "first-session", ...CODEX_OPTS_BASE }); + assert.equal(o1, overlay); + const envBefore = fs.readFileSync(path.join(overlay, ".env"), "utf8"); + const cfgBefore = fs.readFileSync(path.join(overlay, "config.toml"), "utf8"); + assert.ok(envBefore.includes("http://127.0.0.1:11111")); + // Plant a concurrent owner the way a pre-fix bili launch would look: + // live foreign pid in the legacy marker. + fs.writeFileSync(path.join(overlay, ".bili-launch.pid"), `${holder.pid}\n`); + let err: unknown; + try { + prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:22222", conversationId: "second-session", ...CODEX_OPTS_BASE }); + } catch (e) { + err = e; + } + assert.ok(err instanceof OverlayBusyError, `expected OverlayBusyError, got: ${String(err)}`); + assert.equal((err as OverlayBusyError).holderPid, holder.pid); + assert.equal(fs.readFileSync(path.join(overlay, ".env"), "utf8"), envBefore); + assert.equal(fs.readFileSync(path.join(overlay, "config.toml"), "utf8"), cfgBefore); + assert.equal(fs.readFileSync(path.join(overlay, ".bili-launch.pid"), "utf8"), `${holder.pid}\n`); + const owner = JSON.parse(fs.readFileSync(path.join(overlay, ".bili-launch.lock", "owner.json"), "utf8")) as { pid: number }; + assert.equal(owner.pid, process.pid); + } finally { + releaseOverlayLease(); + holder.stop(); + cleanup(home); + } +}); + +test("foreign live lease: prepare refuses before writing anything (#1952)", async () => { + const home = mkdtemp("bili-1952-lease-"); + const overlay = `${home}-bili`; + fs.writeFileSync(path.join(home, "config.toml"), 'model = "audit-model"\n'); + const holder = await spawnHolder(); + try { + plantLease(overlay, holder.pid, "foreign-token"); + let err: unknown; + try { + prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:11111", conversationId: "s1", ...CODEX_OPTS_BASE }); + } catch (e) { + err = e; + } + assert.ok(err instanceof OverlayBusyError, `expected OverlayBusyError, got: ${String(err)}`); + assert.equal((err as OverlayBusyError).holderPid, holder.pid); + assert.ok(!fs.existsSync(path.join(overlay, ".env"))); + assert.ok(!fs.existsSync(path.join(overlay, ".bili-launch.pid"))); + } finally { + releaseOverlayLease(); + holder.stop(); + cleanup(home); + } +}); + +test("partially written owner record reads busy (#1952)", async () => { + const home = mkdtemp("bili-1952-partial-"); + const overlay = `${home}-bili`; + fs.writeFileSync(path.join(home, "config.toml"), 'model = "audit-model"\n'); + try { + plantLease(overlay, 1); + let err: unknown; + try { + prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:11111", conversationId: "s1", ...CODEX_OPTS_BASE }); + } catch (e) { + err = e; + } + assert.ok(err instanceof OverlayBusyError, `expected OverlayBusyError, got: ${String(err)}`); + assert.equal((err as OverlayBusyError).holderPid, undefined); + assert.match((err as Error).message, /\.bili-launch\.lock/); + } finally { + releaseOverlayLease(); + cleanup(home); + } +}); + +test("stale lease from a crashed launch is reclaimed (#1952)", async () => { + const home = mkdtemp("bili-1952-stale-"); + const overlay = `${home}-bili`; + fs.writeFileSync(path.join(home, "config.toml"), 'model = "audit-model"\n'); + try { + plantLease(overlay, deadPid(), "crashed-token"); + const o = prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:11111", conversationId: "s1", ...CODEX_OPTS_BASE }); + assert.equal(o, overlay); + assert.ok(fs.readFileSync(path.join(overlay, ".env"), "utf8").includes("http://127.0.0.1:11111")); + } finally { + releaseOverlayLease(); + cleanup(home); + } +}); + +test("stale legacy marker alone does not block a relaunch (#1952)", async () => { + const home = mkdtemp("bili-1952-stalemk-"); + const overlay = `${home}-bili`; + fs.writeFileSync(path.join(home, "config.toml"), 'model = "audit-model"\n'); + try { + fs.mkdirSync(overlay, { recursive: true }); + fs.writeFileSync(path.join(overlay, ".bili-launch.pid"), `${deadPid()}\n`); + const o = prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:11111", conversationId: "s1", ...CODEX_OPTS_BASE }); + assert.equal(o, overlay); + } finally { + releaseOverlayLease(); + cleanup(home); + } +}); + +test("same-process relaunch after release reuses the overlay (#1952)", async () => { + const home = mkdtemp("bili-1952-roundtrip-"); + const overlay = `${home}-bili`; + fs.writeFileSync(path.join(home, "config.toml"), 'model = "audit-model"\n'); + try { + const o1 = prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:11111", conversationId: "s1", ...CODEX_OPTS_BASE }); + assert.equal(o1, overlay); + releaseOverlayLease(); + const o2 = prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:22222", conversationId: "s2", ...CODEX_OPTS_BASE }); + assert.equal(o2, overlay); + assert.ok(fs.readFileSync(path.join(overlay, ".env"), "utf8").includes("http://127.0.0.1:22222")); + } finally { + releaseOverlayLease(); + cleanup(home); + } +}); + +test("release leaves a token-mismatched (reclaimed) lease untouched (#1952)", async () => { + const home = mkdtemp("bili-1952-token-"); + const overlay = `${home}-bili`; + fs.writeFileSync(path.join(home, "config.toml"), 'model = "audit-model"\n'); + try { + prepareCodexHome({ codexHome: home, origin: "http://127.0.0.1:11111", conversationId: "s1", ...CODEX_OPTS_BASE }); + const ownerFile = path.join(overlay, ".bili-launch.lock", "owner.json"); + const rec = JSON.parse(fs.readFileSync(ownerFile, "utf8")) as Record; + rec.token = "someone-else"; + fs.writeFileSync(ownerFile, JSON.stringify(rec)); + releaseOverlayLease(); + assert.ok(fs.existsSync(path.join(overlay, ".bili-launch.lock"))); + } finally { + releaseOverlayLease(); + cleanup(home); + } +}); + +test("six racing processes: exactly one wins the lease (#1952)", { timeout: 60_000 }, async () => { + const tmp = mkdtemp("bili-1952-race-"); + const overlay = path.join(tmp, "race-overlay"); + fs.mkdirSync(overlay, { recursive: true }); + const launcherUrl = new URL("../src/launcher.ts", import.meta.url).href; + const script = path.join(tmp, "race-child.ts"); + fs.writeFileSync( + script, + [ + `const mod = await import(${JSON.stringify(launcherUrl)});`, + `try { mod.acquireOverlayLease(process.argv[2]); console.log("WIN"); }`, + `catch (err) { console.log(err instanceof Error && err.name === "OverlayBusyError" ? "BUSY" : "ERR:" + String(err)); }`, + // Hold until signaled so every racer contends against a LIVE holder — exiting + // early would leave a stale lease that later racers legitimately reclaim. + // (A pending top-level await alone does NOT hold Node alive — it exits 13.) + `setInterval(() => {}, 60_000);`, + `process.once("SIGTERM", () => { mod.releaseOverlayLease(); process.exit(0); });`, + ].join("\n"), + ); + const K = 6; + const results: string[] = []; + const children: ChildProcess[] = []; + for (let i = 0; i < K; i++) { + children.push(spawn(process.execPath, ["--import", "tsx", script, overlay], { stdio: ["ignore", "pipe", "pipe"] })); + } + try { + await new Promise((resolve, reject) => { + let settled = false; + const finish = (err?: Error): void => { + if (settled) return; + settled = true; + if (err) reject(err); + else resolve(); + }; + for (const c of children) { + let buf = ""; + c.stdout?.on("data", (d: Buffer) => { + buf += d.toString(); + let nl: number; + while ((nl = buf.indexOf("\n")) >= 0 && !settled) { + const line = buf.slice(0, nl).trim(); + buf = buf.slice(nl + 1); + if (!line) continue; + results.push(line); + if (results.length === K) { + finish(); + break; + } + } + }); + c.on("error", (err: Error) => finish(err)); + c.on("close", (code) => { + if (!settled && code !== 0) finish(new Error(`race child exited ${code}`)); + }); + } + }); + } finally { + for (const c of children) { + try { c.kill("SIGTERM"); } catch {} + } + } + assert.equal(results.filter((r) => r === "WIN").length, 1); + assert.equal(results.filter((r) => r === "BUSY").length, K - 1); + cleanup(tmp); +}); + +test("dsh lane: same busy refusal on the shared -bili overlay (#1952)", async () => { + const dshHome = mkdtemp("bili-1952-dsh-"); + const overlay = `${dshHome}-bili`; + fs.writeFileSync(path.join(dshHome, "settings.yaml"), "baseURL: http://127.0.0.1:8199/v1\n"); + const holder = await spawnHolder(); + try { + plantLease(overlay, holder.pid, "dsh-foreign"); + let err: unknown; + try { + prepareDshHome(dshHome, "http://127.0.0.1:4321", [{ key: "dsh", realUpstream: "http://127.0.0.1:8199/v1" }]); + } catch (e) { + err = e; + } + assert.ok(err instanceof OverlayBusyError, `expected OverlayBusyError, got: ${String(err)}`); + assert.equal((err as OverlayBusyError).holderPid, holder.pid); + assert.ok(!fs.existsSync(path.join(overlay, "settings.yaml"))); + } finally { + releaseOverlayLease(); + holder.stop(); + cleanup(dshHome); + } +}); + +test("runLaunch(codex): busy overlay stops this launch's proxy, skips the client, exits 1 (#1952)", async () => { + const codexHome = mkdtemp("bili-1952-run-"); + const overlay = `${codexHome}-bili`; + fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "audit-model"\n'); + const fakeClient = path.join(codexHome, "fake-codex"); + fs.writeFileSync(fakeClient, "#!/bin/sh\nexit 0\n"); + const holder = await spawnHolder(); + plantLease(overlay, holder.pid, "run-foreign"); + + const prevExit = process.exit; + const prevBin = process.env.BILI_CLIENT_BIN; + const prevHome = process.env.CODEX_HOME; + const prevConsoleError = console.error; + const exitCalls: number[] = []; + const errLines: string[] = []; + process.exit = ((code?: number) => { exitCalls.push(code ?? 0); return undefined as never; }) as typeof process.exit; + console.error = ((...a: unknown[]) => { errLines.push(a.map(String).join(" ")); }) as typeof console.error; + process.env.BILI_CLIENT_BIN = fakeClient; + process.env.CODEX_HOME = codexHome; + let clientSpawned = false; + let proxyKilled = false; + const deps: LauncherDeps = { + fetchImpl: async () => ({ ok: true }), + fetchHealthInfo: async () => ({ ok: true, pid: 42423 }), + readInstanceFile: () => undefined, + sleep: () => Promise.resolve(), + zonePreferredPort: () => 45999, + writeZonePort: () => {}, + attachDiag: () => {}, + spawnImpl: (cmd) => { + if (cmd === fakeClient) { + clientSpawned = true; + return makeChild(42424); + } + return makeChild(42423, () => { proxyKilled = true; }); + }, + }; + try { + await runLaunch({ client: "codex", clientArgs: [], overrides: {} }, deps); + assert.deepEqual(exitCalls, [1]); + assert.equal(clientSpawned, false); + if (process.platform !== "win32") assert.equal(proxyKilled, true); + assert.ok(errLines.some((l) => l.includes("another bili launch (pid")), "user-visible busy message"); + } finally { + process.exit = prevExit; + console.error = prevConsoleError; + if (prevBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevBin; + if (prevHome === undefined) delete process.env.CODEX_HOME; + else process.env.CODEX_HOME = prevHome; + releaseOverlayLease(); + holder.stop(); + cleanup(codexHome); + } +}); From dc251bc3af5afcdf4ee762475a0fcd7582b161f2 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 15:41:23 +0800 Subject: [PATCH 2/3] test: hermetic race children (own temp dir) + stderr in failure diagnostics --- tests/overlay-busy-lock.test.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/overlay-busy-lock.test.ts b/tests/overlay-busy-lock.test.ts index deb1c2527b..0ee10dc211 100644 --- a/tests/overlay-busy-lock.test.ts +++ b/tests/overlay-busy-lock.test.ts @@ -219,9 +219,17 @@ test("six racing processes: exactly one wins the lease (#1952)", { timeout: 60_0 ); const K = 6; const results: string[] = []; + const stderrs: string[] = []; const children: ChildProcess[] = []; for (let i = 0; i < K; i++) { - children.push(spawn(process.execPath, ["--import", "tsx", script, overlay], { stdio: ["ignore", "pipe", "pipe"] })); + // Point every temp var at our own fixture so the child's tooling (tsx's + // transform cache) never depends on the host's /tmp state or perms. + children.push( + spawn(process.execPath, ["--import", "tsx", script, overlay], { + stdio: ["ignore", "pipe", "pipe"], + env: { ...process.env, TMPDIR: tmp, TEMP: tmp, TMP: tmp }, + }), + ); } try { await new Promise((resolve, reject) => { @@ -232,7 +240,7 @@ test("six racing processes: exactly one wins the lease (#1952)", { timeout: 60_0 if (err) reject(err); else resolve(); }; - for (const c of children) { + children.forEach((c, i) => { let buf = ""; c.stdout?.on("data", (d: Buffer) => { buf += d.toString(); @@ -248,18 +256,27 @@ test("six racing processes: exactly one wins the lease (#1952)", { timeout: 60_0 } } }); + c.stderr?.on("data", (d: Buffer) => { + stderrs[i] = (stderrs[i] ?? "") + d.toString(); + }); c.on("error", (err: Error) => finish(err)); c.on("close", (code) => { - if (!settled && code !== 0) finish(new Error(`race child exited ${code}`)); + if (!settled && code !== 0) { + finish(new Error(`race child ${i} exited ${code}: ${(stderrs[i] ?? "").trim().slice(0, 400) || "(no stderr)"}`)); + } }); - } + }); }); } finally { for (const c of children) { try { c.kill("SIGTERM"); } catch {} } } - assert.equal(results.filter((r) => r === "WIN").length, 1); + assert.equal( + results.filter((r) => r === "WIN").length, + 1, + `expected exactly one WIN, got results=${JSON.stringify(results)} stderr=${JSON.stringify(stderrs.map((s) => s.trim()).filter(Boolean))}`, + ); assert.equal(results.filter((r) => r === "BUSY").length, K - 1); cleanup(tmp); }); From 83174c7da65703fcc63207dcaa1a82e073125dec Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 3 Oct 2026 15:42:54 +0800 Subject: [PATCH 3/3] =?UTF-8?q?test:=20race=20child=20must=20be=20.mjs=20?= =?UTF-8?q?=E2=80=94=20a=20.ts=20entry=20outside=20any=20package.json=20in?= =?UTF-8?q?fers=20CJS=20and=20esbuild=20rejects=20its=20top-level=20await?= =?UTF-8?q?=20(children=20exit=201=20before=20printing;=20red=20on=20all?= =?UTF-8?q?=204=20CI=20lanes=20and=20locally)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/overlay-busy-lock.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/overlay-busy-lock.test.ts b/tests/overlay-busy-lock.test.ts index 0ee10dc211..a8f234df90 100644 --- a/tests/overlay-busy-lock.test.ts +++ b/tests/overlay-busy-lock.test.ts @@ -203,7 +203,7 @@ test("six racing processes: exactly one wins the lease (#1952)", { timeout: 60_0 const overlay = path.join(tmp, "race-overlay"); fs.mkdirSync(overlay, { recursive: true }); const launcherUrl = new URL("../src/launcher.ts", import.meta.url).href; - const script = path.join(tmp, "race-child.ts"); + const script = path.join(tmp, "race-child.mjs"); fs.writeFileSync( script, [