Skip to content

fix: runCommand crashes on spawn failures such as EMFILE and loses post-signal evidence #66

Closed

Description

@abruption

What happened

runCommand does not handle every way child_process.spawn can fail:

  • On EMFILE/ENFILE, Node returns a ChildProcess without stdio pipes and emits error asynchronously. child.stdout.on(...) then throws TypeError: Cannot read properties of undefined (reading 'on'), the promise rejects instead of resolving with the documented spawn_error failure, and the error listener is never attached, so the later error event crashes the process as an unhandled event.
  • Other libuv errors (for example EBADF) are thrown synchronously from spawn() inside the promise executor and also bypass spawn_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 SIGTERM produce a termination_failed/verification_failed unlock result that still records pid and signalSent: "SIGTERM", with an exit code from the documented table.

Environment

  • codex-unlock 0.4.0 at 6227e6c
  • macOS 27.0 arm64, Node.js 22.14.0

Source evidence

  • spawn() inside the promise executor without try/catch:

    codex-unlock/src/util.ts

    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 error listener is attached:
    child.stdout.on("data", (chunk: Buffer | string) => {
  • error listener registered afterwards:

    codex-unlock/src/util.ts

    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:
    const [startObservation, lockProbe] = await Promise.all([
    processStartTime(owner.pid),
    Promise.resolve(probeLock(inspection.lock.path)),
    ]);
  • Generic error path in the CLI:

    codex-unlock/src/cli.ts

    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

  • runCommand wraps spawn, attaches error before touching stdio, and resolves spawn_error when stdio is missing.
  • Exceptions after the signal boundary are converted into an unlock result that preserves pid and signalSent.
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions