Skip to content

Commit fdeaa69

Browse files
cliffhallclaude
andcommitted
fix(scripts): normalize stopChild's graceMs and correct removeSafe's JSDoc
Copilot review round 1 on #1895. - `stopChild()` trusted `graceMs` as given, but call sites build it as `Number(process.env.X ?? 5000)` — a typo yields `NaN`, which `setTimeout` treats as `0`. That would fire SIGTERM and SIGKILL in the same tick and print "did not exit within NaNms", collapsing the whole escalation silently. New exported `normalizeGraceMs()` falls back to `DEFAULT_EXIT_GRACE_MS` for anything non-finite or non-positive, with two tests: the table of bad values, and an end-to-end one asserting a `NaN` grace period still leaves only SIGTERM sent a turn later. - `removeSafe()`'s `@returns` said "true if the path was removed", which is inaccurate for a missing path (`force: true` makes it a no-op success). Reworded to say what it actually reports. `npm run ci` passes. Claude-Session: https://claude.ai/code/session_01YAt8rqxysNbhYWLhoRm3fU Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent 47fdd61 commit fdeaa69

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎scripts/lib/child-cleanup.mjs‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ import { rmSync } from "node:fs";
2525
/** Default wait after SIGTERM before escalating to SIGKILL. */
2626
export const DEFAULT_EXIT_GRACE_MS = 5000;
2727

28+
/**
29+
* Coerce a caller-supplied grace period to a usable one.
30+
*
31+
* Call sites read this from the environment (`Number(process.env.X ?? 5000)`),
32+
* so a typo yields `NaN` — which `setTimeout` treats as `0`, silently turning
33+
* the whole SIGTERM→SIGKILL escalation into an immediate double-kill and
34+
* printing "did not exit within NaNms". Fall back to the default instead.
35+
*
36+
* @param {unknown} graceMs
37+
*/
38+
export function normalizeGraceMs(graceMs) {
39+
return typeof graceMs === "number" && Number.isFinite(graceMs) && graceMs > 0
40+
? graceMs
41+
: DEFAULT_EXIT_GRACE_MS;
42+
}
43+
2844
/**
2945
* True once the child process is known to be gone (normal exit or signalled).
3046
* Both fields stay `null` while it is alive, and `killed` is NOT a substitute —
@@ -54,7 +70,7 @@ export function hasExited(child) {
5470
* @param {object} [opts]
5571
* @param {string} [opts.label] prefix for warnings, e.g. "pack:verify"
5672
* @param {string} [opts.what] what the child is, for warnings
57-
* @param {number} [opts.graceMs]
73+
* @param {number} [opts.graceMs] non-finite/non-positive falls back to the default
5874
* @param {(msg: string) => void} [opts.warn]
5975
* @returns {Promise<"already-exited" | "exited" | "gave-up">}
6076
*/
@@ -63,11 +79,12 @@ export function stopChild(
6379
{
6480
label = "cleanup",
6581
what = "child process",
66-
graceMs = DEFAULT_EXIT_GRACE_MS,
82+
graceMs: requestedGraceMs = DEFAULT_EXIT_GRACE_MS,
6783
warn = console.warn,
6884
} = {},
6985
) {
7086
if (hasExited(child)) return Promise.resolve("already-exited");
87+
const graceMs = normalizeGraceMs(requestedGraceMs);
7188

7289
return new Promise((resolve) => {
7390
let settled = false;
@@ -111,7 +128,9 @@ export function stopChild(
111128
* @param {object} [opts]
112129
* @param {string} [opts.label]
113130
* @param {(msg: string) => void} [opts.warn]
114-
* @returns {boolean} true if the path was removed
131+
* @returns {boolean} true if the path is gone — which includes a path that was
132+
* never there, since `force: true` makes a missing path a no-op success.
133+
* False means removal threw and a warning was emitted.
115134
*/
116135
export function removeSafe(
117136
path,

‎scripts/lib/child-cleanup.test.mjs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import { mkdtempSync, writeFileSync, existsSync } from "node:fs";
99
import { tmpdir } from "node:os";
1010
import { join } from "node:path";
1111

12-
import { hasExited, removeSafe, stopChild } from "./child-cleanup.mjs";
12+
import {
13+
DEFAULT_EXIT_GRACE_MS,
14+
hasExited,
15+
normalizeGraceMs,
16+
removeSafe,
17+
stopChild,
18+
} from "./child-cleanup.mjs";
1319

1420
/**
1521
* Minimal stand-in for a ChildProcess: records the signals it was sent and lets
@@ -44,6 +50,28 @@ test("hasExited: only exitCode/signalCode count, not `killed`", () => {
4450
assert.equal(hasExited(killed), false);
4551
});
4652

53+
test("normalizeGraceMs: a bad env-derived value can't collapse the escalation", () => {
54+
// `Number(process.env.X ?? 5000)` is how call sites build this, so a typo
55+
// yields NaN — which setTimeout treats as 0, firing SIGTERM and SIGKILL in
56+
// the same tick and printing "within NaNms".
57+
for (const bad of [NaN, 0, -1, Infinity, undefined, null, "3000"])
58+
assert.equal(normalizeGraceMs(bad), DEFAULT_EXIT_GRACE_MS, String(bad));
59+
assert.equal(normalizeGraceMs(25), 25);
60+
});
61+
62+
test("stopChild: a NaN graceMs falls back rather than double-killing at once", async () => {
63+
const child = fakeChild();
64+
const { warnings, warn } = collectWarnings();
65+
const stopped = stopChild(child, { graceMs: NaN, warn });
66+
// With NaN passed through, both timers would already have been scheduled at
67+
// 0ms; a macrotask turn is enough to observe that.
68+
await new Promise((r) => setImmediate(r));
69+
assert.deepEqual(child.signals, ["SIGTERM"]);
70+
assert.deepEqual(warnings, []);
71+
child.emit("exit", 0);
72+
assert.equal(await stopped, "exited");
73+
});
74+
4775
test("stopChild: an already-exited child is not signalled again", async () => {
4876
const child = fakeChild({ exitCode: 0 });
4977
const { warnings, warn } = collectWarnings();

0 commit comments

Comments
 (0)