Skip to content

Commit 5b9d6d2

Browse files
committed
fix(server): require launchd drain before maintenance and restoration
1 parent 8415aee commit 5b9d6d2

2 files changed

Lines changed: 64 additions & 23 deletions

File tree

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

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,32 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
864864
}),
865865
);
866866

867+
it.effect(
868+
"requires launchd shutdown confirmation for maintenance after a failed status probe",
869+
() =>
870+
Effect.gen(function* () {
871+
const { service, control } = yield* makeHarness("darwin");
872+
yield* service.install();
873+
control.failCommand = "launchctl print gui/501/com.t3tools.t3code.service";
874+
let ran = false;
875+
876+
const error = yield* service
877+
.withStopped(
878+
Effect.sync(() => {
879+
ran = true;
880+
}),
881+
)
882+
.pipe(Effect.flip);
883+
884+
expect(error).toMatchObject({
885+
_tag: "BootServiceCommandError",
886+
step: "waiting for the launch agent to stop",
887+
exitCode: 1,
888+
});
889+
expect(ran).toBe(false);
890+
}),
891+
);
892+
867893
// regression: #11995 macOS 15 rejects `bootout --wait`, so the stop waits on `print`
868894
it.effect("starts a booted-out launch agent again only once launchd has removed it", () =>
869895
Effect.gen(function* () {
@@ -893,25 +919,36 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
893919
}),
894920
);
895921

896-
it.effect("fails an install whose launch agent is still loaded after the stop timeout", () =>
897-
Effect.gen(function* () {
898-
const { service, commands, control } = yield* makeHarness("darwin");
899-
const plan = yield* service.install();
900-
const polls = yield* Queue.unbounded<void>();
901-
control.drainPolls = Number.POSITIVE_INFINITY;
902-
control.onDrainPoll = Queue.offer(polls, undefined).pipe(Effect.asVoid);
903-
commands.length = 0;
904-
905-
const reinstall = yield* service.install().pipe(Effect.flip, Effect.forkChild);
906-
yield* Queue.take(polls);
907-
yield* TestClock.adjust("120 seconds");
922+
it.effect.each(["install", "maintenance"] as const)(
923+
"fails %s while launchd is still draining after the stop timeout",
924+
(operation) =>
925+
Effect.gen(function* () {
926+
const { service, commands, control } = yield* makeHarness("darwin");
927+
const plan = yield* service.install();
928+
const polls = yield* Queue.unbounded<void>();
929+
control.drainPolls = Number.POSITIVE_INFINITY;
930+
control.onDrainPoll = Queue.offer(polls, undefined).pipe(Effect.asVoid);
931+
commands.length = 0;
908932

909-
expect(yield* Fiber.join(reinstall)).toMatchObject({
910-
_tag: "BootServiceCommandError",
911-
step: "waiting for the launch agent to stop",
912-
});
913-
expect(commands).not.toContain(`launchctl bootstrap gui/501 ${plan.unitPath}`);
914-
}),
933+
const run =
934+
operation === "install"
935+
? service.install().pipe(Effect.asVoid)
936+
: service.withStopped(
937+
Effect.sync(() => {
938+
commands.push("repair");
939+
}),
940+
);
941+
const reinstall = yield* run.pipe(Effect.flip, Effect.forkChild);
942+
yield* Queue.take(polls);
943+
yield* TestClock.adjust("120 seconds");
944+
945+
expect(yield* Fiber.join(reinstall)).toMatchObject({
946+
_tag: "BootServiceCommandError",
947+
step: "waiting for the launch agent to stop",
948+
});
949+
expect(commands).not.toContain(`launchctl bootstrap gui/501 ${plan.unitPath}`);
950+
expect(commands).not.toContain("repair");
951+
}),
915952
);
916953

917954
it.effect("fails an install when launchd cannot say whether the agent stopped", () =>

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,13 +950,17 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
950950
);
951951
return yield* Effect.acquireUseRelease(
952952
// Require every stop before repair when restoring, so the server releases the database.
953-
// Otherwise stops are best-effort; launchd rejects bootout of an unloaded job.
954-
// A failed stop may still stop the service, so restoration runs either way.
955-
runSteps(manager.stop.map((step) => ({ ...step, optional: !restore }))).pipe(Effect.exit),
953+
// Launchd drain confirmation stays strict even when the initial status probe failed.
954+
runSteps(
955+
manager.stop.map((step) => ({
956+
...step,
957+
optional: !restore && step.untilExit === undefined,
958+
})),
959+
).pipe(Effect.exit),
956960
(stopped): Effect.Effect<A, E | BootServiceError, R> =>
957961
Exit.isSuccess(stopped) ? operation : Effect.failCause(stopped.cause),
958-
(_, exit) => {
959-
if (!restore) return Effect.void;
962+
(stopped, exit) => {
963+
if (!restore || (manager.kind === "launchd" && Exit.isFailure(stopped))) return Effect.void;
960964
// Start only: `activate` would also enable a unit the user disabled.
961965
// When the operation itself failed, its error is the one worth reporting.
962966
const start = runSteps(manager.restart);

0 commit comments

Comments
 (0)