Skip to content

Commit 4b669dc

Browse files
committed
fix(server): bound launchd shutdown verification
1 parent 3b7ab6b commit 4b669dc

2 files changed

Lines changed: 57 additions & 21 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
194194
fixtures: Map<string, (call: number) => ProcessRunner.ProcessRunOutput>;
195195
callCounts: Map<string, number>;
196196
signals: Map<string, Deferred.Deferred<void>>;
197+
delays: Map<string, Duration.Input>;
197198
stateAfterStop?: string;
198199
linger: string;
199200
enabled: boolean;
@@ -203,6 +204,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
203204
fixtures: new Map(),
204205
callCounts: new Map(),
205206
signals: new Map(),
207+
delays: new Map(),
206208
linger: "yes",
207209
enabled: true,
208210
active: true,
@@ -218,6 +220,8 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
218220
control.callCounts.set(command, call);
219221
const signal = control.signals.get(command);
220222
if (signal !== undefined) yield* Deferred.succeed(signal, undefined);
223+
const delay = control.delays.get(command);
224+
if (delay !== undefined) yield* Effect.sleep(delay);
221225
const fixture = control.fixtures.get(command);
222226
if (fixture !== undefined) return fixture(call);
223227
const failed = command === control.failCommand;
@@ -946,6 +950,36 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
946950
}).pipe(Effect.provide(TestClock.layer())),
947951
);
948952

953+
it.effect("shares one stop timeout across bootout and absence verification", () =>
954+
Effect.gen(function* () {
955+
const { service, commands, control } = yield* makeHarness("darwin");
956+
yield* service.install();
957+
commands.length = 0;
958+
control.callCounts.clear();
959+
const bootoutStarted = yield* Deferred.make<void>();
960+
const firstPrint = yield* Deferred.make<void>();
961+
control.signals.set(`launchctl bootout ${launchdServiceTarget}`, bootoutStarted);
962+
control.signals.set(`launchctl print ${launchdServiceTarget}`, firstPrint);
963+
control.delays.set(`launchctl bootout ${launchdServiceTarget}`, Duration.seconds(60));
964+
control.fixtures.set(`launchctl print ${launchdServiceTarget}`, () => processResult());
965+
966+
const installFiber = yield* service.install().pipe(Effect.forkChild);
967+
yield* Deferred.await(bootoutStarted);
968+
yield* TestClock.adjust(Duration.seconds(60));
969+
yield* Deferred.await(firstPrint);
970+
yield* Effect.yieldNow;
971+
yield* TestClock.adjust(Duration.seconds(60));
972+
const error = yield* Fiber.join(installFiber).pipe(Effect.flip);
973+
974+
expect(error).toMatchObject({
975+
_tag: "BootServiceCommandError",
976+
step: "waiting for the launch agent to stop",
977+
timedOut: true,
978+
});
979+
expect(commands).not.toContain(`launchctl enable ${launchdServiceTarget}`);
980+
}).pipe(Effect.provide(TestClock.layer())),
981+
);
982+
949983
it.effect("uninstalls when launchd confirms the GUI domain is absent", () =>
950984
Effect.gen(function* () {
951985
const { service, fs, control } = yield* makeHarness("darwin");

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -711,27 +711,27 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
711711
const runLaunchdBootout = Effect.fn("cloud.boot_service.run_launchd_bootout")(function* (
712712
entry: Extract<BootServiceStep, { readonly operation: "launchd-bootout" }>,
713713
) {
714-
const bootoutResult = yield* runner
715-
.run({ command: entry.command, args: entry.args, timeout: entry.timeout })
716-
.pipe(Effect.mapError((cause) => new BootServiceCommandError({ step: entry.step, cause })));
717-
const bootoutError =
718-
bootoutResult.code === 0
719-
? undefined
720-
: new BootServiceCommandError({
721-
step: entry.step,
722-
exitCode: bootoutResult.code === null ? undefined : Number(bootoutResult.code),
723-
stdoutLength: bootoutResult.stdout.length,
724-
stderrLength: bootoutResult.stderr.length,
725-
});
726-
if (
727-
bootoutError !== undefined &&
728-
!isConfirmedLaunchdBootoutNotLoaded(bootoutResult) &&
729-
!isConfirmedLaunchdNotLoaded(bootoutResult, entry.verifyAbsent.notLoadedMessages)
730-
) {
731-
return yield* bootoutError;
732-
}
733-
734714
yield* Effect.gen(function* () {
715+
const bootoutResult = yield* runner
716+
.run({ command: entry.command, args: entry.args, timeout: entry.timeout })
717+
.pipe(Effect.mapError((cause) => new BootServiceCommandError({ step: entry.step, cause })));
718+
const bootoutError =
719+
bootoutResult.code === 0
720+
? undefined
721+
: new BootServiceCommandError({
722+
step: entry.step,
723+
exitCode: bootoutResult.code === null ? undefined : Number(bootoutResult.code),
724+
stdoutLength: bootoutResult.stdout.length,
725+
stderrLength: bootoutResult.stderr.length,
726+
});
727+
if (
728+
bootoutError !== undefined &&
729+
!isConfirmedLaunchdBootoutNotLoaded(bootoutResult) &&
730+
!isConfirmedLaunchdNotLoaded(bootoutResult, entry.verifyAbsent.notLoadedMessages)
731+
) {
732+
return yield* bootoutError;
733+
}
734+
735735
while (true) {
736736
const printResult = yield* runner
737737
.run({ command: "launchctl", args: ["print", entry.verifyAbsent.serviceTarget] })
@@ -758,7 +758,9 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
758758
}
759759
}).pipe(
760760
Effect.timeoutOrElse({
761-
duration: STOP_STEP_TIMEOUT,
761+
// One deadline covers both bootout and absence verification. Without
762+
// this outer bound, each phase could consume a full stop timeout.
763+
duration: entry.timeout ?? STOP_STEP_TIMEOUT,
762764
orElse: () =>
763765
new BootServiceCommandError({
764766
step: "waiting for the launch agent to stop",

0 commit comments

Comments
 (0)