Skip to content

Commit 909621b

Browse files
georgenijoclaude
andcommitted
fix(web): a server that rolls back after an app update no longer shows restarting for 32 minutes
After Update everything hands a launcher-managed server off and the desktop app relaunches, the restored follower settled only when the server reported the target version. A rollback restarts the previous version, so the row kept showing "Restarting" until the 32-minute deadline. Keep the launcher updateId with the handed-off server and settle with this update's rollback or failure reason when the lifecycle ready payload reports its outcome. Outcomes for other update ids are ignored. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ade4730 commit 909621b

3 files changed

Lines changed: 102 additions & 5 deletions

File tree

‎apps/web/src/components/sidebar/updateEverythingRun.test.ts‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { EnvironmentId } from "@t3tools/contracts";
1+
import { EnvironmentId, type ServerSelfUpdateOutcome } from "@t3tools/contracts";
2+
import { AsyncResult, Atom } from "effect/reactivity";
23
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
34

45
import { appAtomRegistry } from "~/rpc/atomRegistry";
56
import { serverEnvironment } from "~/state/server";
7+
import { toastManager } from "../ui/toast";
68
import { startDesktopUpdateLaunchNotice } from "./updateEverythingRun";
79

8-
function relaunchWhileRestarting(environmentId: EnvironmentId) {
10+
function relaunchWhileRestarting(environmentId: EnvironmentId, updateId?: string) {
911
const now = 1_800_000_000_000;
1012
vi.useFakeTimers({ now });
1113
const store = new Map([
@@ -17,7 +19,15 @@ function relaunchWhileRestarting(environmentId: EnvironmentId) {
1719
updatedServers: 0,
1820
updatedProviders: 0,
1921
failures: [],
20-
handedOff: [{ environmentId, label: "Slow", targetVersion: "9.9.9", startedAt: now }],
22+
handedOff: [
23+
{
24+
environmentId,
25+
label: "Slow",
26+
targetVersion: "9.9.9",
27+
startedAt: now,
28+
...(updateId === undefined ? {} : { updateId }),
29+
},
30+
],
2131
}),
2232
],
2333
]);
@@ -67,4 +77,42 @@ describe("startDesktopUpdateLaunchNotice", () => {
6777
stop();
6878
}
6979
});
80+
81+
it("reports this update's rollback instead of restarting until the timeout", async () => {
82+
const environmentId = EnvironmentId.make("rolled-back-machine");
83+
const stateAtom = serverEnvironment.updateStateAtom(environmentId);
84+
const outcomeAtom = Atom.make<AsyncResult.AsyncResult<ServerSelfUpdateOutcome>>(
85+
AsyncResult.initial(),
86+
);
87+
using _outcome = vi.spyOn(serverEnvironment, "updateOutcome").mockReturnValue(outcomeAtom);
88+
using addToast = vi.spyOn(toastManager, "add");
89+
const stop = relaunchWhileRestarting(environmentId, "update-2");
90+
const release = appAtomRegistry.mount(stateAtom);
91+
const outcome = (id: string, reason: string) =>
92+
AsyncResult.success<ServerSelfUpdateOutcome>({
93+
id,
94+
fromVersion: "9.9.8",
95+
targetVersion: "9.9.9",
96+
status: "rolled-back",
97+
reason,
98+
});
99+
try {
100+
appAtomRegistry.set(outcomeAtom, outcome("update-1", "an earlier attempt"));
101+
expect(appAtomRegistry.get(stateAtom)).toMatchObject({ status: "running" });
102+
103+
appAtomRegistry.set(outcomeAtom, outcome("update-2", "candidate-exited:78"));
104+
expect(appAtomRegistry.get(stateAtom)).toEqual({ status: "idle" });
105+
expect(addToast).toHaveBeenCalledWith(
106+
expect.objectContaining({
107+
type: "error",
108+
description: expect.objectContaining({
109+
props: expect.objectContaining({ children: "Slow: candidate-exited:78" }),
110+
}),
111+
}),
112+
);
113+
} finally {
114+
release();
115+
stop();
116+
}
117+
});
70118
});

