diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts index 57a11b85c8..d2cf96925e 100644 --- a/apps/server/src/cloud/bootService.test.ts +++ b/apps/server/src/cloud/bootService.test.ts @@ -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("RunAtLoad\n "); expect(plist).toContain("KeepAlive\n "); expect(plist).toContain("ThrottleInterval\n 5"); + expect(plist).toContain("ExitTimeOut\n 90"); }); 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(); 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), + ); }), ); diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts index e0266c1996..3552697f31 100644 --- a/apps/server/src/cloud/bootService.ts +++ b/apps/server/src/cloud/bootService.ts @@ -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; - /** 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); diff --git a/docs/user/background-service.md b/docs/user/background-service.md index f0632cba99..5daf2fb4f1 100644 --- a/docs/user/background-service.md +++ b/docs/user/background-service.md @@ -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.