Skip to content

Commit 21a92a7

Browse files
committed
fix(server): retain complete macOS launcher recovery copies
1 parent 3d92694 commit 21a92a7

2 files changed

Lines changed: 320 additions & 58 deletions

File tree

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

Lines changed: 265 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
904904
const failingFs = FileSystem.FileSystem.of({
905905
...fs,
906906
copy: (from, to, options) =>
907-
to === `${launcherDir}.next`
907+
to.startsWith(`${launcherDir}.next`)
908908
? Effect.fail(
909909
PlatformError.systemError({
910910
_tag: "PermissionDenied",
@@ -926,39 +926,41 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
926926
}),
927927
);
928928

929-
it.effect("retries launcher cleanup after uninstall removed the plist", () =>
930-
Effect.gen(function* () {
931-
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");
932-
const path = yield* Path.Path;
933-
const launcherDir = path.join(baseDir, "runtime", "launcher");
934-
yield* service.install();
935-
yield* fs.makeDirectory(`${launcherDir}.next`);
936-
yield* fs.makeDirectory(`${launcherDir}.prev`);
937-
const failingFs = FileSystem.FileSystem.of({
938-
...fs,
939-
remove: (target, options) =>
940-
target === launcherDir
941-
? Effect.fail(
942-
PlatformError.systemError({
943-
_tag: "PermissionDenied",
944-
module: "FileSystem",
945-
method: "remove",
946-
pathOrDescriptor: target,
947-
}),
948-
)
949-
: fs.remove(target, options),
950-
});
951-
const failing = yield* makeService().pipe(
952-
Effect.provideService(FileSystem.FileSystem, failingFs),
953-
);
954-
expect((yield* failing.uninstall.pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
955-
expect((yield* service.status).installed).toBe(false);
956-
expect(yield* service.uninstall).toBe(true);
957-
for (const directory of [launcherDir, `${launcherDir}.next`, `${launcherDir}.prev`]) {
958-
expect(yield* fs.exists(directory)).toBe(false);
959-
}
960-
expect(yield* service.uninstall).toBe(false);
961-
}),
929+
it.effect.each(["", ".next", ".prev"])(
930+
"retries launcher%s cleanup after uninstall removed the plist",
931+
(suffix) =>
932+
Effect.gen(function* () {
933+
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");
934+
const path = yield* Path.Path;
935+
const launcherDir = path.join(baseDir, "runtime", "launcher");
936+
yield* service.install();
937+
yield* fs.makeDirectory(`${launcherDir}.next`);
938+
yield* fs.makeDirectory(`${launcherDir}.prev`);
939+
const failingFs = FileSystem.FileSystem.of({
940+
...fs,
941+
remove: (target, options) =>
942+
target === `${launcherDir}${suffix}`
943+
? Effect.fail(
944+
PlatformError.systemError({
945+
_tag: "PermissionDenied",
946+
module: "FileSystem",
947+
method: "remove",
948+
pathOrDescriptor: target,
949+
}),
950+
)
951+
: fs.remove(target, options),
952+
});
953+
const failing = yield* makeService().pipe(
954+
Effect.provideService(FileSystem.FileSystem, failingFs),
955+
);
956+
expect((yield* failing.uninstall.pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
957+
expect((yield* service.status).installed).toBe(false);
958+
expect(yield* service.uninstall).toBe(true);
959+
for (const directory of [launcherDir, `${launcherDir}.next`, `${launcherDir}.prev`]) {
960+
expect(yield* fs.exists(directory)).toBe(false);
961+
}
962+
expect(yield* service.uninstall).toBe(false);
963+
}),
962964
);
963965

964966
it.effect("puts the previous launcher copy back when the swap fails on macOS", () =>
@@ -994,7 +996,7 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
994996
}),
995997
);
996998

997-
it.effect("retains recovery when publication and rollback both fail", () =>
999+
it.effect("retains both complete copies when publication and rollback fail", () =>
9981000
Effect.gen(function* () {
9991001
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");
10001002
const path = yield* Path.Path;
@@ -1017,16 +1019,223 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
10171019
const updated = yield* makeService(undefined, "1.2.4").pipe(
10181020
Effect.provideService(FileSystem.FileSystem, failingFs),
10191021
);
1022+
const runtime = pinnedRuntimePaths(path, baseDir, "1.2.4", "darwin");
1023+
yield* fs.writeFileString(runtime.entryPath, "new launcher\n");
1024+
yield* fs.chmod(runtime.entryPath, 0o755);
1025+
yield* fs.writeFileString(path.join(runtime.versionDir, "native.node"), "native payload\n");
10201026
expect((yield* updated.install().pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
10211027
expect(yield* fs.exists(launcherDir)).toBe(false);
10221028
expect(yield* fs.readFileString(path.join(`${launcherDir}.prev`, ".install-complete"))).toBe(
10231029
"1.2.3\n",
10241030
);
1025-
expect(yield* fs.exists(`${launcherDir}.next`)).toBe(false);
1031+
expect(yield* fs.readFileString(path.join(`${launcherDir}.next`, ".install-complete"))).toBe(
1032+
"1.2.4\n",
1033+
);
1034+
expect(yield* fs.readFileString(path.join(`${launcherDir}.next`, "t3"))).toBe(
1035+
"new launcher\n",
1036+
);
1037+
expect((yield* fs.stat(path.join(`${launcherDir}.next`, "t3"))).mode & 0o777).toBe(0o755);
1038+
expect(yield* fs.readFileString(path.join(`${launcherDir}.next`, "native.node"))).toBe(
1039+
"native payload\n",
1040+
);
1041+
expect((yield* updated.status).current).toBe(false);
1042+
expect(yield* fs.exists(path.join(baseDir, "runtime", SERVICE_RESTART_PENDING_FILE))).toBe(
1043+
true,
1044+
);
10261045
const retry = yield* makeService(undefined, "1.2.4");
10271046
yield* retry.install();
10281047
expect((yield* retry.status).current).toBe(true);
10291048
expect(yield* fs.exists(`${launcherDir}.prev`)).toBe(false);
1049+
expect(yield* fs.exists(`${launcherDir}.next`)).toBe(false);
1050+
}),
1051+
);
1052+
1053+
it.effect("recovers a first install's complete stage before a fresh copy can fail", () =>
1054+
Effect.gen(function* () {
1055+
const { makeService, fs, baseDir, commands } = yield* makeHarness("darwin");
1056+
const path = yield* Path.Path;
1057+
const launcherDir = path.join(baseDir, "runtime", "launcher");
1058+
const failingFs = FileSystem.FileSystem.of({
1059+
...fs,
1060+
rename: (from, to) =>
1061+
from === `${launcherDir}.next` && to === launcherDir
1062+
? Effect.fail(
1063+
PlatformError.systemError({
1064+
_tag: "PermissionDenied",
1065+
module: "FileSystem",
1066+
method: "rename",
1067+
pathOrDescriptor: from,
1068+
}),
1069+
)
1070+
: fs.rename(from, to),
1071+
});
1072+
const service = yield* makeService().pipe(
1073+
Effect.provideService(FileSystem.FileSystem, failingFs),
1074+
);
1075+
expect((yield* service.install().pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
1076+
expect(yield* fs.exists(launcherDir)).toBe(false);
1077+
expect(yield* fs.exists(`${launcherDir}.prev`)).toBe(false);
1078+
expect(yield* fs.readFileString(path.join(`${launcherDir}.next`, ".install-complete"))).toBe(
1079+
"1.2.3\n",
1080+
);
1081+
expect(commands.some((command) => command.startsWith("launchctl "))).toBe(false);
1082+
const retry = yield* makeService().pipe(
1083+
Effect.provideService(
1084+
FileSystem.FileSystem,
1085+
FileSystem.FileSystem.of({
1086+
...fs,
1087+
copy: (from, to, options) =>
1088+
to.startsWith(`${launcherDir}.next`)
1089+
? Effect.fail(
1090+
PlatformError.systemError({
1091+
_tag: "PermissionDenied",
1092+
module: "FileSystem",
1093+
method: "copy",
1094+
pathOrDescriptor: to,
1095+
}),
1096+
)
1097+
: fs.copy(from, to, options),
1098+
}),
1099+
),
1100+
);
1101+
expect((yield* retry.install().pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
1102+
expect(yield* fs.readFileString(path.join(launcherDir, ".install-complete"))).toBe("1.2.3\n");
1103+
expect(yield* fs.readFileString(path.join(launcherDir, "t3"))).toBe("#!/bin/sh\n");
1104+
yield* (yield* makeService()).install();
1105+
expect((yield* (yield* makeService()).status).current).toBe(true);
1106+
}),
1107+
);
1108+
1109+
it.effect.each(["absent", "empty", "entry-only"] as const)(
1110+
"recovers a complete stage with an %s fixed copy before runtime validation fails",
1111+
(fixed) =>
1112+
Effect.gen(function* () {
1113+
const { service, fs, baseDir, runtime, control } = yield* makeHarness("darwin");
1114+
const path = yield* Path.Path;
1115+
const launcherDir = path.join(baseDir, "runtime", "launcher");
1116+
yield* service.install();
1117+
yield* fs.rename(launcherDir, `${launcherDir}.next`);
1118+
yield* fs.makeDirectory(`${launcherDir}.prev`);
1119+
if (fixed !== "absent") yield* fs.makeDirectory(launcherDir);
1120+
if (fixed === "entry-only")
1121+
yield* fs.writeFileString(path.join(launcherDir, "t3"), "partial");
1122+
control.failCommand = `${runtime.entryPath} --version`;
1123+
expect((yield* service.install().pipe(Effect.flip))._tag).toBe("BootServiceCommandError");
1124+
expect(yield* fs.readFileString(path.join(launcherDir, ".install-complete"))).toBe(
1125+
"1.2.3\n",
1126+
);
1127+
expect(yield* fs.readFileString(path.join(launcherDir, "t3"))).toBe("#!/bin/sh\n");
1128+
}),
1129+
);
1130+
1131+
it.effect("restores the retired launcher before runtime validation can fail", () =>
1132+
Effect.gen(function* () {
1133+
const { service, fs, baseDir, runtime, control } = yield* makeHarness("darwin");
1134+
const path = yield* Path.Path;
1135+
const launcherDir = path.join(baseDir, "runtime", "launcher");
1136+
yield* service.install();
1137+
yield* fs.rename(launcherDir, `${launcherDir}.prev`);
1138+
yield* fs.makeDirectory(`${launcherDir}.next`);
1139+
control.failCommand = `${runtime.entryPath} --version`;
1140+
expect((yield* service.install().pipe(Effect.flip))._tag).toBe("BootServiceCommandError");
1141+
expect(yield* fs.readFileString(path.join(launcherDir, ".install-complete"))).toBe("1.2.3\n");
1142+
}),
1143+
);
1144+
1145+
it.effect.each(["read", "exists", "rename"] as const)(
1146+
"retains the only complete stage when recovery fails at %s",
1147+
(operation) =>
1148+
Effect.gen(function* () {
1149+
const { service, makeService, fs, baseDir, commands } = yield* makeHarness("darwin");
1150+
const path = yield* Path.Path;
1151+
const launcherDir = path.join(baseDir, "runtime", "launcher");
1152+
yield* service.install();
1153+
yield* fs.rename(launcherDir, `${launcherDir}.next`);
1154+
commands.length = 0;
1155+
const denied = (target: string) =>
1156+
Effect.fail(
1157+
PlatformError.systemError({
1158+
_tag: "PermissionDenied",
1159+
module: "FileSystem",
1160+
method: operation,
1161+
pathOrDescriptor: target,
1162+
}),
1163+
);
1164+
const failingFs = FileSystem.FileSystem.of({
1165+
...fs,
1166+
readFileString: (target, ...args) =>
1167+
operation === "read" && target === path.join(`${launcherDir}.next`, ".install-complete")
1168+
? denied(target)
1169+
: fs.readFileString(target, ...args),
1170+
exists: (target) =>
1171+
operation === "exists" && target === path.join(launcherDir, "t3")
1172+
? denied(target)
1173+
: fs.exists(target),
1174+
rename: (from, to) =>
1175+
operation === "rename" && to === launcherDir ? denied(from) : fs.rename(from, to),
1176+
});
1177+
const retry = yield* makeService().pipe(
1178+
Effect.provideService(FileSystem.FileSystem, failingFs),
1179+
);
1180+
expect((yield* retry.install().pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
1181+
expect(
1182+
yield* fs.readFileString(path.join(`${launcherDir}.next`, ".install-complete")),
1183+
).toBe("1.2.3\n");
1184+
expect(yield* fs.readFileString(path.join(`${launcherDir}.next`, "t3"))).toBe(
1185+
"#!/bin/sh\n",
1186+
);
1187+
expect(yield* fs.exists(launcherDir)).toBe(false);
1188+
expect(commands).toEqual([]);
1189+
}),
1190+
);
1191+
1192+
it.effect("does not mark a partial launcher copy complete or recover it", () =>
1193+
Effect.gen(function* () {
1194+
const { service, makeService, fs, baseDir, runtime, control } = yield* makeHarness("darwin");
1195+
const path = yield* Path.Path;
1196+
const launcherDir = path.join(baseDir, "runtime", "launcher");
1197+
yield* fs.writeFileString(path.join(runtime.versionDir, "native.node"), "native payload");
1198+
let partialHadMarker = false;
1199+
const failingFs = FileSystem.FileSystem.of({
1200+
...fs,
1201+
copy: (from, to, options) =>
1202+
fs.copy(from, to, options).pipe(
1203+
Effect.andThen(
1204+
to === `${launcherDir}.next` || to.endsWith("/native.node")
1205+
? fs.remove(path.join(`${launcherDir}.next`, "native.node"), { force: true }).pipe(
1206+
Effect.andThen(
1207+
fs.exists(path.join(`${launcherDir}.next`, ".install-complete")),
1208+
),
1209+
Effect.tap((exists) =>
1210+
Effect.sync(() => {
1211+
partialHadMarker = exists;
1212+
}),
1213+
),
1214+
Effect.andThen(
1215+
Effect.fail(
1216+
PlatformError.systemError({
1217+
_tag: "PermissionDenied",
1218+
module: "FileSystem",
1219+
method: "copy",
1220+
pathOrDescriptor: to,
1221+
}),
1222+
),
1223+
),
1224+
)
1225+
: Effect.void,
1226+
),
1227+
),
1228+
});
1229+
const failing = yield* makeService().pipe(
1230+
Effect.provideService(FileSystem.FileSystem, failingFs),
1231+
);
1232+
expect((yield* failing.install().pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
1233+
expect(partialHadMarker).toBe(false);
1234+
expect(yield* fs.exists(`${launcherDir}.next`)).toBe(true);
1235+
expect(yield* fs.exists(path.join(`${launcherDir}.next`, ".install-complete"))).toBe(false);
1236+
control.failCommand = `${runtime.entryPath} --version`;
1237+
expect((yield* service.install().pipe(Effect.flip))._tag).toBe("BootServiceCommandError");
1238+
expect(yield* fs.exists(launcherDir)).toBe(false);
10301239
}),
10311240
);
10321241

@@ -1086,21 +1295,27 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
10861295
}),
10871296
);
10881297

1089-
it.effect("keeps the launcher copy when an install refuses a downgrade on macOS", () =>
1090-
Effect.gen(function* () {
1091-
const { makeService, fs, baseDir } = yield* makeHarness("darwin");
1092-
const path = yield* Path.Path;
1093-
yield* (yield* makeService(undefined, "1.2.4")).install();
1298+
it.effect.each([false, true])(
1299+
"preserves downgrade refusal after launcher recovery=%s",
1300+
(recover) =>
1301+
Effect.gen(function* () {
1302+
const { makeService, fs, baseDir } = yield* makeHarness("darwin");
1303+
const path = yield* Path.Path;
1304+
yield* (yield* makeService(undefined, "1.2.4")).install();
1305+
if (recover) {
1306+
const launcherDir = path.join(baseDir, "runtime", "launcher");
1307+
yield* fs.rename(launcherDir, `${launcherDir}.prev`);
1308+
}
10941309

1095-
const older = yield* makeService(undefined, "1.2.3");
1096-
expect((yield* older.install().pipe(Effect.flip))._tag).toBe(
1097-
"BootServiceDowngradeRefusedError",
1098-
);
1099-
expect(
1100-
yield* fs.readFileString(path.join(baseDir, "runtime", "launcher", ".install-complete")),
1101-
).toBe("1.2.4\n");
1102-
expect(yield* fs.exists(path.join(baseDir, "runtime", "launcher.next"))).toBe(false);
1103-
}),
1310+
const older = yield* makeService(undefined, "1.2.3");
1311+
expect((yield* older.install().pipe(Effect.flip))._tag).toBe(
1312+
"BootServiceDowngradeRefusedError",
1313+
);
1314+
expect(
1315+
yield* fs.readFileString(path.join(baseDir, "runtime", "launcher", ".install-complete")),
1316+
).toBe("1.2.4\n");
1317+
expect(yield* fs.exists(path.join(baseDir, "runtime", "launcher.next"))).toBe(false);
1318+
}),
11041319
);
11051320

11061321
it.effect("restarts the launch agent when repair fails", () =>

0 commit comments

Comments
 (0)