From 639ac329a837295c628a04274607032bccb3690d Mon Sep 17 00:00:00 2001 From: Vantz Stockwell Date: Sat, 14 Mar 2026 00:50:35 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20TDZ=20crash=20on=20SSH=20connect=20failu?= =?UTF-8?q?re=20=E2=80=94=20sessionId=20referenced=20before=20assignment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When SSH timed out, the onClose callback referenced `const sessionId` before connect() resolved, causing a Temporal Dead Zone ReferenceError that killed the process. Changed to `let` with try/catch so connection failures send an error message to the client instead of crashing. Co-Authored-By: Claude Opus 4.6 --- backend/src/terminal/terminal.gateway.ts | 26 +++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/backend/src/terminal/terminal.gateway.ts b/backend/src/terminal/terminal.gateway.ts index 35b7612..8f6d11e 100644 --- a/backend/src/terminal/terminal.gateway.ts +++ b/backend/src/terminal/terminal.gateway.ts @@ -45,16 +45,22 @@ export class TerminalGateway { private async handleMessage(client: any, msg: any) { switch (msg.type) { case 'connect': { - const sessionId = await this.ssh.connect( - msg.hostId, - (data) => this.send(client, { type: 'data', sessionId, data }), - (reason) => this.send(client, { type: 'disconnected', sessionId, reason }), - async (fingerprint, isNew) => { - // Send verification request to client - this.send(client, { type: 'host-key-verify', fingerprint, isNew }); - return true; // auto-accept for now, full flow in Task 12 - }, - ); + let sessionId = ''; + try { + sessionId = await this.ssh.connect( + msg.hostId, + (data) => this.send(client, { type: 'data', sessionId, data }), + (reason) => this.send(client, { type: 'disconnected', sessionId, reason }), + async (fingerprint, isNew) => { + this.send(client, { type: 'host-key-verify', fingerprint, isNew }); + return true; // auto-accept for now, full flow in Task 12 + }, + ); + } catch (err: any) { + this.logger.error(`[WS] SSH connect failed: ${err.message}`); + this.send(client, { type: 'error', message: `Connection failed: ${err.message}` }); + break; + } const sessions = this.clientSessions.get(client) || []; sessions.push(sessionId); this.clientSessions.set(client, sessions);