From 827b9ce3d985f0e7e9672c05a8452d4a05d173f0 Mon Sep 17 00:00:00 2001 From: Li-Lee <4946929+Li-Lee@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:50:50 -0500 Subject: [PATCH] fix: back off when a reconnect attempt fails before connecting An abnormal close (1006) only reconnected with backoff when wasConnectedRef was set. connectToHost resets that flag, so when a reconnect attempt itself failed before connecting (the network still down right after the computer wakes), the close took the "first connection failed" branch: it showed the error and set isConnecting to false. The effect that connects a visible tab then called connectToHost again on the next frame, which failed at once again, many times a second until the network came back, each time adding "Connection closed unexpectedly" to the connection log. A failed attempt during a reconnect (reconnectAttempts > 0) now goes on with attemptReconnection's backoff. A tab's first connection that fails stops at the error like a rejected connection or an expired login does, so the effect does not retry it on the next frame; the error screen offers Reconnect. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + src/frontend/terminal/Terminal.tsx | 9 +- .../terminal/abnormal-close-backoff.test.ts | 97 +++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/frontend/terminal/abnormal-close-backoff.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 6822719..53943e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Opening a terminal in a folder always treats the folder name as plain text - Command history is only saved for hosts you can reach, and a failed save no longer shows database errors - Connecting to a shared host you have no login for says so, instead of saying the host is not on the sync server (needs Termix 26.11.2) +- A terminal that loses its connection while the network is down (for example right after the computer wakes) retries with the usual growing wait instead of many times a second, which filled the connection log with "Connection closed unexpectedly". A first connection that fails stops at the error with its Reconnect button ## 1.4.0 diff --git a/src/frontend/terminal/Terminal.tsx b/src/frontend/terminal/Terminal.tsx index f3b5268..89f7e41 100644 --- a/src/frontend/terminal/Terminal.tsx +++ b/src/frontend/terminal/Terminal.tsx @@ -2741,11 +2741,18 @@ const TerminalInner = forwardRef( message: t("terminal.websocketAbnormalClose"), }); - if (wasConnectedRef.current) { + // A failed reconnect attempt never got connected, so + // wasConnectedRef is false for it too. It keeps backing off; + // only a tab's first connection stops at the error. + if (wasConnectedRef.current || reconnectAttempts.current > 0) { attemptReconnection(); } else { updateConnectionError(t("terminal.websocketAbnormalClose")); setIsConnecting(false); + // Otherwise the effect that connects a visible tab tries again + // on the next frame, over and over while the server can't be + // reached. The error screen has a Reconnect button. + shouldNotReconnectRef.current = true; } return; } diff --git a/tests/frontend/terminal/abnormal-close-backoff.test.ts b/tests/frontend/terminal/abnormal-close-backoff.test.ts new file mode 100644 index 0000000..ebe2d23 --- /dev/null +++ b/tests/frontend/terminal/abnormal-close-backoff.test.ts @@ -0,0 +1,97 @@ +// @vitest-environment node +import fs from "node:fs"; +import vm from "node:vm"; +import { describe, expect, it, vi } from "vitest"; + +const source = fs.readFileSync( + new URL("../../../src/frontend/terminal/Terminal.tsx", import.meta.url), + "utf8", +); +// What the socket's close listener does with an abnormal close (1006). +const abnormalClose = source.slice( + source.indexOf(" if (event.code === 1006) {"), + source.indexOf(" if (event.code === 1008) {"), +); +// The effect that connects a visible tab that is neither connected nor connecting. +const effectStart = source.indexOf( + " if (!terminal || !hostConfig || !isVisible) return;", +); +const autoConnect = source.slice( + effectStart, + source.indexOf( + " }, [terminal, hostConfig.id, isVisible, isConnected, isConnecting]);", + effectStart, + ), +); + +function close(state: { wasConnected: boolean; attempts: number }) { + const context = { + event: { code: 1006 }, + console: { warn: vi.fn() }, + t: (key: string) => key, + addLog: vi.fn(), + wasConnectedRef: { current: state.wasConnected }, + reconnectAttempts: { current: state.attempts }, + shouldNotReconnectRef: { current: false }, + attemptReconnection: vi.fn(), + updateConnectionError: vi.fn(), + setIsConnecting: vi.fn(), + }; + vm.runInNewContext(`(() => {${abnormalClose}})()`, context); + return context; +} + +// The auto-connect effect right after that close: not connected, not connecting. +function autoConnectAfter(shouldNotReconnect: boolean) { + const context = { + terminal: { cols: 80, rows: 24 }, + hostConfig: { id: 1 }, + isVisible: true, + isConnected: false, + isConnecting: false, + isReconnectingRef: { current: false }, + reconnectTimeoutRef: { current: null }, + shouldNotReconnectRef: { current: shouldNotReconnect }, + webSocketRef: { current: { readyState: 3 } }, + WebSocket: { OPEN: 1, CONNECTING: 0 }, + setIsConnecting: vi.fn(), + fitTerminal: vi.fn(), + scheduleNotify: vi.fn(), + requestAnimationFrame: (fn: () => void) => fn(), + connectToHost: vi.fn(), + }; + vm.runInNewContext(`(() => {${autoConnect}})()`, context); + return context.connectToHost; +} + +describe("an abnormal close (1006)", () => { + it("reconnects with backoff when the session was up", () => { + const context = close({ wasConnected: true, attempts: 0 }); + expect(context.attemptReconnection).toHaveBeenCalledOnce(); + expect(context.setIsConnecting).not.toHaveBeenCalled(); + }); + + it("keeps backing off when a reconnect attempt fails before connecting", () => { + // E.g. right after the computer wakes, before the network is back. + const context = close({ wasConnected: false, attempts: 2 }); + expect(context.attemptReconnection).toHaveBeenCalledOnce(); + expect(context.updateConnectionError).not.toHaveBeenCalled(); + expect(context.shouldNotReconnectRef.current).toBe(false); + }); + + it("stops at the error when a tab's first connection fails", () => { + const context = close({ wasConnected: false, attempts: 0 }); + expect(context.attemptReconnection).not.toHaveBeenCalled(); + expect(context.updateConnectionError).toHaveBeenCalledWith( + "terminal.websocketAbnormalClose", + ); + expect(context.setIsConnecting).toHaveBeenCalledWith(false); + expect(context.shouldNotReconnectRef.current).toBe(true); + // So the auto-connect effect does not try again on the next frame. + expect(autoConnectAfter(true)).not.toHaveBeenCalled(); + }); + + it("is what keeps the auto-connect effect from retrying at once", () => { + expect(autoConnectAfter(false)).toHaveBeenCalledWith(80, 24); + }); +});