Skip to content

Commit ea59528

Browse files
committed
fix(desktop): serialize update channel changes
1 parent 867d21b commit ea59528

2 files changed

Lines changed: 113 additions & 46 deletions

File tree

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

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface UpdatesHarnessOptions {
2727
void,
2828
ElectronUpdater.ElectronUpdaterCheckForUpdatesError
2929
>;
30+
readonly beforeSetUpdateChannel?: Effect.Effect<void>;
3031
readonly setUpdateChannelError?: DesktopAppSettings.DesktopSettingsWriteError;
3132
readonly setDisableDifferentialDownload?: Effect.Effect<void>;
3233
readonly stopBackend?: Effect.Effect<void>;
@@ -153,22 +154,41 @@ function makeHarness(options: UpdatesHarnessOptions = {}) {
153154
),
154155
);
155156

157+
let testSettings: DesktopAppSettings.DesktopSettings = {
158+
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
159+
};
156160
const setUpdateChannelError = options.setUpdateChannelError;
157-
const settingsLayer = setUpdateChannelError
158-
? Layer.succeed(DesktopAppSettings.DesktopAppSettings, {
159-
get: Effect.succeed(DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS),
160-
load: Effect.succeed(DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS),
161-
setMainWindowBounds: () => Effect.die("unexpected main window bounds update"),
162-
setServerExposureMode: () => Effect.die("unexpected server exposure update"),
163-
setTailscaleServe: () => Effect.die("unexpected Tailscale Serve update"),
164-
setUpdateChannel: () => Effect.fail(setUpdateChannelError),
165-
setWslBackendEnabled: () => Effect.die("unexpected WSL backend toggle"),
166-
setWslDistro: () => Effect.die("unexpected WSL distro change"),
167-
setWslOnly: () => Effect.die("unexpected WSL-only toggle"),
168-
applyWslWindowsFallback: Effect.die("unexpected WSL Windows fallback"),
169-
applyWslWindowsFallbackInMemory: Effect.die("unexpected WSL Windows fallback"),
170-
} satisfies DesktopAppSettings.DesktopAppSettings["Service"])
171-
: DesktopAppSettings.layer;
161+
const settingsLayer =
162+
setUpdateChannelError || options.beforeSetUpdateChannel
163+
? Layer.succeed(DesktopAppSettings.DesktopAppSettings, {
164+
get: Effect.sync(() => testSettings),
165+
load: Effect.sync(() => testSettings),
166+
setMainWindowBounds: () => Effect.die("unexpected main window bounds update"),
167+
setServerExposureMode: () => Effect.die("unexpected server exposure update"),
168+
setTailscaleServe: () => Effect.die("unexpected Tailscale Serve update"),
169+
setUpdateChannel: (channel) =>
170+
setUpdateChannelError
171+
? Effect.fail(setUpdateChannelError)
172+
: (options.beforeSetUpdateChannel ?? Effect.void).pipe(
173+
Effect.andThen(
174+
Effect.sync(() => {
175+
const changed = testSettings.updateChannel !== channel;
176+
testSettings = {
177+
...testSettings,
178+
updateChannel: channel,
179+
updateChannelConfiguredByUser: true,
180+
};
181+
return { settings: testSettings, changed };
182+
}),
183+
),
184+
),
185+
setWslBackendEnabled: () => Effect.die("unexpected WSL backend toggle"),
186+
setWslDistro: () => Effect.die("unexpected WSL distro change"),
187+
setWslOnly: () => Effect.die("unexpected WSL-only toggle"),
188+
applyWslWindowsFallback: Effect.die("unexpected WSL Windows fallback"),
189+
applyWslWindowsFallbackInMemory: Effect.die("unexpected WSL Windows fallback"),
190+
} satisfies DesktopAppSettings.DesktopAppSettings["Service"])
191+
: DesktopAppSettings.layer;
172192

173193
const layer = DesktopUpdates.layer.pipe(
174194
Layer.provideMerge(updaterLayer),
@@ -689,6 +709,38 @@ describe("DesktopUpdates", () => {
689709
}),
690710
);
691711

