Skip to content

Commit 1f6e9cc

Browse files
committed
fix(server): keep a remote server update running if the client disconnects
The boot-service download ran on the RPC stream fiber, so a dropped socket interrupted prepare and cleared the in-flight guard. Prepare now runs on the self-update service scope. A later call for the same version joins that work instead of starting over.
1 parent bbd98a5 commit 1f6e9cc

3 files changed

Lines changed: 226 additions & 109 deletions

File tree

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,26 @@ import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
1818
import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
1919
import * as ServerSelfUpdate from "./selfUpdate.ts";
2020

21+
interface DownloadGate {
22+
readonly hold: Deferred.Deferred<void>;
23+
readonly started: Deferred.Deferred<void>;
24+
readonly secondDownload: Deferred.Deferred<void>;
25+
}
26+
2127
interface HarnessOptions {
2228
readonly mode?: "web" | "desktop";
2329
readonly managed?: boolean;
2430
readonly preflight?: "ready" | "blocked";
2531
readonly requestUpdate?: ServiceLauncherClient.ServiceLauncherClient["Service"]["requestUpdate"];
2632
readonly desktopAppUpdate?: DesktopAppUpdate.DesktopAppUpdate["Service"];
33+
readonly downloadGate?: DownloadGate;
2734
}
2835

2936
// The staged runtime is a release archive: the fake client serves SHA256SUMS
3037
// and the tarball, and the fake runner stands in for tar before it answers
3138
// the staged preflight.
3239
const archiveBytes = new TextEncoder().encode("not really a tarball");
33-
const releaseHttpClient = (order: string[]) =>
40+
const releaseHttpClient = (order: string[], downloadGate?: DownloadGate) =>
3441
HttpClient.make((request) =>
3542
Effect.gen(function* () {
3643
if (request.url.endsWith("/SHA256SUMS")) {
@@ -44,6 +51,17 @@ const releaseHttpClient = (order: string[]) =>
4451
);
4552
}
4653
order.push("download");
54+
if (downloadGate !== undefined) {
55+
const downloadCount = order.filter((step) => step === "download").length;
56+
if (downloadCount > 1) {
57+
// A second archive fetch means the first attempt was cancelled.
58+
// Complete it so the test can observe that instead of hanging.
59+
yield* Deferred.succeed(downloadGate.secondDownload, undefined).pipe(Effect.ignore);
60+
} else {
61+
yield* Deferred.succeed(downloadGate.started, undefined);
62+
yield* Deferred.await(downloadGate.hold);
63+
}
64+
}
4765
return HttpClientResponse.fromWeb(request, new Response(archiveBytes));
4866
}),
4967
);
@@ -119,7 +137,7 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
119137
run: () => Effect.die("unexpected desktop app update run"),
120138
},
121139
),
122-
Effect.provideService(HttpClient.HttpClient, releaseHttpClient(order)),
140+
Effect.provideService(HttpClient.HttpClient, releaseHttpClient(order, options.downloadGate)),
123141
Effect.provideService(HostProcess.Platform, "linux"),
124142
Effect.provideService(HostProcess.Architecture, "x64"),
125143
Effect.provide(ServerConfig.layer({ ...config, mode: options.mode ?? "web" })),
@@ -422,4 +440,61 @@ it.layer(NodeServices.layer)("server self update", (it) => {
422440
expect((yield* Fiber.join(first)).updateId).toBe("launcher-id");
423441
}),
424442
);
443+
444+
it.effect("keeps preparing the runtime after the caller is interrupted", () =>
445+
Effect.gen(function* () {
446+
const downloadGate: DownloadGate = {
447+
hold: yield* Deferred.make<void>(),
448+
started: yield* Deferred.make<void>(),
449+
secondDownload: yield* Deferred.make<void>(),
450+
};
451+
const stages: string[] = [];
452+
const { selfUpdate, order } = yield* makeHarness({ downloadGate });
453+
const first = yield* selfUpdate
454+
.update({ targetVersion: "1.1.0" }, (stage) => Effect.sync(() => void stages.push(stage)))
455+
.pipe(Effect.forkChild({ startImmediately: true }));
456+
yield* Deferred.await(downloadGate.started);
457+
458+
yield* Fiber.interrupt(first);
459+
const interrupted = yield* Fiber.await(first);
460+
expect(interrupted._tag).toBe("Failure");
461+
if (interrupted._tag === "Failure") {
462+
expect(Cause.hasInterruptsOnly(interrupted.cause)).toBe(true);
463+
}
464+
465+
const reattached = yield* selfUpdate
466+
.update({ targetVersion: "1.1.0" })
467+
.pipe(Effect.forkChild({ startImmediately: true }));
468+
const conflicting = yield* Effect.race(
469+
selfUpdate.update({ targetVersion: "1.1.1" }).pipe(
470+
Effect.flip,
471+
Effect.map((error) => error.reason),
472+
),
473+
Deferred.await(downloadGate.secondDownload).pipe(Effect.as("second-download" as const)),
474+
);
475+
expect(conflicting).toBe("A server update is already in progress.");
476+
477+
yield* Deferred.succeed(downloadGate.hold, undefined);
478+
expect(yield* Fiber.join(reattached)).toEqual({
479+
targetVersion: "1.1.0",
480+
method: "boot-service",
481+
updateId: "launcher-id",
482+
});
483+
expect(order).toEqual(["download", "extract", "preflight", "accept"]);
484+
expect(stages).toEqual(["downloading", "installing"]);
485+
}),
486+
);
487+
488+
it.effect("starts another update after preparation fails", () =>
489+
Effect.gen(function* () {
490+
const { selfUpdate, order } = yield* makeHarness({ preflight: "blocked" });
491+
expect((yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason).toBe(
492+
"local update required",
493+
);
494+
expect((yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason).toBe(
495+
"local update required",
496+
);
497+
expect(order.filter((step) => step === "download")).toEqual(["download", "download"]);
498+
}),
499+
);
425500
});

0 commit comments

Comments
 (0)