Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions scripts/claude_version_utils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,18 @@ function runClaudeCli(cliPath) {
stdio: 'inherit',
env: process.env
});

// Forward signals to child process so it gets killed when parent is killed
// This prevents orphaned Claude processes when switching between local/remote modes
const forwardSignal = (signal) => {
if (child.pid && !child.killed) {
child.kill(signal);
}
};
process.on('SIGTERM', () => forwardSignal('SIGTERM'));
process.on('SIGINT', () => forwardSignal('SIGINT'));
process.on('SIGHUP', () => forwardSignal('SIGHUP'));

child.on('exit', (code) => {
process.exit(code || 0);
});
Expand Down
22 changes: 22 additions & 0 deletions src/claude/claudeLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,28 @@ export async function claudeLocal(opts: {
env,
});

// Forward signals to child process to prevent orphaned processes
// Note: signal: opts.abort handles programmatic abort (mode switching),
// but direct OS signals (e.g., kill, Ctrl+C) need explicit forwarding
const forwardSignal = (signal: NodeJS.Signals) => {
if (child.pid && !child.killed) {
child.kill(signal);
}
};
const onSigterm = () => forwardSignal('SIGTERM');
const onSigint = () => forwardSignal('SIGINT');
const onSighup = () => forwardSignal('SIGHUP');
process.on('SIGTERM', onSigterm);
process.on('SIGINT', onSigint);
process.on('SIGHUP', onSighup);

// Cleanup signal handlers when child exits to avoid leaks
child.on('exit', () => {
process.off('SIGTERM', onSigterm);
process.off('SIGINT', onSigint);
process.off('SIGHUP', onSighup);
});

// Listen to the custom fd (fd 3) for thinking state tracking
if (child.stdio[3]) {
const rl = createInterface({
Expand Down