Skip to content

Commit 1ea3e40

Browse files
committed
fix(server): preserve launchd timeout errors
1 parent 4b669dc commit 1ea3e40

2 files changed

Lines changed: 56 additions & 6 deletions

File tree

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@ const launchdNotLoadedMessage =
9292
'Could not find service "com.t3tools.t3code.service" in domain for user gui: 501';
9393

9494
const processResult = (input?: {
95-
readonly code?: number;
95+
readonly code?: number | null;
9696
readonly stdout?: string;
9797
readonly stderr?: string;
98+
readonly timedOut?: boolean;
9899
}): ProcessRunner.ProcessRunOutput => ({
99100
stdout: input?.stdout ?? "",
100101
stderr: input?.stderr ?? "",
101-
code: ChildProcessSpawner.ExitCode(input?.code ?? 0),
102-
timedOut: false,
102+
code: input?.code === null ? null : ChildProcessSpawner.ExitCode(input?.code ?? 0),
103+
timedOut: input?.timedOut ?? false,
103104
stdoutTruncated: false,
104105
stderrTruncated: false,
105106
stdoutInvalidUtf8: false,
@@ -189,6 +190,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
189190

190191
const commands: string[] = [];
191192
const timeouts = new Map<string, unknown>();
193+
const timeoutBehaviors = new Map<string, ProcessRunner.ProcessRunInput["timeoutBehavior"]>();
192194
const control: {
193195
failCommand: string | undefined;
194196
fixtures: Map<string, (call: number) => ProcessRunner.ProcessRunOutput>;
@@ -216,6 +218,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
216218
const command = `${input.command} ${input.args.join(" ")}`;
217219
commands.push(command);
218220
timeouts.set(command, input.timeout);
221+
timeoutBehaviors.set(command, input.timeoutBehavior);
219222
const call = (control.callCounts.get(command) ?? 0) + 1;
220223
control.callCounts.set(command, call);
221224
const signal = control.signals.get(command);
@@ -310,7 +313,17 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
310313
),
311314
);
312315
const service = yield* makeService();
313-
return { service, makeService, fs, statePath, commands, timeouts, control, runtime };
316+
return {
317+
service,
318+
makeService,
319+
fs,
320+
statePath,
321+
commands,
322+
timeouts,
323+
timeoutBehaviors,
324+
control,
325+
runtime,
326+
};
314327
});
315328

316329
it.layer(NodeServices.layer)("boot service install", (it) => {
@@ -925,6 +938,32 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
925938
}),
926939
);
927940

941+
it.effect.each([
942+
{
943+
command: `launchctl bootout ${launchdServiceTarget}`,
944+
expectedStep: "stopping the installed launch agent",
945+
},
946+
{
947+
command: `launchctl print ${launchdServiceTarget}`,
948+
expectedStep: "checking whether the launch agent stopped",
949+
},
950+
])("preserves a timed-out result from $command", ({ command, expectedStep }) =>
951+
Effect.gen(function* () {
952+
const { service, control, timeoutBehaviors } = yield* makeHarness("darwin");
953+
yield* service.install();
954+
control.fixtures.set(command, () => processResult({ code: null, timedOut: true }));
955+
956+
const error = yield* service.install().pipe(Effect.flip);
957+
958+
expect(error).toMatchObject({
959+
_tag: "BootServiceCommandError",
960+
step: expectedStep,
961+
timedOut: true,
962+
});
963+
expect(timeoutBehaviors.get(command)).toBe("timedOutResult");
964+
}),
965+
);
966+
928967
it.effect("times out instead of bootstrapping while the launch agent remains loaded", () =>
929968
Effect.gen(function* () {
930969
const { service, commands, control } = yield* makeHarness("darwin");

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
713713
) {
714714
yield* Effect.gen(function* () {
715715
const bootoutResult = yield* runner
716-
.run({ command: entry.command, args: entry.args, timeout: entry.timeout })
716+
.run({
717+
command: entry.command,
718+
args: entry.args,
719+
timeout: entry.timeout,
720+
timeoutBehavior: "timedOutResult",
721+
})
717722
.pipe(Effect.mapError((cause) => new BootServiceCommandError({ step: entry.step, cause })));
718723
const bootoutError =
719724
bootoutResult.code === 0
@@ -723,6 +728,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
723728
exitCode: bootoutResult.code === null ? undefined : Number(bootoutResult.code),
724729
stdoutLength: bootoutResult.stdout.length,
725730
stderrLength: bootoutResult.stderr.length,
731+
timedOut: bootoutResult.timedOut,
726732
});
727733
if (
728734
bootoutError !== undefined &&
@@ -734,7 +740,11 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
734740

735741
while (true) {
736742
const printResult = yield* runner
737-
.run({ command: "launchctl", args: ["print", entry.verifyAbsent.serviceTarget] })
743+
.run({
744+
command: "launchctl",
745+
args: ["print", entry.verifyAbsent.serviceTarget],
746+
timeoutBehavior: "timedOutResult",
747+
})
738748
.pipe(
739749
Effect.mapError(
740750
(cause) =>
@@ -752,6 +762,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
752762
exitCode: printResult.code === null ? undefined : Number(printResult.code),
753763
stdoutLength: printResult.stdout.length,
754764
stderrLength: printResult.stderr.length,
765+
timedOut: printResult.timedOut,
755766
});
756767
}
757768
yield* Effect.sleep(LAUNCHD_STOP_POLL_INTERVAL);

0 commit comments

Comments
 (0)