From 62ffa7973c037cc37b42c607fc855098fd6add8f Mon Sep 17 00:00:00 2001 From: Eric Wheeler Date: Mon, 10 Mar 2025 18:49:16 -0700 Subject: [PATCH] fix: prevent spurious onDidEndTerminalShellExecution from breaking terminal output Add explicit checks and error logging to handle problematic event sequence: 0. terminal.running=false 1. terminal.shellIntegration.executeCommand(command) 2. onDidEndTerminalShellExecution // from unexpected 'OSC 633 D' sequence 3. onDidStartTerminalShellExecution 4. stream begins 5. onDidEndTerminalShellExecution The first onDidEndTerminalShellExecution (from unexpected OSC 633 D) is ignored because terminal.running is false, preventing process=undefined from being set prematurely. After the stream begins and sets terminal.running to true, the second onDidEndTerminalShellExecution proceeds normally. Signed-off-by: Eric Wheeler --- src/integrations/terminal/TerminalRegistry.ts | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/integrations/terminal/TerminalRegistry.ts b/src/integrations/terminal/TerminalRegistry.ts index 589c3cc345..13c11cc9e1 100644 --- a/src/integrations/terminal/TerminalRegistry.ts +++ b/src/integrations/terminal/TerminalRegistry.ts @@ -43,11 +43,42 @@ export class TerminalRegistry { async (e: vscode.TerminalShellExecutionEndEvent) => { const terminalInfo = this.getTerminalByVSCETerminal(e.terminal) const process = terminalInfo?.process - const exitDetails = process - ? TerminalProcess.interpretExitCode(e?.exitCode) - : { exitCode: e?.exitCode } + + if (!terminalInfo) { + console.error("[TerminalRegistry] Shell execution ended but terminal not found:", { + exitCode: e?.exitCode, + }) + return + } + + if (!terminalInfo.running) { + console.error( + "[TerminalRegistry] Shell execution end event received, but process is not running for terminal:", + { + terminalId: terminalInfo?.id, + command: process?.command, + exitCode: e?.exitCode, + }, + ) + return + } + + if (!process) { + console.error( + "[TerminalRegistry] Shell execution end event received on running terminal, but process is undefined:", + { + terminalId: terminalInfo.id, + exitCode: e?.exitCode, + }, + ) + return + } + + const exitDetails = TerminalProcess.interpretExitCode(e?.exitCode) console.info("[TerminalRegistry] Shell execution ended:", { ...exitDetails, + terminalId: terminalInfo.id, + command: process?.command ?? "", }) // Signal completion to any waiting processes