Skip to content

The agent that grows with you

fix(desktop): keep an SSH-torn-down terminal session until its disconnect reaches the tab

Teknium5f792efb3ac2
diff --git a/apps/desktop/electron/terminal-gateway-owner.test.ts b/apps/desktop/electron/terminal-gateway-owner.test.ts
index 21a7db46fb0..cbd98825f05 100644
--- a/apps/desktop/electron/terminal-gateway-owner.test.ts
+++ b/apps/desktop/electron/terminal-gateway-owner.test.ts
@@ -54,7 +54,12 @@ function fakePty() {
return {
listeners,
pty: {
- kill: vi.fn(() => listeners.exit?.({ exitCode: null, signal: '1' })),
+ // A real PTY reports its exit once, however often kill() runs.
+ kill: vi.fn(() => {
+ const exit = listeners.exit
+ listeners.exit = null
+ exit?.({ exitCode: null, signal: '1' })
+ }),
onData: vi.fn(),
onExit: vi.fn((callback: typeof listeners.exit) => {
listeners.exit = callback
@@ -141,6 +146,27 @@ test('an SSH teardown leaves the tab disconnected instead of closing it', async
)
})
+test('an SSH teardown before the tab attaches still delivers disconnected on attach', async () => {
+ const { api, start } = register(null)
+ const nas = await spawnFor(start, { connectionId: 'nas', profile: 'default' })
+ const attach = handles.map.get('hermes:terminal:attach') as (event: unknown, id: string) => boolean
+
+ api.disposeTerminalSessionsForSshScope('conn:nas::default')
+
+ // The shell stops at once; the session lives on until the exit is delivered.
+ assert.equal(nas.fake.pty.kill.mock.calls.length >= 1, true)
+ assert.equal(attach({ sender: nas.sender }, nas.session.id), true)
+
+ const exits = nas.sender.send.mock.calls.filter(([channel]) => String(channel).endsWith(':exit'))
+
+ assert.deepEqual(
+ exits.map(([, payload]) => payload),
+ [{ code: null, signal: 'disconnected' }]
+ )
+ // Delivered once, then the session is gone.
+ assert.equal(attach({ sender: nas.sender }, nas.session.id), false)
+})
+
test('app quit kills shells without telling the tabs they exited', async () => {
const { api, start } = register(null)
const local = await spawnFor(start, { connectionId: 'local' })
diff --git a/apps/desktop/electron/terminal-ipc.ts b/apps/desktop/electron/terminal-ipc.ts
index bcf2478703e..0526711850a 100644
--- a/apps/desktop/electron/terminal-ipc.ts
+++ b/apps/desktop/electron/terminal-ipc.ts
@@ -273,9 +273,22 @@ export function registerTerminalIpc({
// edited gateway reconnects and a removed one says so.
function disposeTerminalSessionsForSshScope(scope: string) {
for (const [id, info] of [...terminalSessions.entries()]) {
- if (info.sshScope === scope) {
- info.outputGate.exit({ code: null, signal: 'disconnected' })
- disposeTerminalSession(id)
+ if (info.sshScope !== scope) {
+ continue
+ }
+
+ info.outputGate.exit({ code: null, signal: 'disconnected' })
+
+ // An attached tab got the exit and the flush already disposed the
+ // session. An unattached one keeps its entry (as a normal exit does) so
+ // the late attach still delivers `disconnected` and arms Enter; only the
+ // shell stops now.
+ if (terminalSessions.has(id)) {
+ try {
+ info.pty.kill()
+ } catch {
+ // Already gone.
+ }
}
}
}