Skip to content
TypeScript

fix(server): keep a remote server update running when the client disconnects (#18980)

macoa75ea468b13d
diff --git a/apps/server/src/cloud/selfUpdate.test.ts b/apps/server/src/cloud/selfUpdate.test.ts
index af202046ba5..3a852c7bc54 100644
--- a/apps/server/src/cloud/selfUpdate.test.ts
+++ b/apps/server/src/cloud/selfUpdate.test.ts
@@ -8,6 +8,7 @@ import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Path from "effect/Path";
+import * as Stream from "effect/Stream";
import { HttpClient, HttpClientResponse } from "effect/http";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
@@ -22,12 +23,13 @@ interface HarnessOptions {
readonly mode?: "web" | "desktop";
readonly managed?: boolean;
readonly preflight?: "ready" | "blocked";
+ readonly beforeDownload?: Effect.Effect<void>;
readonly requestUpdate?: ServiceLauncherClient.ServiceLauncherClient["Service"]["requestUpdate"];
readonly desktopAppUpdate?: DesktopAppUpdate.DesktopAppUpdate["Service"];
}
const archiveBytes = new TextEncoder().encode("not really a tarball");
-const releaseHttpClient = (order: string[]) =>
+const releaseHttpClient = (order: string[], beforeDownload?: Effect.Effect<void>) =>
HttpClient.make((request) =>
Effect.gen(function* () {
if (request.url.endsWith("/SHA256SUMS")) {
@@ -40,6 +42,7 @@ const releaseHttpClient = (order: string[]) =>
new Response(`${hex} t3-1.1.0-linux-x64.tar.gz\n`),
);
}
+ if (beforeDownload) yield* beforeDownload;
order.push("download");
return HttpClientResponse.fromWeb(request, new Response(archiveBytes));
}),
@@ -116,7 +119,7 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
run: () => Effect.die("unexpected desktop app update run"),
},
),
- Effect.provideService(HttpClient.HttpClient, releaseHttpClient(order)),
+ Effect.provideService(HttpClient.HttpClient, releaseHttpClient(order, options.beforeDownload)),
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.Architecture, "x64"),
Effect.provide(ServerConfig.layer({ ...config, mode: options.mode ?? "web" })),
@@ -418,4 +421,46 @@ it.layer(NodeServices.layer)("server self update", (it) => {
expect((yield* Fiber.join(first)).updateId).toBe("launcher-id");
}),
);
+
+ it.effect("keeps preparing after the progress stream closes", () =>
+ Effect.gen(function* () {
+ const releaseDownload = yield* Deferred.make<void>();
+ const sawDownloading = yield* Deferred.make<void>();
+ const accepted = yield* Deferred.make<void>();
+ const { selfUpdate, order } = yield* makeHarness({
+ beforeDownload: Deferred.await(releaseDownload),
+ requestUpdate: () =>
+ Effect.sync(() => {
+ order.push("accept");
+ }).pipe(Effect.andThen(Deferred.succeed(accepted, undefined)), Effect.as("launcher-id")),
+ });
+
+ const consumed = yield* ServerSelfUpdate.updateProgressStream(selfUpdate, {
+ targetVersion: "1.1.0",
+ }).pipe(
+ Stream.tap((event) =>
+ event.type === "progress" && event.stage === "downloading"
+ ? Deferred.succeed(sawDownloading, undefined)
+ : Effect.void,
+ ),
+ Stream.runDrain,
+ Effect.forkChild,
+ );
+
+ yield* Deferred.await(sawDownloading);
+ // The socket drop ends the stream. The download is still held.
+ yield* Fiber.interrupt(consumed);
+ expect((yield* selfUpdate.update({ targetVersion: "1.2.0" }).pipe(Effect.flip)).reason).toBe(
+ "A server update is already in progress.",
+ );
+
+ yield* Deferred.succeed(releaseDownload, undefined);
+ // `installing` is offered only after the stream queue has shut down.
+ yield* Deferred.await(accepted);
+ expect(order).toEqual(["download", "extract", "preflight", "accept"]);
+ expect((yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason).toBe(
+ "A server update is already in progress.",
+ );
+ }),
+ );
});
diff --git a/apps/server/src/cloud/selfUpdate.ts b/apps/server/src/cloud/selfUpdate.ts
index 89e6b2b8701..df683f814de 100644
--- a/apps/server/src/cloud/selfUpdate.ts
+++ b/apps/server/src/cloud/selfUpdate.ts
@@ -2,6 +2,7 @@ import {
ServerSelfUpdateError,
type ServerSelfUpdateCapability,
type ServerSelfUpdateInput,
+ type ServerSelfUpdateProgressEvent,
type ServerSelfUpdateProgressStage,
type ServerSelfUpdateResult,
type ThreadId,
@@ -18,6 +19,8 @@ import * as Ref from "effect/Ref";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
+import * as Queue from "effect/Queue";
+import * as Stream from "effect/Stream";
import { HttpClient } from "effect/http";
import { CLI_RELEASE_BASE_URL_ENV } from "@t3tools/shared/cliRelease";
@@ -339,3 +342,26 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
export const layer = Layer.effect(ServerSelfUpdate, make()).pipe(
Layer.provide(ProcessRunner.layer),
);
+
+// The RPC stream used to own this fiber, so a dropped client socket interrupted
+// the download. Offers after the queue shuts down are ignored; the update
+// still finishes, and the in-flight guard stays as it was.
+export const updateProgressStream = (
+ selfUpdate: ServerSelfUpdate["Service"],
+ input: ServerSelfUpdateInput,
+) =>
+ Stream.callback<ServerSelfUpdateProgressEvent, ServerSelfUpdateError>((queue) => {
+ const offer = (event: ServerSelfUpdateProgressEvent) =>
+ Queue.offer(queue, event).pipe(Effect.ignore);
+
+ return selfUpdate
+ .update(input, (stage) => offer({ type: "progress", stage }))
+ .pipe(
+ Effect.flatMap((result) => offer({ type: "complete", result })),
+ Effect.catchTags({
+ ServerSelfUpdateError: (error) => Queue.fail(queue, error).pipe(Effect.ignore),
+ }),
+ Effect.andThen(Queue.end(queue).pipe(Effect.ignore)),
+ Effect.forkDetach,
+ );
+ });
diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts
index 7e9181cb722..b886499bfdf 100644
--- a/apps/server/src/ws.ts
+++ b/apps/server/src/ws.ts
@@ -77,8 +77,6 @@ import {
ProviderSetupError,
RelayClientInstallFailedError,
type RelayClientInstallProgressEvent,
- type ServerSelfUpdateError,
- type ServerSelfUpdateProgressEvent,
type ServerConfig as ClientServerConfig,
type ServerConfigStreamEvent,
type ServerLifecycleStreamEvent,
@@ -2335,28 +2333,7 @@ const layerWsRpc = (
[WS_METHODS.providerInstallRemove]: (input) => providerInstallation.remove(input),
[WS_METHODS.serverUpdateServer]: (input) => serverSelfUpdate.update(input),
[WS_METHODS.serverUpdateServerWithProgress]: (input) =>
- Stream.callback<ServerSelfUpdateProgressEvent, ServerSelfUpdateError>((queue) =>
- serverSelfUpdate
- .update(input, (stage) =>
- Queue.offer(queue, {
- type: "progress",
- stage,
- }).pipe(Effect.asVoid),
- )
- .pipe(
- Effect.flatMap((result) =>
- Queue.offer(queue, {
- type: "complete",
- result,
- }),
- ),
- Effect.catchTags({
- ServerSelfUpdateError: (error) => Queue.fail(queue, error),
- }),
- Effect.andThen(Queue.end(queue)),
- Effect.forkScoped,
- ),
- ),
+ ServerSelfUpdate.updateProgressStream(serverSelfUpdate, input),
[WS_METHODS.serverCommitDesktopUpdate]: (input) =>
serverSelfUpdate.commitDesktopUpdate(input.requestId),
[WS_METHODS.serverUpsertKeybinding]: (rule) =>