Invalidate stale backing PTYs
This commit is contained in:
@@ -183,6 +183,24 @@ describe('DaemonServer', () => {
|
||||
'Session not found'
|
||||
)
|
||||
})
|
||||
|
||||
it('emits exit when a fire-and-forget write targets a missing session', async () => {
|
||||
await startServer()
|
||||
const c = await connectClient()
|
||||
|
||||
const exitEvent = new Promise<unknown>((resolve) => {
|
||||
c.onEvent((event) => resolve(event))
|
||||
})
|
||||
|
||||
c.notify('write', { sessionId: 'missing-session', data: 'hi' })
|
||||
|
||||
await expect(exitEvent).resolves.toMatchObject({
|
||||
type: 'event',
|
||||
event: 'exit',
|
||||
sessionId: 'missing-session',
|
||||
payload: { code: -1 }
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('authentication', () => {
|
||||
|
||||
@@ -4,7 +4,13 @@ import { writeFileSync, chmodSync, unlinkSync } from 'fs'
|
||||
import { encodeNdjson, createNdjsonParser } from './ndjson'
|
||||
import { TerminalHost } from './terminal-host'
|
||||
import type { SubprocessHandle } from './session'
|
||||
import { PROTOCOL_VERSION, NOTIFY_PREFIX, type HelloMessage, type DaemonRequest } from './types'
|
||||
import {
|
||||
PROTOCOL_VERSION,
|
||||
NOTIFY_PREFIX,
|
||||
SessionNotFoundError,
|
||||
type HelloMessage,
|
||||
type DaemonRequest
|
||||
} from './types'
|
||||
|
||||
export type DaemonServerOptions = {
|
||||
socketPath: string
|
||||
@@ -230,11 +236,25 @@ export class DaemonServer {
|
||||
}
|
||||
|
||||
case 'write':
|
||||
this.host.write(request.payload.sessionId, request.payload.data)
|
||||
try {
|
||||
this.host.write(request.payload.sessionId, request.payload.data)
|
||||
} catch (err) {
|
||||
if (err instanceof SessionNotFoundError) {
|
||||
this.sendExitEvent(client, request.payload.sessionId, -1)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
return {}
|
||||
|
||||
case 'resize':
|
||||
this.host.resize(request.payload.sessionId, request.payload.cols, request.payload.rows)
|
||||
try {
|
||||
this.host.resize(request.payload.sessionId, request.payload.cols, request.payload.rows)
|
||||
} catch (err) {
|
||||
if (err instanceof SessionNotFoundError) {
|
||||
this.sendExitEvent(client, request.payload.sessionId, -1)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
return {}
|
||||
|
||||
case 'kill':
|
||||
@@ -274,4 +294,25 @@ export class DaemonServer {
|
||||
throw new Error(`Unknown request type: ${(request as { type: string }).type}`)
|
||||
}
|
||||
}
|
||||
|
||||
private sendExitEvent(
|
||||
client: ConnectedClient | undefined,
|
||||
sessionId: string,
|
||||
code: number
|
||||
): void {
|
||||
if (!client?.streamSocket) {
|
||||
return
|
||||
}
|
||||
// Why: write/resize are notification-heavy and intentionally do not wait
|
||||
// for replies. If their target session is gone, this synthetic exit is the
|
||||
// only signal the renderer gets to clear stale terminal pane bindings.
|
||||
client.streamSocket.write(
|
||||
encodeNdjson({
|
||||
type: 'event',
|
||||
event: 'exit',
|
||||
sessionId,
|
||||
payload: { code }
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +65,13 @@ vi.mock('../providers/ssh-git-dispatch', () => ({
|
||||
}))
|
||||
|
||||
const { deployAndLaunchRelay } = await import('./ssh-relay-deploy')
|
||||
const { registerSshPtyProvider, unregisterSshPtyProvider, getPtyIdsForConnection } =
|
||||
await import('../ipc/pty')
|
||||
const {
|
||||
registerSshPtyProvider,
|
||||
unregisterSshPtyProvider,
|
||||
getPtyIdsForConnection,
|
||||
clearProviderPtyState,
|
||||
deletePtyOwnership
|
||||
} = await import('../ipc/pty')
|
||||
const { registerSshFilesystemProvider, unregisterSshFilesystemProvider } =
|
||||
await import('../providers/ssh-filesystem-dispatch')
|
||||
const { registerSshGitProvider, unregisterSshGitProvider } =
|
||||
@@ -80,11 +85,12 @@ function createMockDeps() {
|
||||
const mockPortForward = {
|
||||
removeAllForwards: vi.fn()
|
||||
} as unknown as SshPortForwardManager
|
||||
const getMainWindow = vi.fn().mockReturnValue({
|
||||
const mockWindow = {
|
||||
isDestroyed: () => false,
|
||||
webContents: { send: vi.fn() }
|
||||
})
|
||||
return { mockConn, mockStore, mockPortForward, getMainWindow }
|
||||
}
|
||||
const getMainWindow = vi.fn().mockReturnValue(mockWindow)
|
||||
return { mockConn, mockStore, mockPortForward, getMainWindow, mockWindow }
|
||||
}
|
||||
|
||||
function mockDeploySuccess() {
|
||||
@@ -186,6 +192,36 @@ describe('SshRelaySession', () => {
|
||||
expect(mockAttach).toHaveBeenCalledWith('pty-2')
|
||||
})
|
||||
|
||||
it('invalidates and broadcasts remote PTYs that cannot reattach after relay reconnect', async () => {
|
||||
const { mockConn, mockStore, mockPortForward, getMainWindow, mockWindow } = createMockDeps()
|
||||
const session = new SshRelaySession('target-1', getMainWindow, mockStore, mockPortForward)
|
||||
await session.establish(mockConn)
|
||||
vi.clearAllMocks()
|
||||
mockDeploySuccess()
|
||||
|
||||
const { getSshPtyProvider } = await import('../ipc/pty')
|
||||
const mockAttach = vi
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new Error('missing pty'))
|
||||
.mockResolvedValueOnce(undefined)
|
||||
vi.mocked(getSshPtyProvider).mockReturnValue({
|
||||
attach: mockAttach,
|
||||
dispose: vi.fn()
|
||||
} as unknown as ReturnType<typeof getSshPtyProvider>)
|
||||
vi.mocked(getPtyIdsForConnection).mockReturnValue(['pty-stale', 'pty-live'])
|
||||
|
||||
await session.reconnect(mockConn)
|
||||
|
||||
expect(mockAttach).toHaveBeenCalledWith('pty-stale')
|
||||
expect(mockAttach).toHaveBeenCalledWith('pty-live')
|
||||
expect(clearProviderPtyState).toHaveBeenCalledWith('pty-stale')
|
||||
expect(deletePtyOwnership).toHaveBeenCalledWith('pty-stale')
|
||||
expect(mockWindow.webContents.send).toHaveBeenCalledWith('pty:exit', {
|
||||
id: 'pty-stale',
|
||||
code: -1
|
||||
})
|
||||
})
|
||||
|
||||
it('dispose transitions to disposed and unregisters providers', async () => {
|
||||
const { mockConn, mockStore, mockPortForward, getMainWindow } = createMockDeps()
|
||||
const session = new SshRelaySession('target-1', getMainWindow, mockStore, mockPortForward)
|
||||
|
||||
@@ -192,8 +192,21 @@ export class SshRelaySession {
|
||||
}
|
||||
try {
|
||||
await ptyProvider.attach(ptyId)
|
||||
} catch {
|
||||
// PTY may have exited during the disconnect — ignore
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
`[ssh-relay-session] Dropping stale PTY ${ptyId} for ${this.targetId} after relay reattach failed: ${
|
||||
err instanceof Error ? err.message : String(err)
|
||||
}`
|
||||
)
|
||||
clearProviderPtyState(ptyId)
|
||||
deletePtyOwnership(ptyId)
|
||||
// Why: if the new relay cannot reattach this id, the remote
|
||||
// backing process is gone. Tell the renderer so it clears stale
|
||||
// pane bindings instead of keeping a cursor-only terminal.
|
||||
const win = this.getMainWindow()
|
||||
if (win && !win.isDestroyed()) {
|
||||
win.webContents.send('pty:exit', { id: ptyId, code: -1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user