Always revalidate desktop updates before install

This commit is contained in:
Mohamed Boudra
2026-07-16 20:17:58 +02:00
parent 04c71c5890
commit 7d80fdfd12
8 changed files with 340 additions and 285 deletions
@@ -17,7 +17,10 @@ class FakeAppUpdateRuntime implements AppUpdateRuntime {
> = [];
private gate: ((info: RuntimeUpdateInfo) => boolean | Promise<boolean>) | null = null;
private configuration: AppUpdateRuntimeConfiguration | null = null;
private downloadableUpdate: RuntimeUpdateInfo | null = null;
private downloadedUpdate: RuntimeUpdateInfo | null = null;
checkCount = 0;
installedVersions: string[] = [];
configure(input: AppUpdateRuntimeConfiguration): void {
this.configuration = input;
@@ -59,6 +62,7 @@ class FakeAppUpdateRuntime implements AppUpdateRuntime {
}
finishUpdateDownload(info: RuntimeUpdateInfo): void {
this.downloadedUpdate = info;
this.configuration?.onUpdateDownloaded(info);
}
@@ -80,12 +84,22 @@ class FakeAppUpdateRuntime implements AppUpdateRuntime {
}
if (!result || !this.gate) return result;
const admitted = await this.gate(result.updateInfo);
return { ...result, isUpdateAvailable: result.isUpdateAvailable && admitted };
const isUpdateAvailable = result.isUpdateAvailable && admitted;
this.downloadableUpdate = isUpdateAvailable ? result.updateInfo : null;
return { ...result, isUpdateAvailable };
}
async downloadUpdate(): Promise<void> {}
async downloadUpdate(): Promise<void> {
if (this.downloadableUpdate) {
this.finishUpdateDownload(this.downloadableUpdate);
}
}
quitAndInstall(): void {}
quitAndInstall(): void {
if (this.downloadedUpdate) {
this.installedVersions.push(this.downloadedUpdate.version);
}
}
}
function createService(input?: { now?: () => number; bucket?: () => Promise<number> }) {
@@ -179,6 +193,111 @@ describe("app update service", () => {
});
});
it("replaces a downloaded update when a newer release is admitted", async () => {
const { runtime, service } = createService({ bucket: async () => 0 });
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: rolledOutUpdate });
await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
runtime.finishUpdateDownload(rolledOutUpdate);
const newerUpdate = { ...rolledOutUpdate, version: "1.2.5" };
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: newerUpdate });
const result = await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
expect(result).toEqual({
hasUpdate: true,
readyToInstall: false,
currentVersion: "1.2.3",
latestVersion: "1.2.5",
body: null,
date: "2026-04-28T00:00:00.000Z",
errorMessage: null,
});
});
it("installs the newest admitted release when quitting with an older download", async () => {
const { runtime, service } = createService({ bucket: async () => 0 });
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: rolledOutUpdate });
await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
runtime.finishUpdateDownload(rolledOutUpdate);
const newerUpdate = { ...rolledOutUpdate, version: "1.2.5" };
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: newerUpdate });
const installed = await service.installUpdateOnQuit({
currentVersion: "1.2.3",
releaseChannel: "stable",
});
expect(installed).toBe(true);
expect(runtime.installedVersions).toEqual(["1.2.5"]);
});
it("does not install an older download while its replacement is still rolling out", async () => {
const now = Date.parse("2026-04-28T12:00:00.000Z");
const { runtime, service } = createService({ now: () => now, bucket: async () => 0.4 });
const olderUpdate = {
...rolledOutUpdate,
releaseDate: "2026-04-27T00:00:00.000Z",
};
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: olderUpdate });
await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
runtime.finishUpdateDownload(olderUpdate);
const newerUpdate = {
...rolledOutUpdate,
version: "1.2.5",
releaseDate: "2026-04-28T12:00:00.000Z",
};
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: newerUpdate });
const installed = await service.installUpdateOnQuit({
currentVersion: "1.2.3",
releaseChannel: "stable",
});
expect(installed).toBe(false);
expect(runtime.installedVersions).toEqual([]);
});
it("rechecks for the newest release before a manual install", async () => {
const { runtime, service } = createService({ bucket: async () => 0.99 });
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: rolledOutUpdate });
await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "manual",
});
runtime.finishUpdateDownload(rolledOutUpdate);
const newerUpdate = { ...rolledOutUpdate, version: "1.2.5" };
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: newerUpdate });
const result = await service.downloadAndInstallUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
});
expect(result.installed).toBe(true);
expect(runtime.installedVersions).toEqual(["1.2.5"]);
});
it("trusts the runtime availability decision before comparing versions", async () => {
const { runtime, service } = createService({ bucket: async () => 0 });
runtime.nextCheck({ isUpdateAvailable: false, updateInfo: rolledOutUpdate });
@@ -322,40 +441,8 @@ describe("app update service", () => {
});
});
it("keeps preparation errors emitted before the update check rejects", async () => {
const { runtime, service } = createService();
const deferredCheck = runtime.deferNextCheck();
const pending = service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
runtime.prepareUpdate(rolledOutUpdate);
runtime.failRuntime(new Error("sha512 checksum mismatch"));
deferredCheck.reject(new Error("sha512 checksum mismatch"));
const checkResult = await pending;
expect(checkResult.errorMessage).toBe("sha512 checksum mismatch");
const automaticResult = await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
expect(automaticResult).toEqual({
hasUpdate: true,
readyToInstall: false,
currentVersion: "1.2.3",
latestVersion: "1.2.4",
body: null,
date: "2026-04-28T00:00:00.000Z",
errorMessage: "sha512 checksum mismatch",
});
});
it("returns runtime update errors after an update fails to prepare", async () => {
const { runtime, service } = createService();
it("discovers newer releases after an update fails to prepare", async () => {
const { runtime, service } = createService({ bucket: async () => 0 });
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: rolledOutUpdate });
await service.checkForAppUpdate({
@@ -365,6 +452,8 @@ describe("app update service", () => {
});
runtime.failRuntime(new Error("sha512 checksum mismatch"));
const newerUpdate = { ...rolledOutUpdate, version: "1.2.5" };
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: newerUpdate });
const result = await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
@@ -375,10 +464,10 @@ describe("app update service", () => {
hasUpdate: true,
readyToInstall: false,
currentVersion: "1.2.3",
latestVersion: "1.2.4",
latestVersion: "1.2.5",
body: null,
date: "2026-04-28T00:00:00.000Z",
errorMessage: "sha512 checksum mismatch",
errorMessage: null,
});
});
@@ -445,91 +534,4 @@ describe("app update service", () => {
errorMessage: null,
});
});
it("returns runtime update errors to multiple automatic checks before a manual retry clears them", async () => {
const { runtime, service } = createService();
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: rolledOutUpdate });
await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "manual",
});
runtime.failRuntime(new Error("sha512 checksum mismatch"));
const firstAutomaticResult = await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
const secondAutomaticResult = await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
expect(firstAutomaticResult).toEqual({
hasUpdate: true,
readyToInstall: false,
currentVersion: "1.2.3",
latestVersion: "1.2.4",
body: null,
date: "2026-04-28T00:00:00.000Z",
errorMessage: "sha512 checksum mismatch",
});
expect(secondAutomaticResult).toEqual(firstAutomaticResult);
runtime.nextCheck(null);
const retryResult = await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "manual",
});
expect(runtime.checkCount).toBe(2);
expect(retryResult).toEqual({
hasUpdate: false,
readyToInstall: false,
currentVersion: "1.2.3",
latestVersion: "1.2.3",
body: null,
date: null,
errorMessage: null,
});
});
it("keeps runtime update errors visible after a manual retry fails", async () => {
const { runtime, service } = createService();
runtime.nextCheck({ isUpdateAvailable: true, updateInfo: rolledOutUpdate });
await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "manual",
});
runtime.failRuntime(new Error("sha512 checksum mismatch"));
runtime.failNextCheck(new Error("network down"));
const retryResult = await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "manual",
});
const automaticResult = await service.checkForAppUpdate({
currentVersion: "1.2.3",
releaseChannel: "stable",
intent: "automatic",
});
expect(retryResult.errorMessage).toBe("network down");
expect(automaticResult).toEqual({
hasUpdate: true,
readyToInstall: false,
currentVersion: "1.2.3",
latestVersion: "1.2.4",
body: null,
date: "2026-04-28T00:00:00.000Z",
errorMessage: "sha512 checksum mismatch",
});
});
});
@@ -62,6 +62,10 @@ export interface AppUpdateService {
},
onBeforeQuit?: () => Promise<void>,
): Promise<AppUpdateInstallResult>;
installUpdateOnQuit(input: {
currentVersion: string;
releaseChannel: AppReleaseChannel;
}): Promise<boolean>;
}
export interface AppUpdateServiceDeps {
@@ -112,10 +116,7 @@ function getErrorMessage(error: unknown): string {
export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateService {
let cachedUpdateInfo: RuntimeUpdateInfo | null = null;
let downloadedUpdateVersion: string | null = null;
let downloading = false;
let configuredReleaseChannel: AppReleaseChannel | null = null;
let runtimeErrorMessage: string | null = null;
let inFlightUpdateCheckCount = 0;
function isReadyToInstallVersion(version: string): boolean {
return downloadedUpdateVersion === version;
@@ -124,23 +125,6 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
function clearUpdateState(): void {
cachedUpdateInfo = null;
downloadedUpdateVersion = null;
downloading = false;
runtimeErrorMessage = null;
}
function buildRuntimeErrorResult(currentVersion: string): AppUpdateCheckResult | null {
if (!runtimeErrorMessage) {
return null;
}
const info = cachedUpdateInfo;
return buildCheckResult({
currentVersion,
hasUpdate: info?.version !== undefined && info.version !== currentVersion,
readyToInstall: false,
info,
errorMessage: runtimeErrorMessage,
});
}
function configureRuntime(releaseChannel: AppReleaseChannel, intent: AppUpdateCheckIntent): void {
@@ -166,23 +150,15 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
const alreadyReady = downloadedUpdateVersion === info.version;
cachedUpdateInfo = info;
downloadedUpdateVersion = alreadyReady ? info.version : null;
downloading = !alreadyReady;
runtimeErrorMessage = null;
},
onUpdateDownloaded(info) {
cachedUpdateInfo = info;
downloadedUpdateVersion = info.version;
downloading = false;
runtimeErrorMessage = null;
},
onUpdateNotAvailable() {
clearUpdateState();
},
onError(error) {
downloading = false;
if (inFlightUpdateCheckCount === 0 || cachedUpdateInfo) {
runtimeErrorMessage = getErrorMessage(error);
}
deps.reportRuntimeError?.(error);
},
});
@@ -207,28 +183,7 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
configureRuntime(releaseChannel, intent);
const runtimeErrorResult = buildRuntimeErrorResult(currentVersion);
if (runtimeErrorResult && intent === "automatic") {
return runtimeErrorResult;
}
const cachedVersion = cachedUpdateInfo?.version ?? null;
if (
!runtimeErrorResult &&
intent === "automatic" &&
cachedVersion &&
cachedVersion !== currentVersion
) {
return buildCheckResult({
currentVersion,
hasUpdate: true,
readyToInstall: isReadyToInstallVersion(cachedVersion),
info: cachedUpdateInfo,
});
}
try {
inFlightUpdateCheckCount += 1;
const result = await deps.runtime.checkForUpdates();
if (!result || !result.updateInfo || !result.isUpdateAvailable) {
clearUpdateState();
@@ -245,8 +200,6 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
if (hasUpdate) {
cachedUpdateInfo = info;
downloading = !isReadyToInstallVersion(latestVersion);
runtimeErrorMessage = null;
return buildCheckResult({
currentVersion,
hasUpdate: true,
@@ -269,8 +222,6 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
readyToInstall: false,
errorMessage: getErrorMessage(error),
});
} finally {
inFlightUpdateCheckCount -= 1;
}
}
@@ -292,6 +243,26 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
};
}
const check = await checkForAppUpdate({
currentVersion,
releaseChannel,
intent: "manual",
});
if (!check.hasUpdate) {
return {
installed: false,
version: currentVersion,
message: check.errorMessage ?? "No update available.",
};
}
return installCachedUpdate(currentVersion, onBeforeQuit);
}
async function installCachedUpdate(
currentVersion: string,
onBeforeQuit?: () => Promise<void>,
): Promise<AppUpdateInstallResult> {
if (!cachedUpdateInfo) {
return {
installed: false,
@@ -300,8 +271,6 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
};
}
configureRuntime(releaseChannel, "manual");
const readyVersion = cachedUpdateInfo.version;
if (isReadyToInstallVersion(readyVersion)) {
await performQuitAndInstall(deps.runtime, onBeforeQuit);
@@ -312,20 +281,16 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
};
}
if (downloading) {
return {
installed: false,
version: currentVersion,
message: "Update is still being prepared. Try again in a moment.",
};
}
downloading = true;
try {
await deps.runtime.downloadUpdate();
if (cachedUpdateInfo?.version !== readyVersion) {
return {
installed: false,
version: currentVersion,
message: "A newer update was found and will be installed later.",
};
}
downloadedUpdateVersion = readyVersion;
downloading = false;
await performQuitAndInstall(deps.runtime, onBeforeQuit);
return {
@@ -334,7 +299,6 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
message: "Update downloaded. The app will restart shortly.",
};
} catch (error) {
downloading = false;
const message = error instanceof Error ? error.message : String(error);
deps.reportInstallError?.(message);
return {
@@ -345,8 +309,33 @@ export function createAppUpdateService(deps: AppUpdateServiceDeps): AppUpdateSer
}
}
async function installUpdateOnQuit({
currentVersion,
releaseChannel,
}: {
currentVersion: string;
releaseChannel: AppReleaseChannel;
}): Promise<boolean> {
if (!deps.isPackaged() || !downloadedUpdateVersion) {
return false;
}
const check = await checkForAppUpdate({
currentVersion,
releaseChannel,
intent: "automatic",
});
if (!check.hasUpdate) {
return false;
}
const result = await installCachedUpdate(currentVersion);
return result.installed;
}
return {
checkForAppUpdate,
downloadAndInstallUpdate,
installUpdateOnQuit,
};
}
@@ -19,18 +19,8 @@ import {
resolveStagingUserId,
rolloutManifestSchema,
shouldAdmitToRollout,
shouldAutoInstallOnQuit,
} from "./auto-updater";
describe("shouldAutoInstallOnQuit", () => {
it("auto-installs on quit everywhere except Linux AppImage", () => {
expect(shouldAutoInstallOnQuit({ platform: "linux", isAppImage: true })).toBe(false);
expect(shouldAutoInstallOnQuit({ platform: "linux", isAppImage: false })).toBe(true);
expect(shouldAutoInstallOnQuit({ platform: "darwin", isAppImage: false })).toBe(true);
expect(shouldAutoInstallOnQuit({ platform: "win32", isAppImage: false })).toBe(true);
});
});
describe("shouldAdmitToRollout", () => {
it("admits beta, missing rollout hours, zero-hour rollout, and missing release date", () => {
expect(
+14 -19
View File
@@ -76,31 +76,16 @@ export function getStagingUserId(): Promise<string> {
return cachedStagingUserIdPromise;
}
// AppImages have no install step. electron-updater "installs" by unlinking the
// running file and mv-ing the downloaded one into place; on app quit it does this
// via a *blocking* execFileSync(newAppImage, { APPIMAGE_EXIT_AFTER_INSTALL: "true" }).
// That env var is only honored by AppImageLauncher, so without it the freshly
// launched process boots the full app and never exits — the quit hangs forever,
// with the old binary already deleted. We therefore install AppImages only on
// explicit quitAndInstall (the "Update now" button), which takes the non-blocking
// spawn path. Every other target keeps auto-install-on-quit, which works there.
export function shouldAutoInstallOnQuit(input: {
platform: NodeJS.Platform;
isAppImage: boolean;
}): boolean {
return !(input.platform === "linux" && input.isAppImage);
}
class ElectronAppUpdateRuntime implements AppUpdateRuntime {
private configured = false;
configure(input: AppUpdateRuntimeConfiguration): void {
autoUpdater.autoDownload = true;
autoUpdater.autoRunAppAfterInstall = true;
autoUpdater.autoInstallOnAppQuit = shouldAutoInstallOnQuit({
platform: process.platform,
isAppImage: Boolean(process.env.APPIMAGE),
});
// Paseo revalidates the current manifest before explicitly installing on quit.
// Electron's built-in handler would install an older download without checking
// whether a newer release has superseded it.
autoUpdater.autoInstallOnAppQuit = false;
autoUpdater.allowPrerelease = input.releaseChannel === "beta";
autoUpdater.channel = input.releaseChannel === "beta" ? "beta" : "latest";
autoUpdater.allowDowngrade = false;
@@ -194,3 +179,13 @@ export async function downloadAndInstallUpdate(
onBeforeQuit,
);
}
export async function installAppUpdateOnQuit({
currentVersion,
releaseChannel,
}: {
currentVersion: string;
releaseChannel: AppReleaseChannel;
}): Promise<boolean> {
return appUpdateService.installUpdateOnQuit({ currentVersion, releaseChannel });
}