Skip to content

Commit 07b9758

Browse files
olddonkeyclaude
andcommitted
fix(tests): reap the recovery proxy instead of trusting stop
The update-recovery case starts a real detached proxy, and its cleanup ran the reap only when `node ocx.mjs stop` exited non-zero. That exit code is a claim, not proof: `stop` also reports success when it finds no live runtime to stop, which is indistinguishable here from one it failed to stop. On that path nothing killed the proxy, and the block went straight on to `rmSync` the fixture tree. The survivor was reparented to init with its package tree, `src` symlink, and config deleted underneath it — unable to serve, unable to exit. One escapee sat at 99% of a core for three and a half hours, 202 minutes of CPU time, listening on nothing. It also held the test machine lock, which is how it surfaced. Cleanup now resolves the pid before anything destroys the record of it, runs `stop` for its graceful path, then verifies liveness and reaps regardless of what `stop` claimed. `rmSync` moves inside a nested finally so it still always runs, but strictly after the reap: deleting the tree out from under a live detached process is what turned a missed kill into a permanently spinning orphan. Reaping the recorded pid is enough because bin/ocx.mjs mirrors its Bun child's exit, so the node launcher follows it down. Reaping alone would still fail silently, because the case passed the whole time it was leaking. An afterAll now audits the pid, so a future regression is red instead of invisible. Verified by A/B: with `stop` stubbed to exit 0 without killing, the old cleanup reports 15 pass / 0 fail and leaves the orphan, the new one reports 15 pass / 0 fail and leaves nothing, and the old cleanup under the new guard turns red. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent a57b962 commit 07b9758

1 file changed

Lines changed: 46 additions & 18 deletions

File tree

‎tests/update-stop-first.test.ts‎

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { describe, expect, test } from "bun:test";
1+
import { afterAll, describe, expect, test } from "bun:test";
22
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
33
import { createServer } from "node:net";
44
import { tmpdir } from "node:os";
55
import { dirname, join } from "node:path";
66
import { runNpmCachePreflight } from "../src/update/npm-cache-preflight.mjs";
7-
import { killProxy } from "../src/lib/process-control";
7+
import { isProcessAlive, killProxy } from "../src/lib/process-control";
88

99
const repoRoot = join(import.meta.dir, "..");
1010

@@ -63,6 +63,16 @@ const serverSource = readFileSync(join(import.meta.dir, "..", "src", "server", "
6363
const dispatchSource = readFileSync(join(import.meta.dir, "..", "src", "cli", "dispatch.ts"), "utf8");
6464

6565
describe("update stops the running proxy before replacing files", () => {
66+
// The recovery case starts a real detached proxy, and its own result says nothing about
67+
// whether cleanup reaped it — it stayed green while an escapee spun on a deleted tree for
68+
// hours. Auditing the pid once the suite is done turns a silent leak back into a red test.
69+
let auditedRecoveryPid: number | undefined;
70+
71+
afterAll(() => {
72+
if (auditedRecoveryPid === undefined) return;
73+
expect(isProcessAlive(auditedRecoveryPid)).toBe(false);
74+
});
75+
6676
test("a failed cache pre-flight aborts before the stop callback can run", () => {
6777
let stopped = false;
6878
const malformedSpawn = (() => ({ status: 0, signal: null, stdout: "not-json", stderr: "" })) as never;
@@ -227,24 +237,42 @@ esac
227237
expect(runtime.pid).toBeGreaterThan(0);
228238
recoveredPid = runtime.pid;
229239
} finally {
230-
const stopped = existsSync(launcher)
231-
? Bun.spawnSync(["node", launcher, "stop"], {
232-
cwd: root,
233-
env,
234-
stdout: "ignore",
235-
stderr: "ignore",
236-
timeout: UPDATE_SPAWN_TIMEOUT_MS,
237-
})
238-
: null;
239-
if (stopped?.exitCode !== 0) {
240-
if (!recoveredPid) {
241-
try {
242-
recoveredPid = JSON.parse(readFileSync(join(opencodexHome, "runtime-port.json"), "utf8")).pid;
243-
} catch { /* the proxy never wrote runtime state */ }
240+
// Resolve the pid FIRST. `stop` rewrites runtime-port.json and the rmSync below
241+
// deletes it outright, so this is the last moment the detached proxy the recovery
242+
// path started can still be identified at all.
243+
if (!recoveredPid) {
244+
try {
245+
recoveredPid = JSON.parse(readFileSync(join(opencodexHome, "runtime-port.json"), "utf8")).pid;
246+
} catch { /* the proxy never wrote runtime state */ }
247+
}
248+
auditedRecoveryPid = Number.isSafeInteger(recoveredPid) && recoveredPid! > 0
249+
? recoveredPid
250+
: undefined;
251+
if (existsSync(launcher)) {
252+
Bun.spawnSync(["node", launcher, "stop"], {
253+
cwd: root,
254+
env,
255+
stdout: "ignore",
256+
stderr: "ignore",
257+
timeout: UPDATE_SPAWN_TIMEOUT_MS,
258+
});
259+
}
260+
try {
261+
// `stop` exiting 0 is a claim, not proof: it also reports success when it finds no
262+
// live runtime to stop, which is indistinguishable here from one it failed to stop.
263+
// Gating the reap on that exit code let a detached proxy survive, get reparented to
264+
// init, and then spin on a fixture tree this same block had already deleted — one
265+
// escapee burned a full core for hours. Verify liveness and reap regardless.
266+
// bin/ocx.mjs mirrors its Bun child's exit, so reaping the recorded child pid takes
267+
// the node launcher with it.
268+
if (Number.isSafeInteger(recoveredPid) && recoveredPid! > 0 && isProcessAlive(recoveredPid!)) {
269+
killProxy(recoveredPid!);
244270
}
245-
if (Number.isSafeInteger(recoveredPid) && recoveredPid! > 0) killProxy(recoveredPid!);
271+
} finally {
272+
// Ordered after the reap on purpose: deleting the tree out from under a live
273+
// detached proxy is what turned a missed kill into a permanently spinning orphan.
274+
rmSync(root, { recursive: true, force: true });
246275
}
247-
rmSync(root, { recursive: true, force: true });
248276
}
249277
},
250278
RECOVERY_CASE_TIMEOUT_MS,

0 commit comments

Comments
 (0)