fix(desktop): preserve quit-lifecycle handoff deadline on macOS

This commit is contained in:
Mohamed Boudra
2026-11-25 14:23:06 +01:00
parent bebcec4e51
commit 81be69e25b
3 changed files with 18 additions and 45 deletions
@@ -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<void>;
restart: boolean;
signal?: AbortSignal;
},
): Promise<void> {
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 {
@@ -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", () => {
+5 -25
View File
@@ -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<AppUpdateInstallLaunchResult> {
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<AppUpdateInstallLaunchResult>((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<AppUpdateInstallLaunchResult> {
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,
});
}
}