From ce1d59fee9e452c062ce318833e8a8b345abe274 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 04:30:16 +0800 Subject: [PATCH 1/4] fix(test): hermetic per-test zone base for port-zone-live e2e (#3212) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The live zone tests squatted the machine-global fixed zone base (18787) directly, so any concurrent leg or leaked daemon on the same runner turned them red with EADDRINUSE (#3044 tests on master since 2026-10-14). Each test now reserves its own stable port run through the existing BILI_ZONE_PORT knob (scan above the zone default, below the ephemeral floor), claims the head with a real listener as the arbiter, and releases it (waiting for the socket to free) before the scenario — no new config surface, no ci.yml change. --- tests/port-zone-live.test.ts | 161 ++++++++++++++++++++++++----------- 1 file changed, 113 insertions(+), 48 deletions(-) diff --git a/tests/port-zone-live.test.ts b/tests/port-zone-live.test.ts index 475579a71a..280e9e794b 100644 --- a/tests/port-zone-live.test.ts +++ b/tests/port-zone-live.test.ts @@ -7,17 +7,28 @@ import os from "node:os"; import path from "node:path"; import { ensureProxyRunning } from "../src/launcher.ts"; import { isPidAlive, portZoneFilePath, readZonePort } from "../src/instance.ts"; -import { ZONE_PORT_BASE } from "../src/config.ts"; import { UPGRADE_REPLACEMENT_ENV } from "../src/listen-policy.ts"; // #1660 live regression: the sticky-zone lifecycle against REAL proxy // processes — real spawn handshake, real EADDRINUSE ladder, real attach // probe, real sticky follow. The mocked sequences in port-zone.test.ts pin -// the decision table; this file pins the WIRING: the bug class where a call -// site pre-resolves the port and the settle never fires (90cc7e8) only -// surfaces with a real child writing a real instance file. Gated exactly -// like the claude-native live e2e (#1248): the dedicated CI runner sets -// ACP_TEST_CLAUDE_NATIVE=1; locally loaded sandboxes skip. +// the decision table (incl. the ZONE_PORT_BASE === 18787 default); this file +// pins the WIRING: the bug class where a call site pre-resolves the port and +// the settle never fires (90cc7e8) only surfaces with a real child writing a +// real instance file. Gated exactly like the claude-native live e2e (#1248): +// the dedicated CI runner sets ACP_TEST_CLAUDE_NATIVE=1; locally loaded +// sandboxes skip. +// +// #3212: the zone base is MACHINE-GLOBAL state. On the shared CI runners, +// concurrent legs and daemons leaked by cancelled jobs (cancel-in-progress +// kills the step, not detached children) squat the fixed 18787, and under +// load the SIGKILL→socket-teardown lag stretches past the next test's squat +// — either way these wiring tests turned into port-fighting flakes (the +// master red storm since #3143 sharding multiplied concurrent legs). Every +// test now reserves a PRIVATE zone base (reserveZoneBase) and hands it to +// the spawned daemons through the existing BILI_ZONE_PORT knob +// (resolveZonePortBase, src/config.ts) — zero dependence on who else is on +// the box. The default constant stays pinned by the mocked suite. const LIVE = process.env.ACP_TEST_CLAUDE_NATIVE === "1"; const liveSkip = LIVE ? undefined : "set ACP_TEST_CLAUDE_NATIVE=1 (live proxy e2e; #1248)"; @@ -86,6 +97,48 @@ function freePort(): Promise { }); } +async function waitPortFree(port: number, ms = 5_000): Promise { + // A SIGKILLed daemon's listen socket frees only once the kernel tears it + // down — under CI load that lag stretches past a fixed sleep (#3212). + const deadline = Date.now() + ms; + while (await portOpen(port)) { + if (Date.now() >= deadline) throw new Error(`port ${port} still occupied after ${ms}ms`); + await new Promise((r) => setTimeout(r, 50)); + } +} + +// #3212: reserve a private zone base. Scan the stable sub-ephemeral region +// (above the real 18787 base, below Linux's 32768 ephemeral floor) for a run +// long enough for the child's full EADDRINUSE ladder, then CLAIM the head +// with a real listen — the bind is the arbiter, so two concurrent pickers +// (other legs on a shared runner) can never land on the same base. +const LADDER_RUN_LEN = 20; // server.ts MAX_LISTEN_ATTEMPTS=17 + slack +const ZONE_SCAN_START = 18_800; +const ZONE_SCAN_END = 30_000; + +async function reserveZoneBase(): Promise<{ base: number; release: () => void }> { + const claim = net.createServer((sock) => sock.destroy()); + for (let base = ZONE_SCAN_START; base + LADDER_RUN_LEN <= ZONE_SCAN_END; base += 25) { + let runFree = true; + for (let p = base; p < base + LADDER_RUN_LEN; p++) { + if (await portOpen(p)) { runFree = false; break; } + } + if (!runFree) continue; + const bound = await new Promise((resolve) => { + claim.once("error", () => resolve(false)); + claim.listen(base, "127.0.0.1", () => resolve(true)); + }); + if (!bound) continue; // stolen between probe and bind — next candidate + return { + base, + release: () => { + try { claim.close(); } catch {} + }, + }; + } + throw new Error(`no free ${LADDER_RUN_LEN}-port zone run in ${ZONE_SCAN_START}..${ZONE_SCAN_END} (#3212)`); +} + /** A dumb port squatter: accepts nothing — destroys every connection so * health probes fail fast instead of hanging on a silent socket. */ async function squatted(port: number): Promise { @@ -108,16 +161,28 @@ async function squatted(port: number): Promise { // Redirect the WHOLE view — this process's state-dir reads AND the spawned // child's (the launcher builds the child env off process.env) — so // port-zone.json, the instance file and the #707 starting marker stay in a -// throwaway sandbox. -async function withSandbox(fn: () => Promise): Promise { +// throwaway sandbox. #3212: also pin a PRIVATE zone base via BILI_ZONE_PORT +// (the launcher's child env inherits it), then release + verify the base is +// actually free before the scenario starts — every test gets an exclusive, +// machine-local zone regardless of who else is on the runner. +async function withSandbox(fn: (base: number) => Promise): Promise { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-zone-live-")); - const prev = process.env.XDG_STATE_HOME; + const prevXdg = process.env.XDG_STATE_HOME; process.env.XDG_STATE_HOME = path.join(home, "state"); + const prevZone = process.env.BILI_ZONE_PORT; + const zone = await reserveZoneBase(); + process.env.BILI_ZONE_PORT = String(zone.base); try { - return await fn(); + // The claim IS the squatter while held; hand the test a VERIFIED-free + // base (release, then wait out the kernel teardown). + zone.release(); + await waitPortFree(zone.base); + return await fn(zone.base); } finally { - if (prev === undefined) delete process.env.XDG_STATE_HOME; - else process.env.XDG_STATE_HOME = prev; + if (prevZone === undefined) delete process.env.BILI_ZONE_PORT; + else process.env.BILI_ZONE_PORT = prevZone; + if (prevXdg === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = prevXdg; } } @@ -151,13 +216,13 @@ test( "zone live: lane'd port-0 launch binds the base, settles sticky; a repeat attaches", { timeout: 180_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (base) => { ensureDistBuilt(); const h1 = await launch({ lane: "zcode" }); trackKill(h1.child?.pid); try { - assert.equal(h1.port, ZONE_PORT_BASE, "fresh lane'd launch binds the zone base"); - assert.equal(readZonePort("zcode"), ZONE_PORT_BASE, "the bound port settles sticky"); + assert.equal(h1.port, base, "fresh lane'd launch binds the zone base"); + assert.equal(readZonePort("zcode"), base, "the bound port settles sticky"); const h2 = await launch({ lane: "zcode" }); assert.equal(h2.attached, true, "repeat launch attaches to the live daemon"); assert.equal(h2.origin, h1.origin); @@ -172,25 +237,25 @@ test( "zone live: squatter on the sticky port → ladder drift settles sticky; a repeat attaches at the drifted port", { timeout: 240_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (base) => { ensureDistBuilt(); const h1 = await launch({ lane: "zcode" }); const daemon1 = h1.child?.pid; trackKill(daemon1); let daemon2: number | undefined; try { - assert.equal(h1.port, ZONE_PORT_BASE, "precondition: first launch on the base"); + assert.equal(h1.port, base, "precondition: first launch on the base"); killNow(daemon1); - await new Promise((r) => setTimeout(r, 300)); // kernel releases the listen socket - const squatter = await squatted(ZONE_PORT_BASE); + await waitPortFree(base); // kernel releases the listen socket (#3212) + const squatter = await squatted(base); const h2 = await launch({ lane: "zcode" }); daemon2 = h2.child?.pid; trackKill(daemon2); - assert.equal(h2.port, ZONE_PORT_BASE + 1, "preferred base occupied → +1 ladder drift"); - assert.equal(readZonePort("zcode"), ZONE_PORT_BASE + 1, "sticky follows the drift"); + assert.equal(h2.port, base + 1, "preferred base occupied → +1 ladder drift"); + assert.equal(readZonePort("zcode"), base + 1, "sticky follows the drift"); const h3 = await launch({ lane: "zcode" }); assert.equal(h3.attached, true); - assert.equal(h3.port, ZONE_PORT_BASE + 1, "later launches follow the drift"); + assert.equal(h3.port, base + 1, "later launches follow the drift"); // Eager close — the next test expects the zone base free; the // after() backstop alone would hold it past this test's end. squatter.close(); @@ -206,7 +271,7 @@ test( "zone live: explicit strictPort pin binds exactly and never settles a zone record", { timeout: 180_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (_base) => { ensureDistBuilt(); const port = await freePort(); const h = await launch({ lane: "zcode", port, strictPort: true }); @@ -226,20 +291,20 @@ test( "zone live: a second lane spawns its own daemon past the first and keeps an independent sticky key", { timeout: 240_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (base) => { ensureDistBuilt(); const h1 = await launch({ lane: "zcode" }); const daemon1 = h1.child?.pid; trackKill(daemon1); try { - assert.equal(h1.port, ZONE_PORT_BASE); + assert.equal(h1.port, base); const h2 = await launch({ lane: "kimi" }); trackKill(h2.child?.pid); try { assert.equal(h2.attached, undefined, "cross-lane instances are never attach targets"); - assert.equal(h2.port, ZONE_PORT_BASE + 1, "second lane ladders past the first lane's daemon"); - assert.equal(readZonePort("zcode"), ZONE_PORT_BASE); - assert.equal(readZonePort("kimi"), ZONE_PORT_BASE + 1, "each lane settles its own sticky key"); + assert.equal(h2.port, base + 1, "second lane ladders past the first lane's daemon"); + assert.equal(readZonePort("zcode"), base); + assert.equal(readZonePort("kimi"), base + 1, "each lane settles its own sticky key"); } finally { killNow(h2.child?.pid); } @@ -254,13 +319,13 @@ test( "zone live: an unlane'd launch is ephemeral and leaves no zone record", { timeout: 180_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (base) => { ensureDistBuilt(); const h = await launch({}); trackKill(h.child?.pid); try { assert.ok(h.port > 0); - assert.notEqual(h.port, ZONE_PORT_BASE, "unlane'd traffic never rides the zone base"); + assert.notEqual(h.port, base, "unlane'd traffic never rides the zone base"); assert.equal(fs.existsSync(portZoneFilePath()), false, "no sticky record without a lane"); } finally { killNow(h.child?.pid); @@ -282,7 +347,7 @@ test( "zone live: upgrade-restart overlap — new build waits out the same-lane predecessor and rebinds the SAME port (#1723)", { timeout: 240_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (base) => { ensureDistBuilt(); const v2Script = makeV2Script(); const h1 = await launch({ lane: "zcode" }); @@ -290,7 +355,7 @@ test( trackKill(daemon1); let daemon2: number | undefined; try { - assert.equal(h1.port, ZONE_PORT_BASE, "precondition: the old build holds the zone base"); + assert.equal(h1.port, base, "precondition: the old build holds the zone base"); const p2 = launch({ lane: "zcode", script: v2Script }); // Let the new child hit EADDRINUSE and enter its predecessor // wait, then retire the old build the way an exiting host's @@ -301,8 +366,8 @@ test( daemon2 = h2.child?.pid; trackKill(daemon2); assert.equal(h2.attached, undefined, "different builds never attach to each other"); - assert.equal(h2.port, ZONE_PORT_BASE, "waited out the predecessor and rebound the SAME port — no drift"); - assert.equal(readZonePort("zcode"), ZONE_PORT_BASE, "sticky did NOT ratchet up"); + assert.equal(h2.port, base, "waited out the predecessor and rebound the SAME port — no drift"); + assert.equal(readZonePort("zcode"), base, "sticky did NOT ratchet up"); } finally { killNow(daemon1); killNow(daemon2); @@ -316,7 +381,7 @@ test( "zone live: predecessor that never leaves — bounded wait exhausts and the +1 ladder takes over as before (#1723)", { timeout: 300_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (base) => { ensureDistBuilt(); const v2Script = makeV2Script(); const h1 = await launch({ lane: "zcode" }); @@ -324,7 +389,7 @@ test( trackKill(daemon1); let daemon2: number | undefined; try { - assert.equal(h1.port, ZONE_PORT_BASE, "precondition: the old build holds the zone base"); + assert.equal(h1.port, base, "precondition: the old build holds the zone base"); // daemon1's owner (this test process) stays alive — no // parent-gone flush ever comes, so the holder never frees the // port. The new child's bounded wait (~5s) must give up and @@ -333,8 +398,8 @@ test( const h2 = await launch({ lane: "zcode", script: v2Script }); daemon2 = h2.child?.pid; trackKill(daemon2); - assert.equal(h2.port, ZONE_PORT_BASE + 1, "budget exhausted → the +1 ladder still lands the launch"); - assert.equal(readZonePort("zcode"), ZONE_PORT_BASE + 1, "sticky follows (residual drift, now warned)"); + assert.equal(h2.port, base + 1, "budget exhausted → the +1 ladder still lands the launch"); + assert.equal(readZonePort("zcode"), base + 1, "sticky follows (residual drift, now warned)"); } finally { killNow(daemon1); killNow(daemon2); @@ -356,16 +421,16 @@ test( "zone live: marked upgrade-replacement fails fast on an occupied port instead of hopping (#3044)", { timeout: 240_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (base) => { ensureDistBuilt(); // A non-bili squatter holds the zone base: no same-lane predecessor // is recognized, so the marked child gets no wait budget at all — // first EADDRINUSE must be its last breath. Under the pre-#3044 // ladder this exact spawn drifted to base+1 and idled there. - const squatter = await squatted(ZONE_PORT_BASE); + const squatter = await squatted(base); const child = spawn( process.execPath, - [distCli, "start", "--host", "127.0.0.1", "--port", String(ZONE_PORT_BASE)], + [distCli, "start", "--host", "127.0.0.1", "--port", String(base)], { env: { ...process.env, @@ -384,8 +449,8 @@ test( setTimeout(() => resolve(undefined), 120_000).unref(); }); assert.equal(exitCode, 1, "the marked replacement must fail fast with exit code 1 (upgrade pending, host decides retry)"); - assert.equal(await portOpen(ZONE_PORT_BASE + 1), false, "nothing may have hopped onto the next port — that idle hopper IS the #3044 orphan shape"); - assert.equal(await portOpen(ZONE_PORT_BASE), true, "the squatter still owns the base port"); + assert.equal(await portOpen(base + 1), false, "nothing may have hopped onto the next port — that idle hopper IS the #3044 orphan shape"); + assert.equal(await portOpen(base), true, "the squatter still owns the base port"); squatter.close(); } finally { killNow(child.pid); @@ -398,7 +463,7 @@ test( "zone live: unmarked lane'd spawn against a LIVE same-build twin exits instead of hopping (#3044)", { timeout: 240_000, skip: LIVE ? process.platform !== "linux" : liveSkip }, async () => { - await withSandbox(async () => { + await withSandbox(async (base) => { ensureDistBuilt(); // The 2026-10-13 recurrence shape: a host respawn raced an attach // scan and spawned while an identical instance already served the @@ -409,10 +474,10 @@ test( const h1 = await launch({ lane: "zcode" }); const daemon1 = h1.child?.pid; trackKill(daemon1); - assert.equal(h1.port, ZONE_PORT_BASE, "precondition: the twin serves the zone base"); + assert.equal(h1.port, base, "precondition: the twin serves the zone base"); const child = spawn( process.execPath, - [distCli, "start", "--host", "127.0.0.1", "--port", String(ZONE_PORT_BASE)], + [distCli, "start", "--host", "127.0.0.1", "--port", String(base)], { env: { ...process.env, BILI_LAUNCH_TOKEN: "3044-twin-test", BILI_LAUNCHER_LANE: "zcode" }, stdio: "ignore", @@ -426,8 +491,8 @@ test( setTimeout(() => resolve(undefined), 120_000).unref(); }); assert.equal(exitCode, 1, "the duplicate must fail fast with exit code 1"); - assert.equal(await portOpen(ZONE_PORT_BASE + 1), false, "nothing may have hopped onto the next port — the hopper IS the orphan shape"); - assert.equal(await portOpen(ZONE_PORT_BASE), true, "the serving twin keeps its port"); + assert.equal(await portOpen(base + 1), false, "nothing may have hopped onto the next port — the hopper IS the orphan shape"); + assert.equal(await portOpen(base), true, "the serving twin keeps its port"); assert.ok(daemon1 !== undefined && isPidAlive(daemon1), "the serving twin must still be alive"); } finally { killNow(child.pid); From 66f607c9ea5d2cf274d05f5f66bbb1d69850bfac Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 04:30:16 +0800 Subject: [PATCH 2/4] fix(test): pin the #2877 attach-policy flip in pinned-port hook e2e (#3212) #2877 flipped the LANEd-hook default to own-lane, so a lane-less manual daemon on the pinned port is refused (fail fast + remedy) instead of ridden; the pre-existing e2e still asserted the old attach behavior and went deterministically red on every leg. The e2e now pins the documented restore path (native.attachPolicy=global-fallback), and a sibling test pins the new default: fail fast, actionable remedy, exit 0, daemon untouched, no port-hop. --- tests/claude-native.test.ts | 57 ++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/tests/claude-native.test.ts b/tests/claude-native.test.ts index e094643cb2..3e6c853ab1 100644 --- a/tests/claude-native.test.ts +++ b/tests/claude-native.test.ts @@ -1562,7 +1562,11 @@ test("watcher route: shared proxies take watcher registrations, daemons refuse ( // for lifecycle-less LANE'd squatters (unit-tested in launcher.test.ts) and // for non-bili listeners (spawn EADDRINUSE). Linux-only like the other hook // e2es (fake claude walks /proc). -test("hook e2e: manual `bili start` on the pinned port is attached, not refused (#1322/#1335/#1660)", { timeout: 180_000, skip: LIVE_E2E ? process.platform !== "linux" : liveSkip }, async () => { +// #2877 (#3212): the LANEd-hook DEFAULT flipped to own-lane — riding a +// lane-less daemon is now OPT-IN. This scenario pins the documented restore +// path (native.attachPolicy=global-fallback); the sibling test below pins +// the new default (fail fast + actionable remedy). +test("hook e2e: manual `bili start` on the pinned port is attached via global-fallback opt-in (#1322/#1335/#1660/#2877)", { timeout: 180_000, skip: LIVE_E2E ? process.platform !== "linux" : liveSkip }, async () => { const distCli = path.resolve(import.meta.dirname, "..", "dist", "index.js"); const distScript = path.resolve(import.meta.dirname, "..", "dist", "claude-native-bootstrap.js"); ensureDistBuilt(distCli); @@ -1570,6 +1574,10 @@ test("hook e2e: manual `bili start` on the pinned port is attached, not refused const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-squatter-")); const xdg = { home, config: path.join(home, "cfg"), state: path.join(home, "state"), cache: path.join(home, "cache"), data: path.join(home, "data") }; fs.mkdirSync(path.join(home, "tmp"), { recursive: true }); + // The documented #2877 restore path: opt back into riding lane-less daemons. + const cfgDir = path.join(xdg.config, "billion-context"); + fs.mkdirSync(cfgDir, { recursive: true }); + fs.writeFileSync(path.join(cfgDir, "billion-context.json"), JSON.stringify({ native: { attachPolicy: "global-fallback" } })); const port = await freePort(); const instanceFile = path.join(xdg.state, "billion-context", "proxy-origin"); const baseEnv = { @@ -1644,6 +1652,53 @@ test("hook e2e: manual `bili start` on the pinned port is attached, not refused } }); +// #2877 (#3212): the sibling of the global-fallback scenario above. Under the +// NEW default (own-lane) a lane-less manual daemon on the pinned port must NOT +// be ridden: the hook fails fast with an actionable remedy, stays non-fatal to +// claude (exit 0), leaves the user's daemon untouched and never port-hops. +// This is the exact failure shape #3212's stale e2e caught in CI — pre-#2877 +// it attached, post-#2877 it refuses; pinning both sides keeps the flip honest. +test("hook e2e: default own-lane policy fails fast on a lane-less pinned-port daemon (#2877/#3212)", { timeout: 180_000, skip: LIVE_E2E ? process.platform !== "linux" : liveSkip }, async () => { + const distCli = path.resolve(import.meta.dirname, "..", "dist", "index.js"); + const distScript = path.resolve(import.meta.dirname, "..", "dist", "claude-native-bootstrap.js"); + ensureDistBuilt(distCli); + ensureDistBuilt(distScript); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-ownlane-")); + const xdg = { home, config: path.join(home, "cfg"), state: path.join(home, "state"), cache: path.join(home, "cache"), data: path.join(home, "data") }; + fs.mkdirSync(path.join(home, "tmp"), { recursive: true }); + // NO config file: whatever the hook sees here IS the shipped default. + const port = await freePort(); + const instanceFile = path.join(xdg.state, "billion-context", "proxy-origin"); + const baseEnv = { + PATH: process.env.PATH ?? "/usr/bin:/bin", + HOME: xdg.home, + XDG_CONFIG_HOME: xdg.config, + XDG_STATE_HOME: xdg.state, + XDG_CACHE_HOME: xdg.cache, + XDG_DATA_HOME: xdg.data, + NO_COLOR: "1", + }; + const squatter = spawnProxy(distCli, port, baseEnv); + try { + assert.ok(await waitForPort(port, 60_000), "squatter daemon up on the stable port"); + assert.ok( + await waitForProxyVisible(port, instanceFile, 60_000), + "squatter fully visible to the attach machinery (health + identity record, #1903)", + ); + const nextWasFree = !(await canConnect(port + 1)); + const r = await runHook(distScript, port, xdg); + assert.equal(r.code, 0, "the hook stays non-fatal for claude even when its proxy cannot start"); + assert.match(r.stderr, /bring-up failed/, "fail-fast is loud"); + assert.match(r.stderr, /lifecycle-less bili proxy/, "the refusal names the lane-less occupant"); + assert.match(r.stderr, /attachExternal/, "the remedy is actionable (attachExternal opt-in)"); + assert.ok(await canConnect(port), "the user's daemon survives untouched"); + assert.equal(nextWasFree, !(await canConnect(port + 1)), "nothing may have port-hopped behind the refusal"); + } finally { + if (squatter > 1) killPid(squatter); + await rmHome(home); + } +}); + function spawnProxy(distCli: string, port: number, env: NodeJS.ProcessEnv): number { const child = spawn(process.execPath, [distCli, "start", "--host", "127.0.0.1", "--port", String(port)], { env, stdio: "ignore" }); return child.pid ?? 0; From 7cc36823529236b1b8444defa44f72fae97f23b8 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 05:01:36 +0800 Subject: [PATCH 3/4] fix(test): silence #3182-family CI flakes on this branch (#3215 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This head went red on three windows lanes with two pre-existing failure signatures that PR #3197 already fixes on its branch: - auto-restart "survives SIGTERM" stub: POSIX-only scenario — Windows terminates on SIGTERM uncatchably, so the stub can never model a SIGTERM-surviving replacement (w22-2 + w24-2 reds). Skip on win32. - claude-native "occupied stable port": the accepted probe socket's ECONNRESET was unhandled and crashed the test process (w24-3 red). Destroy connections eagerly. Both hunks are verbatim mirrors of #3197's fixes so the branches stay trivially reconcilable when the owner consolidates them. --- tests/auto-restart.test.ts | 2 +- tests/claude-native.test.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/auto-restart.test.ts b/tests/auto-restart.test.ts index 6bbf7987f7..fe4ca00fbb 100644 --- a/tests/auto-restart.test.ts +++ b/tests/auto-restart.test.ts @@ -662,7 +662,7 @@ test("performSelfRestart: port taken over by another bili instance → parent ex } }); -test("performSelfRestart: a replacement that survives SIGTERM is escalated loudly (#3044)", { timeout: 30_000 }, async () => { +test("performSelfRestart: a replacement that survives SIGTERM is escalated loudly (#3044)", { timeout: 30_000, skip: process.platform === "win32" ? "POSIX-only: Windows terminates on SIGTERM uncatchably, so a SIGTERM-surviving stub cannot be modeled (was a w24 CI red, #3182 family)" : false }, async () => { const { server, port } = await startServer(); const { log, entries } = makeLog(); const record: SpawnRecord = {}; diff --git a/tests/claude-native.test.ts b/tests/claude-native.test.ts index 3e6c853ab1..1eb82949f8 100644 --- a/tests/claude-native.test.ts +++ b/tests/claude-native.test.ts @@ -1211,6 +1211,12 @@ test("hook e2e: an occupied stable port fails loud — never port-hops", { timeo // claude model call on the dead original port (found live on the // host of issue #964: two test listeners on 48787/48788). const squatter = net.createServer(); + // #3182 family (w22-3 CI red): the hook's probe of the port holder gets + // abruptly reset by this foreign listener; without a connection handler + // the accepted socket's ECONNRESET was unhandled and crashed the test + // process. Kill every connection eagerly — hostile squatter, and no + // stray socket events. + squatter.on("connection", (c) => c.destroy()); squatter.listen(0, "127.0.0.1"); await once(squatter, "listening"); const port = (squatter.address() as net.AddressInfo).port; From fad1925ce83ac7581cee78f5e7ab80024799e7ef Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 05:01:36 +0800 Subject: [PATCH 4/4] refactor(test): drop dead empty catch in reserveZoneBase release (#3215 review) claim is always bound when release() runs (the bind is the arbiter); net.Server.close() on a listening server does not throw synchronously, so the try/catch swallowed nothing. Review nit per code-cleanliness rules (no empty catch). --- tests/port-zone-live.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/port-zone-live.test.ts b/tests/port-zone-live.test.ts index 280e9e794b..d2381bb496 100644 --- a/tests/port-zone-live.test.ts +++ b/tests/port-zone-live.test.ts @@ -131,9 +131,7 @@ async function reserveZoneBase(): Promise<{ base: number; release: () => void }> if (!bound) continue; // stolen between probe and bind — next candidate return { base, - release: () => { - try { claim.close(); } catch {} - }, + release: () => claim.close(), }; } throw new Error(`no free ${LADDER_RUN_LEN}-port zone run in ${ZONE_SCAN_START}..${ZONE_SCAN_END} (#3212)`);