fix: address Copilot review round 3 (#2339)

- The spawn itself — spawnSpec and the synchronous argument validation in
  child_process.spawn — now sits inside the try whose finally releases the
  lease, so a command that fails before it starts cannot strand the lock
  until stale takeover. Tested with a non-array args value.
- A genuine release failure no longer falls through to the "released
  after" line; the lock may still be blocking the queue, and the message
  that says so is the last word.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PsmUAZ6aLUAFqenjJKSSRp
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-09-12 22:42:57 -04:00
commit d3e5432532
2 changed files with 53 additions and 27 deletions

View file

@ -460,12 +460,24 @@ export async function runUnderLease({
log( log(
`gate-lease: could not release the lease (${err?.message ?? err}). A waiter takes it over once it is ${formatDuration(STALE_MS)} stale — unless whatever blocked this removal persists, in which case remove ${lockPathOf(dir)} by hand.`, `gate-lease: could not release the lease (${err?.message ?? err}). A waiter takes it over once it is ${formatDuration(STALE_MS)} stale — unless whatever blocked this removal persists, in which case remove ${lockPathOf(dir)} by hand.`,
); );
// Not "released after": the lock may still be blocking the queue.
return;
} }
log( log(
`gate-lease: released after ${formatDuration(Date.now() - startedRunning)}${waited >= pollMs ? ` (waited ${formatDuration(waited)} first)` : ""}.`, `gate-lease: released after ${formatDuration(Date.now() - startedRunning)}${waited >= pollMs ? ` (waited ${formatDuration(waited)} first)` : ""}.`,
); );
}; };
const signals = ["SIGINT", "SIGTERM", "SIGHUP"];
let stoppedBy = null;
let escalation = null;
let handlers = [];
let outcome;
// Everything from here to the child's exit is inside the `try`, so a
// synchronous failure to even start it — an argument `winShellArgs`
// refuses, a spawn option Node rejects — releases the lease the same way
// an asynchronous spawn error does.
try {
const spec = spawnSpec(command, args); const spec = spawnSpec(command, args);
const child = spawn(spec.command, spec.args, { const child = spawn(spec.command, spec.args, {
stdio, stdio,
@ -474,10 +486,6 @@ export async function runUnderLease({
// Its own group, so one signal reaches every descendant. See the header. // Its own group, so one signal reaches every descendant. See the header.
detached: spec.detached, detached: spec.detached,
}); });
const signals = ["SIGINT", "SIGTERM", "SIGHUP"];
let stoppedBy = null;
let escalation = null;
const onSignal = (signal) => { const onSignal = (signal) => {
if (stoppedBy !== null) return; if (stoppedBy !== null) return;
stoppedBy = signal; stoppedBy = signal;
@ -490,14 +498,11 @@ export async function runUnderLease({
signalTree(child, "SIGKILL"); signalTree(child, "SIGKILL");
}, graceMs); }, graceMs);
}; };
const handlers = signals.map((signal) => { handlers = signals.map((signal) => {
const handler = () => onSignal(signal); const handler = () => onSignal(signal);
process.on(signal, handler); process.on(signal, handler);
return [signal, handler]; return [signal, handler];
}); });
let outcome;
try {
outcome = await new Promise((resolve, reject) => { outcome = await new Promise((resolve, reject) => {
child.once("error", reject); child.once("error", reject);
child.once("exit", (code, signal) => resolve({ code, signal })); child.once("exit", (code, signal) => resolve({ code, signal }));

View file

@ -274,6 +274,27 @@ test("a command that cannot be spawned still releases the lease", async () => {
assert.ok(!existsSync(leaseTarget(dir))); assert.ok(!existsSync(leaseTarget(dir)));
}); });
test("a command that fails synchronously before it starts still releases the lease", async () => {
const dir = freshDir();
const { log } = collectLog();
// `spawn` validates its arguments synchronously: a non-array `args` throws
// before any child exists, the same class as `winShellArgs` refusing a `%`
// on Windows. That throw must not strand the lock until stale takeover.
await assert.rejects(
runUnderLease({
command: process.execPath,
args: "-e",
dir,
log,
stdio: "ignore",
pollMs: 25,
}),
{ code: "ERR_INVALID_ARG_TYPE" },
);
assert.ok(!existsSync(lockPathOf(dir)));
assert.ok(!existsSync(leaseTarget(dir)));
});
test("a lock replaced mid-run (stale takeover) is the winner's, and is left alone", async () => { test("a lock replaced mid-run (stale takeover) is the winner's, and is left alone", async () => {
const dir = freshDir(); const dir = freshDir();
const { lines, log } = collectLog(); const { lines, log } = collectLog();