fix(server): stop steps outlive the service manager's shutdown window
Review findings: raising ExitTimeOut to 90 put the stop above the ProcessRunner's 60s default, so a slow shutdown got its bootout cancelled and the strict bootstrap raced a still-loaded job. Stop and deactivate steps on both platforms now run with a 120s timeout. Also corrects the launchd default-timeout comment (system-defined, ~5s, not 20s), pins ExitTimeOut and stop timeouts in tests, and documents the Login Items toggle. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
653a7880ce
commit
ed497ee20b
3 files changed
+49
-7
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
HostProcessUserId,
|
||||
} from "@t3tools/shared/hostProcess";
|
||||
import * as ConfigProvider from "effect/ConfigProvider";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
@@ -70,6 +71,7 @@ it("restarts the launch agent on the systemd cadence", () => {
|
||||
expect(plist).toContain("<key>RunAtLoad</key>\n <true/>");
|
||||
expect(plist).toContain("<key>KeepAlive</key>\n <true/>");
|
||||
expect(plist).toContain("<key>ThrottleInterval</key>\n <integer>5</integer>");
|
||||
expect(plist).toContain("<key>ExitTimeOut</key>\n <integer>90</integer>");
|
||||
});
|
||||
|
||||
it("appends both stdio streams to the boot service log", () => {
|
||||
@@ -113,12 +115,14 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
|
||||
yield* fs.writeFileString(runtime.sentinelPath, "1.2.3\n");
|
||||
|
||||
const commands: string[] = [];
|
||||
const timeouts = new Map<string, unknown>();
|
||||
const control: { failCommand: string | undefined } = { failCommand: undefined };
|
||||
const runner = ProcessRunner.ProcessRunner.of({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
const command = `${input.command} ${input.args.join(" ")}`;
|
||||
commands.push(command);
|
||||
timeouts.set(command, input.timeout);
|
||||
return {
|
||||
stdout: input.args[1] === "--version" ? "t3 v1.2.3\n" : "",
|
||||
stderr: "",
|
||||
@@ -151,13 +155,13 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
|
||||
),
|
||||
),
|
||||
);
|
||||
return { service, fs, statePath, commands, control };
|
||||
return { service, fs, statePath, commands, timeouts, control };
|
||||
});
|
||||
|
||||
it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
it.effect("installs, reports current state, and uninstalls", () =>
|
||||
Effect.gen(function* () {
|
||||
const { service, fs, statePath, commands } = yield* makeHarness();
|
||||
const { service, fs, statePath, commands, timeouts } = yield* makeHarness();
|
||||
const plan = yield* service.install;
|
||||
|
||||
expect(parseServiceState(yield* fs.readFileString(statePath))).toEqual({
|
||||
@@ -183,6 +187,11 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
expect(yield* service.uninstall).toBe(true);
|
||||
expect((yield* service.status).installed).toBe(false);
|
||||
expect(commands.some((command) => command.startsWith("npm "))).toBe(false);
|
||||
// The stop can block up to systemd's 90s TimeoutStopSec; the runner's
|
||||
// 60s default would cancel it mid-shutdown.
|
||||
expect(timeouts.get("systemctl --user disable --now t3code.service")).toEqual(
|
||||
Duration.seconds(120),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -251,7 +260,7 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
|
||||
it.effect("installs, reports current state, and uninstalls on macOS", () =>
|
||||
Effect.gen(function* () {
|
||||
const { service, fs, statePath, commands } = yield* makeHarness("darwin");
|
||||
const { service, fs, statePath, commands, timeouts } = yield* makeHarness("darwin");
|
||||
const plan = yield* service.install;
|
||||
|
||||
expect(plan.unitPath.endsWith("Library/LaunchAgents/com.t3tools.t3code.service.plist")).toBe(
|
||||
@@ -267,6 +276,11 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
expect((yield* service.status).installed).toBe(false);
|
||||
expect(commands.some((command) => command.startsWith("npm "))).toBe(false);
|
||||
expect(commands.some((command) => command.startsWith("systemctl "))).toBe(false);
|
||||
// A bootout can block up to the plist's 90s ExitTimeOut; the runner's
|
||||
// 60s default would cancel it and let bootstrap race a loaded job.
|
||||
expect(timeouts.get("launchctl bootout gui/501/com.t3tools.t3code.service")).toEqual(
|
||||
Duration.seconds(120),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -106,8 +106,8 @@ export function renderBootServicePlist(
|
||||
// ExitTimeOut 90 matches systemd's default TimeoutStopSec. A plain stop
|
||||
// completes within the launcher's 5s child grace, but a stop that queues
|
||||
// behind an in-flight update transition can take much longer; launchd's
|
||||
// default 20s would SIGKILL the launcher (and, with it, the process group)
|
||||
// mid-handoff.
|
||||
// system-defined default (5s on current macOS) would SIGKILL the launcher
|
||||
// (and, with it, the process group) mid-handoff.
|
||||
// ProcessType Interactive opts out of background-job resource throttling.
|
||||
// AbandonProcessGroup stays at its default (false): launchd reaps leftover
|
||||
// process-group members only when the launcher itself exits — the analog of
|
||||
@@ -158,10 +158,24 @@ export interface BootServiceStep {
|
||||
readonly step: string;
|
||||
readonly command: string;
|
||||
readonly args: ReadonlyArray<string>;
|
||||
/** Non-zero exit is logged and ignored: idempotent domain operations. */
|
||||
/**
|
||||
* Non-zero exit is logged and ignored. Reserved for steps whose common
|
||||
* failures (not loaded, already enabled) leave a state a later strict step
|
||||
* either tolerates or fails loudly on.
|
||||
*/
|
||||
readonly optional?: boolean;
|
||||
/** Override the ProcessRunner default (60s) for steps that block longer. */
|
||||
readonly timeout?: Duration.Input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop commands block until the service manager gives up: 90s by default for
|
||||
* systemd's TimeoutStopSec, and ExitTimeOut=90 in the rendered plist. This
|
||||
* must stay above both, or the runner cancels the stop mid-shutdown and the
|
||||
* next step races a still-loaded service.
|
||||
*/
|
||||
const STOP_STEP_TIMEOUT = Duration.seconds(120);
|
||||
|
||||
/**
|
||||
* Platform service-manager integration as data: paths, a pure renderer, and
|
||||
* the command steps each flow runs. install/uninstall/status consume this and
|
||||
@@ -203,6 +217,7 @@ export function systemdManager(input: {
|
||||
step: "stopping the installed service",
|
||||
command: "systemctl",
|
||||
args: ["--user", "stop", BOOT_SERVICE_UNIT_FILE],
|
||||
timeout: STOP_STEP_TIMEOUT,
|
||||
},
|
||||
],
|
||||
activate: [
|
||||
@@ -236,6 +251,7 @@ export function systemdManager(input: {
|
||||
step: "stopping the service",
|
||||
command: "systemctl",
|
||||
args: ["--user", "disable", "--now", BOOT_SERVICE_UNIT_FILE],
|
||||
timeout: STOP_STEP_TIMEOUT,
|
||||
},
|
||||
],
|
||||
finalize: [
|
||||
@@ -278,6 +294,7 @@ export function launchdManager(input: {
|
||||
command: "launchctl",
|
||||
args: ["bootout", serviceTarget],
|
||||
optional: true,
|
||||
timeout: STOP_STEP_TIMEOUT,
|
||||
},
|
||||
],
|
||||
activate: [
|
||||
@@ -304,12 +321,15 @@ export function launchdManager(input: {
|
||||
],
|
||||
// No `launchctl disable` here: a persisted override would sabotage a
|
||||
// later reinstall. Removing the plist is what stops the next login load.
|
||||
// A bootout that fails for a reason other than "not loaded" leaves the
|
||||
// job running until logout; the failure is in the boot-service log.
|
||||
deactivate: [
|
||||
{
|
||||
step: "stopping the service",
|
||||
command: "launchctl",
|
||||
args: ["bootout", serviceTarget],
|
||||
optional: true,
|
||||
timeout: STOP_STEP_TIMEOUT,
|
||||
},
|
||||
],
|
||||
finalize: [],
|
||||
@@ -492,7 +512,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
|
||||
Effect.forEach(
|
||||
steps,
|
||||
(entry) => {
|
||||
const run = runStep(entry.step, entry.command, entry.args);
|
||||
const run = runStep(
|
||||
entry.step,
|
||||
entry.command,
|
||||
entry.args,
|
||||
entry.timeout === undefined ? undefined : { timeout: entry.timeout },
|
||||
);
|
||||
// runStep's tapError already appends the failure to the log, so an
|
||||
// ignored optional step still leaves a trace.
|
||||
return entry.optional === true ? run.pipe(Effect.ignore) : run.pipe(Effect.asVoid);
|
||||
|
||||
@@ -58,6 +58,9 @@ Two more macOS notes:
|
||||
attributed to a bare `node` process, or deny access without a prompt. If agent work fails to
|
||||
read those folders, grant Full Disk Access to the node binary listed in the launch agent's
|
||||
`ProgramArguments`.
|
||||
- The agent appears under System Settings → General → Login Items. If it was switched off there,
|
||||
or disabled with `launchctl disable`, macOS will not start it at login until you switch it back
|
||||
on.
|
||||
|
||||
**Windows** is not supported yet.
|
||||
|
||||
|
||||
Reference in new issue
Block a user