Skip to content

Commit 44d60da

Browse files
committed
fix(server): preserve launchd absence verification failures
Keep probe timeout identity after an idempotent bootout. Require complete output and a confirmed nonzero exit before accepting not-loaded evidence, and cover install, restart, and uninstall failure paths. Refs #11995
1 parent 2302f77 commit 44d60da

2 files changed

Lines changed: 143 additions & 18 deletions

File tree

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

Lines changed: 126 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,17 @@ const launchdServiceTarget = "gui/501/com.t3tools.t3code.service";
8787
const launchdNotLoadedMessage =
8888
'Could not find service "com.t3tools.t3code.service" in domain for user gui: 501';
8989

90-
const processResult = (input?: {
91-
readonly code?: number | null;
92-
readonly stdout?: string;
93-
readonly stderr?: string;
94-
readonly timedOut?: boolean;
95-
}): ProcessRunner.ProcessRunOutput => ({
90+
const processResult = (
91+
input?: Partial<Omit<ProcessRunner.ProcessRunOutput, "code">> & { readonly code?: number | null },
92+
): ProcessRunner.ProcessRunOutput => ({
9693
stdout: input?.stdout ?? "",
9794
stderr: input?.stderr ?? "",
9895
code: input?.code === null ? null : ChildProcessSpawner.ExitCode(input?.code ?? 0),
9996
timedOut: input?.timedOut ?? false,
100-
stdoutTruncated: false,
101-
stderrTruncated: false,
102-
stdoutInvalidUtf8: false,
103-
stderrInvalidUtf8: false,
97+
stdoutTruncated: input?.stdoutTruncated ?? false,
98+
stderrTruncated: input?.stderrTruncated ?? false,
99+
stdoutInvalidUtf8: input?.stdoutInvalidUtf8 ?? false,
100+
stderrInvalidUtf8: input?.stderrInvalidUtf8 ?? false,
104101
});
105102

106103
it("recognizes only confirmed launchd not-loaded responses", () => {
@@ -982,6 +979,125 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
982979
}).pipe(Effect.provide(TestClock.layer())),
983980
);
984981

982+
it.effect.each(["install", "restart", "uninstall"] as const)(
983+
"preserves a probe timeout after an idempotent bootout during $0",
984+
(operation) =>
985+
Effect.gen(function* () {
986+
const { service, commands, control, fs } = yield* makeHarness("darwin");
987+
const plan = yield* service.install();
988+
commands.length = 0;
989+
control.fixtures.set(`launchctl bootout ${launchdServiceTarget}`, () =>
990+
processResult({ code: 3, stderr: "Boot-out failed: 3: No such process\n" }),
991+
);
992+
control.fixtures.set(`launchctl print ${launchdServiceTarget}`, () =>
993+
processResult({ code: null, timedOut: true }),
994+
);
995+
996+
const action =
997+
operation === "install"
998+
? service.install().pipe(Effect.asVoid)
999+
: service[operation].pipe(Effect.asVoid);
1000+
const error = yield* action.pipe(Effect.flip);
1001+
1002+
expect(error).toMatchObject({
1003+
_tag: "BootServiceCommandError",
1004+
step: "checking whether the launch agent stopped",
1005+
timedOut: true,
1006+
});
1007+
expect(error.message).toContain("timed out");
1008+
expect(commands.filter((command) => command.startsWith("launchctl "))).toEqual([
1009+
`launchctl bootout ${launchdServiceTarget}`,
1010+
`launchctl print ${launchdServiceTarget}`,
1011+
]);
1012+
expect(yield* fs.exists(plan.unitPath)).toBe(true);
1013+
}),
1014+
);
1015+
1016+
it.effect.each([
1017+
{
1018+
name: "permission denied",
1019+
result: processResult({ code: 1, stderr: "Operation not permitted" }),
1020+
},
1021+
{ name: "empty failure", result: processResult({ code: 113 }) },
1022+
{
1023+
name: "wrong label",
1024+
result: processResult({
1025+
code: 113,
1026+
stderr: launchdNotLoadedMessage.replace(".service", ".other"),
1027+
}),
1028+
},
1029+
{
1030+
name: "missing GUI domain",
1031+
result: processResult({ code: 125, stderr: "Could not find domain for user gui: 501" }),
1032+
},
1033+
{
1034+
name: "extra error",
1035+
result: processResult({
1036+
code: 113,
1037+
stderr: `${launchdNotLoadedMessage}\nOperation not permitted`,
1038+
}),
1039+
},
1040+
{ name: "signal exit", result: processResult({ code: null, stderr: launchdNotLoadedMessage }) },
1041+
{
1042+
name: "truncated stderr",
1043+
result: processResult({ code: 113, stderr: launchdNotLoadedMessage, stderrTruncated: true }),
1044+
},
1045+
{
1046+
name: "truncated stdout",
1047+
result: processResult({ code: 113, stderr: launchdNotLoadedMessage, stdoutTruncated: true }),
1048+
},
1049+
{
1050+
name: "invalid stderr",
1051+
result: processResult({
1052+
code: 113,
1053+
stderr: launchdNotLoadedMessage,
1054+
stderrInvalidUtf8: true,
1055+
}),
1056+
},
1057+
{
1058+
name: "invalid stdout",
1059+
result: processResult({
1060+
code: 113,
1061+
stderr: launchdNotLoadedMessage,
1062+
stdoutInvalidUtf8: true,
1063+
}),
1064+
},
1065+
])("fails closed on $name during absence verification", ({ result }) =>
1066+
Effect.gen(function* () {
1067+
const { service, commands, control, fs } = yield* makeHarness("darwin");
1068+
const plan = yield* service.install();
1069+
commands.length = 0;
1070+
control.fixtures.set(`launchctl print ${launchdServiceTarget}`, () => result);
1071+
1072+
const error = yield* service.install().pipe(Effect.flip);
1073+
1074+
expect(error).toMatchObject({
1075+
_tag: "BootServiceCommandError",
1076+
step: "checking whether the launch agent stopped",
1077+
});
1078+
expect(commands.filter((command) => command.startsWith("launchctl "))).toEqual([
1079+
`launchctl bootout ${launchdServiceTarget}`,
1080+
`launchctl print ${launchdServiceTarget}`,
1081+
]);
1082+
expect(yield* fs.exists(plan.unitPath)).toBe(true);
1083+
}),
1084+
);
1085+
1086+
it.effect("keeps the plist when uninstall cannot confirm absence", () =>
1087+
Effect.gen(function* () {
1088+
const { service, commands, control, fs } = yield* makeHarness("darwin");
1089+
const plan = yield* service.install();
1090+
commands.length = 0;
1091+
control.fixtures.set(`launchctl print ${launchdServiceTarget}`, () =>
1092+
processResult({ code: 1, stderr: "Operation not permitted" }),
1093+
);
1094+
1095+
expect((yield* service.uninstall.pipe(Effect.flip))._tag).toBe("BootServiceCommandError");
1096+
expect(yield* fs.exists(plan.unitPath)).toBe(true);
1097+
expect(commands).not.toContain(`launchctl bootstrap gui/501 ${plan.unitPath}`);
1098+
}),
1099+
);
1100+
9851101
it.effect("shares one stop timeout across bootout and absence verification", () =>
9861102
Effect.gen(function* () {
9871103
const { service, commands, control } = yield* makeHarness("darwin");

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,18 @@ export type BootServiceStep = BootServiceStepBase &
224224
const STOP_STEP_TIMEOUT = Duration.seconds(120);
225225
const LAUNCHD_STOP_POLL_INTERVAL = Duration.millis(100);
226226

227-
function launchdOutputLines(result: ProcessRunner.ProcessRunOutput): ReadonlyArray<string> {
227+
function launchdFailureLines(result: ProcessRunner.ProcessRunOutput): ReadonlyArray<string> {
228+
if (
229+
result.code === null ||
230+
result.code === 0 ||
231+
result.timedOut ||
232+
result.stdoutTruncated ||
233+
result.stderrTruncated ||
234+
result.stdoutInvalidUtf8 ||
235+
result.stderrInvalidUtf8
236+
) {
237+
return [];
238+
}
228239
return `${result.stdout}\n${result.stderr}`
229240
.split(/\r?\n/)
230241
.map((line) => line.trim())
@@ -235,8 +246,7 @@ export function isConfirmedLaunchdNotLoaded(
235246
result: ProcessRunner.ProcessRunOutput,
236247
notLoadedMessages: ReadonlyArray<string>,
237248
): boolean {
238-
if (result.code === 0 || result.timedOut) return false;
239-
const lines = launchdOutputLines(result);
249+
const lines = launchdFailureLines(result);
240250
return (
241251
(lines.length === 1 && notLoadedMessages.includes(lines[0] ?? "")) ||
242252
(lines.length === 2 &&
@@ -248,8 +258,7 @@ export function isConfirmedLaunchdNotLoaded(
248258
export function isConfirmedLaunchdBootoutNotLoaded(
249259
result: ProcessRunner.ProcessRunOutput,
250260
): boolean {
251-
if (result.code === 0 || result.timedOut) return false;
252-
const lines = launchdOutputLines(result);
261+
const lines = launchdFailureLines(result);
253262
return lines.length === 1 && lines[0] === "Boot-out failed: 3: No such process";
254263
}
255264

@@ -727,7 +736,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
727736
})
728737
.pipe(Effect.mapError((cause) => new BootServiceCommandError({ step: entry.step, cause })));
729738
const bootoutError =
730-
bootoutResult.code === 0
739+
bootoutResult.code === 0 && !bootoutResult.timedOut
731740
? undefined
732741
: new BootServiceCommandError({
733742
step: entry.step,
@@ -761,8 +770,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
761770
),
762771
);
763772
if (isConfirmedLaunchdNotLoaded(printResult, entry.verifyAbsent.notLoadedMessages)) return;
764-
if (bootoutError !== undefined) return yield* bootoutError;
765-
if (printResult.code !== 0) {
773+
if (printResult.code !== 0 || printResult.timedOut) {
766774
return yield* new BootServiceCommandError({
767775
step: "checking whether the launch agent stopped",
768776
exitCode: printResult.code === null ? undefined : Number(printResult.code),
@@ -771,6 +779,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
771779
timedOut: printResult.timedOut,
772780
});
773781
}
782+
if (bootoutError !== undefined) return yield* bootoutError;
774783
yield* Effect.sleep(LAUNCHD_STOP_POLL_INTERVAL);
775784
}
776785
}).pipe(

0 commit comments

Comments
 (0)