Skip to content

Commit 09995fa

Browse files
ozgesolidkeyclaude
andcommitted
Fix unhandled SSH error crash during connection phase
SshHandler.emit('error') was called before wireConnectionEvents() had a chance to attach the listener, crashing the process with ERR_UNHANDLED_ERROR on any connection failure (EHOSTUNREACH, etc.). Track a settled flag; during the connection phase surface errors via reject() only. After resolve() fires the listener is wired, so post- connection errors can emit normally. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 99bbb39 commit 09995fa

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/main/sshHandler.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ export class SshHandler extends EventEmitter {
162162

163163
return new Promise((resolve, reject) => {
164164
const client = this.client!;
165+
// Track whether the promise has settled so we know whether to reject
166+
// (connection phase) or emit (post-connection phase) on errors.
167+
let settled = false;
165168

166169
client.on('ready', () => {
167170
if (config.remotePath) {
@@ -196,6 +199,7 @@ export class SshHandler extends EventEmitter {
196199
}
197200
});
198201

202+
settled = true;
199203
resolve(this.tempFilePath!);
200204
});
201205
} else {
@@ -207,6 +211,7 @@ export class SshHandler extends EventEmitter {
207211
return;
208212
}
209213
this.sftp = sftp;
214+
settled = true;
210215
resolve(this.tempFilePath!);
211216
});
212217
}
@@ -220,9 +225,15 @@ export class SshHandler extends EventEmitter {
220225
reject(new Error('PASSPHRASE_NEEDED'));
221226
return;
222227
}
223-
this.emit('error', msg);
228+
if (settled) {
229+
// Post-connection error — the listener from wireConnectionEvents is in place
230+
this.emit('error', msg);
231+
}
224232
this.cleanup();
225-
reject(err);
233+
if (!settled) {
234+
// Connection phase — surface via rejection, not event (no listener yet)
235+
reject(err);
236+
}
226237
});
227238

228239
client.on('close', () => {

0 commit comments

Comments
 (0)