|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
1 | 2 | import { assert, describe, it } from "@effect/vitest"; |
2 | 3 | import * as Cause from "effect/Cause"; |
3 | 4 | import * as Effect from "effect/Effect"; |
4 | 5 | import * as Fiber from "effect/Fiber"; |
| 6 | +import * as FileSystem from "effect/FileSystem"; |
5 | 7 | import * as Layer from "effect/Layer"; |
| 8 | +import * as Path from "effect/Path"; |
6 | 9 | import * as PlatformError from "effect/PlatformError"; |
7 | 10 | import * as Sink from "effect/Sink"; |
8 | 11 | import * as Stream from "effect/Stream"; |
9 | 12 | import * as TestClock from "effect/testing/TestClock"; |
10 | | -import { ChildProcessSpawner } from "effect/process"; |
| 13 | +import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
11 | 14 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
12 | 15 |
|
13 | 16 | import { |
@@ -385,3 +388,67 @@ describe("tailscale", () => { |
385 | 388 | }); |
386 | 389 | }); |
387 | 390 | }); |
| 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 | +}); |
0 commit comments