Skip to content

Commit 0e4163e

Browse files
vitalyiegorovclaude
andcommitted
fix(server): keep a failed service start out of the current state
On launchd, `t3 service install` writes the plist and state before `launchctl bootstrap`. When bootstrap fails, nothing on disk records it, so the next `t3 service install` reports "already installed" while the previous launcher keeps running. Write the existing `.restart-pending` marker on every install, not only for `start: false`. A successful start still removes it; a failed one now leaves `status` reporting `restart-pending`, so the retry repairs the service. Fixes #12197 Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 1451e7c commit 0e4163e

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
354354
current: true,
355355
installedVersion: "1.2.3",
356356
});
357+
// A started install that reached the end removes its own marker.
358+
const path = yield* Path.Path;
359+
expect(
360+
yield* fs.exists(path.join(path.dirname(statePath), SERVICE_RESTART_PENDING_FILE)),
361+
).toBe(false);
357362
const pendingState = JSON.stringify({
358363
protocol: SERVICE_LAUNCHER_PROTOCOL,
359364
activeVersion: "1.2.3",
@@ -858,6 +863,40 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
858863
}),
859864
);
860865

866+
it.effect("keeps a launch agent install that failed to start out of the current state", () =>
867+
Effect.gen(function* () {
868+
const { service, fs, statePath, commands, control } = yield* makeHarness("darwin");
869+
const path = yield* Path.Path;
870+
const plan = yield* service.install();
871+
const markerPath = path.join(path.dirname(statePath), SERVICE_RESTART_PENDING_FILE);
872+
expect(yield* fs.exists(markerPath)).toBe(false);
873+
control.failCommand = `launchctl bootstrap gui/501 ${plan.unitPath}`;
874+
875+
expect((yield* service.install().pipe(Effect.flip))._tag).toBe("BootServiceCommandError");
876+
877+
// A failed bootstrap leaves nothing else on disk to tell the difference
878+
// from a healthy install, so without the marker `t3 service install`
879+
// would call itself already installed and never retry.
880+
expect(yield* fs.readFileString(markerPath)).toBe("1.2.3\n");
881+
const failed = yield* service.status;
882+
expect(failed.problems).toEqual(["restart-pending"]);
883+
expect(failed.current).toBe(false);
884+
885+
// What `reconcileService` does with a status that is not current: install again.
886+
control.failCommand = undefined;
887+
commands.length = 0;
888+
yield* service.install();
889+
890+
expect(commands.filter((command) => command.startsWith("launchctl "))).toEqual([
891+
"launchctl bootout --wait gui/501/com.t3tools.t3code.service",
892+
"launchctl enable gui/501/com.t3tools.t3code.service",
893+
`launchctl bootstrap gui/501 ${plan.unitPath}`,
894+
]);
895+
expect(yield* fs.exists(markerPath)).toBe(false);
896+
expect((yield* service.status).current).toBe(true);
897+
}),
898+
);
899+
861900
it.effect("restarts without overwriting a pending remote update on macOS", () =>
862901
Effect.gen(function* () {
863902
const { service, fs, statePath, commands } = yield* makeHarness("darwin");

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,13 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
810810
yield* fs
811811
.makeDirectory(path.dirname(unitPath), { recursive: true })
812812
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
813-
if (!start && installed) {
814-
// Written first: once the files below name the new version, the running
815-
// service is behind them, and a failure between the two writes must not
816-
// leave it looking current.
817-
yield* fs.writeFileString(restartPendingPath, `${input.cliVersion}\n`, { mode: 0o600 });
818-
}
813+
// Written first, on every install: once the files below name the new
814+
// version, whatever is running is behind them. A started install removes
815+
// the marker after `activate`, and so do `restart` and the launcher when
816+
// it comes up on that version, so a failed start leaves `status`
817+
// reporting `restart-pending` and the next install repairs it instead
818+
// of calling itself current (#12197).
819+
yield* fs.writeFileString(restartPendingPath, `${input.cliVersion}\n`, { mode: 0o600 });
819820
yield* writeDurably(
820821
statePath,
821822
// @effect-diagnostics-next-line preferSchemaOverJson:off - fixed launcher-owned document.

0 commit comments

Comments
 (0)