From 10d51b143a2fc5edea3c983e87470a921e572421 Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Mon, 16 Nov 2026 21:42:47 +0800 Subject: [PATCH] fix: stop two windows taking one terminal session from each other --- locales/en.json | 2 +- src/frontend/terminal/Terminal.tsx | 15 +++---- .../terminal/session-taken-over.test.ts | 42 +++++++++++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 tests/frontend/terminal/session-taken-over.test.ts diff --git a/locales/en.json b/locales/en.json index a22a9ec..b8b3ada 100644 --- a/locales/en.json +++ b/locales/en.json @@ -67,7 +67,7 @@ "searchRegex": "Use Regular Expression", "searchResultCount": "{{index}} of {{count}}", "searchWholeWord": "Match Whole Word", - "sessionTakenOver": "Session was opened in another tab. Reconnecting...", + "sessionTakenOver": "This session was opened in another window or tab.", "tmuxAttached": "Attached clients", "tmuxAttachedCount_one": "{{count}} attached", "tmuxAttachedCount_other": "{{count}} attached", diff --git a/src/frontend/terminal/Terminal.tsx b/src/frontend/terminal/Terminal.tsx index f3b5268..13cca61 100644 --- a/src/frontend/terminal/Terminal.tsx +++ b/src/frontend/terminal/Terminal.tsx @@ -2621,23 +2621,20 @@ const TerminalInner = forwardRef( webSocketRef.current.close(); } } else if (msg.type === "sessionTakenOver") { + // Another window has the session now. Taking it straight back + // makes the two windows take it from each other forever. sessionIdRef.current = null; - - if (terminal) { - terminal.clear(); - } + shouldNotReconnectRef.current = true; setIsConnected(false); - setIsConnecting(true); + setIsConnecting(false); + updateConnectionError(t("terminal.sessionTakenOver")); addLog({ type: "warning", stage: "connection", message: t("terminal.sessionTakenOver"), }); - - const cols = terminal?.cols || 80; - const rows = terminal?.rows || 24; - connectToHost(cols, rows); + webSocketRef.current?.close(); } else if (msg.type === "tmux_sessions_available") { setTmuxSessionPicker({ sessions: msg.sessions, diff --git a/tests/frontend/terminal/session-taken-over.test.ts b/tests/frontend/terminal/session-taken-over.test.ts new file mode 100644 index 0000000..0fca44c --- /dev/null +++ b/tests/frontend/terminal/session-taken-over.test.ts @@ -0,0 +1,42 @@ +// @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", +); +const start = source.indexOf('if (msg.type === "sessionTakenOver") {'); +const branch = source.slice( + start, + source.indexOf('} else if (msg.type === "tmux_sessions_available")', start), +); + +describe("a session another window took over", () => { + it("stays stopped instead of taking the session straight back", () => { + const context = { + msg: { type: "sessionTakenOver" }, + t: (key: string) => key, + terminal: { cols: 80, rows: 24, clear: vi.fn() }, + sessionIdRef: { current: "s1" }, + shouldNotReconnectRef: { current: false }, + webSocketRef: { current: { close: vi.fn() } }, + setIsConnected: vi.fn(), + setIsConnecting: vi.fn(), + updateConnectionError: vi.fn(), + addLog: vi.fn(), + connectToHost: vi.fn(), + }; + vm.runInNewContext(`${branch}}`, context); + + expect(context.connectToHost).not.toHaveBeenCalled(); + expect(context.shouldNotReconnectRef.current).toBe(true); + expect(context.sessionIdRef.current).toBeNull(); + expect(context.setIsConnecting).toHaveBeenCalledWith(false); + expect(context.updateConnectionError).toHaveBeenCalledWith( + "terminal.sessionTakenOver", + ); + expect(context.webSocketRef.current.close).toHaveBeenCalledOnce(); + }); +});