Skip to content

Commit f64c38e

Browse files
committed
fix(terminal): improve stream closure handling and fix TypeScript errors
- Add graceful error handling for terminal.write() when stream is closed - Check terminal element availability before writing to prevent crashes - Export SSHTerminalOutput and SSHTerminalStatus types from TerminalsContext - Fix RemoteTerminal: Remove deprecated fastScrollModifier and onFocus/onBlur - Comment out unavailable ClipboardAddon (package not installed) - Add eslint-disable comment for intentionally unused _getTracker variable These changes ensure the terminal components handle unexpected stream closures without crashing the GUI and fix Tauri integration issues where metadata from the Rust side might be missing or undefined.
1 parent 1b115df commit f64c38e

4 files changed

Lines changed: 28 additions & 9 deletions

File tree

cortex-gui/src/components/TerminalPanel.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,17 @@ export function TerminalPanel() {
17121712
const unsubscribe = subscribeToOutput((output) => {
17131713
if (output.terminal_id === terminalInfo.id) {
17141714
// Write output directly to terminal - no chunking for real-time TUI responsiveness
1715-
terminal.write(output.data);
1715+
// Guard against writing to a disposed terminal to prevent crashes
1716+
try {
1717+
// Check if terminal is still usable before writing
1718+
if (terminal.element && !terminal.element.classList.contains('disposed')) {
1719+
terminal.write(output.data);
1720+
}
1721+
} catch (e) {
1722+
// Terminal may be disposed or in an invalid state - this is expected during cleanup
1723+
console.debug(`[Terminal] Stream write failed for ${terminalInfo.id}, terminal may be closing:`, e);
1724+
return; // Skip further processing for this output
1725+
}
17161726

17171727
// Defer non-critical processing to avoid blocking TUI updates
17181728
// Use requestIdleCallback for lower priority tasks

cortex-gui/src/components/TerminalStickyScroll.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ export function TerminalStickyScroll(props: TerminalStickyScrollProps): JSX.Elem
480480
};
481481

482482
// Expose tracker methods for parent component (used by TerminalPanel)
483+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
483484
const _getTracker = (): CommandTrackerResult => tracker();
484485

485486
return (

cortex-gui/src/components/remote/RemoteTerminal.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import { WebLinksAddon } from "@xterm/addon-web-links";
2828
import { SearchAddon } from "@xterm/addon-search";
2929
import { Unicode11Addon } from "@xterm/addon-unicode11";
3030
import { WebglAddon } from "@xterm/addon-webgl";
31-
import { ClipboardAddon } from "@xterm/addon-clipboard";
31+
// ClipboardAddon disabled - package not installed. Using manual clipboard handling instead.
32+
// import { ClipboardAddon } from "@xterm/addon-clipboard";
3233
import { Icon } from "../ui/Icon";
3334
import {
3435
useTerminals,
@@ -149,8 +150,9 @@ export function RemoteTerminal(props: RemoteTerminalProps) {
149150
convertEol: false,
150151
smoothScrollDuration: 0,
151152
wordSeparator: ts.wordSeparators || " ()[]{}',\"`-''",
152-
fastScrollModifier: "alt",
153-
fastScrollSensitivity: 5,
153+
// fastScrollModifier removed in newer xterm.js versions
154+
// fastScrollModifier: "alt",
155+
// fastScrollSensitivity: 5,
154156
cursorInactiveStyle: "none",
155157
rescaleOverlappingGlyphs: true,
156158
drawBoldTextInBrightColors: false,
@@ -163,13 +165,14 @@ export function RemoteTerminal(props: RemoteTerminalProps) {
163165
const webLinksAddon = new WebLinksAddon((_event, uri) => {
164166
window.open(uri, "_blank");
165167
});
166-
const clipboardAddon = new ClipboardAddon();
168+
// ClipboardAddon disabled - using manual clipboard handling
169+
// const clipboardAddon = new ClipboardAddon();
167170

168171
terminal.loadAddon(fitAddon);
169172
terminal.loadAddon(searchAddon);
170173
terminal.loadAddon(webLinksAddon);
171174
terminal.loadAddon(unicode11Addon);
172-
terminal.loadAddon(clipboardAddon);
175+
// terminal.loadAddon(clipboardAddon);
173176
terminal.unicode.activeVersion = "11";
174177

175178
terminal.open(containerRef);
@@ -226,9 +229,12 @@ export function RemoteTerminal(props: RemoteTerminalProps) {
226229
}
227230
});
228231

229-
// Track focus
230-
terminal.onFocus(() => setIsFocused(true));
231-
terminal.onBlur(() => setIsFocused(false));
232+
// Track focus using textarea element events
233+
const textareaEl = terminal.element?.querySelector('.xterm-helper-textarea');
234+
if (textareaEl) {
235+
textareaEl.addEventListener('focus', () => setIsFocused(true));
236+
textareaEl.addEventListener('blur', () => setIsFocused(false));
237+
}
232238

233239
// Initial fit
234240
requestAnimationFrame(() => {

cortex-gui/src/context/TerminalsContext.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export type {
7575
MoveToGroupOptions,
7676
SSHConfig,
7777
SSHTerminalInfo,
78+
SSHTerminalOutput,
79+
SSHTerminalStatus,
7880
TerminalEnvironment,
7981
TerminalQuickFix,
8082
TerminalQuickFixAction,

0 commit comments

Comments
 (0)