codex-plugin-cc icon indicating copy to clipboard operation
codex-plugin-cc copied to clipboard

fix: make cancel work under Git Bash on a non-English Windows (#423)

Open xoonjaeho opened this issue 4 months ago • 3 comments

Problem

Two defects make /codex:cancel unusable when Claude Code runs from Git Bash on a non-English Windows. The second one is invisible until the first is fixed, which is why they are in one PR.

1. MSYS rewrites taskkill's /PID flag into a path.

runCommand spawns through $SHELL on Windows (process.mjs:12), which is Git Bash in this setup. MSYS then converts the /PID argument before taskkill.exe sees it:

$ node codex-companion.mjs cancel <job-id>
taskkill /PID 5548 /T /F: exit=1: ERROR: Invalid argument/option - 'C:/Program Files/Git/PID'

Every cancel fails. This is the same defect as #411 and #220, and this PR uses #411's approach — MSYS_NO_PATHCONV=1 + MSYS2_ARG_CONV_EXCL=* scoped to the taskkill child only. Scoping matters: setting those on the node process instead breaks the /c/... path translation node needs to resolve its own modules.

2. Dead-pid detection is English-only, so an already-stopped process throws.

Once the flags survive, taskkill runs and reports "no such process" — but looksLikeMissingProcessMessage matches English text only. On a localized Windows nothing matches, so terminateProcessTree falls through to throw new Error(formatCommandFailure(result)) instead of returning delivered: false. This is #423; #424 also addresses it but currently conflicts and carries unrelated changes.

The fix uses the exit code, which is locale-independent.

Verification

Measured on ko-KR Windows 11, Git Bash, node v24.14.0:

case exit message English regex matches
live pid 0 성공: PID 35580인 프로세스가 종료되었습니다 —
dead pid 128 오류: 프로세스 "35580"을(를) 찾을 수 없습니다 false

Before the env change, the same call fails with Invalid argument/option - 'C:/Program Files/Git/PID'. After it, the flags arrive intact.

Test suite on the same host: 92 tests / 83 pass / 9 fail, against a pre-change baseline of 91 / 79 / 12. No new failures, and three previously-failing tests now pass:

  • cancel stops an active background job and marks it cancelled
  • cancel sends turn interrupt to the shared app-server before killing a brokered task
  • session end fully cleans up jobs for the ending session

(The 9 remaining failures are pre-existing on Windows — Unix-socket, temp-dir and real-CLI assertions. CI is ubuntu-only so they do not appear there.)

Both changes were mutation-checked: reverting either production line makes its test fail, and restoring makes it pass.

Tests

  • extended terminateProcessTree uses taskkill on Windows to assert the spawn env (from #411)
  • added terminateProcessTree treats exit code 128 as already stopped on a non-English Windows

Both inject platform, runCommandImpl and killImpl, so they perform no real spawn and run identically on Linux CI.

Relation to existing PRs

This supersedes #411 (defect 1 only) and #424 (defect 2 only, currently conflicting). Credit to #411 for the env approach — I verified it empirically and kept it as-is. Happy to rebase, split, or close this in favour of either if a maintainer prefers.

Fixes #423


Why this matters beyond cancel

terminateProcessTree is not only reached from /codex:cancel. It is also the killer session-lifecycle-hook.mjs:105 passes as killProcess when the SessionEnd hook reaps a broker. On a Windows host running Claude Code from Git Bash, that reap is silently a no-op today for the same MSYS reason — taskkill never receives /PID.

That turns out to matter for the app-server broker leak. broker-lifecycle.mjs spawns brokers detached + unref, the broker has no idle timeout, and the stale-teardown path at broker-lifecycle.mjs:120 calls teardownBrokerSession without killProcess, so a broker that misses the 150 ms endpoint probe is replaced while the old process keeps running and its state files are deleted — invisible to the reaper forever. On this host that produced four live brokers for a single cwd where broker.json named one, and a per-orphan footprint of ~9 processes.

Wiring terminateProcessTree into that teardown is the natural fix for the leak — but on Windows + Git Bash it would still be a no-op without the patch in this PR. So this change is a prerequisite for that fix landing effectively on Windows, not just a cancel bug. Filing the broker fix separately; flagging the dependency here so the two are not evaluated in isolation.

xoonjaeho avatar Aug 01 '26 00:08 xoonjaeho

Pushed a second commit to this branch: d5c0b31, same function, adjacent case.

taskkill /PID <n> /T /F walks the tree, and it exits 255 when it killed the target but could not reap one descendant — "The process with PID (child process of PID ) could not be terminated. Reason: The operation attempted is not supported." A console host or an already-exiting child produces it. terminateProcessTree tolerated only exit 0 and exit 128, so that threw, and the throw propagates out of handleCancel — /codex:cancel fails on Windows even though the process it was asked to kill is dead.

The exit code cannot separate a partial kill from a real failure, and that message is localized too, so keying on it would reintroduce exactly the bug the exit-128 check in this PR exists to avoid. It probes the target with signal 0 instead: gone ⇒ the kill did what the caller asked; still alive ⇒ throws as before. Two tests, each checked by breaking the line it protects.

How it was found is worth a line: tests/runtime.test.mjs:1740 ("cancel sends turn interrupt to the shared app-server before killing a brokered task") had been written off as flaky on Windows. It is not flaky in that direction — it failed 2/2 in a clean environment, and the taskkill 255 above is why. After this commit the dominant failure mode is gone, though the test still fails intermittently under full-suite parallelism from some other cause I have not isolated yet, so I am not claiming it as a full fix for that test.

xoonjaeho avatar Aug 01 '26 08:08 xoonjaeho

The finding on this PR — reporting tree termination after checking only the parent — is real and fixed in bfdf8db.

You are right that the branch conflated two different facts. They are separate now: delivered answers "did the target die", and a new treeConfirmed answers "is everything under it accounted for". Exit 0 confirms both. Exit 128 means there was no process, so no tree is in doubt. Only the branch that tolerates a non-zero taskkill whose root turned out to be gone — exit 255's unreapable descendant, and exit 1's race with an already-exiting root — reports treeConfirmed: false.

Windows offers no way to enumerate what survived once the root is gone, so honest reporting is as far as this can go. To keep that from being a pure relabel, handleCancel now writes it to the job log instead of reporting a clean cancellation — nothing read the return value before, which was the substance of your point.

Also carries a timeout passthrough for runCommand, which had none; the broker work stacked on this branch needs it to bound a probe.

xoonjaeho avatar Aug 01 '26 09:08 xoonjaeho

The finding on bfdf8db — the missing-root branch reporting treeConfirmed: true — is real and fixed in f9d718d.

You were right that the previous round drew the line in the wrong place. taskkill exiting 128 says the pid is not alive at that instant; it says nothing about what the process spawned while it was. /T has nothing left to walk from once the root is gone, so the tree is unaccounted for and the branch now reports treeConfirmed: false.

One thing worth recording, since it also answers the obvious objection that this makes cancellation warn all the time: it does not, and the reason is upstream of terminateProcessTree. resolveCancelableJob (lib/job-control.mjs:281) only ever returns a job whose status is queued or running, and handleCancel writes pid: null after a cancel, so a repeat cancel returns attempted: false and never reaches the warning. Exit 128 on the cancel path therefore means "the state still calls this job running, but its recorded worker pid is dead" — a worker that died mid-turn. That is precisely the case where its codex app-server child (the direct spawn at lib/app-server.mjs:190, taken when there is no broker record) is orphaned and still running. Rare path, and the warning fires only there.

The POSIX side keeps treeConfirmed: true on kill(-pid) ESRCH: an empty process group is genuine evidence about non-detached descendants, which is more than Windows can offer here. A detached grandchild escapes the group either way, so that branch is weaker than it looks — noting it rather than changing it in this PR.

tests/process.test.mjs flips the assertion for the Korean-locale exit-128 case. Suite is unchanged otherwise: same 9 pre-existing failures before and after, none in these paths.

xoonjaeho avatar Aug 02 '26 00:08 xoonjaeho

Same fix as #423, shipped in CBEPX/codex-plugin-cc v1.4.1 (https://github.com/CBEPX/codex-plugin-cc/releases/tag/v1.4.1), used here as the reference; thanks @xoonjaeho. /codex:cancel on Windows now verifies the recorded process by start time and kills the verified tree through the in-box Windows PowerShell 5.1, with no taskkill and no $SHELL involved, so neither Git Bash argument mangling nor non-English console output matters. A worker that vanished first is process-missing; a partially surviving tree gives cancellationPending with survivors.

/plugin marketplace add CBEPX/codex-plugin-cc
/plugin install codex@cbepx

This is a fork release, not an upstream fix, so I'm leaving this issue open for the maintainers here.

CBEPX avatar Sep 29 '26 11:09 CBEPX