Skip to content

Commit 4130507

Browse files
kcazinclaudejuliusmarminge
authored
fix(server): device-host SSH tunnels exit when the server dies (pingdotgg#17448)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 1e2f88f commit 4130507

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎apps/server/src/device/SshDeviceHost.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ it.effect("preserves installed status after probes and cleans failed agent activ
2121
const modes: string[] = [];
2222
const owners: string[] = [];
2323
const tunnelArgs: string[][] = [];
24+
const tunnelStdins: unknown[] = [];
2425
const commandArgs: string[][] = [];
2526
let forwards = 0;
2627
let failForward = true;
2728
let rejectConfig = true;
2829
const spawner = ChildProcessSpawner.make((command) =>
2930
Effect.gen(function* () {
3031
if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
31-
const forwarding = command.args.includes("-N");
32+
const forwarding = command.args.includes("-L");
3233
(forwarding ? tunnelArgs : commandArgs).push([...command.args]);
34+
if (forwarding) tunnelStdins.push(command.options.stdin);
3335
let output = "";
3436
if (forwarding) {
3537
if (failForward) {
@@ -143,7 +145,15 @@ it.effect("preserves installed status after probes and cleans failed agent activ
143145
expect(index).toBeGreaterThan(0);
144146
expect(args[index - 1]).toBe("-o");
145147
}
148+
// The tunnel lives only as long as the server holds the write end of its stdin.
149+
expect(args).not.toContain("-N");
150+
expect(args.slice(args.indexOf("test.example") + 1)).toEqual([
151+
"sh",
152+
"-c",
153+
"'exec cat >/dev/null'",
154+
]);
146155
}
156+
expect(tunnelStdins).toEqual(tunnelArgs.map(() => "pipe"));
147157
expect(commandArgs.length).toBeGreaterThan(0);
148158
for (const args of commandArgs) {
149159
for (const option of multiplexingOff) expect(args).not.toContain(option);

‎apps/server/src/device/SshDeviceHost.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,21 @@ export const make = Effect.fn("SshDeviceHost.make")(function* (
254254
"ServerAliveInterval=10",
255255
"-o",
256256
"ServerAliveCountMax=3",
257-
"-N",
257+
"-T",
258258
"-L",
259259
`127.0.0.1:${hubPort}:127.0.0.1:${remote.hubPort}`,
260260
...(remote.daemonPort === undefined
261261
? []
262262
: ["-L", `127.0.0.1:${daemonPort}:127.0.0.1:${remote.daemonPort}`]),
263263
config.target,
264+
// The server holds the only write end of this stdin, so when it exits for any
265+
// reason the remote `cat` sees EOF and ssh exits once its forwarded connections
266+
// close, instead of being orphaned.
267+
"sh",
268+
"-c",
269+
quoteRemoteArg("exec cat >/dev/null"),
264270
],
265-
{ stdin: "ignore", stdout: "ignore", stderr: "pipe" },
271+
{ stdin: "pipe", stdout: "ignore", stderr: "pipe" },
266272
),
267273
)
268274
.pipe(

0 commit comments

Comments
 (0)