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
This commit is contained in:
uroma
2026-01-19 19:16:03 +00:00
Unverified
parent a7d2f37219
commit 8730641efd
3 changed files with 36 additions and 9 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -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');
}
}
/**