Skip to content

Commit 12421da

Browse files
fix(ssh): timed-out SSH commands no longer leave a proxy helper running (pingdotgg#35775)
1 parent 6b292d9 commit 12421da

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎packages/ssh/src/command.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,45 @@ describe("ssh command", () => {
149149
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
150150
);
151151

152+
it.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(
153+
"a timed-out command leaves no helper running that ignored SIGTERM",
154+
() =>
155+
Effect.gen(function* () {
156+
const nativeSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
157+
// Stands in for ssh and a ProxyCommand: ssh exits on SIGTERM, the proxy ignores it.
158+
const spawner = ChildProcessSpawner.make((command) =>
159+
nativeSpawner.spawn(
160+
ChildProcess.make(
161+
process.execPath,
162+
[
163+
"-e",
164+
`const proxy = require("node:child_process").spawn("sh", ["-c", "trap '' TERM; exec sleep 60"], { stdio: "ignore" });
165+
process.stdout.write(proxy.pid + "\\n");
166+
setInterval(() => {}, 1000);`,
167+
],
168+
command._tag === "StandardCommand" ? command.options : {},
169+
),
170+
),
171+
);
172+
const failure = yield* runSshCommand(
173+
{ alias: "synthetic-fixture", hostname: "synthetic-fixture", username: null, port: null },
174+
{ timeoutMs: 1_000, keepErrorOutputTail: true },
175+
).pipe(
176+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
177+
Effect.flip,
178+
);
179+
assert.instanceOf(failure, SshCommandError);
180+
assert.equal(failure.message, "SSH command timed out after 1000ms.");
181+
const proxyPid = Number(failure.stdout?.trim());
182+
assert.isTrue(Number.isSafeInteger(proxyPid) && proxyPid > 1);
183+
const state = (yield* nativeSpawner.string(
184+
ChildProcess.make("ps", ["-p", String(proxyPid), "-o", "stat="]),
185+
)).trim();
186+
if (state !== "" && !state.startsWith("Z")) process.kill(proxyPid, "SIGKILL");
187+
assert.isTrue(state === "" || state.startsWith("Z"), `proxy still running (${state})`);
188+
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
189+
);
190+
152191
it.live("enables server-alive checks for long remote commands", () =>
153192
Effect.gen(function* () {
154193
// Ask OpenSSH for its effective configuration without opening a connection.

‎packages/ssh/src/command.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
274274
ChildProcess.make(sshCommand, args, {
275275
env: environment,
276276
extendEnv: true,
277+
forceKillAfter: "1 second",
277278
stdin: {
278279
stream: stdinStream(input.stdin),
279280
endOnDone: true,

0 commit comments

Comments
 (0)