Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/tailscale/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"effect": "catalog:"
},
"devDependencies": {
"@effect/platform-node": "catalog:",
"@effect/vitest": "catalog:",
"@types/node": "catalog:",
"vite-plus": "catalog:"
Expand Down
47 changes: 46 additions & 1 deletion packages/tailscale/src/tailscale.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { assert, describe, it } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Deferred from "effect/Deferred";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import { ChildProcessSpawner } from "effect/process";
import { ChildProcess, ChildProcessSpawner } from "effect/process";
import * as HostProcess from "@t3tools/shared/HostProcess";

import {
Expand Down Expand Up @@ -245,4 +248,46 @@ describe("tailscale", () => {
assert.deepEqual(killGraces, [500, 500]);
});
});

it.live("cancels Serve even when the CLI ignores SIGTERM", () =>
Effect.gen(function* () {
const nativeSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const started = yield* Deferred.make<{
readonly child: ChildProcessSpawner.ChildProcessHandle;
readonly ready: string;
}>();
const spawner = ChildProcessSpawner.make((command) =>
Effect.gen(function* () {
if (!ChildProcess.isStandardCommand(command)) return yield* nativeSpawner.spawn(command);
// Code 91 marks fixture self-release so a failed cancellation cannot leave a child behind.
const child = yield* nativeSpawner.spawn(
ChildProcess.make(
process.execPath,
[
"-e",
'process.on("SIGTERM", () => {}); setTimeout(() => process.exit(91), 5000); process.stdout.write("ready");',
],
command.options,
),
);
const ready = yield* Stream.runHead(child.stdout);
yield* Deferred.succeed(started, {
child,
ready: ready._tag === "Some" ? new TextDecoder().decode(ready.value) : "",
});
return child;
}),
);
const fiber = yield* ensureTailscaleServe({ localPort: 3773 }).pipe(
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
Effect.forkChild,
);
const { child, ready } = yield* Deferred.await(started);
assert.equal(ready, "ready");
yield* Fiber.interrupt(fiber);
const result = yield* Effect.result(child.exitCode);
if (result._tag === "Success") assert.notEqual(result.success, 91);
else assert.include(String(result.failure.reason.cause), "SIGKILL");
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
});
7 changes: 5 additions & 2 deletions packages/tailscale/src/tailscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const DEFAULT_TAILSCALE_SERVE_PORT = 443;
*/
const TAILSCALE_STATUS_TIMEOUT = Duration.millis(3_500);
/**
* How long a probe killed on timeout gets to exit after SIGTERM before its
* How long a command killed on timeout gets to exit after SIGTERM before its
* process group is SIGKILLed. Without it the spawner never escalates, so a group
* member that ignores SIGTERM outlives the call. Short because the timeout error
* only surfaces once cleanup finishes, and a healthy `tailscale` exits at once.
Expand Down Expand Up @@ -388,8 +388,11 @@ const runTailscaleCommand = Effect.fn("tailscale.runTailscaleCommand")(function*
argumentCount: args.length,
};
const timeout = Duration.fromInputUnsafe(timeoutInput);
const command = ChildProcess.make(executable, args, {
forceKillAfter: TAILSCALE_KILL_GRACE,
});
return yield* Effect.gen(function* () {
const child = yield* spawner.spawn(ChildProcess.make(executable, args)).pipe(
const child = yield* spawner.spawn(command).pipe(
Effect.mapError((cause) => new TailscaleCommandSpawnError({ ...commandContext, cause })),
Effect.catchDefect((cause) =>
Effect.fail(new TailscaleCommandSpawnError({ ...commandContext, cause })),
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading