Skip to content

Commit 0a78c68

Browse files
committed
fix(server): a timed-out tailscale probe no longer outlives its call
tailscale status and debug prefs were spawned without forceKillAfter, so on timeout cleanup sent SIGTERM to the process group and never escalated. A group member that ignores SIGTERM, such as the macOS binary behind the sh wrapper, kept running. Escalate to SIGKILL after a 500 ms grace. Fixes #21609
1 parent 1451e7c commit 0a78c68

4 files changed

Lines changed: 83 additions & 2 deletions

File tree

‎packages/tailscale/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"effect": "catalog:"
1818
},
1919
"devDependencies": {
20+
"@effect/platform-node": "catalog:",
2021
"@effect/vitest": "catalog:",
2122
"@types/node": "catalog:",
2223
"vite-plus": "catalog:"

‎packages/tailscale/src/tailscale.test.ts‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
12
import { assert, describe, it } from "@effect/vitest";
23
import * as Cause from "effect/Cause";
34
import * as Effect from "effect/Effect";
45
import * as Fiber from "effect/Fiber";
6+
import * as FileSystem from "effect/FileSystem";
57
import * as Layer from "effect/Layer";
8+
import * as Path from "effect/Path";
69
import * as PlatformError from "effect/PlatformError";
710
import * as Sink from "effect/Sink";
811
import * as Stream from "effect/Stream";
912
import * as TestClock from "effect/testing/TestClock";
10-
import { ChildProcessSpawner } from "effect/process";
13+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1114
import * as HostProcess from "@t3tools/shared/HostProcess";
1215

1316
import {
@@ -385,3 +388,67 @@ describe("tailscale", () => {
385388
});
386389
});
387390
});
391+
392+
// The wrapper keeps the default SIGTERM disposition while its `sleep` child
393+
// inherits an ignored one, so SIGTERM ends the leader and leaves a group member.
394+
const sigtermIgnoringTailscale = (readyPath: string) => `#!/bin/sh
395+
trap '' TERM
396+
sleep 60 &
397+
child=$!
398+
trap - TERM
399+
echo "$$ $child" > '${readyPath}'
400+
wait
401+
`;
402+
403+
const isProcessAlive = (pid: number) =>
404+
Effect.try(() => process.kill(pid, 0)).pipe(
405+
Effect.match({ onFailure: () => false, onSuccess: () => true }),
406+
);
407+
408+
describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("tailscale probe cleanup", () => {
409+
it.effect("kills a timed-out status probe whose process group ignores SIGTERM", () =>
410+
Effect.gen(function* () {
411+
const fs = yield* FileSystem.FileSystem;
412+
const path = yield* Path.Path;
413+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
414+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-tailscale-" });
415+
const readyPath = path.join(directory, "ready");
416+
// The fixture reports its PIDs through a FIFO so the test blocks on the
417+
// fixture being up instead of polling for a file.
418+
yield* spawner.exitCode(ChildProcess.make("mkfifo", [readyPath]));
419+
yield* fs.writeFileString(
420+
path.join(directory, "tailscale"),
421+
sigtermIgnoringTailscale(readyPath),
422+
{ mode: 0o700 },
423+
);
424+
yield* Effect.acquireRelease(
425+
Effect.sync(() => {
426+
const originalPath = process.env.PATH;
427+
process.env.PATH = `${directory}:${originalPath ?? ""}`;
428+
return originalPath;
429+
}),
430+
(originalPath) =>
431+
Effect.sync(() => {
432+
process.env.PATH = originalPath;
433+
}),
434+
);
435+
436+
const fiber = yield* readTailscaleStatus.pipe(Effect.flip, Effect.forkScoped);
437+
const pids = (yield* fs.readFileString(readyPath)).trim().split(" ").map(Number);
438+
yield* Effect.addFinalizer(() =>
439+
Effect.forEach(
440+
pids,
441+
(pid) => Effect.try(() => process.kill(pid, "SIGKILL")).pipe(Effect.ignore),
442+
{ discard: true },
443+
),
444+
);
445+
assert.deepEqual(yield* Effect.forEach(pids, isProcessAlive), [true, true]);
446+
447+
yield* TestClock.adjust(TAILSCALE_STATUS_TIMEOUT);
448+
const error = yield* Fiber.join(fiber);
449+
450+
assert.instanceOf(error, TailscaleCommandTimeoutError);
451+
assert.deepEqual(yield* Effect.forEach(pids, isProcessAlive), [false, false]);
452+
}).pipe(Effect.provide(NodeServices.layer)),
453+
);
454+
});

‎packages/tailscale/src/tailscale.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export const DEFAULT_TAILSCALE_SERVE_PORT = 443;
1515
* that follows the timeout also takes time (`taskkill` on Windows).
1616
*/
1717
export const TAILSCALE_STATUS_TIMEOUT = Duration.millis(3_500);
18+
/**
19+
* How long a probe killed on timeout gets to exit after SIGTERM before its
20+
* process group is SIGKILLed. Without it the spawner never escalates, so a group
21+
* member that ignores SIGTERM outlives the call. Short because the timeout error
22+
* only surfaces once cleanup finishes, and a healthy `tailscale` exits at once.
23+
*/
24+
const TAILSCALE_KILL_GRACE = Duration.millis(500);
1825
const TAILSCALE_SERVE_TIMEOUT = Duration.seconds(10);
1926
const TAILSCALE_PROBE_TIMEOUT = Duration.millis(2_500);
2027

@@ -220,8 +227,11 @@ const readTailscaleOutput = (subcommand: "status" | "debug", args: readonly stri
220227
subcommand,
221228
argumentCount: args.length,
222229
};
230+
const command = ChildProcess.make(executable, [...args], {
231+
forceKillAfter: TAILSCALE_KILL_GRACE,
232+
});
223233
return yield* Effect.gen(function* () {
224-
const child = yield* spawner.spawn(ChildProcess.make(executable, [...args])).pipe(
234+
const child = yield* spawner.spawn(command).pipe(
225235
Effect.mapError((cause) => new TailscaleCommandSpawnError({ ...commandContext, cause })),
226236
// Spawning can also fail as a defect rather than a typed error - a
227237
// non-directory entry on PATH makes node throw ENOTDIR synchronously.

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)