From b89fb608b6f4073b096bb41c2b5601160a5a885a Mon Sep 17 00:00:00 2001 From: Fusion Date: Sun, 24 May 2026 16:08:51 +0200 Subject: [PATCH] fix: explicitly close WebSocket with code 1000 when loops end When any of the read/write/heartbeat loops ends, we were cancelling remaining tasks but not explicitly closing the WebSocket. This caused the connection to be dropped with 1006 abnormal closure instead of a clean 1000 close. The frontend then reconnected, creating a loop. --- apps/api/src/api/terminal.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/api/src/api/terminal.py b/apps/api/src/api/terminal.py index c43ed91..99187a1 100644 --- a/apps/api/src/api/terminal.py +++ b/apps/api/src/api/terminal.py @@ -100,6 +100,12 @@ async def terminal_websocket( # Cancel remaining tasks for task in pending: task.cancel() + + # Close WebSocket cleanly to avoid 1006 abnormal closure + try: + await websocket.close(code=1000) + except Exception: + pass except Exception as exc: logger.error("Terminal session error for instance %s: %s", instance_id, str(exc), exc_info=True)