Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(terminal): clean up disconnected client streams

0xken15b1a14f406b93
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.ts
index 79f9dd201..dee3f2304 100644
--- a/cli/src/backendSocket.spec.ts
+++ b/cli/src/backendSocket.spec.ts
@@ -2,6 +2,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import { readFileSync } from 'fs'
import { fileURLToPath } from 'url'
import { BackendSocket, compactRuntimePickerModels, deviceAgentListItem, grokHistoryPage } from './backendSocket.js'
+import type { TerminalStreamManager } from './lib/terminalStreamManager.js'
const wsMock = vi.hoisted(() => {
const instances: MockWebSocket[] = []
@@ -302,6 +303,37 @@ describe('BackendSocket outbound queue', () => {
await socket.stop()
})
+
+ it('releases E2EE and terminal state for the exact disconnected web connId', async () => {
+ const socket = new BackendSocket('token')
+ const closeConnection = vi.fn(async () => undefined)
+ const stop = vi.fn(async () => undefined)
+ socket.setTerminalStreamManager({
+ closeConnection,
+ stop,
+ } as unknown as TerminalStreamManager)
+ const dropSession = vi.spyOn(socket.e2ee, 'dropSession')
+ socket.connect()
+ const ws = wsMock.instances[0]
+ ws.open()
+
+ ws.message({
+ t: 'down',
+ connId: 'web-terminal-1',
+ frame: { type: '__client_disconnected', payload: {} },
+ })
+
+ await vi.waitFor(() => {
+ expect(dropSession).toHaveBeenCalledWith('web-terminal-1')
+ expect(closeConnection).toHaveBeenCalledWith(
+ 'web-terminal-1',
+ 'client connection closed',
+ false,
+ )
+ })
+ await socket.stop()
+ expect(stop).toHaveBeenCalledOnce()
+ })
})
describe('compact runtime picker catalog', () => {
diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.ts
index 62865a849..290d8ae40 100644
--- a/cli/src/backendSocket.ts
+++ b/cli/src/backendSocket.ts
@@ -677,6 +677,18 @@ export class BackendSocket {
}
return
}
+ if (type === '__client_disconnected') {
+ // The backend is authoritative for the outer connId. Drop both kinds of
+ // connection-scoped state immediately; otherwise a dead Desktop keeps a
+ // terminal controller lease until the 30-second heartbeat timeout.
+ this.e2ee.dropSession(connId)
+ await this.terminalStreams?.closeConnection(
+ connId,
+ 'client connection closed',
+ false,
+ )
+ return
+ }
// Other backend-hub internal control frames (__clients_dirty) — not for us; drop silently.
if (type.startsWith('__')) return