From bfc8f4da77fe5842694261596dffa0ba62802cc1 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Tue, 24 Feb 2026 22:11:26 -0300 Subject: [PATCH] fix: improve WebSocket reconnection logic and add debugging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add connection timeout (5s) to detect silent failures - Log WebSocket close events with code and reason - Prevent infinite reconnection loops after max attempts - Clear connection timeout when WebSocket opens or closes - Show user-friendly error after max reconnection attempts This helps diagnose why WebSocket connections are failing and prevents the infinite reconnection loop issue. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- ui/suite/chat/chat.html | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ui/suite/chat/chat.html b/ui/suite/chat/chat.html index 0771b6d..0f066f3 100644 --- a/ui/suite/chat/chat.html +++ b/ui/suite/chat/chat.html @@ -871,11 +871,20 @@ currentUserId + "&bot_name=" + currentBotName; - + console.log("Connecting WebSocket to:", url); ws = new WebSocket(url); + // Add connection timeout to detect silent failures + var connectionTimeout = setTimeout(function() { + if (ws.readyState !== WebSocket.OPEN) { + console.error("WebSocket connection timeout"); + ws.close(); + } + }, 5000); + ws.onopen = function () { + clearTimeout(connectionTimeout); console.log("WebSocket connected to:", url); reconnectAttempts = 0; disconnectNotified = false; @@ -923,7 +932,9 @@ } }; - ws.onclose = function () { + ws.onclose = function (event) { + clearTimeout(connectionTimeout); + console.log("WebSocket closed:", event.code, event.reason); updateConnectionStatus("disconnected"); if (!disconnectNotified) { notify("Disconnected from chat server", "error"); @@ -933,6 +944,9 @@ reconnectAttempts++; updateConnectionStatus("connecting"); setTimeout(connectWebSocket, 1000 * reconnectAttempts); + } else { + console.error("Max reconnection attempts reached. Stopping reconnection."); + notify("Could not reconnect to chat server after multiple attempts", "error"); } };