Skip to content

Commit 66b12ec

Browse files
kcazinclaude
andcommitted
fix(server): device-host SSH tunnels exit when the server dies
The device-host tunnel ran `ssh -N` with stdin ignored in its own session, so only the host's stop finalizer could end it. When the server died before that finalizer ran (the service launcher SIGKILLs it 5 s into a self-update; also OOM or a crash), the tunnel was orphaned with its forwards and SSH session. Give the tunnel a stdin pipe and a remote command that exits on EOF: the server holds the only write end, so once it is gone ssh exits as soon as its forwarded connections close. Fixes #17438 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 43f8a8d commit 66b12ec

2 files changed

Lines changed: 77 additions & 3 deletions

File tree

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import * as Net from "@t3tools/shared/Net";
4+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Layer from "effect/Layer";
@@ -9,6 +10,7 @@ import * as Sink from "effect/Sink";
910
import * as Stream from "effect/Stream";
1011
import * as HttpClient from "effect/http/HttpClient";
1112
import * as HttpClientResponse from "effect/http/HttpClientResponse";
13+
import * as ChildProcess from "effect/process/ChildProcess";
1214
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1315
import * as ServerConfig from "../config.ts";
1416
import * as DeviceHost from "./DeviceHost.ts";
@@ -26,7 +28,7 @@ it.effect("preserves installed status after probes and cleans failed agent activ
2628
const spawner = ChildProcessSpawner.make((command) =>
2729
Effect.gen(function* () {
2830
if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
29-
const forwarding = command.args.includes("-N");
31+
const forwarding = command.args.includes("-L");
3032
let output = "";
3133
if (forwarding) {
3234
if (failForward) {
@@ -136,3 +138,69 @@ it.effect("preserves installed status after probes and cleans failed agent activ
136138
expect(forwards).toBe(0);
137139
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
138140
);
141+
142+
it.live.skipIf(HostProcessPlatform.defaultValue() === "win32")(
143+
"ends the tunnel when the server's end of its stdin pipe closes",
144+
() =>
145+
Effect.gen(function* () {
146+
const fs = yield* FileSystem.FileSystem;
147+
const home = yield* fs.makeTempDirectoryScoped();
148+
const real = yield* ChildProcessSpawner.ChildProcessSpawner;
149+
let tunnel: ChildProcessSpawner.ChildProcessHandle | undefined;
150+
let tunnelStdin: unknown;
151+
const target = "test.example";
152+
const spawner = ChildProcessSpawner.make((command) =>
153+
Effect.gen(function* () {
154+
if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
155+
if (command.args.includes("-L")) {
156+
// Run the tunnel's remote command locally, as the remote shell would.
157+
const remote = command.args.slice(command.args.indexOf(target) + 1).join(" ");
158+
tunnelStdin = command.options.stdin;
159+
tunnel = yield* real.spawn(
160+
ChildProcess.make("sh", ["-c", remote], { ...command.options, stdin: "pipe" }),
161+
);
162+
return tunnel;
163+
}
164+
const output = JSON.stringify({
165+
nodePath: "/node",
166+
platforms: [],
167+
hubPort: 1234,
168+
helpers: { serveSimAxSettings: null, serveSimCli: null },
169+
});
170+
return ChildProcessSpawner.makeHandle({
171+
pid: ChildProcessSpawner.ProcessId(123),
172+
stdout: Stream.make(new TextEncoder().encode(output)),
173+
stderr: Stream.empty,
174+
all: Stream.empty,
175+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
176+
isRunning: Effect.succeed(false),
177+
kill: () => Effect.void,
178+
stdin: Sink.drain,
179+
getInputFd: () => Sink.drain,
180+
getOutputFd: () => Stream.empty,
181+
unref: Effect.succeed(Effect.void),
182+
});
183+
}),
184+
);
185+
const host = yield* SshDeviceHost.make({ id: "test", label: "Test", target }).pipe(
186+
Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
187+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
188+
Effect.provideService(
189+
HttpClient.HttpClient,
190+
HttpClient.make((request) =>
191+
Effect.succeed(HttpClientResponse.fromWeb(request, new Response("ok"))),
192+
),
193+
),
194+
);
195+
yield* host.ensureReady(() => Effect.void);
196+
const handle = tunnel;
197+
if (!handle) return yield* Effect.die("Tunnel was not spawned");
198+
expect(tunnelStdin).toBe("pipe");
199+
// A pipe buffers at most 64 KiB, so this write only completes while the remote command
200+
// keeps reading stdin; one that has already exited fails it with EPIPE. The sink then
201+
// ends stdin, which is what the server's death does.
202+
yield* Stream.make(new Uint8Array(1 << 20)).pipe(Stream.run(handle.stdin));
203+
expect(yield* handle.exitCode).toBe(0);
204+
yield* host.stop;
205+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
206+
);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,21 @@ export const make = Effect.fn("SshDeviceHost.make")(function* (
246246
"ServerAliveInterval=10",
247247
"-o",
248248
"ServerAliveCountMax=3",
249-
"-N",
249+
"-T",
250250
"-L",
251251
`127.0.0.1:${hubPort}:127.0.0.1:${remote.hubPort}`,
252252
...(remote.daemonPort === undefined
253253
? []
254254
: ["-L", `127.0.0.1:${daemonPort}:127.0.0.1:${remote.daemonPort}`]),
255255
config.target,
256+
// The server holds the only write end of this stdin, so when it exits for any
257+
// reason (including SIGKILL) the remote `cat` sees EOF and ssh exits once its
258+
// forwarded connections close, instead of being orphaned.
259+
"sh",
260+
"-c",
261+
quoteRemoteArg("exec cat >/dev/null"),
256262
],
257-
{ stdin: "ignore", stdout: "ignore", stderr: "pipe" },
263+
{ stdin: "pipe", stdout: "ignore", stderr: "pipe" },
258264
),
259265
)
260266
.pipe(

0 commit comments

Comments
 (0)