fix: harden terminal WebSocket edge cases
Deploy Dashboard (Dev) / deploy-dev (push) Successful in 2m44s

- Wrap tryRefreshSession in try-finally to ensure authRecoveryInFlight is always reset
- Fix variable scope issue: use const tabState instead of reassigning d
- Add error handling for process.stdin.write() to catch BrokenPipeError/OSError
- Verify WebSocket instance before closing in ping timeout callback
- Properly await conn.wait_closed() to prevent SSH connection leaks
- Gracefully cancel and wait for read_task with 2s timeout

These fixes prevent race conditions, resource leaks, and improve error recovery.
This commit is contained in:
Gan, Jimmy
2026-03-15 23:18:20 +08:00
parent d19707422d
commit b742ab8fef
2 changed files with 39 additions and 19 deletions
+13
View File
@@ -190,18 +190,31 @@ async def ws_endpoint(websocket: WebSocket, host: str = Query("nas")):
rows = struct.unpack("!H", data[3:5])[0] rows = struct.unpack("!H", data[3:5])[0]
process.channel.change_terminal_size(cols, rows) process.channel.change_terminal_size(cols, rows)
else: else:
try:
process.stdin.write(data) process.stdin.write(data)
except (BrokenPipeError, OSError) as e:
logger.warning("Failed to write to stdin for %s: %s", host, e)
break
elif "text" in msg and msg["text"]: elif "text" in msg and msg["text"]:
if msg["text"] == "__ping__": if msg["text"] == "__ping__":
logger.debug("Received ping from %s, sending pong", host) logger.debug("Received ping from %s, sending pong", host)
await websocket.send_text("__pong__") await websocket.send_text("__pong__")
continue continue
try:
process.stdin.write(msg["text"].encode()) process.stdin.write(msg["text"].encode())
except (BrokenPipeError, OSError) as e:
logger.warning("Failed to write to stdin for %s: %s", host, e)
break
except Exception as e: except Exception as e:
logger.warning("WebSocket receive loop ended for %s: %s", host, e) logger.warning("WebSocket receive loop ended for %s: %s", host, e)
finally: finally:
read_task.cancel() read_task.cancel()
try:
await asyncio.wait_for(read_task, timeout=2.0)
except (asyncio.TimeoutError, asyncio.CancelledError):
pass
process.close() process.close()
finally: finally:
conn.close() conn.close()
await conn.wait_closed()
await _release_session(session_id) await _release_session(session_id)
+13 -6
View File
@@ -300,7 +300,9 @@
ws.send("__ping__"); ws.send("__ping__");
// Set timeout to detect missing pong // Set timeout to detect missing pong
pongTimeout = setTimeout(() => { pongTimeout = setTimeout(() => {
if (ws.readyState === 1) { // Check if still the same WebSocket instance and still open
const currentTab = tabData.get(tabId);
if (currentTab?.ws === ws && ws.readyState === 1) {
ws.close(1000, "keepalive ping timeout"); ws.close(1000, "keepalive ping timeout");
} }
}, PONG_TIMEOUT); }, PONG_TIMEOUT);
@@ -343,8 +345,8 @@
}; };
ws.onclose = async (e) => { ws.onclose = async (e) => {
const reason = e.reason || `Connection closed (code ${e.code})`; const reason = e.reason || `Connection closed (code ${e.code})`;
d = tabData.get(tabId); const tabState = tabData.get(tabId);
if (d) d.ws = null; if (tabState) tabState.ws = null;
// Handle auth failures (code 1008) // Handle auth failures (code 1008)
if (e.code === 1008) { if (e.code === 1008) {
@@ -359,8 +361,8 @@
// Try to refresh session // Try to refresh session
if (!closedManually && !authRecoveryInFlight) { if (!closedManually && !authRecoveryInFlight) {
authRecoveryInFlight = true; authRecoveryInFlight = true;
try {
const refreshed = await tryRefreshSession(); const refreshed = await tryRefreshSession();
authRecoveryInFlight = false;
if (refreshed && !closedManually) { if (refreshed && !closedManually) {
reconnecting = true; reconnecting = true;
updateTab(tabId, { updateTab(tabId, {
@@ -372,6 +374,11 @@
void connect(false); void connect(false);
return; return;
} }
} catch (err) {
console.error("Session refresh failed:", err);
} finally {
authRecoveryInFlight = false;
}
} }
handleAuthExpired("Session expired — please log in again"); handleAuthExpired("Session expired — please log in again");
return; return;
@@ -386,8 +393,8 @@
} }
}; };
ws.onerror = () => { ws.onerror = () => {
d = tabData.get(tabId); const tabState = tabData.get(tabId);
if (d) d.ws = null; if (tabState) tabState.ws = null;
if (!closedManually) scheduleReconnect("Connection failed"); if (!closedManually) scheduleReconnect("Connection failed");
else updateTab(tabId, { connected: false, error: "Connection failed" }); else updateTab(tabId, { connected: false, error: "Connection failed" });
}; };