Skip to content

Commit e49fd29

Browse files
committed
fix(server): restore launchd only after confirming removal
1 parent 5b9d6d2 commit e49fd29

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

‎apps/server/src/cloud/bootService.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
9090
const timeouts = new Map<string, unknown>();
9191
const control: {
9292
failCommand: string | undefined;
93+
failOnce?: boolean;
9394
stateAfterStop?: string;
9495
linger: string;
9596
enabled: boolean;
@@ -115,6 +116,10 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
115116
invocations.push(input);
116117
timeouts.set(command, input.timeout);
117118
const failed = command === control.failCommand;
119+
if (failed && control.failOnce) {
120+
control.failCommand = undefined;
121+
control.failOnce = false;
122+
}
118123
if (!failed && command === "loginctl enable-linger --no-ask-password 501")
119124
control.linger = "yes";
120125
if (!failed && command === "systemctl --user enable t3code.service") control.enabled = true;
@@ -890,6 +895,36 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
890895
}),
891896
);
892897

898+
it.effect(
899+
"restores launchd after a failed drain probe only when a final probe confirms removal",
900+
() =>
901+
Effect.gen(function* () {
902+
const { service, commands, control } = yield* makeHarness("darwin");
903+
const plan = yield* service.install();
904+
const polls = yield* Queue.unbounded<void>();
905+
control.drainPolls = 1;
906+
control.onDrainPoll = Effect.sync(() => {
907+
control.failCommand = "launchctl print gui/501/com.t3tools.t3code.service";
908+
control.failOnce = true;
909+
}).pipe(Effect.andThen(Queue.offer(polls, undefined)), Effect.asVoid);
910+
commands.length = 0;
911+
912+
const maintenance = yield* service
913+
.withStopped(Effect.sync(() => commands.push("repair")))
914+
.pipe(Effect.flip, Effect.forkChild);
915+
yield* Queue.take(polls);
916+
yield* TestClock.adjust("1 second");
917+
918+
expect(yield* Fiber.join(maintenance)).toMatchObject({
919+
_tag: "BootServiceCommandError",
920+
step: "waiting for the launch agent to stop",
921+
});
922+
expect(commands).not.toContain("repair");
923+
expect(commands.at(-2)).toBe("launchctl print gui/501/com.t3tools.t3code.service");
924+
expect(commands.at(-1)).toBe(`launchctl bootstrap gui/501 ${plan.unitPath}`);
925+
}),
926+
);
927+
893928
// regression: #11995 macOS 15 rejects `bootout --wait`, so the stop waits on `print`
894929
it.effect("starts a booted-out launch agent again only once launchd has removed it", () =>
895930
Effect.gen(function* () {

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ export class BootService extends Context.Service<
510510
readonly restart: Effect.Effect<"restarted" | "queued" | "skipped", BootServiceError>;
511511
/**
512512
* Runs the operation with this home's service stopped; other homes are left alone.
513-
* Restores a previously enabled or loaded service even if stopping failed.
513+
* Restores a previously enabled or loaded service; launchd requires confirmed removal.
514514
* The operation's failure takes precedence over a restart failure.
515515
*/
516516
readonly withStopped: <A, E, R>(
@@ -959,13 +959,21 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
959959
).pipe(Effect.exit),
960960
(stopped): Effect.Effect<A, E | BootServiceError, R> =>
961961
Exit.isSuccess(stopped) ? operation : Effect.failCause(stopped.cause),
962-
(stopped, exit) => {
963-
if (!restore || (manager.kind === "launchd" && Exit.isFailure(stopped))) return Effect.void;
964-
// Start only: `activate` would also enable a unit the user disabled.
965-
// When the operation itself failed, its error is the one worth reporting.
966-
const start = runSteps(manager.restart);
967-
return Exit.isFailure(exit) ? Effect.ignore(start) : start;
968-
},
962+
(stopped, exit) =>
963+
Effect.gen(function* () {
964+
if (!restore) return;
965+
if (manager.kind === "launchd" && Exit.isFailure(stopped)) {
966+
const result = yield* probe("launchctl", [
967+
"print",
968+
`gui/${uid}/${BOOT_SERVICE_LAUNCHD_LABEL}`,
969+
]);
970+
if (Option.isNone(result) || ![112, 113].includes(Number(result.value.code))) return;
971+
}
972+
// Start only: `activate` would also enable a unit the user disabled.
973+
// When the operation itself failed, its error is the one worth reporting.
974+
const start = runSteps(manager.restart);
975+
return yield* Exit.isFailure(exit) ? Effect.ignore(start) : start;
976+
}),
969977
);
970978
});
971979

0 commit comments

Comments
 (0)