From bc9520f1b131c8c7c97cab93c3d8f8a9e4c15b62 Mon Sep 17 00:00:00 2001 From: Tim Olson Date: Wed, 1 Apr 2026 22:43:02 -0400 Subject: [PATCH] fix spurious WebSocket reconnect killing active sessions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When send() was called while the socket was still CONNECTING (state 0, not yet OPEN), it queued the message and scheduled a reconnect after 1 second. The socket then connected successfully, but 1 second later the scheduled reconnect fired and closed the working session (code 1005), disrupting any in-flight requests (e.g. get_bars). Two fixes: 1. Don't schedule a reconnect when the socket is CONNECTING — it is already in the process of connecting, no reconnect needed. 2. Cancel any pending reconnect timer in onopen — if a timer was already scheduled before the connection succeeded, clear it. Co-Authored-By: Claude Sonnet 4.6 --- web/src/composables/useWebSocket.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/web/src/composables/useWebSocket.ts b/web/src/composables/useWebSocket.ts index c21d9008..77a7df8c 100644 --- a/web/src/composables/useWebSocket.ts +++ b/web/src/composables/useWebSocket.ts @@ -70,6 +70,11 @@ class WebSocketManager { this.ws.onopen = () => { console.log('[WebSocket] Connected successfully') + // Cancel any pending reconnect timer — we're already connected + if (this.reconnectTimeout) { + clearTimeout(this.reconnectTimeout) + this.reconnectTimeout = null + } this.isConnected.value = true this.isAuthenticated.value = false // Wait for 'connected' message from server this.reconnectAttempts = 0 // Reset reconnection counter @@ -149,8 +154,8 @@ class WebSocketManager { } else { console.log('[WebSocket] Queuing message (not connected yet):', message.type, '- Queue size:', this.messageQueue.length + 1) this.messageQueue.push(message) - // Trigger reconnection if not already in progress - if (this.token && !this.reconnectTimeout) { + // Only schedule reconnect if socket is CLOSED/undefined — not when it's still CONNECTING + if (this.token && !this.reconnectTimeout && this.ws?.readyState !== WebSocket.CONNECTING) { this.scheduleReconnect() } }