Skip to content

Commit d353fba

Browse files
fix(desktop): stalled update downloads time out and say why (#26244)
1 parent 8c15be9 commit d353fba

9 files changed

Lines changed: 142 additions & 16 deletions

File tree

‎apps/desktop/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"effect": "catalog:",
2525
"electron": "44.10.1",
2626
"electron-store": "^8.2.0",
27-
"electron-updater": "^6.8.9",
27+
"electron-updater": "6.8.9",
2828
"electron-webauthn": "catalog:",
2929
"ffi-rs": "1.3.2",
3030
"playwright-core": "1.64.1",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// @effect-diagnostics nodeBuiltinImport:off -- Fake Electron net request and response emitters.
2+
import * as NodeEvents from "node:events";
3+
import { ElectronHttpExecutor } from "electron-updater/out/electronHttpExecutor.js";
4+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5+
6+
function startRequest() {
7+
const request = Object.assign(new NodeEvents.EventEmitter(), { abort: vi.fn() });
8+
const response = new NodeEvents.EventEmitter();
9+
const reject = vi.fn();
10+
new ElectronHttpExecutor().addErrorAndTimeoutHandlers(request, reject);
11+
// Electron's net.ClientRequest emits these as soon as the request body is sent, before any response.
12+
request.emit("finish");
13+
request.emit("close");
14+
return { request, response, reject };
15+
}
16+
17+
describe("ElectronHttpExecutor idle timeout", () => {
18+
beforeEach(() => {
19+
vi.useFakeTimers();
20+
});
21+
afterEach(() => {
22+
vi.useRealTimers();
23+
});
24+
25+
it("aborts a request that receives no response for 60 seconds", () => {
26+
const { request, reject } = startRequest();
27+
28+
vi.advanceTimersByTime(59_999);
29+
expect(reject).not.toHaveBeenCalled();
30+
vi.advanceTimersByTime(1);
31+
32+
expect(request.abort).toHaveBeenCalledOnce();
33+
expect(reject).toHaveBeenCalledWith(new Error("Request timed out"));
34+
});
35+
36+
it("keeps a slow download alive while data keeps arriving", () => {
37+
const { request, response, reject } = startRequest();
38+
request.emit("response", response);
39+
40+
for (let elapsed = 0; elapsed < 180_000; elapsed += 30_000) {
41+
vi.advanceTimersByTime(30_000);
42+
response.emit("data", Buffer.alloc(1));
43+
}
44+
45+
expect(request.abort).not.toHaveBeenCalled();
46+
expect(reject).not.toHaveBeenCalled();
47+
});
48+
49+
it("stops the timer once the response ends", () => {
50+
const { request, response, reject } = startRequest();
51+
request.emit("response", response);
52+
53+
response.emit("end");
54+
vi.advanceTimersByTime(600_000);
55+
56+
expect(request.abort).not.toHaveBeenCalled();
57+
expect(reject).not.toHaveBeenCalled();
58+
});
59+
});

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,33 @@ describe("DesktopUpdates", () => {
426426
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
427427
});
428428

429+
it.effect("shows why a download failed, not only the channel", () => {
430+
const harness = makeHarness({
431+
downloadUpdate: Effect.fail(
432+
new ElectronUpdater.ElectronUpdaterDownloadUpdateError({
433+
channel: "nightly",
434+
cause: new Error("net::ERR_CONTENT_LENGTH_MISMATCH"),
435+
}),
436+
),
437+
});
438+
439+
return Effect.scoped(
440+
Effect.gen(function* () {
441+
const updates = yield* DesktopUpdates.DesktopUpdates;
442+
yield* updates.configure;
443+
harness.emit("update-available", { version: "1.2.4" });
444+
yield* flushCallbacks;
445+
446+
const result = yield* updates.download;
447+
assert.isFalse(result.completed);
448+
449+
const state = yield* updates.getState;
450+
assert.equal(state.errorContext, "download");
451+
assert.include(state.message ?? "", "ERR_CONTENT_LENGTH_MISMATCH");
452+
}),
453+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
454+
});
455+
429456
it.effect("restores download state and permits retry after interruption", () =>
430457
Effect.gen(function* () {
431458
const actionStarted = yield* Deferred.make<void>();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,13 @@ export const make = Effect.gen(function* () {
614614
Effect.catchTags({
615615
ElectronUpdaterDownloadUpdateError: Effect.fn("desktop.updates.handleDownloadFailure")(
616616
function* (error) {
617+
const cause = describeUpdateErrorCause(error.cause);
617618
yield* updateState((current) =>
618-
reduceDesktopUpdateStateOnDownloadFailure(current, error.message),
619+
reduceDesktopUpdateStateOnDownloadFailure(current, `${error.message} ${cause}`),
619620
);
620621
yield* logUpdaterError(error.message, {
621622
errorTag: error._tag,
622-
cause: describeUpdateErrorCause(error.cause),
623+
cause,
623624
channel: error.channel,
624625
});
625626
return { accepted: true, completed: false };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface UpdatesHarnessOptions {
2727
readonly beforeSetUpdateChannel?: Effect.Effect<void>;
2828
readonly setUpdateChannelError?: DesktopAppSettings.DesktopSettingsWriteError;
2929
readonly setDisableDifferentialDownload?: Effect.Effect<void>;
30-
readonly downloadUpdate?: Effect.Effect<void>;
30+
readonly downloadUpdate?: Effect.Effect<void, ElectronUpdater.ElectronUpdaterDownloadUpdateError>;
3131
readonly quitAndInstall?: Effect.Effect<void, ElectronUpdater.ElectronUpdaterQuitAndInstallError>;
3232
readonly stopBackend?: Effect.Effect<void>;
3333
readonly startBackend?: Effect.Effect<void>;

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ import { environmentIdsKey } from "~/state/environmentAtomList";
116116
import { environmentSessionStatesAtom } from "~/state/session";
117117
import { useCheckProviderUpdates } from "../ProviderUpdatesAction";
118118
import { canUpdateServer } from "../ServerUpdateAction";
119+
import { downloadLocalUpdate } from "../sidebar/SidebarUpdatePill";
119120
import { useUpdateEverythingRun } from "../sidebar/updateEverythingRun";
120121
import { awaitsTrackSwitch, channelSwitchLabel } from "../sidebar/updateEverything.logic";
121122
import { isHostedByThisApp } from "../sidebar/useUpdateEverything";
@@ -205,15 +206,7 @@ function AboutVersionSection() {
205206
const action = updateState ? resolveDesktopUpdateButtonAction(updateState) : "none";
206207

207208
if (action === "download") {
208-
void bridge.downloadUpdate().catch((error: unknown) => {
209-
toastManager.add(
210-
stackedThreadToast({
211-
type: "error",
212-
title: "Could not download update",
213-
description: error instanceof Error ? error.message : "Download failed.",
214-
}),
215-
);
216-
});
209+
void downloadLocalUpdate(bridge, { notifyDownloaded: false });
217210
return;
218211
}
219212

‎patches/[email protected]‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
diff --git a/out/electronHttpExecutor.js b/out/electronHttpExecutor.js
2+
index 0ae9e1126e1dc136977914ef1bfd46709fc9db1b..3932b6607ca41e6663aae001111c9b18594ed37a 100644
3+
--- a/out/electronHttpExecutor.js
4+
+++ b/out/electronHttpExecutor.js
5+
@@ -61,6 +61,35 @@ class ElectronHttpExecutor extends builder_util_runtime_1.HttpExecutor {
6+
}
7+
return request;
8+
}
9+
+ // Electron's net.ClientRequest never emits "socket", so the inherited socket idle timeout never
10+
+ // arms and a stalled request stays pending forever. Reset an idle timer on every chunk instead.
11+
+ addTimeOutHandler(request, callback, timeout) {
12+
+ let timer = null;
13+
+ const clear = () => {
14+
+ if (timer != null) {
15+
+ clearTimeout(timer);
16+
+ timer = null;
17+
+ }
18+
+ };
19+
+ const arm = () => {
20+
+ clear();
21+
+ timer = setTimeout(() => {
22+
+ timer = null;
23+
+ request.abort();
24+
+ callback(new Error("Request timed out"));
25+
+ }, timeout);
26+
+ };
27+
+ request.on("response", (response) => {
28+
+ arm();
29+
+ response.on("data", arm);
30+
+ response.on("end", clear);
31+
+ response.on("aborted", clear);
32+
+ response.on("error", clear);
33+
+ });
34+
+ request.on("abort", clear);
35+
+ request.on("error", clear);
36+
+ arm();
37+
+ }
38+
addRedirectHandlers(request, options, reject, redirectCount, handler) {
39+
request.on("redirect", (statusCode, method, redirectUrl) => {
40+
// no way to modify request options, abort old and make a new one

‎pnpm-lock.yaml‎

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ overrides:
129129
react-native-webview: 14.0.1
130130
react-native-svg: 15.16.1
131131
app-builder-lib: 26.16.1
132+
electron-updater: 6.8.9
132133
expo-modules-core: 58.0.30
133134
# Keep jsdom's optional peer consistent so Vite+ has one test-runner instance.
134135
"@exodus/bytes>@noble/hashes": "1.8.0"
@@ -256,6 +257,9 @@ patchedDependencies:
256257
# Observe the owned host exit independently of descendants retaining its stdio.
257258
"@muse-code/[email protected]": patches/@[email protected]
258259
260+
# Electron net requests never emit "socket", so upstream's idle timeout never fires and a
261+
# stalled update download hangs forever.
262+
259263
# Local agents honor the attribution settings in the user's Cursor CLI cli-config.json.
260264
261265
# A failed Artifacts.cached effect hangs every later caller: https://github.com/alchemy-run/alchemy/issues/2981

0 commit comments

Comments
 (0)