712+
it.effect("rejects checks while an update channel change is being persisted", () =>
713+
Effect.gen(function* () {
714+
const channelChangeStarted = yield* Deferred.make<void>();
715+
const releaseChannelChange = yield* Deferred.make<void>();
716+
const harness = makeHarness({
717+
beforeSetUpdateChannel: Deferred.succeed(channelChangeStarted, undefined).pipe(
718+
Effect.andThen(Deferred.await(releaseChannelChange)),
719+
),
720+
});
721+
722+
yield* Effect.scoped(
723+
Effect.gen(function* () {
724+
const updates = yield* DesktopUpdates.DesktopUpdates;
725+
yield* updates.configure;
726+
727+
const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
728+
yield* Deferred.await(channelChangeStarted);
729+
730+
const checkResult = yield* updates.check("manual");
731+
assert.isFalse(checkResult.checked);
732+
assert.equal(harness.checkCount(), 0);
733+
734+
yield* Deferred.succeed(releaseChannelChange, undefined);
735+
const state = yield* Fiber.join(channelFiber);
736+
737+
assert.equal(state.channel, "nightly");
738+
assert.equal(harness.checkCount(), 1);
739+
}),
740+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
741+
}),
742+
);
743+
692744
it.effect("preserves settings failure context when an update channel cannot be persisted", () => {
693745
const diskFailure = new Error("disk exploded");
694746
const settingsFailure = new DesktopAppSettings.DesktopSettingsWriteError({

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

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
const AUTO_UPDATE_STARTUP_DELAY = "15 seconds";
4646
const AUTO_UPDATE_POLL_INTERVAL = "4 minutes";
4747

48-
type UpdateAction = "check" | "download" | "install";
48+
type UpdateAction = "check" | "download" | "install" | "channel";
4949

5050
const AppUpdateYmlConfig = Schema.Record(Schema.String, Schema.String);
5151
type AppUpdateYmlConfig = typeof AppUpdateYmlConfig.Type;
@@ -70,7 +70,7 @@ const currentIsoTimestamp = DateTime.now.pipe(Effect.map(DateTime.formatIso));
7070
export class DesktopUpdateActionInProgressError extends Schema.TaggedErrorClass<DesktopUpdateActionInProgressError>()(
7171
"DesktopUpdateActionInProgressError",
7272
{
73-
action: Schema.Literals(["check", "download", "install"]),
73+
action: Schema.Literals(["check", "download", "install", "channel"]),
7474
requestedChannel: DesktopUpdateChannelSchema,
7575
},
7676
) {
@@ -118,7 +118,7 @@ export class DesktopUpdateEventHandlingError extends Schema.TaggedErrorClass<Des
118118
export class DesktopUpdaterReportedError extends Schema.TaggedErrorClass<DesktopUpdaterReportedError>()(
119119
"DesktopUpdaterReportedError",
120120
{
121-
operation: Schema.Literals(["check", "download", "install", "background"]),
121+
operation: Schema.Literals(["check", "download", "install", "channel", "background"]),
122122
cause: Schema.Defect(),
123123
},
124124
) {
@@ -320,6 +320,12 @@ export const make = Effect.gen(function* () {
320320
Option.isSome(activeAction) ? [false, activeAction] : [true, Option.some(action)],
321321
);
322322

323+
const tryStartChannelChange = Ref.modify(activeUpdateActionRef, (activeAction) =>
324+
Option.isSome(activeAction)
325+
? [activeAction, activeAction]
326+
: [Option.none<UpdateAction>(), Option.some<UpdateAction>("channel")],
327+
);
328+
323329
const finishUpdateAction = (action: UpdateAction): Effect.Effect<void> =>
324330
Ref.update(activeUpdateActionRef, (activeAction) =>
325331
Option.isSome(activeAction) && activeAction.value === action ? Option.none() : activeAction,
@@ -344,7 +350,10 @@ export const make = Effect.gen(function* () {
344350

345351
const shouldEnableAutoUpdates = resolveDisabledReason.pipe(Effect.map(Option.isNone));
346352

347-
const checkForUpdates = Effect.fn("desktop.updates.checkForUpdates")(function* (reason: string) {
353+
const checkForUpdates = Effect.fn("desktop.updates.checkForUpdates")(function* (
354+
reason: string,
355+
actionReservation: "acquire" | "held" = "acquire",
356+
) {
348357
yield* Effect.annotateCurrentSpan({ reason });
349358
if (yield* Ref.get(desktopState.quitting)) return false;
350359
if (!(yield* Ref.get(updaterConfiguredRef))) return false;
@@ -358,9 +367,9 @@ export const make = Effect.gen(function* () {
358367
return false;
359368
}
360369

361-
if (!(yield* tryStartUpdateAction("check"))) return false;
370+
if (actionReservation === "acquire" && !(yield* tryStartUpdateAction("check"))) return false;
362371

363-
return yield* Effect.gen(function* () {
372+
const check = Effect.gen(function* () {
364373
const checkedAt = yield* currentIsoTimestamp;
365374
yield* setState(reduceDesktopUpdateStateOnCheckStart(state, checkedAt));
366375
yield* logUpdaterInfo("checking for updates", { reason });
@@ -383,7 +392,11 @@ export const make = Effect.gen(function* () {
383392
}),
384393
}),
385394
);
386-
}).pipe(Effect.ensuring(finishUpdateAction("check")));
395+
});
396+
397+
return yield* actionReservation === "held"
398+
? check
399+
: check.pipe(Effect.ensuring(finishUpdateAction("check")));
387400
});
388401

389402
const downloadAvailableUpdate = Effect.gen(function* () {
@@ -778,41 +791,43 @@ export const make = Effect.gen(function* () {
778791
nextChannel: DesktopUpdateChannel,
779792
) {
780793
yield* Effect.annotateCurrentSpan({ channel: nextChannel });
781-
const activeAction = yield* activeUpdateAction;
794+
const activeAction = yield* tryStartChannelChange;
782795
if (Option.isSome(activeAction)) {
783796
return yield* new DesktopUpdateActionInProgressError({
784797
action: activeAction.value,
785798
requestedChannel: nextChannel,
786799
});
787800
}
788801

789-
const state = yield* Ref.get(updateStateRef);
790-
if (nextChannel === state.channel) {
791-
return state;
792-
}
802+
return yield* Effect.gen(function* () {
803+
const state = yield* Ref.get(updateStateRef);
804+
if (nextChannel === state.channel) {
805+
return state;
806+
}
793807

794-
yield* desktopSettings
795-
.setUpdateChannel(nextChannel)
796-
.pipe(
797-
Effect.mapError(
798-
(cause) => new DesktopUpdateChannelPersistenceError({ channel: nextChannel, cause }),
799-
),
800-
);
808+
yield* desktopSettings
809+
.setUpdateChannel(nextChannel)
810+
.pipe(
811+
Effect.mapError(
812+
(cause) => new DesktopUpdateChannelPersistenceError({ channel: nextChannel, cause }),
813+
),
814+
);
801815

802-
const enabled = yield* shouldEnableAutoUpdates;
803-
yield* setState(createBaseUpdateState(nextChannel, enabled, environment));
816+
const enabled = yield* shouldEnableAutoUpdates;
817+
yield* setState(createBaseUpdateState(nextChannel, enabled, environment));
804818

805-
if (!enabled || !(yield* Ref.get(updaterConfiguredRef))) {
806-
return yield* Ref.get(updateStateRef);
807-
}
819+
if (!enabled || !(yield* Ref.get(updaterConfiguredRef))) {
820+
return yield* Ref.get(updateStateRef);
821+
}
808822

809-
yield* applyAutoUpdaterChannel(nextChannel);
810-
const allowDowngrade = yield* electronUpdater.allowDowngrade;
811-
yield* electronUpdater.setAllowDowngrade(true);
812-
yield* checkForUpdates("channel-change").pipe(
813-
Effect.ensuring(electronUpdater.setAllowDowngrade(allowDowngrade).pipe(Effect.ignore)),
814-
);
815-
return yield* Ref.get(updateStateRef);
823+
yield* applyAutoUpdaterChannel(nextChannel);
824+
const allowDowngrade = yield* electronUpdater.allowDowngrade;
825+
yield* electronUpdater.setAllowDowngrade(true);
826+
yield* checkForUpdates("channel-change", "held").pipe(
827+
Effect.ensuring(electronUpdater.setAllowDowngrade(allowDowngrade).pipe(Effect.ignore)),
828+
);
829+
return yield* Ref.get(updateStateRef);
830+
}).pipe(Effect.ensuring(finishUpdateAction("channel")));
816831
}),
817832
check: Effect.fn("desktop.updates.check")(function* (reason: string) {
818833
yield* Effect.annotateCurrentSpan({ reason });

0 commit comments

Comments
 (0)