Browse Source

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 <[email protected]>
Eric Wheeler 11 months ago
parent
commit
62ffa7973c
1 changed files with 34 additions and 3 deletions
  1. 34 3
      src/integrations/terminal/TerminalRegistry.ts

+ 34 - 3
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 ?? "<unknown>",
 					})
 
 					// Signal completion to any waiting processes