‎apps/web/src/components/sidebar/updateEverythingRun.tsx‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { useAtomValue } from "@effect/atom-react";
2-
import { serverUpdateStateAtom } from "@t3tools/client-runtime/state/server";
2+
import {
3+
ServerUpdateTerminalError,
4+
serverUpdateStateAtom,
5+
} from "@t3tools/client-runtime/state/server";
36
import { EnvironmentId } from "@t3tools/contracts";
47
import * as Option from "effect/Option";
58
import * as Schema from "effect/Schema";
@@ -163,6 +166,7 @@ const HandedOffServer = Schema.Struct({
163166
label: Schema.String,
164167
targetVersion: Schema.String,
165168
startedAt: Schema.Number,
169+
updateId: Schema.optionalKey(Schema.String),
166170
});
167171
type HandedOffServer = typeof HandedOffServer.Type;
168172

@@ -186,12 +190,14 @@ export function saveUpdateEverythingResult(result: UpdateEverythingResult): void
186190
machine.handedOffVersion ??
187191
(state.status === "running" ? state.targetVersion : machine.server?.targetVersion);
188192
if (targetVersion === undefined) return [];
193+
const updateId = state.status === "running" ? state.updateId : undefined;
189194
return [
190195
{
191196
environmentId: machine.environmentId,
192197
label: machine.label,
193198
targetVersion,
194199
startedAt: machine.startedAt,
200+
...(updateId === undefined ? {} : { updateId }),
195201
},
196202
];
197203
});
@@ -285,7 +291,10 @@ function followHandedOffServers(servers: ReadonlyArray<HandedOffServer>): void {
285291
if (settled) return;
286292
settled = true;
287293
clearTimeout(timer);
288-
queueMicrotask(() => unsubscribe());
294+
queueMicrotask(() => {
295+
unsubscribe();
296+
unsubscribeOutcome();
297+
});
289298
appAtomRegistry.set(stateAtom, { status: "idle" });
290299
releaseState();
291300
settleUpdateRunMachine(server.environmentId, failure);
@@ -318,6 +327,31 @@ function followHandedOffServers(servers: ReadonlyArray<HandedOffServer>): void {
318327
},
319328
{ immediate: true },
320329
);
330+
// A rollback restarts the old version, which the version check above never accepts.
331+
const unsubscribeOutcome =
332+
server.updateId === undefined
333+
? () => {}
334+
: appAtomRegistry.subscribe(
335+
serverEnvironment.updateOutcome({ environmentId: server.environmentId, input: {} }),
336+
(result) => {
337+
const outcome = AsyncResult.getOrElse(result, () => null);
338+
if (
339+
outcome === null ||
340+
outcome.id !== server.updateId ||
341+
outcome.status === "committed"
342+
) {
343+
return;
344+
}
345+
settle(
346+
new ServerUpdateTerminalError({
347+
targetVersion: server.targetVersion,
348+
status: outcome.status,
349+
...(outcome.reason === undefined ? {} : { reason: outcome.reason }),
350+
}).message,
351+
);
352+
},
353+
{ immediate: true },
354+
);
321355
}
322356
}
323357

‎packages/client-runtime/src/state/server.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export type ServerUpdateState =
7171
readonly stage: ServerUpdateStage;
7272
readonly fromVersion: string;
7373
readonly targetVersion: string;
74+
/** The service launcher's id for an accepted update, used to find its outcome later. */
75+
readonly updateId?: string;
7476
}
7577
| {
7678
readonly status: "failed";
@@ -982,6 +984,7 @@ export function createServerEnvironmentAtoms<R, E>(
982984
stage: currentStage,
983985
fromVersion,
984986
targetVersion,
987+
...(updateResult.updateId === undefined ? {} : { updateId: updateResult.updateId }),
985988
});
986989
return updateResult;
987990
}),
@@ -1381,6 +1384,18 @@ export function createServerEnvironmentAtoms<R, E>(
13811384
}),
13821385
configProjection,
13831386
welcome,
1387+
updateOutcome: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
1388+
label: "environment-data:server:update-outcome",
1389+
tag: WS_METHODS.subscribeServerLifecycle,
1390+
transform: (stream) =>
1391+
stream.pipe(
1392+
Stream.filterMap((event) =>
1393+
event.type === "ready" && event.payload.updateOutcome !== undefined
1394+
? Result.succeed(event.payload.updateOutcome)
1395+
: Result.failVoid,
1396+
),
1397+
),
1398+
}),
13841399
legacyThreadMigration: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
13851400
label: "environment-data:server:legacy-thread-migration",
13861401
tag: WS_METHODS.subscribeServerLifecycle,

0 commit comments

Comments
 (0)