Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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
- A terminal tab with Auto tmux whose session ended (for example while the computer slept) goes back to the tmux session it was showing, even a renamed one, instead of asking which session to open

## 1.4.0
Expand Down
9 changes: 8 additions & 1 deletion src/frontend/terminal/Terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2764,11 +2764,18 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
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;
}
Expand Down
97 changes: 97 additions & 0 deletions tests/frontend/terminal/abnormal-close-backoff.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});