test: reap the brokers and temp dirs a suite run leaves behind
makeTempDir gives every test a fresh cwd, and ensureBrokerSession keys the app-server broker by cwd and spawns it detached + unref. So every test that reaches the app server spawns a brand-new broker, the reuse branch can never hit, and nothing shuts any of them down: a broker exits only on a broker/shutdown RPC or a signal, and the SessionEnd hook that would normally reap it never fires under npm test.
Measured on one Windows host after repeated suite runs:
| processes | 2257 against a ~330 baseline |
of which node.exe with the test temp-dir prefix |
772 |
orphaned codex-plugin-test-* temp dirs |
1443 (14 MB) |
stale cxc-* broker session dirs |
388, against 389 live brokers |
Each leaked test broker holds a 4-process unit (broker → bash -c "codex app-server" → env node <fixture> → conhost).
The change: makeTempDir records the dirs it creates, and a process.on("exit") handler shuts down any broker registered against them and removes the dir.
Verification — same file, same suite, on Windows:
leaked node.exe |
temp dirs | cxc-* dirs |
|
|---|---|---|---|
| before | 58 | +104 | 30 |
| after | 0 | 0 | 0 |
Zero growth held over three consecutive full-suite runs.
Known limits, both deliberate:
exitonly, so an interrupted (Ctrl-C) run still leaks. A signal handler here would also have to re-raise, and every normal run goes throughexit.- On Windows,
terminateProcessTreereachestaskkillthrough$SHELL; under Git Bash the/PIDflag is rewritten into a path and the kill silently fails. #577 fixes that. This PR is still correct without it — the temp dirs go either way, and the kill works on Linux/macOS and from cmd — but on a Git Bash host it needs #577 to actually reap.