From 78c25f30c57d18b954ad8a3bcd0df8faf8abdf28 Mon Sep 17 00:00:00 2001 From: Gaurav Ghosh Date: Wed, 8 Apr 2026 11:51:54 -0700 Subject: [PATCH] fix(core): prevent race condition crash during shell auto-promotion --- packages/core/src/services/shellExecutionService.ts | 5 +++++ packages/core/src/tools/shell.ts | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/core/src/services/shellExecutionService.ts b/packages/core/src/services/shellExecutionService.ts index 95b3f2d17b..ffc714b375 100644 --- a/packages/core/src/services/shellExecutionService.ts +++ b/packages/core/src/services/shellExecutionService.ts @@ -1341,6 +1341,11 @@ export class ShellExecutionService { const activePty = this.activePtys.get(pid); const activeChild = this.activeChildProcesses.get(pid); + // If the process has already exited, there is no need to background it. + if (!activePty && !activeChild) { + return; + } + const resolvedSessionId = sessionId ?? activePty?.sessionId ?? activeChild?.sessionId; const resolvedCommand = diff --git a/packages/core/src/tools/shell.ts b/packages/core/src/tools/shell.ts index 0407cb99bf..053cc4b00d 100644 --- a/packages/core/src/tools/shell.ts +++ b/packages/core/src/tools/shell.ts @@ -515,7 +515,13 @@ export class ShellToolInvocation extends BaseToolInvocation< if (shouldAutoPromote && currentPid) { if (this._autoPromoteTimer) clearTimeout(this._autoPromoteTimer); this._autoPromoteTimer = setTimeout(() => { - ShellExecutionService.background(currentPid!); + const sessionId = + this.context.config?.getSessionId?.() ?? 'default'; + ShellExecutionService.background( + currentPid!, + sessionId, + strippedCommand, + ); }, waitMs); } };