Skip to content

Detect a kept-alive claude CLI that stops reading stdin (#178) - #212

Merged
currentbits merged 2 commits into
mainfrom
coder/a-kept-alive-claude-cli-that-stops-readi-e95202
Aug 17, 2026
Merged

currentbits merged 2 commits into
mainfrom
coder/a-kept-alive-claude-cli-that-stops-readi-e95202

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Closes #178.

The bug

electron/runner.js keeps an interactive claude child alive across turns and delivers the next turn by writing an NDJSON user line on its stdin instead of spawning. If that child stops reading stdin but never exits, the write still succeeds into the pipe buffer, nothing ever comes back, and the turn sits on "Agent working…" forever. The existing recovery (onExit → spawnForTurn()) only fires when the child actually exits, so this variant had no detection at all.

This was the standing remainder in docs/ISSUES.md — left open because wall-clock watchdogs had already been rejected: a legitimately slow turn's first token took 48s on a 21MB resumed session (issue #17), so no timeout could be short enough to catch a dead CLI and long enough to survive a slow one.

Why a timer is safe here

I probed the real claude CLI 2.1.219 (logs quoted in #178). On a reused kept-alive child, turn two produces:

  • command_lifecycle {"state":"queued"} at +0ms after the stdin write
  • system/init at +31ms (+46ms in a second run)
  • first thinking_tokens only ~800ms later

That is first-line latency, not first-token latency, and it is unrelated to how slow the model is. The old objection does not apply to it.

The change

  • electron/runner.js: on the reuse path only, arm a 60s "no event of any kind" timer (CLAUDE_ACK_MS, overridable via CODER_CLAUDE_ACK_MS). On expiry, if the run is still current and nothing has arrived, call the existing disposeClaudeSession().
  • Killing the wedged child produces the exit that the existing onExit respawn branch already handles, which re-runs the turn with --resume. No new respawn logic was added — the timer's only job is to convert "wedged but alive" into "exited".
  • The timer is disarmed on the first event and on every run-ending path (clearRun, onExit, onError), and is unref()'d so it never holds the process open.

Deliberately not done: no timer on cold spawns (a fresh --resume of a huge session has no proven sub-second ACK); no CLI flag — --replay-user-messages does work but its echo arrives late, after thinking, and would feed our own prompt text back through the <task-notification> scan at runner.js:1642.

Known ceiling, recorded in the code and in docs/ISSUES.md: any line counts as the ACK, so a stray background task-notification from an earlier turn could satisfy it and mask a hang. That fails safe (miss a hang, never kill a healthy turn); the upgrade path is correlating the command_lifecycle command_uuid.

Verification

  • New test respawns when the reused CLI stops reading stdin without exiting, with a fake-CLI scenario deaf-reuse that completes turn one and then stays alive forever without reading stdin.

  • Proven red before green: with the runner.js hunk reverted the new test fails with waitFor timed out (25/26), and passes with it (26/26).

  • Full suite: core 38/38, renderer 716/716, memory 122/122. Electron is 803/804 — the one failure, wireClient × webBridge over a real socket, reproduces identically on a clean tree at main and is unrelated to this change.

  • PR created by the Claude Code agent

Willem and others added 2 commits August 17, 2026 08:37
… Corrupt store load silently discards all user data, with no
A reused interactive claude child that stops reading stdin without ever
exiting hung the turn on "Agent working…" forever: the stdin write lands
in the pipe buffer, nothing comes back, and the respawn recovery only
fires on process exit.

Probing the real CLI 2.1.219 shows a reused warm child acknowledges a
stdin turn in milliseconds — command_lifecycle{queued} at 0ms and
system/init at ~31-46ms after the write, before any model work. That is
first-LINE latency, unrelated to the first-TOKEN latency (48s on a 21MB
resumed session) that made every earlier wall-clock watchdog unsafe.

So: on the reuse path only, a 60s no-event timer disposes the wedged
child. Killing it produces the exit that the existing onExit respawn
already handles, which re-runs the turn with --resume. No new respawn
logic, no CLI flag, and nothing armed on a cold spawn.

Co-Authored-By: Claude Opus 5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@currentbits
currentbits merged commit 83bb490 into main Aug 17, 2026
1 check failed
@currentbits
currentbits deleted the coder/a-kept-alive-claude-cli-that-stops-readi-e95202 branch October 3, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A kept-alive claude CLI that stops reading stdin hangs the turn silently, forever

1 participant