diff --git a/packages/ssh/src/tunnel.test.ts b/packages/ssh/src/tunnel.test.ts index 7e7a5a5427..d792c93c83 100644 --- a/packages/ssh/src/tunnel.test.ts +++ b/packages/ssh/src/tunnel.test.ts @@ -14,6 +14,7 @@ import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; import { SshPasswordPrompt } from "./auth.ts"; import { + buildSupervisedSshTunnelArgs, buildRemoteLaunchScript, buildRemotePairingScript, buildRemoteStopScript, @@ -89,6 +90,50 @@ function commandArgs(command: ChildProcess.Command): ReadonlyArray { } describe("ssh tunnel scripts", () => { + it("disables connection multiplexing for the supervised port-forward command", () => { + const target = { + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 2222, + } as const; + + assert.deepEqual( + buildSupervisedSshTunnelArgs({ + target, + batchMode: "no", + localPort: 41_773, + remotePort: 3_773, + hostSpec: "julius@devbox", + }), + [ + "-o", + "BatchMode=no", + "-o", + "ConnectTimeout=10", + "-p", + "2222", + "-o", + "ControlMaster=no", + "-o", + "ControlPath=none", + "-o", + "ControlPersist=no", + "-o", + "ExitOnForwardFailure=yes", + "-o", + "ServerAliveInterval=15", + "-o", + "ServerAliveCountMax=3", + "-n", + "-N", + "-L", + "41773:127.0.0.1:3773", + "julius@devbox", + ], + ); + }); + it("builds the remote t3 runner with npx and npm fallbacks", () => { const script = buildRemoteT3RunnerScript({ nodeEngineRange: TEST_NODE_ENGINE_RANGE }); @@ -382,6 +427,17 @@ describe("ssh tunnel scripts", () => { const first = yield* manager.ensureEnvironment(target); assert.equal(first.httpBaseUrl, "http://127.0.0.1:41773/"); + const firstTunnelArgs = spawnedCommands.find((args) => args.includes("-N")); + assert.isDefined(firstTunnelArgs); + assert.include(firstTunnelArgs, "ControlMaster=no"); + assert.include(firstTunnelArgs, "ControlPath=none"); + assert.include(firstTunnelArgs, "ControlPersist=no"); + for (const args of spawnedCommands.filter((entry) => !entry.includes("-N"))) { + assert.notInclude(args, "ControlMaster=no"); + assert.notInclude(args, "ControlPath=none"); + assert.notInclude(args, "ControlPersist=no"); + } + yield* manager.disconnectEnvironment(target); assert.equal(tunnelKillCount, 1); assert.equal(stopCommandCount, 1); diff --git a/packages/ssh/src/tunnel.ts b/packages/ssh/src/tunnel.ts index 016d5e9a85..c94bfefc88 100644 --- a/packages/ssh/src/tunnel.ts +++ b/packages/ssh/src/tunnel.ts @@ -917,6 +917,38 @@ const reserveLocalTunnelPort = Effect.fn("ssh/tunnel.reserveLocalTunnelPort")(fu return yield* net.reserveLoopbackPort(); }); +export function buildSupervisedSshTunnelArgs(input: { + readonly target: DesktopSshEnvironmentTarget; + readonly batchMode: "yes" | "no"; + readonly localPort: number; + readonly remotePort: number; + readonly hostSpec: string; +}): ReadonlyArray { + return [ + ...baseSshArgs(input.target, { batchMode: input.batchMode }), + // This process owns the tunnel lifecycle. Inherited ControlMaster settings + // can otherwise hand the forward to an existing master and let `ssh -N` + // exit while the manager believes it still supervises the connection. + "-o", + "ControlMaster=no", + "-o", + "ControlPath=none", + "-o", + "ControlPersist=no", + "-o", + "ExitOnForwardFailure=yes", + "-o", + "ServerAliveInterval=15", + "-o", + "ServerAliveCountMax=3", + "-n", + "-N", + "-L", + `${input.localPort}:127.0.0.1:${input.remotePort}`, + input.hostSpec, + ]; +} + const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input: { readonly key: string; readonly resolvedTarget: DesktopSshEnvironmentTarget; @@ -956,22 +988,13 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input: }), ), ); - const args = [ - ...baseSshArgs(input.resolvedTarget, { - batchMode: input.authOptions.batchMode ?? "no", - }), - "-o", - "ExitOnForwardFailure=yes", - "-o", - "ServerAliveInterval=15", - "-o", - "ServerAliveCountMax=3", - "-n", - "-N", - "-L", - `${input.localPort}:127.0.0.1:${input.remotePort}`, + const args = buildSupervisedSshTunnelArgs({ + target: input.resolvedTarget, + batchMode: input.authOptions.batchMode ?? "no", + localPort: input.localPort, + remotePort: input.remotePort, hostSpec, - ]; + }); const sshCommand = yield* resolveSshCommand; const tunnelCommand = [sshCommand, ...args]; const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;