fix(server): start the launch agent once via strict bootstrap
Review findings: bootstrap of a RunAtLoad/KeepAlive plist already starts the job, so the trailing kickstart -k killed a server it just booted. Bootstrap is now the strict last step. ExitTimeOut raised to 90 to match systemd's default stop timeout for update handoffs. Docs corrected for headless SSH installs, TCC prompts, and FileVault vs auto-login. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
adff784c33
commit
653a7880ce
3 files changed
+27
-33
No files matched your search
@@ -67,8 +67,8 @@ it("keeps launchd pinned to the stable launcher rather than a versioned server",
|
||||
it("restarts the launch agent on the systemd cadence", () => {
|
||||
const plist = BootService.renderBootServicePlist(macPlan, { homeDir: "/Users/theo" });
|
||||
|
||||
expect(plist).toContain("<key>RunAtLoad</key>");
|
||||
expect(plist).toContain("<key>KeepAlive</key>");
|
||||
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>");
|
||||
});
|
||||
|
||||
@@ -276,7 +276,7 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
yield* service.install;
|
||||
const plistPath = (yield* service.status).unitPath;
|
||||
commands.length = 0;
|
||||
control.failCommand = "launchctl kickstart -k gui/501/com.t3tools.t3code.service";
|
||||
control.failCommand = `launchctl bootstrap gui/501 ${plistPath}`;
|
||||
|
||||
const error = yield* service.install.pipe(Effect.flip);
|
||||
expect(error._tag).toBe("BootServiceCommandError");
|
||||
@@ -284,9 +284,7 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
"launchctl bootout gui/501/com.t3tools.t3code.service",
|
||||
"launchctl enable gui/501/com.t3tools.t3code.service",
|
||||
`launchctl bootstrap gui/501 ${plistPath}`,
|
||||
"launchctl kickstart -k gui/501/com.t3tools.t3code.service",
|
||||
`launchctl bootstrap gui/501 ${plistPath}`,
|
||||
"launchctl kickstart -k gui/501/com.t3tools.t3code.service",
|
||||
]);
|
||||
}),
|
||||
);
|
||||
@@ -326,7 +324,6 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
|
||||
expect(commands.filter((command) => command.startsWith("launchctl "))).toEqual([
|
||||
"launchctl bootout gui/501/com.t3tools.t3code.service",
|
||||
`launchctl bootstrap gui/501 ${plistPath}`,
|
||||
"launchctl kickstart -k gui/501/com.t3tools.t3code.service",
|
||||
]);
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -103,8 +103,11 @@ export function renderBootServicePlist(
|
||||
): string {
|
||||
// KeepAlive + ThrottleInterval mirror Restart=always + RestartSec=5. launchd
|
||||
// has no StartLimitBurst analog; a hard crash loop respawns every 5s forever.
|
||||
// ExitTimeOut must exceed the server's graceful shutdown plus the launcher's
|
||||
// 5s child SIGKILL grace, or launchd (default 20s) SIGKILLs mid-shutdown.
|
||||
// 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.
|
||||
// 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
|
||||
@@ -138,7 +141,7 @@ export function renderBootServicePlist(
|
||||
` <key>ThrottleInterval</key>`,
|
||||
` <integer>5</integer>`,
|
||||
` <key>ExitTimeOut</key>`,
|
||||
` <integer>30</integer>`,
|
||||
` <integer>90</integer>`,
|
||||
` <key>ProcessType</key>`,
|
||||
` <string>Interactive</string>`,
|
||||
` <key>StandardOutPath</key>`,
|
||||
@@ -258,10 +261,13 @@ export function launchdManager(input: {
|
||||
);
|
||||
const domainTarget = `gui/${input.uid}`;
|
||||
const serviceTarget = `${domainTarget}/${BOOT_SERVICE_LAUNCHD_LABEL}`;
|
||||
// bootout/enable/bootstrap are optional: they fail on not-loaded /
|
||||
// already-loaded states that are fine to proceed from. The strict
|
||||
// `kickstart -k` runs last, so a genuinely broken gui domain (e.g. an SSH
|
||||
// session with nobody logged in at the screen) still fails the flow loudly.
|
||||
// bootout/enable are optional: they fail on not-loaded states that are fine
|
||||
// to proceed from. The strict `bootstrap` runs last and is also the start:
|
||||
// loading a RunAtLoad/KeepAlive plist starts the job, so a separate
|
||||
// kickstart would kill and restart a server it just booted. A lingering job
|
||||
// that survived bootout, or a gui domain with nobody logged in at the
|
||||
// screen (SSH install), makes bootstrap fail the flow loudly rather than
|
||||
// silently keeping a stale server.
|
||||
return {
|
||||
kind: "launchd",
|
||||
unitPath,
|
||||
@@ -282,30 +288,18 @@ export function launchdManager(input: {
|
||||
args: ["enable", serviceTarget],
|
||||
optional: true,
|
||||
},
|
||||
{
|
||||
step: "loading the launch agent",
|
||||
command: "launchctl",
|
||||
args: ["bootstrap", domainTarget, unitPath],
|
||||
optional: true,
|
||||
},
|
||||
// Start last. No administrative state write occurs after this succeeds.
|
||||
{
|
||||
step: "starting the service",
|
||||
command: "launchctl",
|
||||
args: ["kickstart", "-k", serviceTarget],
|
||||
args: ["bootstrap", domainTarget, unitPath],
|
||||
},
|
||||
],
|
||||
restart: [
|
||||
{
|
||||
step: "reloading the launch agent after a failed update",
|
||||
command: "launchctl",
|
||||
args: ["bootstrap", domainTarget, unitPath],
|
||||
optional: true,
|
||||
},
|
||||
{
|
||||
step: "restarting the service after a failed update",
|
||||
command: "launchctl",
|
||||
args: ["kickstart", "-k", serviceTarget],
|
||||
args: ["bootstrap", domainTarget, unitPath],
|
||||
},
|
||||
],
|
||||
// No `launchctl disable` here: a persisted override would sabotage a
|
||||
|
||||
@@ -46,15 +46,18 @@ when the machine boots and keeps running after you log out (lingering is enabled
|
||||
**macOS** uses a launch agent at `~/Library/LaunchAgents/com.t3tools.t3code.service.plist`. It
|
||||
starts when you log in, not when the Mac boots, and it stops when you log out; macOS has no
|
||||
equivalent of Linux lingering for user agents. For a Mac that should stay reachable unattended,
|
||||
turn on automatic login (System Settings → Users & Groups) and keep the Mac from sleeping.
|
||||
turn on automatic login (System Settings → Users & Groups; unavailable while FileVault is on) and
|
||||
keep the Mac from sleeping.
|
||||
|
||||
Two more macOS notes:
|
||||
|
||||
- Installing over SSH only starts the agent immediately when someone is also logged in at the
|
||||
Mac's screen. Otherwise the install completes and the agent starts at the next login.
|
||||
- A background agent cannot answer macOS privacy prompts. If agent work fails to read protected
|
||||
folders such as Desktop, Documents, or Downloads, grant Full Disk Access to the node binary
|
||||
listed in the launch agent's `ProgramArguments`.
|
||||
- Installing over SSH needs someone logged in at the Mac's screen to start the agent right away.
|
||||
Without that, the install command reports an error at the final start step, but the agent is
|
||||
fully installed and starts at the next login.
|
||||
- macOS may show privacy prompts for protected folders such as Desktop, Documents, or Downloads,
|
||||
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`.
|
||||
|
||||
**Windows** is not supported yet.
|
||||
|
||||
|
||||
Reference in new issue
Block a user