Repository navigation
Closed
Description
What happened
runCommand does not handle every way child_process.spawn can fail:
- On
EMFILE/ENFILE, Node returns aChildProcesswithout stdio pipes and emitserrorasynchronously.child.stdout.on(...)then throwsTypeError: Cannot read properties of undefined (reading 'on'), the promise rejects instead of resolving with the documentedspawn_errorfailure, and theerrorlistener is never attached, so the latererrorevent crashes the process as an unhandled event. - Other libuv errors (for example
EBADF) are thrown synchronously fromspawn()inside the promise executor and also bypassspawn_error.
When this happens after SIGTERM (the post-signal ps poll), the owner has already been signaled but the evidence is lost. Reproduced by exhausting file descriptors only for spawns after the signal, using a fixture owner:
owner signalCode: SIGTERM
stdout: {"command":"unlock","status":"error","errorCode":"command_failed","exitCode":3,...} (no pid / signalSent)
actual process exit status: 1
stderr: Unhandled 'error' event ... spawn ps EMFILE
The JSON claims exitCode: 3 while the process exits 1 (not in the documented exit-code table), stderr is written in --json mode, and a caller cannot tell that SIGTERM was sent.
What you expected
- Every spawn failure resolves as
{ kind: "spawn_error" }without crashing. - Failures after
SIGTERMproduce atermination_failed/verification_failedunlock result that still recordspidandsignalSent: "SIGTERM", with an exit code from the documented table.
Environment
codex-unlock0.4.0 at6227e6c- macOS 27.0 arm64, Node.js 22.14.0
Source evidence
spawn()inside the promise executor without try/catch:Lines 46 to 52 in 6227e6c
return await new Promise((resolve) => { const isolatedProcessGroup = process.platform !== "win32"; const child = spawn(executable, args, { detached: isolatedProcessGroup, env: { ...process.env, LC_ALL: "C", LANG: "C" }, stdio: ["ignore", "pipe", "pipe"], }); - Stdio used before the
errorlistener is attached:Line 104 in 6227e6c
child.stdout.on("data", (chunk: Buffer | string) => { errorlistener registered afterwards:Lines 129 to 132 in 6227e6c
child.on("error", (error: NodeJS.ErrnoException) => { spawnError = error; failure ??= { kind: "spawn_error", message: errorText(error) }; }); - Post-signal process polling:
Lines 96 to 99 in 6227e6c
const [startObservation, lockProbe] = await Promise.all([ processStartTime(owner.pid), Promise.resolve(probeLock(inspection.lock.path)), ]); - Generic error path in the CLI:
Lines 387 to 403 in 6227e6c
} catch (error) { if (parsed.json) { console.log( JSON.stringify( withClientUpdate( cliError(parsed.command, "command_failed", 3, errorText(error)), advisory.clientUpdate, ), null, 2, ), ); } else { console.error(`codex-unlock: ${errorText(error)}`); } finishAutomaticAdvisory(advisory); return 3;
Acceptance criteria
runCommandwrapsspawn, attacheserrorbefore touching stdio, and resolvesspawn_errorwhen stdio is missing.- Exceptions after the signal boundary are converted into an unlock result that preserves
pidandsignalSent. - Tests cover a synchronous spawn throw and a missing-stdio child (for example by injecting a spawn stub).
Scope and limitations
- Requires descriptor exhaustion or another spawn-level failure (for example a low hard limit or system-wide
ENFILE); there is no remote trigger. Before the signal boundary the failure refuses safely, but with a non-contract exit code and crash output.
Checked
- security: bound external command execution time and output #13 bounded command time/output and covers
ENOENTspawn errors (test/util.test.mjs), notEMFILE/synchronous throws.
Activity
fix: parse Linux lsof file sets and call spawn directly
fix: harden diagnostic command execution
fix: parse Linux lsof file sets and call spawn directly
fix: harden diagnostic command execution (#74)
fix: classify post-signal owner exit and lock reacquisition