Skip to content

Commit 7f40bda

Browse files
committed
fix(desktop): keep windows if update install fails
Install stopped backends, destroyed every window, then called quitAndInstall. If that last step failed, quitting was cleared and an error was stored, but the windows were already gone. There was no path to open one again. Call quitAndInstall first. Destroy windows only after that starts.
1 parent f6f2be3 commit 7f40bda

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎apps/desktop/src/updates/DesktopUpdates.test.ts‎

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface UpdatesHarnessOptions {
3131
readonly setUpdateChannelError?: DesktopAppSettings.DesktopSettingsWriteError;
3232
readonly setDisableDifferentialDownload?: Effect.Effect<void>;
3333
readonly stopBackend?: Effect.Effect<void>;
34+
readonly quitAndInstall?: Effect.Effect<void, ElectronUpdater.ElectronUpdaterQuitAndInstallError>;
3435
readonly env?: Record<string, string | undefined>;
3536
}
3637

@@ -43,6 +44,7 @@ function makeHarness(options: UpdatesHarnessOptions = {}) {
4344
const feedUrls: ElectronUpdater.ElectronUpdaterFeedUrl[] = [];
4445
const listeners = new Map<string, Set<(...args: readonly unknown[]) => void>>();
4546
const sentStates: DesktopUpdateState[] = [];
47+
const installSteps: string[] = [];
4648

4749
const addListener = (eventName: string, listener: (...args: readonly unknown[]) => void) => {
4850
const eventListeners = listeners.get(eventName) ?? new Set();
@@ -84,7 +86,10 @@ function makeHarness(options: UpdatesHarnessOptions = {}) {
8486
checkCount += 1;
8587
}).pipe(Effect.andThen(options.checkForUpdates ?? Effect.void)),
8688
downloadUpdate: Effect.void,
87-
quitAndInstall: () => Effect.void,
89+
quitAndInstall: () =>
90+
Effect.sync(() => {
91+
installSteps.push("quitAndInstall");
92+
}).pipe(Effect.andThen(options.quitAndInstall ?? Effect.void)),
8893
on: (eventName, listener) =>
8994
Effect.acquireRelease(
9095
Effect.sync(() => {
@@ -109,7 +114,9 @@ function makeHarness(options: UpdatesHarnessOptions = {}) {
109114
Effect.sync(() => {
110115
sentStates.push(state as DesktopUpdateState);
111116
}),
112-
destroyAll: Effect.void,
117+
destroyAll: Effect.sync(() => {
118+
installSteps.push("destroyAll");
119+
}),
113120
syncAllAppearance: () => Effect.void,
114121
} satisfies ElectronWindow.ElectronWindow["Service"]);
115122

@@ -219,6 +226,7 @@ function makeHarness(options: UpdatesHarnessOptions = {}) {
219226
0,
220227
),
221228
sentStates,
229+
installSteps,
222230
emit: (eventName: string, payload?: unknown) => {
223231
for (const listener of listeners.get(eventName) ?? []) {
224232
listener(payload);
@@ -725,6 +733,56 @@ describe("DesktopUpdates", () => {
725733
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
726734
});
727735

736+
it.effect("destroys windows only after quitAndInstall starts", () => {
737+
const harness = makeHarness();
738+
739+
return Effect.scoped(
740+
Effect.gen(function* () {
741+
const updates = yield* DesktopUpdates.DesktopUpdates;
742+
yield* updates.configure;
743+
harness.emit("update-downloaded", { version: "1.2.4" });
744+
yield* flushCallbacks;
745+
746+
const result = yield* updates.install;
747+
assert.isTrue(result.accepted);
748+
assert.deepEqual(harness.installSteps, ["quitAndInstall", "destroyAll"]);
749+
}),
750+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
751+
});
752+
753+
it.effect("keeps windows when quitAndInstall fails", () => {
754+
const harness = makeHarness({
755+
quitAndInstall: Effect.fail(
756+
new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
757+
channel: null,
758+
isSilent: true,
759+
isForceRunAfter: true,
760+
cause: new Error("installer refused"),
761+
}),
762+
),
763+
});
764+
765+
return Effect.scoped(
766+
Effect.gen(function* () {
767+
const desktopState = yield* DesktopState.DesktopState;
768+
const updates = yield* DesktopUpdates.DesktopUpdates;
769+
yield* updates.configure;
770+
harness.emit("update-downloaded", { version: "1.2.4" });
771+
yield* flushCallbacks;
772+
773+
const result = yield* updates.install;
774+
assert.isTrue(result.accepted);
775+
assert.isFalse(result.completed);
776+
assert.isFalse(yield* Ref.get(desktopState.quitting));
777+
assert.deepEqual(harness.installSteps, ["quitAndInstall"]);
778+
779+
const failedState = yield* updates.getState;
780+
assert.equal(failedState.status, "downloaded");
781+
assert.equal(failedState.errorContext, "install");
782+
}),
783+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
784+
});
785+
728786
it.effect("persists channel changes through the settings service", () => {
729787
const harness = makeHarness();
730788

‎apps/desktop/src/updates/DesktopUpdates.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,13 @@ export const make = Effect.gen(function* () {
497497
(instance) => instance.stop({ timeout: Duration.seconds(5) }),
498498
{ concurrency: "unbounded" },
499499
);
500-
yield* electronWindow.destroyAll;
501500
yield* electronUpdater.quitAndInstall({
502501
isSilent: true,
503502
isForceRunAfter: true,
504503
});
504+
// Close windows only after quitAndInstall has started. If install
505+
// fails, the user still has a window.
506+
yield* electronWindow.destroyAll;
505507
return { accepted: true, completed: false };
506508
}).pipe(
507509
Effect.catchTags({

0 commit comments

Comments
 (0)