From 81be69e25b9cbbad3d73f076cee839aa2e6d597d Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 25 Nov 2026 11:16:19 +0100 Subject: [PATCH] fix(desktop): preserve quit-lifecycle handoff deadline on macOS --- .../src/features/app-update-service.ts | 5 ---- .../desktop/src/features/auto-updater.test.ts | 28 ++++++++--------- packages/desktop/src/features/auto-updater.ts | 30 ++++--------------- 3 files changed, 18 insertions(+), 45 deletions(-) diff --git a/packages/desktop/src/features/app-update-service.ts b/packages/desktop/src/features/app-update-service.ts index 115ab3a4cf..99aaafecd9 100644 --- a/packages/desktop/src/features/app-update-service.ts +++ b/packages/desktop/src/features/app-update-service.ts @@ -64,7 +64,6 @@ export interface AppUpdateInstallRequest { targetVersion: string; isSilent: boolean; isForceRunAfter: boolean; - signal?: AbortSignal; } export interface AppUpdateInstallLaunchResult { @@ -151,12 +150,10 @@ async function performQuitAndInstall( targetVersion, onBeforeQuit, restart, - signal, }: { targetVersion: string; onBeforeQuit?: () => Promise; restart: boolean; - signal?: AbortSignal; }, ): Promise { if (onBeforeQuit) await onBeforeQuit(); @@ -164,7 +161,6 @@ async function performQuitAndInstall( targetVersion, isSilent: !restart, isForceRunAfter: restart, - signal, }); if (launch && !launch.started) { throw new Error(launch.errorMessage ?? "Failed to launch update installer."); @@ -586,7 +582,6 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer targetVersion: readyVersion, onBeforeQuit, restart, - signal, }); return { diff --git a/packages/desktop/src/features/auto-updater.test.ts b/packages/desktop/src/features/auto-updater.test.ts index c6f2e3b7ca..c29fb675e9 100644 --- a/packages/desktop/src/features/auto-updater.test.ts +++ b/packages/desktop/src/features/auto-updater.test.ts @@ -241,18 +241,16 @@ describe("checkForAppUpdate", () => { consoleError.mockRestore(); }); - it("waits on macOS until Squirrel either reports an async preparation error or signals quit", async () => { + it("waits on macOS explicit installs until Squirrel reports an error or signals quit, while returning immediately on quit-time handoff", async () => { const updater = Object.assign(new EventEmitter(), { quitAndInstall: vi.fn(), }); const nativeUpdater = new EventEmitter(); - const appEvents = new EventEmitter(); const failedInstall = runElectronQuitAndInstall({ platform: "darwin", updater, nativeUpdater, - appEvents, isSilent: false, isForceRunAfter: true, }); @@ -273,24 +271,24 @@ describe("checkForAppUpdate", () => { platform: "darwin", updater, nativeUpdater, - appEvents, isSilent: false, isForceRunAfter: true, }); nativeUpdater.emit("before-quit-for-update"); await expect(restartInstall).resolves.toEqual({ started: true }); - const quitInstall = runElectronQuitAndInstall({ - platform: "darwin", - updater, - nativeUpdater, - appEvents, - isSilent: true, - isForceRunAfter: false, - }); - appEvents.emit("before-quit"); - await expect(quitInstall).resolves.toEqual({ started: true }); - expect(appEvents.listenerCount("before-quit")).toBe(0); + // Quit-time install (isForceRunAfter: false) returns immediately so + // quit-lifecycle owns its separate handoff deadline. + await expect( + runElectronQuitAndInstall({ + platform: "darwin", + updater, + nativeUpdater, + isSilent: true, + isForceRunAfter: false, + }), + ).resolves.toEqual({ started: true }); + expect(nativeUpdater.listenerCount("before-quit-for-update")).toBe(0); }); it("logs the update handoff with current and selected target versions", () => { diff --git a/packages/desktop/src/features/auto-updater.ts b/packages/desktop/src/features/auto-updater.ts index d03a032362..eab694470e 100644 --- a/packages/desktop/src/features/auto-updater.ts +++ b/packages/desktop/src/features/auto-updater.ts @@ -188,22 +188,21 @@ export function runElectronQuitAndInstall({ platform, updater, nativeUpdater, - appEvents, isSilent, isForceRunAfter, - signal, }: { platform: NodeJS.Platform; updater: QuitAndInstallEventSource & { quitAndInstall(isSilent?: boolean, isForceRunAfter?: boolean): void; }; nativeUpdater?: QuitAndInstallEventSource; - appEvents?: QuitAndInstallEventSource; isSilent: boolean; isForceRunAfter: boolean; - signal?: AbortSignal; }): Promise { - if (platform !== "darwin" || typeof nativeUpdater?.on !== "function") { + // Only await macOS Squirrel preparation for explicit Download / Restart Now + // installs (isForceRunAfter === true). Quit-time installs (isForceRunAfter === false) + // return immediately so quit-lifecycle owns its separate handoff deadline. + if (platform !== "darwin" || !isForceRunAfter || typeof nativeUpdater?.on !== "function") { let launchError: unknown = null; const captureLaunchError = (error: unknown) => { launchError ??= error; @@ -224,7 +223,7 @@ export function runElectronQuitAndInstall({ // With autoInstallOnAppQuit disabled, electron-updater's MacUpdater starts // native Squirrel preparation asynchronously inside quitAndInstall(). Keep - // the install promise pending until Squirrel hands off to quit or fails. + // the explicit install promise pending until Squirrel hands off to quit or fails. return new Promise((resolve) => { let settled = false; const finish = (result: AppUpdateInstallLaunchResult) => { @@ -234,10 +233,6 @@ export function runElectronQuitAndInstall({ nativeUpdater.removeListener?.("error", handleError); nativeUpdater.removeListener?.("update-not-available", handleNotAvailable); nativeUpdater.removeListener?.("before-quit-for-update", handleQuitStarted); - if (!isForceRunAfter) { - appEvents?.removeListener?.("before-quit", handleQuitStarted); - } - signal?.removeEventListener("abort", handleAbort); resolve(result); }; @@ -250,23 +245,11 @@ export function runElectronQuitAndInstall({ const handleQuitStarted = () => { finish({ started: true }); }; - const handleAbort = () => { - finish({ started: false, errorMessage: "Update installation was deferred." }); - }; - - if (signal?.aborted) { - handleAbort(); - return; - } updater.on("error", handleError); nativeUpdater.on("error", handleError); nativeUpdater.on("update-not-available", handleNotAvailable); nativeUpdater.on("before-quit-for-update", handleQuitStarted); - if (!isForceRunAfter) { - appEvents?.on("before-quit", handleQuitStarted); - } - signal?.addEventListener("abort", handleAbort, { once: true }); try { updater.quitAndInstall(isSilent, isForceRunAfter); @@ -360,7 +343,6 @@ class ElectronAppUpdateRuntime implements AppUpdateRuntime { targetVersion, isSilent, isForceRunAfter, - signal, }: AppUpdateInstallRequest): Promise { broadcastDownloadProgress(null); autoUpdater.autoRunAppAfterInstall = isForceRunAfter; @@ -373,10 +355,8 @@ class ElectronAppUpdateRuntime implements AppUpdateRuntime { platform: process.platform, updater: autoUpdater, nativeUpdater: electronAutoUpdater, - appEvents: app, isSilent, isForceRunAfter, - signal, }); } }