Test suite leaks broker processes — 158 orphans found
Problem
Running the codex plugin test suite spawns app-server-broker.mjs processes for each test workspace (/tmp/codex-plugin-test-*), but these processes are never cleaned up after tests complete.
Found 158 stale broker processes on a development machine, each holding a /tmp/cxc-* socket directory. All were from old test runs (dated Apr 3–6), pointing at temp dirs that may or may not still exist.
Evidence
$ ps aux | grep 'app-server-broker.mjs' | grep -v grep | wc -l
167
$ ps aux | grep 'app-server-broker.mjs' | grep -v grep | grep -oP '(?<=--cwd )\S+' | sort | uniq -c | sort -rn | head -5
1 /tmp/codex-plugin-test-zxrQqX
1 /tmp/codex-plugin-test-znwec8
... (158 test dirs total)
$ ls -d /tmp/cxc-* | wc -l
168
Expected behavior
Test teardown should kill broker processes it spawned (or the broker should self-terminate when its cwd disappears / after idle timeout).
Impact
- Resource leak: 167 node processes × ~35MB RSS = ~5.8GB wasted
- Socket dir accumulation in /tmp
- May contribute to Codex review failures when the system is under resource pressure
also did some spring cleaning on my remote box and hit this. ~150 codex app-server processes, never cleaned up.
claude's opinion of the issue (yet to verify manually):
The leak is in SpawnedCodexAppServerClient spawning codex app-server processes that never get cleaned up.
The flow:
- Each Claude session with the Codex plugin calls CodexAppServerClient.connect() (line 332 of app-server.mjs)
- This calls ensureBrokerSession() which spawns a detached, unref'd broker process (broker-lifecycle.mjs:61-68) — detached: true + child.unref() means Node won't wait for it to exit
- The broker in turn spawns codex app-server as a child (app-server.mjs:189)
- Cleanup depends on SessionEnd hook firing (session-lifecycle-hook.mjs:81), which calls sendBrokerShutdown() then teardownBrokerSession()
The problem: When a Claude session dies uncleanly (killed, crashes, terminal closed, OOM'd), the SessionEnd hook never fires. The broker and its codex app-server child are detached and orphaned — nothing reaps them. The teardownBrokerSession function (broker-lifecycle.mjs:173) only kills the process if killProcess is provided AND pid is finite, but the orphaned processes have no parent watching them.
There's also a secondary issue: ensureBrokerSession (broker-lifecycle.mjs:113) checks if an existing broker is still responsive, and if not, tries to tear it down and spawn a new one. But it only knows about the last broker for that cwd (via broker.json). If a session crashed before updating that state file, the old process is just lost — invisible to future sessions, which happily spawn new ones.
TL;DR: 153 orphaned codex app-server processes because cleanup only runs on graceful session exit, and there's no heartbeat, watchdog, or process group management to catch ungraceful exits. Each crashed/killed Claude session leaks one.
extra data point fwiw:
i'm only seeing the issue on my remote box (ubuntu), not on any of my macs
Still reproduces on v1.0.4 (Linux/WSL2, Ubuntu).
After three sequential npm test runs against main plus a locally-rebased branch, leftover state was:
- 178
/tmp/cxc-*/socket directories - 144
<plugin-data>/state/codex-plugin-test-*/workspace state directories - 5 still-running
app-server-broker.mjsprocesses — all with--cwd /tmp/codex-plugin-test-*
So roughly 50–60 socket dirs and 45–50 state dirs leaked per test cycle on this environment.
The broker-process count dropped noticeably once I had #243 cherry-picked locally (the appClient.exitPromise watchdog tears the broker down when fake-codex disconnects), but the filesystem leak — /tmp/cxc-*, state dirs, and the workspace temp dirs from makeTempDir() — is independent of #243 and persists.
The leak is multi-layered. Each broker-spawning test creates:
- a workspace temp dir via
makeTempDir()intests/helpers.mjs - a detached
app-server-broker.mjsfor that cwd viaensureBrokerSession() - a
/tmp/cxc-*/socket dir viacreateBrokerEndpoint() - a state dir under
<plugin-data>/state/<workspace-key>/
None of these register for cleanup at end-of-test or process exit. A central registry in tests/helpers.mjs (track temp dirs + read broker PIDs from state/<key>/broker.json at creation, register a process.on("exit", ...) SIGTERM-and-rm handler) plus per-test t.after(...) for the graceful broker-shutdown RPC would cover all four. Happy to send a PR if it'd help.
Confirming on macOS in v1.0.6 (upstream main, commit db52e28; codex-cli 0.144.4, Node 25.9.0). Running the plugin's npm test from a Claude Code session left behind:
- ~240
app-server-broker.mjsprocesses + ~580 childcodex app-serverprocesses, allPPID=1, ~2.6 GB RSS combined. Runningnpm testfrom a Claude Code session makes this notably worse:tests/fake-codex-fixture.mjs:658(buildEnv) spreads...process.env, so test brokers inherit my realCLAUDE_PLUGIN_DATAand persist state into the live plugin data dir instead of an ephemeral one — the same env-inheritance leak described in #487, part 1. - 391
codex-plugin-test-*directories (~9 MB) accumulated in~/.claude/plugins/data/codex-openai-codex/state/, all from repeated test runs. Manual cleanup (SIGTERM to processes older than 60 min) freed ~2.6 GB; PhysMem unused recovered 1.4 GB → 4.0 GB.
And the compounded effect worth flagging: because these brokers outlive the test and reparent to init, they're never cleaned until reboot — and on a 24 GB machine, several days of such runs fill swap to the brim (I had ~9.5 GB swap used despite a fresh boot), pushing parallel workers into swap. That makes this leak a real developer-machine perf issue, not just cosmetic.
PR #490 (broker idle-timeout) addresses exactly the process half — a broker with no clients shuts itself down, so it at least stops accumulating forever. But the directory half (the test fixture's env-inheritance) needs a separate fix: the fixture should use an ephemeral CLAUDE_PLUGIN_DATA per run rather than spreading the host env. Linking #487 since the two fixes are complementary.
Following up on my earlier leak-breakdown comment, here's how the separate orphan-broker issues I've seen tie together. It's one root with three distinct scenarios, and PR #490 covers them to varying degrees:
| Scenario | What happens | #490 covers? | Issues |
|---|---|---|---|
| 1 — CC crash / closed terminal / unclean exit | Detached broker is reparented to init (PPID=1), connection drops, SessionEnd never fires |
✅ Yes | #450, #416 (orphan part), #193 |
2 — sendBrokerShutdown hangs on /quit |
SessionEnd hook blocks on a hung RPC, so you force-kill CC; broker abandoned |
⚠️ Partial — reaps the abandoned live-idle broker in ~15 min, but does NOT fix the /quit UX hang (client-side timeout, #288/#245) and does not arm for a broker wedged with an active socket |
#288, #245 |
| 3 — broker spawned but endpoint never came up | Never reaches net.createServer / idle logic; stillborn |
⚠️ Not covered — and it DOES accumulate (see #487) | #487 |
#490 (15-min idle-timeout, armed only when sockets.size === 0 && !activeRequestSocket && !activeStreamSocket — app-server-broker.mjs:152 on the PR branch) fixes exactly scenario 1: connection drops → no clients → broker self-exits. That's the valuable win — precisely the class responsible for the 338 orphaned processes I cleaned up here.
Scenario 2 needs a bounded client-side timeout on sendBrokerShutdown (mine is 2s on the fork) for the UX hang itself, though #490 partially mitigates it by reaping the abandoned broker.
Scenario 3 first looked "self-cleaning on read" via the liveness check in ensureBrokerSession (broker-lifecycle.mjs:107 → teardown + clear), but #487 shows the opposite on Windows: the setup-fixture trusts broker.json without a liveness check (stale pid → ~6 spurious failures until manually deleted), and ~200 orphaned brokers accumulate over a day (because taskkill /T /F breaks under Git Bash and shutdown() → appClient.close() doesn't guarantee reaping children). The liveness self-heal exists but is path-specific — not a substitute for real teardown. This is spawn-path robustness, orthogonal to the idle-timer.
Cross-linking the relevant issues above and #487, since the env-inheritance / directory accumulation there is a fourth, orthogonal axis (not a process leak).
Still reproduces on current main (db52e28), four months on. plugins/codex/scripts/lib/broker-lifecycle.mjs and plugins/codex/scripts/app-server-broker.mjs have one commit each in their history — the initial commit — so nothing has changed here since this was filed.
Fresh numbers from today, macOS 26.6 arm64, Node 24. Starting from zero broker processes on the machine, one run of npm test on pristine main leaves:
- 29
app-server-broker.mjsprocesses, 29codex app-serverchildren, 2,845 MB RSS.
Repeated runs accumulate linearly and nothing ever reclaims them. Over a working session of roughly ten suite runs I measured 291 brokers and 28.6 GB resident. Every one had its --cwd under a codex-plugin-test-* temp directory, so all of them came from the suite and none from real sessions. The temp workspaces they were started in are long deleted; the processes are not.
The suite spawns real brokers through CodexAppServerClient.connect() and nothing tears them down: teardownBrokerSession only kills a pid when the caller passes a killProcess callback, and the production call site (plugins/codex/scripts/lib/app-server.mjs:344) passes only { env }. So teardown unlinks the socket, pid file and log file and leaves the process running. The broker has no idle timeout, so it never exits on its own.
One practical note for anyone cleaning up after a run: kill <pid> on a broker does not reliably stop it. Its SIGTERM handler awaits appClient.close(), which waits on the codex app-server child that the signal never reached, so it hangs instead of exiting. Because brokers are spawned detached: true, each leads its own process group, and signalling the group works:
ps -axo pid=,command= | grep '[a]pp-server-broker.mjs serve' | awk '{print $1}' \
| while read -r pid; do kill -TERM -- "-$pid"; done
That reclaimed all 28.6 GB here.
Two things would each independently stop the suite from leaking: an idle timeout on the broker (in flight as #580), or having teardown terminate the process it is tearing down (#566). A CI job that asserts no app-server-broker.mjs survives the suite would keep it from coming back — happy to contribute that if it's wanted.