fix(bin): forward signals from pty wrapper to inner CLI#36
Merged
myobie merged 1 commit intoMay 3, 2026
Merged
Conversation
The bin/pty wrapper used spawnSync, which (a) blocks the wrapper's event loop so any signal handlers we'd register would never run, and (b) leaves no path for SIGTERM/SIGINT/etc. to reach the cli.js child. Under systemd with KillMode=process, the wrapper is the unit's MainPID. systemd SIGTERMs the wrapper, the wrapper dies, but the inner cli.js supervisor — which has its own SIGTERM handler that calls Supervisor.stop() and releases supervisor.lock — never sees the signal. It gets reparented and keeps running. Every subsequent unit invocation then exits with 'another supervisor is already running' because the orphan still holds the lock. Reproduced on a NixOS host where the restart counter climbed past 500 with the original supervisor still alive after an hour. The fix switches to spawn() and forwards SIGTERM/SIGINT/SIGHUP/SIGQUIT/ SIGUSR1/SIGUSR2 to the child, then waits for the child's exit event before propagating its status. Normal exit-code passthrough is unchanged. Signal-death is mapped to 128+signum, matching shell convention. Tests in tests/wrapper-signal-forwarding.test.ts spawn the wrapper running 'supervisor start' against an isolated PTY_SESSION_DIR, SIGTERM the wrapper, and assert the inner cli.js dies and supervisor.lock is released. Verified the test fails against the old spawnSync wrapper (inner pid survives, lock leaks).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Independent from #35.
Problem
bin/ptyis a thin Node wrapper that re-execs the real CLI viaspawnSync(node, [cli.js, ...args]).spawnSyncblocks the wrapper'sevent loop, so any signal the wrapper receives is delivered to the
wrapper only — the inner
cli.jschild never sees it.This is invisible under most service managers because the manager
SIGKILLs the whole cgroup. It bites hard under systemd with
KillMode=process:MainPIDand SIGTERMs it on stop.node cli.js supervisor start(which hasits own SIGTERM handler that runs
Supervisor.stop()and releasessupervisor.lock) is reparented to PID 1 and keeps running.inner CLI.
acquireLock(\"supervisor\")correctly sees the orphan isstill alive and exits 1 with
[supervisor] another supervisor is already running.Restart=on-failureloops forever. Reproduced on a NixOShost where the restart counter climbed past 500 with the original
supervisor still alive after an hour.
The supervisor-side stale-lock check (
src/sessions.ts:578-623) isalready correct — it inspects the holder pid via
kill(pid, 0)andreclaims dead locks. The bug is upstream: the holder is genuinely alive
because the wrapper never told it to shut down.
Change
bin/pty:spawninstead ofspawnSyncso the wrapper's event loop stays live.SIGTERM/SIGINT/SIGHUP/SIGQUIT/SIGUSR1/SIGUSR2tothe child.
exitevent so thechild has time to flush before status is propagated.
128 + signum, matching shell convention.node:child_process+node:os.tests/wrapper-signal-forwarding.test.ts:supervisor startagainst an isolatedPTY_SESSION_DIR, waits forsupervisor.pid+supervisor.locktoappear, SIGTERMs the wrapper, and asserts:
cli.jspid (read fromsupervisor.pid) dies — provesthe signal was forwarded.
supervisor.lockis gone — proves the child shut down gracefullyrather than being SIGKILLed.
Verification
npx vitest run tests/wrapper-signal-forwarding.test.ts— passes.spawnSyncwrapper and confirmed thetest fails (orphan inner pid, leaked lock) — i.e. the test catches
the regression it's meant to catch.
npx vitest run(full suite) — 6 pre-existing failures intests/screenshot.test.ts(vim) andtests/shells.test.ts(zsh)caused by missing local binaries, unrelated to this change.
Confirmed the same 6 fail on
origin/mainwithout this patch.npx tsc -p tsconfig.build.json— clean.Posted on behalf of @schickling
agent_nameagent_session_idagent_toolagent_tool_versionagent_runtimeagent_modelworktreemachinetooling_profile