Skip to content

codex exec: parent-kill should terminate all spawned child processes (Windows + POSIX) #21994

Done

Description

@nagatani-design

Summary

When codex exec --dangerously-bypass-approvals-and-sandbox <prompt> is killed externally (e.g. via Stop-Process on Windows, kill on POSIX), the child processes it spawned during the run keep running indefinitely and consume CPU. There is no Job Object / process group cleanup hook.

Repro (Windows)

# Terminal A
codex exec --dangerously-bypass-approvals-and-sandbox "$(Get-Content prompt.md)" > out.log

# Terminal B
$pid = (Get-Process codex | Select-Object -First 1).Id
Stop-Process -Id $pid -Force
Start-Sleep -Seconds 5

# Many child processes (often `python -` from heredoc invocations) are still alive
Get-CimInstance Win32_Process -Filter "Name='python.exe'" |
  Where-Object { $_.CommandLine -match 'python\.exe" -$' }
# -> shows orphaned subprocesses, each ~10% CPU

Observed impact (2026-05-10)

  • codex exec parent killed
  • 7 orphaned python - (heredoc) children survived
  • Each consumed ~10% CPU for 43-76 minutes before manual cleanup
  • Total wasted CPU time: 7.5 hours across 7 orphans
  • No way to identify orphans without walking ParentProcessId tree manually

Root cause (hypothesis)

Codex CLI on Windows does not attach spawned subprocesses to a Job Object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE. On POSIX, setpgid() + signaling the process group is not done. Therefore OS does not propagate parent termination to children.

Expected behavior

When the codex exec parent receives SIGTERM/SIGKILL/Stop-Process, all descendants spawned during the run terminate within 30 seconds.

Proposed fix

Windows: wrap subprocess spawning with Win32 Job Object:

// pseudo-code, depending on Codex CLI implementation language
let job = create_job_object();
set_job_limits(job, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE);
let child = spawn_subprocess(...);
assign_process_to_job(child, job);
// when job handle is dropped (parent exit), all assigned processes get terminated

POSIX: use setpgid + signal process group:

let child = Command::new(...)
    .pre_exec(|| { setpgid(0, 0); Ok(()) })
    .spawn();
// on parent shutdown:
killpg(child.pid as i32, SIGTERM);

Workaround (currently in use)

We maintain a 30-line PowerShell helper that walks Win32_Process.ParentProcessId recursively and kills each PID. Acceptable but not ideal:

  • Has to be invoked manually (extra step in monitor's runbook)
  • Can race against new spawns
  • Doesn't help users on POSIX

Severity / priority

Medium-low impact for individual users (manual cleanup possible), but for automated pipelines and CI, orphan accumulation can saturate the host machine. Especially relevant for --dangerously-bypass-approvals-and-sandbox use cases where automated kill is the standard recovery path.

References

Environment

  • OS: Windows 11
  • Codex CLI: 0.128.0
  • Login: ChatGPT

Activity

  1. added
    bug
    CLI
    exec
    windows-os
    on May 10, 2026
  2. removed
    CLI
    on May 10, 2026
  3. igor-markin commented on Sep 4, 2026

    @igor-markin

    Confirmed: this process-leak class is still reproducible on a current macOS arm64 Codex Desktop environment.

    Environment:

    • ChatGPT desktop app 26.901.22334 (build 7746), hosting Codex tasks
    • Codex CLI/runtime 0.153.0
    • macOS 27.0 beta (26A5425a), Apple Silicon arm64

    Observed on 2026-09-04:

    1. A Codex Desktop task ran short Python heredoc diagnostics (/opt/homebrew/bin/python3 -) that communicated over local Unix sockets.
    2. After one diagnostic hung, the task found and sent SIGTERM to one inner codex app-server proxy PID. The kill command returned success, and the relevant shell executions were subsequently recorded as completed/failed; the task later produced a final answer.
    3. Two Python heredoc wrappers nevertheless remained alive as direct children of the shared Codex app-server process.
    4. A fresh check 29 and 25 minutes after their launch still showed each process consuming approximately one full CPU core:
    PID    PPID   %CPU  ELAPSED  COMMAND
    41664  38582  100.0   29:01  Python -
    45820  38582   99.4   25:29  Python -
    

    Additional evidence:

    • sample(1) showed the main threads repeatedly traversing sock_recv / poll and Python exception-creation paths, consistent with a tight retry loop after a broken or lost socket peer.
    • lsof showed stdin backed by an unlinked zsh heredoc temporary file, stdout/stderr pipes, and an open Unix socket.
    • The working directories resolved to the Codex task's project subdirectory. A separately and intentionally launched application process had been stopped before these two PIDs were created, so it was not the source.
    • Combined CPU use was approximately 200%; while the processes remained alive, the Mac became hot and battery power draw was roughly 17–18 W.

    This reproduction differs slightly from the original parent-kill trigger: the shared Codex app-server parent remained alive, while individual shell-tool executions were treated as finished. The failure mode is otherwise the same: descendants escaped command ownership and continued running without being represented as active work.

    Expected behavior:

    • When a shell execution finishes, fails, times out, or its task finalizes, Codex should terminate and reap the entire process group it owns, unless the process was explicitly backgrounded.
    • A successful signal to one matching child PID should not be treated as proof of full cleanup; descendants should be re-enumerated and verified.
    • No residual python - process should continue busy-looping after the command is shown as complete.
  4. etraut-openai commented on Dec 1, 2026

    @etraut-openai
    Contributor

    Thanks for the bug report. This will be addressed in an upcoming release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugexecwindows-os

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions