diff --git a/src/main/ssh/ssh-connection.test.ts b/src/main/ssh/ssh-connection.test.ts index f87766951..baae74463 100644 --- a/src/main/ssh/ssh-connection.test.ts +++ b/src/main/ssh/ssh-connection.test.ts @@ -14,6 +14,7 @@ let connectSequence: ('ready' | Error)[] = [] let execBehavior: 'callback' | 'pending' = 'callback' let pendingExecCallback: ((err: Error | undefined, channel: unknown) => void) | null = null let sftpBehavior: 'callback' | 'pending' = 'callback' +let pendingSftpCallback: ((err: Error | undefined, channel: unknown) => void) | null = null type MockSshClient = { setNoDelay: ReturnType @@ -90,13 +91,14 @@ vi.mock('ssh2', () => { pendingExecCallback = cb return } - cb(undefined, {}) + cb(undefined, { close: vi.fn() }) } sftp(cb: (err: Error | undefined, channel: unknown) => void) { if (sftpBehavior === 'pending') { + pendingSftpCallback = cb return } - cb(undefined, {}) + cb(undefined, { end: vi.fn() }) } } return { @@ -188,6 +190,7 @@ describe('SshConnection', () => { execBehavior = 'callback' pendingExecCallback = null sftpBehavior = 'callback' + pendingSftpCallback = null clientInstances = [] spawnSystemSshCommandMock.mockReset() spawnSystemSshCommandMock.mockImplementation(() => createSystemCommandChannel()) @@ -580,10 +583,11 @@ describe('SshConnection', () => { } }) - it('ignores a late exec callback after the channel-open timeout settles', async () => { + it('closes a late exec callback after the channel-open timeout settles', async () => { const conn = new SshConnection(createTarget(), createCallbacks()) await conn.connect() execBehavior = 'pending' + const lateChannel = { close: vi.fn() } vi.useFakeTimers() try { @@ -593,9 +597,10 @@ describe('SshConnection', () => { .catch((error: Error) => error.message) await vi.advanceTimersByTimeAsync(30_000) - pendingExecCallback?.(undefined, {}) + pendingExecCallback?.(undefined, lateChannel) await expect(outcomePromise).resolves.toBe('SSH exec channel timed out') + expect(lateChannel.close).toHaveBeenCalledTimes(1) } finally { vi.useRealTimers() } @@ -622,6 +627,29 @@ describe('SshConnection', () => { } }) + it('ends a late SFTP callback after the channel-open timeout settles', async () => { + const conn = new SshConnection(createTarget(), createCallbacks()) + await conn.connect() + sftpBehavior = 'pending' + const lateSftp = { end: vi.fn() } + + vi.useFakeTimers() + try { + const outcomePromise = conn + .sftp() + .then(() => 'opened') + .catch((error: Error) => error.message) + + await vi.advanceTimersByTimeAsync(30_000) + pendingSftpCallback?.(undefined, lateSftp) + + await expect(outcomePromise).resolves.toBe('SSH SFTP channel timed out') + expect(lateSftp.end).toHaveBeenCalledTimes(1) + } finally { + vi.useRealTimers() + } + }) + it('uses system SSH transport when ProxyUseFdpass is resolved by OpenSSH', async () => { vi.mocked(resolveWithSshG).mockResolvedValueOnce({ hostname: 'example.com', diff --git a/src/main/ssh/ssh-connection.ts b/src/main/ssh/ssh-connection.ts index 8c2138eef..a352ab9db 100644 --- a/src/main/ssh/ssh-connection.ts +++ b/src/main/ssh/ssh-connection.ts @@ -95,8 +95,10 @@ export class SshConnection { throw new Error('Not connected') } const client = this.client - return this.waitForSshCallback('SSH exec channel timed out', (callback) => - client.exec(wrapRemoteCommandForPosixShell(cmd), callback) + return this.waitForSshCallback( + 'SSH exec channel timed out', + (callback) => client.exec(wrapRemoteCommandForPosixShell(cmd), callback), + (channel) => channel.close() ) } @@ -108,14 +110,17 @@ export class SshConnection { throw new Error('Not connected') } const client = this.client - return this.waitForSshCallback('SSH SFTP channel timed out', (callback) => - client.sftp(callback) + return this.waitForSshCallback( + 'SSH SFTP channel timed out', + (callback) => client.sftp(callback), + (sftp) => sftp.end() ) } private waitForSshCallback( timeoutMessage: string, - register: (callback: (error: Error | undefined, value: T) => void) => void + register: (callback: (error: Error | undefined, value: T) => void) => void, + cleanupLateValue?: (value: T) => void ): Promise { return new Promise((resolve, reject) => { let settled = false @@ -125,6 +130,16 @@ export class SshConnection { }, CONNECT_TIMEOUT_MS) const finish = (error: Error | undefined, value?: T): void => { if (settled) { + // Why: ssh2 can invoke the open callback after our timeout has + // rejected. Close that late resource so the remote channel is not + // left open with no owner. + if (!error && value !== undefined) { + try { + cleanupLateValue?.(value) + } catch { + /* best effort */ + } + } return } settled = true