Skip to content

Commit 7d86b8c

Browse files
committed
fix(server): keep a remote server update running if the client disconnects
The progress RPC forked the update into the WebSocket stream scope, so a dropped socket interrupted the boot-service download and cleared the in-flight guard. The update now runs detached from that scope, and progress offers after the stream closes are ignored. Fixes #18944 Co-authored-by: maco <[email protected]>
1 parent 6524a8d commit 7d86b8c

3 files changed

Lines changed: 74 additions & 26 deletions

File tree

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Effect from "effect/Effect";
88
import * as FileSystem from "effect/FileSystem";
99
import * as Fiber from "effect/Fiber";
1010
import * as Path from "effect/Path";
11+
import * as Stream from "effect/Stream";
1112
import { HttpClient, HttpClientResponse } from "effect/http";
1213
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1314

@@ -22,6 +23,7 @@ interface HarnessOptions {
2223
readonly mode?: "web" | "desktop";
2324
readonly managed?: boolean;
2425
readonly preflight?: "ready" | "blocked";
26+
readonly beforeDownload?: Effect.Effect<void>;
2527
readonly requestUpdate?: ServiceLauncherClient.ServiceLauncherClient["Service"]["requestUpdate"];
2628
readonly desktopAppUpdate?: DesktopAppUpdate.DesktopAppUpdate["Service"];
2729
}
@@ -30,7 +32,7 @@ interface HarnessOptions {
3032
// and the tarball, and the fake runner stands in for tar before it answers
3133
// the staged preflight.
3234
const archiveBytes = new TextEncoder().encode("not really a tarball");
33-
const releaseHttpClient = (order: string[]) =>
35+
const releaseHttpClient = (order: string[], beforeDownload?: Effect.Effect<void>) =>
3436
HttpClient.make((request) =>
3537
Effect.gen(function* () {
3638
if (request.url.endsWith("/SHA256SUMS")) {
@@ -43,6 +45,7 @@ const releaseHttpClient = (order: string[]) =>
4345
new Response(`${hex} t3-1.1.0-linux-x64.tar.gz\n`),
4446
);
4547
}
48+
if (beforeDownload) yield* beforeDownload;
4649
order.push("download");
4750
return HttpClientResponse.fromWeb(request, new Response(archiveBytes));
4851
}),
@@ -119,7 +122,7 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
119122
run: () => Effect.die("unexpected desktop app update run"),
120123
},
121124
),
122-
Effect.provideService(HttpClient.HttpClient, releaseHttpClient(order)),
125+
Effect.provideService(HttpClient.HttpClient, releaseHttpClient(order, options.beforeDownload)),
123126
Effect.provideService(HostProcess.Platform, "linux"),
124127
Effect.provideService(HostProcess.Architecture, "x64"),
125128
Effect.provide(ServerConfig.layer({ ...config, mode: options.mode ?? "web" })),
@@ -422,4 +425,46 @@ it.layer(NodeServices.layer)("server self update", (it) => {
422425
expect((yield* Fiber.join(first)).updateId).toBe("launcher-id");
423426
}),
424427
);
428+
429+
it.effect("keeps preparing after the progress stream closes", () =>
430+
Effect.gen(function* () {
431+
const releaseDownload = yield* Deferred.make<void>();
432+
const sawDownloading = yield* Deferred.make<void>();
433+
const accepted = yield* Deferred.make<void>();
434+
const { selfUpdate, order } = yield* makeHarness({
435+
beforeDownload: Deferred.await(releaseDownload),
436+
requestUpdate: () =>
437+
Effect.sync(() => {
438+
order.push("accept");
439+
}).pipe(Effect.andThen(Deferred.succeed(accepted, undefined)), Effect.as("launcher-id")),
440+
});
441+
442+
const consumed = yield* ServerSelfUpdate.updateProgressStream(selfUpdate, {
443+
targetVersion: "1.1.0",
444+
}).pipe(
445+
Stream.tap((event) =>
446+
event.type === "progress" && event.stage === "downloading"
447+
? Deferred.succeed(sawDownloading, undefined)
448+
: Effect.void,
449+
),
450+
Stream.runDrain,
451+
Effect.forkChild,
452+
);
453+
454+
yield* Deferred.await(sawDownloading);
455+
// The socket drop ends the stream. The download is still held.
456+
yield* Fiber.interrupt(consumed);
457+
expect((yield* selfUpdate.update({ targetVersion: "1.2.0" }).pipe(Effect.flip)).reason).toBe(
458+
"A server update is already in progress.",
459+
);
460+
461+
yield* Deferred.succeed(releaseDownload, undefined);
462+
// `installing` is offered only after the stream queue has shut down.
463+
yield* Deferred.await(accepted);
464+
expect(order).toEqual(["download", "extract", "preflight", "accept"]);
465+
expect((yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason).toBe(
466+
"A server update is already in progress.",
467+
);
468+
}),
469+
);
425470
});

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ServerSelfUpdateError,
33
type ServerSelfUpdateCapability,
44
type ServerSelfUpdateInput,
5+
type ServerSelfUpdateProgressEvent,
56
type ServerSelfUpdateProgressStage,
67
type ServerSelfUpdateResult,
78
type ThreadId,
@@ -18,6 +19,8 @@ import * as Ref from "effect/Ref";
1819
import * as FileSystem from "effect/FileSystem";
1920
import * as Layer from "effect/Layer";
2021
import * as Path from "effect/Path";
22+
import * as Queue from "effect/Queue";
23+
import * as Stream from "effect/Stream";
2124
import { HttpClient } from "effect/http";
2225

