Skip to content

Commit 1d31934

Browse files
rookmateclaude
andcommitted
fix(server): run an in-service install from a transient unit
Writing the state file without stopping the launcher first could replace a remote update it had just accepted, and the restart that followed would drop it. Hand the whole install to `systemd-run --user` instead, so it keeps the stop-first path that serialises with the launcher. restart keeps the queued restart: it writes no state. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent b4ef4e6 commit 1d31934

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -629,31 +629,31 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
629629
}),
630630
);
631631

632-
it.effect("updates from inside the service without stopping the process doing it", () =>
632+
it.effect("hands an install from inside the service to a transient unit", () =>
633633
Effect.gen(function* () {
634634
const { service, fs, statePath, commands, makeService } = yield* makeHarness();
635635
const path = yield* Path.Path;
636-
yield* service.install();
636+
const plan = yield* service.install();
637+
const unit = yield* fs.readFileString(plan.unitPath);
638+
const state = yield* fs.readFileString(statePath);
637639
commands.length = 0;
638640

641+
const baseDir = path.dirname(path.dirname(statePath));
639642
const newer = yield* makeService(undefined, "1.2.4", undefined, true);
640-
const plan = yield* newer.install();
643+
yield* newer.install({ allowDowngrade: true });
641644

642645
expect(
643646
commands.filter(
644-
(command) => command.startsWith("systemctl ") && !command.includes("show-environment"),
647+
(command) =>
648+
(command.startsWith("systemctl ") || command.startsWith("systemd-run ")) &&
649+
!command.includes("show-environment"),
645650
),
646651
).toEqual([
647-
"systemctl --user daemon-reload",
648-
"systemctl --user enable t3code.service",
649-
"systemctl --user restart --no-block t3code.service",
652+
`systemd-run --user --collect --quiet --setenv=T3_BOOT_SERVICE_UNIT= ${pinnedRuntimePaths(path, baseDir, "1.2.4", "linux").entryPath} service install --base-dir ${baseDir} --allow-downgrade`,
650653
]);
651-
expect(parseServiceState(yield* fs.readFileString(statePath))?.activeVersion).toBe("1.2.4");
652-
expect(yield* fs.readFileString(plan.unitPath)).toContain("versions/1.2.4/t3");
653-
// Until the launcher comes back on 1.2.4 and clears it.
654-
expect(
655-
yield* fs.exists(path.join(path.dirname(statePath), SERVICE_RESTART_PENDING_FILE)),
656-
).toBe(true);
654+
// The transient unit writes them after stopping the launcher.
655+
expect(yield* fs.readFileString(statePath)).toBe(state);
656+
expect(yield* fs.readFileString(plan.unitPath)).toBe(unit);
657657
}),
658658
);
659659

@@ -677,15 +677,15 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
677677
}),
678678
);
679679

680-
it.effect("leaves the service running when a handoff from inside it fails", () =>
680+
it.effect("leaves the service running when a restart handoff from inside it fails", () =>
681681
Effect.gen(function* () {
682682
const { service, commands, control, makeService } = yield* makeHarness();
683683
yield* service.install();
684684
commands.length = 0;
685685
control.failCommand = "systemctl --user daemon-reload";
686686

687-
const newer = yield* makeService(undefined, "1.2.4", undefined, true);
688-
const error = yield* newer.install().pipe(Effect.flip);
687+
const inside = yield* makeService(undefined, "1.2.3", undefined, true);
688+
const error = yield* inside.restart.pipe(Effect.flip);
689689
expect(error._tag).toBe("BootServiceCommandError");
690690
// Nothing was stopped, so there is nothing to bring back.
691691
expect(

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ export interface BootServiceManager {
229229
/** Best-effort recovery after a failed repair of an installed service. */
230230
readonly restart: ReadonlyArray<BootServiceStep>;
231231
/**
232-
* Replaces stop + activate when the caller runs inside the service, where a
233-
* stop would kill it before the start. The last entry queues the restart
234-
* with the service manager instead of waiting for it.
232+
* Replaces stop + activate for `restart` when the caller runs inside the
233+
* service, where a stop would kill it before the start. The last entry
234+
* queues the restart with the service manager instead of waiting for it.
235235
*/
236236
readonly handoff?: ReadonlyArray<BootServiceStep>;
237237
/** Uninstall, before the unit file is removed. */
@@ -854,13 +854,25 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
854854
// written, from the same read the downgrade check uses; the stop that
855855
// normally serialises against the launcher is skipped on purpose.
856856
const start = options?.start !== false;
857-
// From inside the service the files are written as for start=false, with
858-
// the same guard against a concurrent remote update, and the service
859-
// manager restarts onto them.
860-
const handoff = installed && start && runningInsideService ? manager.handoff : undefined;
861-
const deferred = installed && (!start || handoff !== undefined);
862-
const stopped = installed && start && handoff === undefined;
863-
if (stopped) {
857+
if (installed && start && runningInsideService && manager.kind === "systemd") {
858+
// Stopping the unit from inside it kills this process before the start.
859+
// A transient unit runs the same install from outside, stop first, so it
860+
// still serialises with a remote update the launcher may be starting.
861+
yield* runStep("handing the install to a transient unit", "systemd-run", [
862+
"--user",
863+
"--collect",
864+
"--quiet",
865+
`--setenv=${BOOT_SERVICE_UNIT_ENV}=`,
866+
runtimePaths.entryPath,
867+
"service",
868+
"install",
869+
"--base-dir",
870+
input.baseDir,
871+
...(options?.allowDowngrade === true ? ["--allow-downgrade"] : []),
872+
]);
873+
return plan;
874+
}
875+
if (installed && start) {
864876
yield* runSteps(manager.stop);
865877
}
866878

@@ -889,7 +901,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
889901
yield* fs
890902
.makeDirectory(path.dirname(unitPath), { recursive: true })
891903
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
892-
if (deferred) {
904+
if (!start && installed) {
893905
// Written first: once the files below name the new version, the
894906
// running service is behind them, and a failure between the two
895907
// writes must not leave it looking current. The launcher removes the
@@ -908,7 +920,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
908920
2,
909921
)}\n`,
910922
);
911-
if (deferred) {
923+
if (!start && installed) {
912924
// The launcher only writes this file while a remote update is in
913925
// flight. One that began after the check above lands either before
914926
// this write (then the launcher's copy in memory is what it keeps
@@ -922,10 +934,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
922934
}
923935
yield* writeDurably(unitPath, manager.render(plan));
924936

925-
if (handoff !== undefined) {
926-
// The launcher clears the restart marker once it runs this version.
927-
yield* runSteps(handoff);
928-
} else if (start) {
937+
if (start) {
929938
yield* runSteps(manager.activate);
930939
yield* fs.remove(restartPendingPath, { force: true });
931940
}
@@ -934,7 +943,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
934943
cause._tag === "PlatformError" ? new BootServiceInstallError({ cause }) : cause,
935944
),
936945
Effect.tapError(() =>
937-
stopped ? runSteps(manager.restart).pipe(Effect.ignore) : Effect.void,
946+
installed && start ? runSteps(manager.restart).pipe(Effect.ignore) : Effect.void,
938947
),
939948
);
940949
return plan;
@@ -951,6 +960,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
951960
) {
952961
return false;
953962
}
963+
// Writes no state, and the launcher lets an update transition in flight
964+
// finish before the queued stop tears it down.
954965
if (runningInsideService && manager.handoff !== undefined) {
955966
yield* runSteps(manager.handoff);
956967
return true;

0 commit comments

Comments
 (0)