Skip to content

Commit e407031

Browse files
committed
fix(desktop): keep external install recovery out of updater events
1 parent 4855d24 commit e407031

2 files changed

Lines changed: 120 additions & 54 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ export const makeUpdateEvents = Effect.fn("desktop.updates.makeEvents")(function
3535
checks: UpdateChecks,
3636
install: UpdateInstall,
3737
) {
38-
const { offerable, updateStateRef, setState, updateState, activeUpdateAction } = context;
38+
const {
39+
config,
40+
environment,
41+
offerable,
42+
updateStateRef,
43+
setState,
44+
updateState,
45+
activeUpdateAction,
46+
} = context;
3947
const { checkForUpdates } = checks;
4048
const { recoverFailedInstall } = install;
4149
const lastLoggedDownloadMilestoneRef = yield* Ref.make(-1);
@@ -149,7 +157,10 @@ export const makeUpdateEvents = Effect.fn("desktop.updates.makeEvents")(function
149157
cause,
150158
});
151159
if (Option.isSome(activeAction) && activeAction.value === "install") {
152-
yield* recoverFailedInstall(error.message);
160+
// External handoff owns recovery after cleanup; updater events cannot cancel its helper.
161+
if (environment.platform !== "darwin" || Option.isNone(config.desktopUpdateInstaller)) {
162+
yield* recoverFailedInstall(error.message);
163+
}
153164
yield* logUpdaterError(error.message, {
154165
errorTag: error._tag,
155166
cause: describeUpdateErrorCause(error.cause),

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

Lines changed: 107 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,32 @@ describe("DesktopUpdates", () => {
4141
: (mode === "mixed" ? Effect.fail(ordinary) : Effect.interrupt).pipe(
4242
Effect.ensuring(Effect.die(error)),
4343
);
44-
const harness = makeHarness({ quitAndInstall: failure });
44+
const cleanupStarted = Deferred.makeUnsafe<void>();
45+
const releaseCleanup = Deferred.makeUnsafe<void>();
46+
const harness = makeHarness({
47+
env: { T3CODE_DESKTOP_UPDATE_INSTALLER: "/test/installer" },
48+
quitAndInstall: Deferred.succeed(cleanupStarted, undefined).pipe(
49+
Effect.andThen(Deferred.await(releaseCleanup)),
50+
Effect.andThen(failure),
51+
),
52+
});
4553
return Effect.scoped(
4654
Effect.gen(function* () {
4755
const desktopState = yield* DesktopState.DesktopState;
4856
const updates = yield* DesktopUpdates.DesktopUpdates;
4957
yield* updates.configure;
5058
harness.emit("update-downloaded", { version: "1.2.4" });
5159
yield* flushCallbacks;
52-
const result = yield* updates.install;
60+
const installing = yield* updates.install.pipe(Effect.forkChild);
61+
yield* Deferred.await(cleanupStarted);
62+
harness.emit("error", new Error("updater error during cleanup"));
63+
yield* flushCallbacks;
64+
assert.deepEqual(harness.installSteps, ["quitAndInstall"]);
65+
assert.equal(harness.updateRestartMarkers.size, 1);
66+
assert.equal(harness.installRecords.size, 1);
67+
assert.isFalse((yield* updates.install).accepted);
68+
yield* Deferred.succeed(releaseCleanup, undefined);
69+
const result = yield* Fiber.join(installing);
5370
assert.isTrue(result.accepted);
5471
assert.isFalse(yield* Ref.get(desktopState.quitting));
5572
assert.deepEqual(
@@ -105,60 +122,98 @@ describe("DesktopUpdates", () => {
105122
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
106123
});
107124

108-
it.effect("restores only desired-running instances after an installer error event", () => {
109-
const starts: string[] = [];
110-
const stops: string[] = [];
111-
const makeInstance = (id: DesktopBackendPool.BackendInstanceId, initiallyRunning: boolean) => {
112-
let desiredRunning = initiallyRunning;
113-
return {
114-
id,
115-
label: Effect.succeed(id),
116-
currentConfig: Effect.succeedNone,
117-
start: Effect.sync(() => {
118-
starts.push(id);
119-
desiredRunning = true;
120-
}),
121-
stop: () =>
122-
Effect.sync(() => {
123-
stops.push(id);
124-
desiredRunning = false;
125+
it.effect.each(["pending", "settled", "external"] as const)(
126+
"handles %s installer errors without starting undesired instances",
127+
(phase) => {
128+
const installStarted = Deferred.makeUnsafe<void>();
129+
const releaseInstall = Deferred.makeUnsafe<void>();
130+
const starts: string[] = [];
131+
const stops: string[] = [];
132+
const makeInstance = (
133+
id: DesktopBackendPool.BackendInstanceId,
134+
initiallyRunning: boolean,
135+
) => {
136+
let desiredRunning = initiallyRunning;
137+
return {
138+
id,
139+
label: Effect.succeed(id),
140+
currentConfig: Effect.succeedNone,
141+
start: Effect.sync(() => {
142+
starts.push(id);
143+
desiredRunning = true;
125144
}),
126-
snapshot: Effect.sync(() => ({
127-
desiredRunning,
128-
ready: false,
129-
activePid: Option.none(),
130-
restartAttempt: 0,
131-
restartScheduled: false,
132-
})),
133-
waitForReady: () => Effect.succeed(true),
134-
} satisfies DesktopBackendPool.DesktopBackendInstance;
135-
};
136-
const primary = makeInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, false);
137-
const wsl = makeInstance(DesktopBackendPool.BackendInstanceId("wsl:default"), true);
138-
const harness = makeHarness({ backendInstances: [primary, wsl] });
145+
stop: () =>
146+
Effect.sync(() => {
147+
stops.push(id);
148+
desiredRunning = false;
149+
}),
150+
snapshot: Effect.sync(() => ({
151+
desiredRunning,
152+
ready: false,
153+
activePid: Option.none(),
154+
restartAttempt: 0,
155+
restartScheduled: false,
156+
})),
157+
waitForReady: () => Effect.succeed(true),
158+
} satisfies DesktopBackendPool.DesktopBackendInstance;
159+
};
160+
const primary = makeInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, false);
161+
const wsl = makeInstance(DesktopBackendPool.BackendInstanceId("wsl:default"), true);
162+
const harness = makeHarness({
163+
env: phase === "external" ? { T3CODE_DESKTOP_UPDATE_INSTALLER: "/test/installer" } : {},
164+
backendInstances: [primary, wsl],
165+
quitAndInstall: Deferred.succeed(installStarted, undefined).pipe(
166+
Effect.andThen(Deferred.await(releaseInstall)),
167+
),
168+
});
139169

140-
return Effect.scoped(
141-
Effect.gen(function* () {
142-
const updates = yield* DesktopUpdates.DesktopUpdates;
143-
yield* updates.configure;
144-
harness.emit("update-downloaded", { version: "1.2.4" });
145-
yield* flushCallbacks;
170+
return Effect.scoped(
171+
Effect.gen(function* () {
172+
const updates = yield* DesktopUpdates.DesktopUpdates;
173+
yield* updates.configure;
174+
harness.emit("update-downloaded", { version: "1.2.4" });
175+
yield* flushCallbacks;
146176

147-
yield* updates.install;
148-
assert.sameMembers(stops, [primary.id, wsl.id]);
149-
assert.isFalse((yield* wsl.snapshot).desiredRunning);
150-
const { changes } = yield* updates.subscribe;
151-
const recovered = yield* Stream.runHead(changes).pipe(Effect.forkChild);
152-
yield* flushCallbacks;
153-
harness.emit("error", new Error("native installer refused"));
154-
assert.equal(Option.getOrThrow(yield* Fiber.join(recovered)).errorContext, "install");
177+
const installing = yield* updates.install.pipe(Effect.forkChild);
178+
yield* Deferred.await(installStarted);
179+
if (phase === "settled") {
180+
yield* Deferred.succeed(releaseInstall, undefined);
181+
yield* Fiber.join(installing);
182+
}
183+
assert.sameMembers(stops, [primary.id, wsl.id]);
184+
assert.isFalse((yield* wsl.snapshot).desiredRunning);
185+
const { changes } = yield* updates.subscribe;
186+
const recovered = yield* Stream.runHead(changes).pipe(Effect.forkChild);
187+
yield* flushCallbacks;
188+
harness.emit("error", new Error("native installer refused"));
189+
if (phase !== "settled") {
190+
yield* flushCallbacks;
191+
assert.deepEqual(starts, phase === "external" ? [] : [wsl.id]);
192+
assert.equal(harness.updateRestartMarkers.size, phase === "external" ? 1 : 0);
193+
assert.equal(harness.installRecords.size, phase === "external" ? 1 : 0);
194+
yield* Deferred.succeed(releaseInstall, undefined);
195+
yield* Fiber.join(installing);
196+
}
197+
if (phase === "external") {
198+
harness.emit("error", new Error("updater error after external handoff"));
199+
yield* flushCallbacks;
200+
assert.deepEqual(starts, []);
201+
assert.isFalse((yield* wsl.snapshot).desiredRunning);
202+
assert.equal(harness.updateRestartMarkers.size, 1);
203+
assert.equal(harness.installRecords.size, 1);
204+
return;
205+
}
206+
assert.equal(Option.getOrThrow(yield* Fiber.join(recovered)).errorContext, "install");
207+
assert.equal(harness.updateRestartMarkers.size, 0);
208+
assert.equal(harness.installRecords.size, 0);
155209

156-
assert.deepEqual(starts, [wsl.id]);
157-
assert.isFalse((yield* primary.snapshot).desiredRunning);
158-
assert.isTrue((yield* wsl.snapshot).desiredRunning);
159-
}),
160-
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
161-
});
210+
assert.deepEqual(starts, [wsl.id]);
211+
assert.isFalse((yield* primary.snapshot).desiredRunning);
212+
assert.isTrue((yield* wsl.snapshot).desiredRunning);
213+
}),
214+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
215+
},
216+
);
162217

163218
it.effect("holds the install reservation until failed-install recovery finishes", () => {
164219
const recoveryStarted = Deferred.makeUnsafe<void>();

0 commit comments

Comments
 (0)