From 8730641efddd41665b48c1f4f71b405657426421 Mon Sep 17 00:00:00 2001 From: uroma Date: Mon, 19 Jan 2026 19:16:03 +0000 Subject: [PATCH] debug: add WebSocket stability delay and enhanced checks - Added 100ms delay before sending command to ensure WebSocket stability - Added detailed WebSocket state logging (CONNECTING, OPEN, CLOSING, CLOSED) - Added bufferedAmount check to wait for pending sends - Wrapped ws.send() in try-catch for better error reporting - Split terminal and ws existence checks for clearer debugging --- database.sqlite-shm | Bin 32768 -> 32768 bytes database.sqlite-wal | Bin 683952 -> 733392 bytes public/claude-ide/terminal.js | 45 +++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/database.sqlite-shm b/database.sqlite-shm index e6cdcd3f24d7610d2a395606ff93561d12298ed0..6b32c0eec9df296245c7dbff836f4780e79685cd 100644 GIT binary patch delta 202 zcmZo@U}|V!s+V}A%K!o_K+MR%Ag~EYa{}>1>89I?5l;{9O^w+o-BEI!ks)q_aM#}q zQq=>E0t1k_|B(Pxn2BN8#>V+fn?Eq!v!DFWDUW?Q!)k_g3>!BqGIugf{^M%Gyn<0CkaFC delta 192 zcmZo@U}|V!s+V}A%K!o_K+MR%Ag~Nba{}=fH^z1Te8u*AQ)4ztca$98$@A)xMa8t+ zq^buR1qL8<|04mYFcZV9jg9k}HU}`Tv!9&6Y%uwsQ{Ltv<_S!b|G1g}8K;;g|MGw^ THvjNWVg?G$V%q#AkVg*ywWUFi diff --git a/database.sqlite-wal b/database.sqlite-wal index bc92952eb07f5fbda75fa4d40e31cd5359438574..23c9ec53d600c7cddab8465ffbd5a2c9c64981b9 100644 GIT binary patch delta 327 zcmdmRU-QC4orV_17N!>F7M2#)7Pc1l7LFFqEnFM&r{B25D8TVyVNTGrhqc9%1tv-h z3NtV;Z~`$a5Hka@`(&Gm#*D(-Pbf2O;OF?#qv*rzePG}Agd#=(0nXd99}loEt9Ieq zesB{bzW|3s);fh_stO|8CrkoLbFgl$3JF@{`yRt!CZNF{P=hs6KnA~7{Gs;Fb%E6O z1DilPeT6#hzQteqwEe~*5Le*F)Fks4VN4hXF#-+pgc?*Z1!xeb_fE}u*$-k~z?{^% z#b())q;nD56+VMZO7oa^({NKcs*`|*fPoj(q#4X0C*5w6o_O$SgyQl5VI~0pn3H52 delta 29 kcmcccP-nw^&4w1n7N!>F7M2#)7Pc1l7LFFqEnFM&0kd)n!~g&Q diff --git a/public/claude-ide/terminal.js b/public/claude-ide/terminal.js index 192ac7ac..47404962 100644 --- a/public/claude-ide/terminal.js +++ b/public/claude-ide/terminal.js @@ -699,28 +699,55 @@ class TerminalManager { return; } - this.debugLog('CMD', `Terminal ${terminalId} is ready! Sending command.`); + this.debugLog('CMD', `Terminal ${terminalId} is ready!`); + + // Small delay to ensure WebSocket is stable after switching terminals + this.debugLog('CMD', `Waiting 100ms for WebSocket to stabilize...`); + await new Promise(resolve => setTimeout(resolve, 100)); const terminal = this.terminals.get(terminalId); - if (!terminal || !terminal.ws || terminal.ws.readyState !== WebSocket.OPEN) { - this.debugLog('ERROR', `Cannot send - WebSocket not ready`, { - hasTerminal: !!terminal, - hasWs: !!terminal?.ws, - wsState: terminal?.ws?.readyState, - stateName: terminal?.ws?.readyState === WebSocket.OPEN ? 'OPEN' : 'NOT OPEN' + if (!terminal) { + this.debugLog('ERROR', `Terminal ${terminalId} not found in map`); + return; + } + + if (!terminal.ws) { + this.debugLog('ERROR', `WebSocket not set for terminal ${terminalId}`); + return; + } + + // Check WebSocket state + const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED']; + this.debugLog('CMD', `WebSocket state: ${readyStates[terminal.ws.readyState]} (${terminal.ws.readyState})`); + + if (terminal.ws.readyState !== WebSocket.OPEN) { + this.debugLog('ERROR', `Cannot send - WebSocket not open`, { + wsState: terminal.ws.readyState, + stateName: readyStates[terminal.ws.readyState] }); return; } + // Check if WebSocket has any buffered amount (indicating pending sends) + if (terminal.ws.bufferedAmount > 0) { + this.debugLog('CMD', `WebSocket has ${terminal.ws.bufferedAmount} bytes buffered, waiting...`); + await new Promise(resolve => setTimeout(resolve, 200)); + } + // Send command to terminal const message = JSON.stringify({ type: 'input', data: command }); this.debugLog('CMD', `Sending to WebSocket: ${message}`); - terminal.ws.send(message); - this.debugLog('CMD', `Command sent to terminal ${terminalId}: ${command.trim()}`); + try { + terminal.ws.send(message); + this.debugLog('CMD', `Command sent to terminal ${terminalId}: ${command.trim()}`); + } catch (error) { + this.debugLog('ERROR', `Failed to send command`, { error: error.message, name: error.name }); + showToast(`Failed to send command: ${error.message}`, 'error'); + } } /**