From 83646bf423fdf8e2d92178b73ac806180dd3fcc1 Mon Sep 17 00:00:00 2001 From: Saumya Garg Date: Sat, 4 Apr 2026 14:41:02 -0500 Subject: [PATCH] fix(core): prevent PTY resource leak by terminating active executions on process exit --- .../src/services/executionLifecycleService.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packages/core/src/services/executionLifecycleService.ts b/packages/core/src/services/executionLifecycleService.ts index a16717e3d04..97dab7c394c 100644 --- a/packages/core/src/services/executionLifecycleService.ts +++ b/packages/core/src/services/executionLifecycleService.ts @@ -628,4 +628,34 @@ export class ExecutionLifecycleService { execution.writeInput?.(input); } } + + static cleanupAll(): void { + for (const executionId of this.activeExecutions.keys()) { + try { + this.kill(executionId); + } catch { + // Ignore errors during global cleanup + } + } + } } + +// --- Global Cleanup Hooks --- +// Ensures that if the gemini-cli process exits or is killed, +// any spawned subprocesses (like node-pty shells) are terminated. +let hasCleanedUp = false; +const handleProcessExit = () => { + if (hasCleanedUp) return; + hasCleanedUp = true; + ExecutionLifecycleService.cleanupAll(); +}; + +process.on('exit', handleProcessExit); +process.on('SIGINT', () => { + handleProcessExit(); + process.exit(130); +}); +process.on('SIGTERM', () => { + handleProcessExit(); + process.exit(143); +});