@devries-cameron20 / t3code-1 / commits / df198f9de9
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): keep disabled backends stopped after failed updates (#18414)

Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com>

Alex Southwell committed Oct 19, 2026 at 06:12 UTC df198f9de99fcbb81950d6a479f476e8e7960779
3 files changed +113 -6
apps/desktop/src/updates/DesktopUpdates.test.ts
+85
@@ -14,6 +14,7 @@ import * as Stream from "effect/Stream";
14 import * as TestClock from "effect/testing/TestClock";
15
16 import * as ElectronUpdater from "../electron/ElectronUpdater.ts";
17 +import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
18 import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
19 import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
20 import * as DesktopState from "../app/DesktopState.ts";
@@ -767,6 +768,90 @@ describe("DesktopUpdates", () => {
768 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
769 });
770
771 + it.effect("keeps the disabled primary stopped when quitAndInstall fails", () => {
772 + const harness = makeHarness({
773 + backendDesiredRunning: false,
774 + quitAndInstall: Effect.fail(
775 + new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
776 + channel: "latest",
777 + isSilent: true,
778 + isForceRunAfter: true,
779 + cause: new Error("installer refused"),
780 + }),
781 + ),
782 + });
783 +
784 + return Effect.scoped(
785 + Effect.gen(function* () {
786 + const updates = yield* DesktopUpdates.DesktopUpdates;
787 + const pool = yield* DesktopBackendPool.DesktopBackendPool;
788 + yield* updates.configure;
789 + harness.emit("update-downloaded", { version: "1.2.4" });
790 + yield* flushCallbacks;
791 +
792 + const result = yield* updates.install;
793 + assert.equal(result.state.errorContext, "install");
794 + assert.deepEqual(harness.installSteps, ["quitAndInstall"]);
795 + assert.isFalse((yield* (yield* pool.primary).snapshot).desiredRunning);
796 + }),
797 + ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
798 + });
799 +
800 + it.effect("restores only desired-running instances after an installer error event", () => {
801 + const starts: string[] = [];
802 + const stops: string[] = [];
803 + const makeInstance = (id: DesktopBackendPool.BackendInstanceId, initiallyRunning: boolean) => {
804 + let desiredRunning = initiallyRunning;
805 + return {
806 + id,
807 + label: Effect.succeed(id),
808 + currentConfig: Effect.succeedNone,
809 + start: Effect.sync(() => {
810 + starts.push(id);
811 + desiredRunning = true;
812 + }),
813 + stop: () =>
814 + Effect.sync(() => {
815 + stops.push(id);
816 + desiredRunning = false;
817 + }),
818 + snapshot: Effect.sync(() => ({
819 + desiredRunning,
820 + ready: false,
821 + activePid: Option.none(),
822 + restartAttempt: 0,
823 + restartScheduled: false,
824 + })),
825 + waitForReady: () => Effect.succeed(true),
826 + } satisfies DesktopBackendPool.DesktopBackendInstance;
827 + };
828 + const primary = makeInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, false);
829 + const wsl = makeInstance(DesktopBackendPool.BackendInstanceId("wsl:default"), true);
830 + const harness = makeHarness({ backendInstances: [primary, wsl] });
831 +
832 + return Effect.scoped(
833 + Effect.gen(function* () {
834 + const updates = yield* DesktopUpdates.DesktopUpdates;
835 + yield* updates.configure;
836 + harness.emit("update-downloaded", { version: "1.2.4" });
837 + yield* flushCallbacks;
838 +
839 + yield* updates.install;
840 + assert.sameMembers(stops, [primary.id, wsl.id]);
841 + assert.isFalse((yield* wsl.snapshot).desiredRunning);
842 + const { changes } = yield* updates.subscribe;
843 + const recovered = yield* Stream.runHead(changes).pipe(Effect.forkChild);
844 + yield* flushCallbacks;
845 + harness.emit("error", new Error("native installer refused"));
846 + assert.equal(Option.getOrThrow(yield* Fiber.join(recovered)).errorContext, "install");
847 +
848 + assert.deepEqual(starts, [wsl.id]);
849 + assert.isFalse((yield* primary.snapshot).desiredRunning);
850 + assert.isTrue((yield* wsl.snapshot).desiredRunning);
851 + }),
852 + ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
853 + });
854 +
855 it.effect("holds the install reservation until failed-install recovery finishes", () => {
856 const recoveryStarted = Deferred.makeUnsafe<void>();
857 const releaseRecovery = Deferred.makeUnsafe<void>();
apps/desktop/src/updates/DesktopUpdates.ts
+14 -1
@@ -346,6 +346,9 @@ export const make = Effect.gen(function* () {
346
347 const appUpdateYmlConfigRef = yield* Ref.make<Option.Option<AppUpdateYmlConfig>>(Option.none());
348 const activeUpdateActionRef = yield* Ref.make<Option.Option<UpdateAction>>(Option.none());
349 + const installRunningBackendsRef = yield* Ref.make<
350 + ReadonlyArray<DesktopBackendPool.DesktopBackendInstance>
351 + >([]);
352 const finishedUpdateActions = yield* PubSub.unbounded<UpdateAction>();
353 const updaterConfiguredRef = yield* Ref.make(false);
354 const lastLoggedDownloadMilestoneRef = yield* Ref.make(-1);
@@ -643,6 +646,7 @@ export const make = Effect.gen(function* () {
646 [
647 finishUpdateAction("install"),
648 Ref.set(desktopState.quitting, false),
649 + Ref.set(installRunningBackendsRef, []),
650 removeUpdateRestartMarker,
651 removeInstallRecord,
652 ],
@@ -663,7 +667,10 @@ export const make = Effect.gen(function* () {
667 yield* removeUpdateRestartMarker;
668 yield* removeInstallRecord;
669 yield* Effect.gen(function* () {
666 - const instances = yield* pool.list;
670 + const runningInstances = yield* Ref.getAndSet(installRunningBackendsRef, []);
671 + const instances = (yield* pool.list).filter((instance) =>
672 + runningInstances.includes(instance),
673 + );
674 const restartExit = yield* Effect.forEach(instances, (instance) => instance.start, {
675 concurrency: "unbounded",
676 discard: true,
@@ -729,6 +736,7 @@ export const make = Effect.gen(function* () {
736 }
737
738 yield* Ref.set(desktopState.quitting, true);
739 + yield* Ref.set(installRunningBackendsRef, []);
740
741 return yield* Effect.gen(function* () {
742 yield* writeUpdateRestartMarker;
@@ -740,6 +748,11 @@ export const make = Effect.gen(function* () {
748 // has a chance to run its stop finalizer, so the WSL child gets
749 // hard-killed by the OS instead of receiving SIGTERM + grace.
750 const instances = yield* pool.list;
751 + // stop clears desiredRunning, so retain the recovery set beforehand.
752 + const runningInstances = yield* Effect.filter(instances, (instance) =>
753 + instance.snapshot.pipe(Effect.map((snapshot) => snapshot.desiredRunning)),
754 + );
755 + yield* Ref.set(installRunningBackendsRef, runningInstances);
756 yield* Effect.forEach(
757 instances,
758 (instance) => instance.stop({ timeout: Duration.seconds(5) }),
apps/desktop/src/updates/updatesTestHarness.ts
+14 -5
@@ -29,6 +29,8 @@ export interface UpdatesHarnessOptions {
29 readonly quitAndInstall?: Effect.Effect<void, ElectronUpdater.ElectronUpdaterQuitAndInstallError>;
30 readonly stopBackend?: Effect.Effect<void>;
31 readonly startBackend?: Effect.Effect<void>;
32 + readonly backendDesiredRunning?: boolean;
33 + readonly backendInstances?: ReadonlyArray<DesktopBackendPool.DesktopBackendInstance>;
34 readonly env?: Record<string, string | undefined>;
35 readonly platform?: NodeJS.Platform;
36 readonly packageType?: string | undefined;
@@ -124,24 +126,31 @@ export function makeHarness(options: UpdatesHarnessOptions = {}) {
126 syncAllAppearance: () => Effect.void,
127 } satisfies ElectronWindow.ElectronWindow["Service"]);
128
129 + let backendDesiredRunning = options.backendDesiredRunning ?? true;
130 const stubBackendInstance: DesktopBackendPool.DesktopBackendInstance = {
131 id: DesktopBackendPool.PRIMARY_INSTANCE_ID,
132 label: Effect.succeed("Windows"),
133 start: Effect.sync(() => {
134 + backendDesiredRunning = true;
135 installSteps.push("startBackend");
136 }).pipe(Effect.andThen(options.startBackend ?? Effect.void)),
133 - stop: () => options.stopBackend ?? Effect.void,
137 + stop: () =>
138 + Effect.sync(() => {
139 + backendDesiredRunning = false;
140 + }).pipe(Effect.andThen(options.stopBackend ?? Effect.void)),
141 currentConfig: Effect.succeedNone,
135 - snapshot: Effect.succeed({
136 - desiredRunning: false,
142 + snapshot: Effect.sync(() => ({
143 + desiredRunning: backendDesiredRunning,
144 ready: false,
145 activePid: Option.none(),
146 restartAttempt: 0,
147 restartScheduled: false,
141 - }),
148 + })),
149 waitForReady: () => Effect.succeed(true),
150 };
144 - const layerBackend = DesktopBackendPool.layerTest([stubBackendInstance]);
151 + const layerBackend = DesktopBackendPool.layerTest(
152 + options.backendInstances ?? [stubBackendInstance],
153 + );
154
155 const layerEnvironment = DesktopEnvironment.layer({
156 dirname: "/repo/apps/desktop/src",