diff --git a/apps/desktop/src/sshEnvironment.test.ts b/apps/desktop/src/sshEnvironment.test.ts index fe7274b8b9..6fcc0e783e 100644 --- a/apps/desktop/src/sshEnvironment.test.ts +++ b/apps/desktop/src/sshEnvironment.test.ts @@ -5,9 +5,10 @@ import * as Path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; -import { __test, discoverDesktopSshHosts } from "./sshEnvironment.ts"; +import { __test, DesktopSshEnvironmentManager, discoverDesktopSshHosts } from "./sshEnvironment.ts"; const tempDirectories: string[] = []; +type TunnelEntry = Parameters[0]; afterEach(() => { for (const directory of tempDirectories.splice(0)) { @@ -255,10 +256,17 @@ describe("sshEnvironment", () => { ), ), ).toBe(true); + expect(__test.isSshAuthFailure(new Error("Permission denied (publickey)."))).toBe(true); expect(__test.isSshAuthFailure(new Error("Connection timed out"))).toBe(false); expect(__test.isSshAuthFailure(new Error("mkdir: Permission denied"))).toBe(false); }); + it("allows the remote port picker to run without a state file path", () => { + expect(__test.readSshScriptTemplate("remote-pick-port.cjs")).toContain( + 'const filePath = process.argv[2] ?? "";', + ); + }); + it("settles tunnel shutdown if the child exits before the exit listener attaches", async () => { vi.useFakeTimers(); @@ -307,4 +315,60 @@ describe("sshEnvironment", () => { vi.useRealTimers(); } }); + + it("waits for pending tunnel creation before disposing completed tunnels", async () => { + class ControlledChildProcess extends EventEmitter { + exitCode: number | null = null; + signalCode: NodeJS.Signals | null = null; + readonly killSignals: Array = []; + + kill(signal?: NodeJS.Signals | number): boolean { + this.killSignals.push(typeof signal === "string" ? signal : undefined); + this.signalCode = "SIGTERM"; + this.emit("exit", null, this.signalCode); + return true; + } + } + + let resolvePending!: (entry: TunnelEntry) => void; + const child = new ControlledChildProcess(); + const entry: TunnelEntry = { + key: "devbox", + target: { + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 22, + }, + remotePort: 3773, + localPort: 3774, + httpBaseUrl: "http://127.0.0.1:3774/", + wsBaseUrl: "ws://127.0.0.1:3774/", + process: child as never, + }; + const manager = new DesktopSshEnvironmentManager(); + const internals = manager as unknown as { + pendingTunnelEntries: Map>; + tunnels: Map; + }; + const pending = new Promise((resolve) => { + resolvePending = resolve; + }).then((completedEntry) => { + internals.tunnels.set(completedEntry.key, completedEntry); + return completedEntry; + }); + internals.pendingTunnelEntries.set(entry.key, pending); + + const disposePromise = manager.dispose(); + await Promise.resolve(); + + expect(child.killSignals).toEqual([]); + + resolvePending(entry); + await disposePromise; + + expect(child.killSignals).toEqual(["SIGTERM"]); + expect(internals.tunnels.size).toBe(0); + expect(internals.pendingTunnelEntries.size).toBe(0); + }); }); diff --git a/apps/desktop/src/sshEnvironment.ts b/apps/desktop/src/sshEnvironment.ts index 16a75dc8cb..eb57b8b37e 100644 --- a/apps/desktop/src/sshEnvironment.ts +++ b/apps/desktop/src/sshEnvironment.ts @@ -472,7 +472,7 @@ function isSshAuthFailure(error: unknown): boolean { const message = error instanceof Error ? error.message : String(error); const normalized = message.toLowerCase(); return ( - /permission denied \((?:publickey|password|keyboard-interactive|hostbased|gssapi-with-mic)[^)]+\)/u.test( + /permission denied \((?:publickey|password|keyboard-interactive|hostbased|gssapi-with-mic)[^)]*\)/u.test( normalized, ) || /authentication failed/u.test(normalized) || @@ -1019,13 +1019,12 @@ export class DesktopSshEnvironmentManager { .then(() => resolve()) .catch((error: unknown) => reject(error)); }); - this.tunnels.set(key, tunnelEntry); try { await tunnelReady; + this.tunnels.set(key, tunnelEntry); return tunnelEntry; } catch (error) { await stopTunnel(tunnelEntry).catch(() => undefined); - this.deleteTunnelIfCurrent(tunnelEntry); throw error; } }); @@ -1039,6 +1038,8 @@ export class DesktopSshEnvironmentManager { } async dispose(): Promise { + const pending = [...this.pendingTunnelEntries.values()]; + await Promise.allSettled(pending); const entries = [...this.tunnels.values()]; this.tunnels.clear(); this.pendingTunnelEntries.clear(); @@ -1372,6 +1373,7 @@ export const __test = { normalizeKnownHostsHostname, parseKnownHostsHostnames, parseSshResolveOutput, + readSshScriptTemplate, resolveSshConfigIncludePattern, stopTunnel, }; diff --git a/apps/desktop/src/sshScripts/remote-pick-port.cjs b/apps/desktop/src/sshScripts/remote-pick-port.cjs index 8021fedb43..7051beae89 100644 --- a/apps/desktop/src/sshScripts/remote-pick-port.cjs +++ b/apps/desktop/src/sshScripts/remote-pick-port.cjs @@ -1,6 +1,6 @@ const fs = require("node:fs"); const net = require("node:net"); -const filePath = process.argv[2]; +const filePath = process.argv[2] ?? ""; const defaultPort = Number.parseInt(process.argv[3] ?? "", 10); const scanWindow = Number.parseInt(process.argv[4] ?? "", 10); const raw = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8").trim() : "";