2326
import { CLI_RELEASE_BASE_URL_ENV } from "@t3tools/shared/cliRelease";
@@ -344,3 +347,26 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
344347
export const layer = Layer.effect(ServerSelfUpdate, make()).pipe(
345348
Layer.provide(ProcessRunner.layer),
346349
);
350+
351+
// The RPC stream used to own this fiber, so a dropped client socket interrupted
352+
// the download. Offers after the queue shuts down are ignored; the update
353+
// still finishes, and the in-flight guard stays as it was.
354+
export const updateProgressStream = (
355+
selfUpdate: ServerSelfUpdate["Service"],
356+
input: ServerSelfUpdateInput,
357+
) =>
358+
Stream.callback<ServerSelfUpdateProgressEvent, ServerSelfUpdateError>((queue) => {
359+
const offer = (event: ServerSelfUpdateProgressEvent) =>
360+
Queue.offer(queue, event).pipe(Effect.ignore);
361+
362+
return selfUpdate
363+
.update(input, (stage) => offer({ type: "progress", stage }))
364+
.pipe(
365+
Effect.flatMap((result) => offer({ type: "complete", result })),
366+
Effect.catchTags({
367+
ServerSelfUpdateError: (error) => Queue.fail(queue, error).pipe(Effect.ignore),
368+
}),
369+
Effect.andThen(Queue.end(queue).pipe(Effect.ignore)),
370+
Effect.forkDetach,
371+
);
372+
});

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ import {
7777
ProviderSetupError,
7878
RelayClientInstallFailedError,
7979
type RelayClientInstallProgressEvent,
80-
type ServerSelfUpdateError,
81-
type ServerSelfUpdateProgressEvent,
8280
type ServerConfig as ClientServerConfig,
8381
type ServerConfigStreamEvent,
8482
type ServerLifecycleStreamEvent,
@@ -2378,28 +2376,7 @@ const layerWsRpc = (
23782376
[WS_METHODS.providerInstallRemove]: (input) => providerInstallation.remove(input),
23792377
[WS_METHODS.serverUpdateServer]: (input) => serverSelfUpdate.update(input),
23802378
[WS_METHODS.serverUpdateServerWithProgress]: (input) =>
2381-
Stream.callback<ServerSelfUpdateProgressEvent, ServerSelfUpdateError>((queue) =>
2382-
serverSelfUpdate
2383-
.update(input, (stage) =>
2384-
Queue.offer(queue, {
2385-
type: "progress",
2386-
stage,
2387-
}).pipe(Effect.asVoid),
2388-
)
2389-
.pipe(
2390-
Effect.flatMap((result) =>
2391-
Queue.offer(queue, {
2392-
type: "complete",
2393-
result,
2394-
}),
2395-
),
2396-
Effect.catchTags({
2397-
ServerSelfUpdateError: (error) => Queue.fail(queue, error),
2398-
}),
2399-
Effect.andThen(Queue.end(queue)),
2400-
Effect.forkScoped,
2401-
),
2402-
),
2379+
ServerSelfUpdate.updateProgressStream(serverSelfUpdate, input),
24032380
[WS_METHODS.serverCommitDesktopUpdate]: (input) =>
24042381
serverSelfUpdate.commitDesktopUpdate(input.requestId),
24052382
[WS_METHODS.serverUpsertKeybinding]: (rule) =>

0 commit comments

Comments
 (0)