From 1e71e43bd75cc80fb0d620110544266d109a841d Mon Sep 17 00:00:00 2001 From: Moepchi Date: Wed, 7 Oct 2026 17:04:06 +0000 Subject: [PATCH] fix(web): only reset the reconnect backoff after a stable connection On a flaky network every successful reconnect reset the backoff to 1 s, so a connection that kept dropping after a few seconds made each tab reconnect every few seconds. The backoff now starts over only once a connection held for 30 s. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- web/src/App.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/web/src/App.tsx b/web/src/App.tsx index 0adffdd..0257eef 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -7530,7 +7530,7 @@ function AppInner() { connecting: rec.parked.connecting, }); if (data.type === "connected") { - reconnectAttemptsRef.current.delete(sessionId); + connectedAtRef.current.set(sessionId, Date.now()); void playSound("connect"); } else if (data.type === "poke") void playSound("poke"); @@ -7540,7 +7540,7 @@ function AppInner() { switch (data.type) { case "connected": logClient("info", "Connection", `Connected to ${data.serverName}`); - reconnectAttemptsRef.current.delete(sessionId); + connectedAtRef.current.set(sessionId, Date.now()); autoConnectPendingRef.current = false; hasConnectedRef.current = true; everConnectedRef.current.add(sessionId); @@ -8116,6 +8116,11 @@ function AppInner() { // instantly, which without a delay would tight-loop). Reset to 0 once a // session actually reaches "connected" again (see the two spots below). const reconnectAttemptsRef = useRef>(new Map()); + // The backoff only starts over once a connection held for a while: on a + // flaky network a reconnect that drops again after a few seconds used to + // reset it to 1 s every time, so each tab reconnected every few seconds. + const connectedAtRef = useRef>(new Map()); + const STABLE_CONNECTION_MS = 30_000; // The gateway sends this every 20 s (heartbeat in gateway/src/index.ts). A // network that dies silently never closes the socket on this side, so a // socket that stays quiet this long counts as dropped. @@ -8124,6 +8129,11 @@ function AppInner() { const reconnectTimersRef = useRef>>(new Map()); const scheduleReconnect = (sessionId: string) => { if (reconnectTimersRef.current.has(sessionId)) return; // already scheduled + const connectedAt = connectedAtRef.current.get(sessionId); + connectedAtRef.current.delete(sessionId); + if (connectedAt !== undefined && Date.now() - connectedAt >= STABLE_CONNECTION_MS) { + reconnectAttemptsRef.current.delete(sessionId); + } const attempt = reconnectAttemptsRef.current.get(sessionId) ?? 0; const delay = Math.min(1000 * 2 ** attempt, 30_000); reconnectAttemptsRef.current.set(sessionId, attempt + 1);