Skip to content

Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.

TypeScript claude-code-mod

Watcher: no kill of a child that ended

razorback16bc89efe2d6f3
diff --git a/hooks/lib/frame.ts b/hooks/lib/frame.ts
index d28c843..4d25322 100644
--- a/hooks/lib/frame.ts
+++ b/hooks/lib/frame.ts
@@ -237,12 +237,13 @@ export function watchFrame($: EngineInterface, clip?: ((count: number | null, fi
}
if (!watcher && clip !== undefined && !isClip) return false
watcher?.stop()
- // The stop: a kill once its PID is known (its first line).
+ // The stop: a kill once its PID is known (its first line), and not after
+ // the child ended (its PID can be another process's by then).
let pid = ''
let stopped = false
let counted = false
const kill = () => {
- if (stopped && pid) void Promise.resolve(processRun($, ['kill', pid], { timeoutMs: 5000 })).catch(() => undefined)
+ if (stopped && pid && !me.ended) void Promise.resolve(processRun($, ['kill', pid], { timeoutMs: 5000 })).catch(() => undefined)
}
const stop = () => {
if (stopped) return
diff --git a/tests/frame.test.ts b/tests/frame.test.ts
index 8732cf6..2c152a3 100644
--- a/tests/frame.test.ts
+++ b/tests/frame.test.ts
@@ -519,6 +519,29 @@ test('a clip child that ends on its own starts again after WATCH_RESTART_MS, WAT
expect(watchState()).toEqual({ clip: true, ended: true })
})
+test('a child that ended is not killed at its stop: its PID can be another process now', async () => {
+ // The clipboard part off: a child with no argument.
+ watchFrame($, null)
+ const child = children.at(-1)!
+ expect(child.argv).toEqual(['perl', '-e', WATCH_PL])
+ child.say('pid 201')
+ await flush()
+ child.end(0)
+ await flush()
+ expect(watchState()).toEqual({ clip: false, ended: true })
+ // The clipboard part on: the ended child is stopped, with no kill.
+ watchFrame($, () => {})
+ await flush()
+ expect(kills).not.toContain('201')
+ // A child that runs is killed as before.
+ const next = children.at(-1)!
+ next.say('pid 202')
+ await flush()
+ watchFrame($, null)
+ await flush()
+ expect(kills).toContain('202')
+})
+
test('Ghostty is told by TERM_PROGRAM or TERM, and cmux (its engine) counts too', () => {
const was = { program: S.termProgram, name: S.termName, cmux: S.isCmux }
try {