Skip to content

Commit e87c98f

Browse files
committed
fix(updates): preserve continuation through restart handoff
1 parent 657bce1 commit e87c98f

6 files changed

Lines changed: 263 additions & 91 deletions

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,74 @@ it.layer(NodeServices.layer)("server self update", (it) => {
209209
}),
210210
);
211211

212+
it.effect("keeps continuation markers after the boot-service handoff is accepted", () =>
213+
Effect.gen(function* () {
214+
const events: string[] = [];
215+
const selfUpdate = yield* ServerSelfUpdate.withRunningThreadContinuation({
216+
mode: "web",
217+
selfUpdate: {
218+
update: (
219+
_input,
220+
reportProgress = () => Effect.void,
221+
onHandoffAccepted = () => Effect.void,
222+
) =>
223+
reportProgress("installing").pipe(
224+
Effect.andThen(onHandoffAccepted()),
225+
Effect.andThen(Effect.interrupt),
226+
),
227+
commitDesktopUpdate: () => Effect.never,
228+
},
229+
prepare: Effect.sync(() => {
230+
events.push("prepare");
231+
return [ThreadId.make("thread-accepted-boot-handoff")];
232+
}),
233+
clear: () => Effect.sync(() => void events.push("clear")),
234+
});
235+
236+
const exit = yield* selfUpdate
237+
.update({ targetVersion: "1.1.0", continueRunningThreads: true })
238+
.pipe(Effect.exit);
239+
240+
expect(exit._tag).toBe("Failure");
241+
expect(events).toEqual(["prepare"]);
242+
}),
243+
);
244+
245+
it.effect("keeps continuation markers after the desktop handoff is accepted", () =>
246+
Effect.gen(function* () {
247+
const events: string[] = [];
248+
const selfUpdate = yield* ServerSelfUpdate.withRunningThreadContinuation({
249+
mode: "desktop",
250+
selfUpdate: {
251+
update: () =>
252+
Effect.succeed({
253+
targetVersion: "1.2.0",
254+
method: "desktop-app" as const,
255+
desktopUpdateToken: "accepted-desktop-token",
256+
}),
257+
commitDesktopUpdate: (_requestId, onHandoffAccepted = () => Effect.void) =>
258+
onHandoffAccepted().pipe(Effect.andThen(Effect.interrupt)),
259+
},
260+
prepare: Effect.sync(() => {
261+
events.push("prepare");
262+
return [ThreadId.make("thread-accepted-desktop-handoff")];
263+
}),
264+
clear: () => Effect.sync(() => void events.push("clear")),
265+
});
266+
267+
yield* selfUpdate.update({
268+
targetVersion: "1.2.0",
269+
continueRunningThreads: true,
270+
});
271+
const exit = yield* selfUpdate
272+
.commitDesktopUpdate("accepted-desktop-token")
273+
.pipe(Effect.exit);
274+
275+
expect(exit._tag).toBe("Failure");
276+
expect(events).toEqual(["prepare"]);
277+
}),
278+
);
279+
212280
it.effect("stages and preflights before asking the launcher for an update ID", () =>
213281
Effect.gen(function* () {
214282
const { selfUpdate, order } = yield* makeHarness();

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

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ export class ServerSelfUpdate extends Context.Service<
4646
reportProgress?: (
4747
stage: ServerSelfUpdateProgressStage,
4848
) => Effect.Effect<void, ServerSelfUpdateError>,
49+
onHandoffAccepted?: () => Effect.Effect<void>,
4950
) => Effect.Effect<ServerSelfUpdateResult, ServerSelfUpdateError>;
5051
readonly commitDesktopUpdate: (
5152
requestId: string,
53+
onHandoffAccepted?: () => Effect.Effect<void>,
5254
) => Effect.Effect<never, ServerSelfUpdateError>;
5355
}
5456
>()("t3/cloud/selfUpdate/ServerSelfUpdate") {}
@@ -67,10 +69,13 @@ export const withRunningThreadContinuation = Effect.fn(
6769
const clearOnError = <A>(
6870
effect: Effect.Effect<A, ServerSelfUpdateError>,
6971
threadIds: () => ReadonlyArray<ThreadId>,
72+
shouldClear: () => boolean,
7073
): Effect.Effect<A, ServerSelfUpdateError> =>
7174
effect.pipe(
7275
Effect.catchCause((cause) =>
73-
input.clear(threadIds()).pipe(Effect.andThen(Effect.failCause(cause))),
76+
(shouldClear() ? input.clear(threadIds()) : Effect.void).pipe(
77+
Effect.andThen(Effect.failCause(cause)),
78+
),
7479
),
7580
);
7681

@@ -79,25 +84,32 @@ export const withRunningThreadContinuation = Effect.fn(
7984
reportProgress = () => Effect.void,
8085
) => {
8186
let prepared = false;
87+
let handoffAccepted = false;
8288
let continuationThreadIds: ReadonlyArray<ThreadId> = [];
8389
return clearOnError(
8490
input.selfUpdate
85-
.update(request, (stage) =>
86-
(request.continueRunningThreads === true &&
87-
input.mode !== "desktop" &&
88-
stage === "installing" &&
89-
!prepared
90-
? input.prepare.pipe(
91-
Effect.tap((threadIds) =>
92-
Effect.sync(() => {
93-
prepared = true;
94-
continuationThreadIds = threadIds;
95-
}),
96-
),
97-
Effect.asVoid,
98-
)
99-
: Effect.void
100-
).pipe(Effect.andThen(reportProgress(stage))),
91+
.update(
92+
request,
93+
(stage) =>
94+
(request.continueRunningThreads === true &&
95+
input.mode !== "desktop" &&
96+
stage === "installing" &&
97+
!prepared
98+
? input.prepare.pipe(
99+
Effect.tap((threadIds) =>
100+
Effect.sync(() => {
101+
prepared = true;
102+
continuationThreadIds = threadIds;
103+
}),
104+
),
105+
Effect.asVoid,
106+
)
107+
: Effect.void
108+
).pipe(Effect.andThen(reportProgress(stage))),
109+
() =>
110+
Effect.sync(() => {
111+
handoffAccepted = true;
112+
}),
101113
)
102114
.pipe(
103115
Effect.tap((result) => {
@@ -112,6 +124,7 @@ export const withRunningThreadContinuation = Effect.fn(
112124
}),
113125
),
114126
() => continuationThreadIds,
127+
() => !handoffAccepted,
115128
);
116129
};
117130

@@ -123,16 +136,22 @@ export const withRunningThreadContinuation = Effect.fn(
123136
HashSet.has(tokens, requestId),
124137
HashSet.remove(tokens, requestId),
125138
]);
139+
let handoffAccepted = false;
126140
let continuationThreadIds: ReadonlyArray<ThreadId> = [];
127141
return yield* clearOnError(
128142
Effect.gen(function* () {
129143
continuationThreadIds = shouldContinue ? yield* input.prepare : [];
130-
return yield* input.selfUpdate.commitDesktopUpdate(requestId);
144+
return yield* input.selfUpdate.commitDesktopUpdate(requestId, () =>
145+
Effect.sync(() => {
146+
handoffAccepted = true;
147+
}),
148+
);
131149
}),
132150
() => continuationThreadIds,
151+
() => !handoffAccepted,
133152
).pipe(
134153
Effect.catchCause((cause) =>
135-
(shouldContinue
154+
(shouldContinue && !handoffAccepted
136155
? Ref.update(desktopContinuationTokens, HashSet.add(requestId))
137156
: Effect.void
138157
).pipe(Effect.andThen(Effect.failCause(cause))),
@@ -161,7 +180,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
161180

162181
const update: ServerSelfUpdate["Service"]["update"] = Effect.fn(
163182
"cloud.server_self_update.update",
164-
)(function* (input, reportProgress = () => Effect.void) {
183+
)(function* (input, reportProgress = () => Effect.void, onHandoffAccepted = () => Effect.void) {
165184
if (capability === "desktop-managed") {
166185
// input.targetVersion is meaningless here: the desktop app's own
167186
// update feed decides what it downloads, and the result carries what
@@ -273,9 +292,8 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
273292
);
274293

275294
yield* reportProgress("installing");
276-
const updateId = yield* launcher
277-
.requestUpdate({ targetVersion, dbPath: serverConfig.dbPath })
278-
.pipe(
295+
const updateId = yield* Effect.uninterruptible(
296+
launcher.requestUpdate({ targetVersion, dbPath: serverConfig.dbPath }).pipe(
279297
Effect.mapError((error) =>
280298
failWith(
281299
error._tag === "ServiceLauncherRejectedError"
@@ -284,7 +302,9 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
284302
error,
285303
),
286304
),
287-
);
305+
Effect.tap(() => onHandoffAccepted()),
306+
),
307+
);
288308

289309
yield* Effect.logInfo("Server update prepared; handing off to the service launcher.", {
290310
updateId,
@@ -297,7 +317,8 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
297317

298318
return ServerSelfUpdate.of({
299319
update,
300-
commitDesktopUpdate: (requestId) => desktopAppUpdate.commit(requestId),
320+
commitDesktopUpdate: (requestId, onHandoffAccepted) =>
321+
desktopAppUpdate.commit(requestId, onHandoffAccepted),
301322
});
302323
});
303324

‎apps/server/src/desktopUpdate/DesktopAppUpdate.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export class DesktopAppUpdate extends Context.Service<
5555
) => Effect.Effect<ServerSelfUpdateResult, ServerSelfUpdateError>;
5656
/** Starts the prepared install. Success stops this server, so this effect
5757
returns only when installation fails or times out. */
58-
readonly commit: (requestId: string) => Effect.Effect<never, ServerSelfUpdateError>;
58+
readonly commit: (
59+
requestId: string,
60+
onHandoffAccepted?: () => Effect.Effect<void>,
61+
) => Effect.Effect<never, ServerSelfUpdateError>;
5962
}
6063
>()("t3/desktopUpdate/DesktopAppUpdate") {}
6164

@@ -184,7 +187,7 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* (
184187

185188
const commit: DesktopAppUpdate["Service"]["commit"] = Effect.fn(
186189
"desktopUpdate.desktopAppUpdate.commit",
187-
)(function* (requestId) {
190+
)(function* (requestId, onHandoffAccepted = () => Effect.void) {
188191
if (!available) {
189192
return yield* failWith("This server cannot commit a desktop app update.");
190193
}
@@ -195,11 +198,12 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* (
195198
onNone: () => changes,
196199
onSome: (report) => Stream.concat(Stream.make(report), changes),
197200
});
198-
yield* receiver
199-
.commitDesktopUpdate(requestId)
200-
.pipe(
201+
yield* Effect.uninterruptible(
202+
receiver.commitDesktopUpdate(requestId).pipe(
201203
Effect.mapError((error) => failWith("Could not reach the T3 Code desktop app.", error)),
202-
);
204+
Effect.tap(() => onHandoffAccepted()),
205+
),
206+
);
203207
return yield* reports.pipe(
204208
Stream.filter((report) => report.requestId === requestId && report.outcome === "failed"),
205209
Stream.runHead,

0 commit comments

Comments
 (0)