Skip to content
TypeScript

fix(server): device-host SSH tunnels exit when the server dies (#17448)

Killian Cazin4130507748ac
diff --git a/apps/server/src/device/SshDeviceHost.test.ts b/apps/server/src/device/SshDeviceHost.test.ts
index 3cffc11adb5..57e7226e430 100644
--- a/apps/server/src/device/SshDeviceHost.test.ts
+++ b/apps/server/src/device/SshDeviceHost.test.ts
@@ -21,6 +21,7 @@ it.effect("preserves installed status after probes and cleans failed agent activ
const modes: string[] = [];
const owners: string[] = [];
const tunnelArgs: string[][] = [];
+ const tunnelStdins: unknown[] = [];
const commandArgs: string[][] = [];
let forwards = 0;
let failForward = true;
@@ -28,8 +29,9 @@ it.effect("preserves installed status after probes and cleans failed agent activ
const spawner = ChildProcessSpawner.make((command) =>
Effect.gen(function* () {
if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
- const forwarding = command.args.includes("-N");
+ const forwarding = command.args.includes("-L");
(forwarding ? tunnelArgs : commandArgs).push([...command.args]);
+ if (forwarding) tunnelStdins.push(command.options.stdin);
let output = "";
if (forwarding) {
if (failForward) {
@@ -143,7 +145,15 @@ it.effect("preserves installed status after probes and cleans failed agent activ
expect(index).toBeGreaterThan(0);
expect(args[index - 1]).toBe("-o");
}
+ // The tunnel lives only as long as the server holds the write end of its stdin.
+ expect(args).not.toContain("-N");
+ expect(args.slice(args.indexOf("test.example") + 1)).toEqual([
+ "sh",
+ "-c",
+ "'exec cat >/dev/null'",
+ ]);
}
+ expect(tunnelStdins).toEqual(tunnelArgs.map(() => "pipe"));
expect(commandArgs.length).toBeGreaterThan(0);
for (const args of commandArgs) {
for (const option of multiplexingOff) expect(args).not.toContain(option);
diff --git a/apps/server/src/device/SshDeviceHost.ts b/apps/server/src/device/SshDeviceHost.ts
index 76afd9488df..217cf84392a 100644
--- a/apps/server/src/device/SshDeviceHost.ts
+++ b/apps/server/src/device/SshDeviceHost.ts
@@ -254,15 +254,21 @@ export const make = Effect.fn("SshDeviceHost.make")(function* (
"ServerAliveInterval=10",
"-o",
"ServerAliveCountMax=3",
- "-N",
+ "-T",
"-L",
`127.0.0.1:${hubPort}:127.0.0.1:${remote.hubPort}`,
...(remote.daemonPort === undefined
? []
: ["-L", `127.0.0.1:${daemonPort}:127.0.0.1:${remote.daemonPort}`]),
config.target,
+ // The server holds the only write end of this stdin, so when it exits for any
+ // reason the remote `cat` sees EOF and ssh exits once its forwarded connections
+ // close, instead of being orphaned.
+ "sh",
+ "-c",
+ quoteRemoteArg("exec cat >/dev/null"),
],
- { stdin: "ignore", stdout: "ignore", stderr: "pipe" },
+ { stdin: "pipe", stdout: "ignore", stderr: "pipe" },
),
)
.pipe(