diff --git a/src/main/updater.test.ts b/src/main/updater.test.ts index 3cc6d575f..2611ed5af 100644 --- a/src/main/updater.test.ts +++ b/src/main/updater.test.ts @@ -185,6 +185,40 @@ describe('updater', () => { ) }) + it('defers quitAndInstall through the shared main-process entrypoint', async () => { + vi.useFakeTimers() + + const mainWindow = { webContents: { send: vi.fn() } } + const { setupAutoUpdater, quitAndInstall } = await import('./updater') + + setupAutoUpdater(mainWindow as never) + quitAndInstall() + + expect(autoUpdaterMock.quitAndInstall).not.toHaveBeenCalled() + + await vi.advanceTimersByTimeAsync(99) + expect(autoUpdaterMock.quitAndInstall).not.toHaveBeenCalled() + + await vi.advanceTimersByTimeAsync(1) + expect(autoUpdaterMock.quitAndInstall).toHaveBeenCalledTimes(1) + expect(autoUpdaterMock.quitAndInstall).toHaveBeenCalledWith(false, true) + }) + + it('ignores duplicate quitAndInstall requests while the shared delay is pending', async () => { + vi.useFakeTimers() + + const mainWindow = { webContents: { send: vi.fn() } } + const { setupAutoUpdater, quitAndInstall } = await import('./updater') + + setupAutoUpdater(mainWindow as never) + quitAndInstall() + quitAndInstall() + + await vi.advanceTimersByTimeAsync(100) + + expect(autoUpdaterMock.quitAndInstall).toHaveBeenCalledTimes(1) + }) + it('runs a startup check immediately when the last background check is stale', async () => { const mainWindow = { webContents: { send: vi.fn() } } const setLastUpdateCheckAt = vi.fn() diff --git a/src/main/updater.ts b/src/main/updater.ts index baac75084..73ed57f66 100644 --- a/src/main/updater.ts +++ b/src/main/updater.ts @@ -19,6 +19,7 @@ import { const AUTO_UPDATE_CHECK_INTERVAL_MS = 36 * 60 * 60 * 1000 const AUTO_UPDATE_RETRY_INTERVAL_MS = 60 * 60 * 1000 +const QUIT_AND_INSTALL_DELAY_MS = 100 let mainWindowRef: BrowserWindow | null = null let currentStatus: UpdateStatus = { state: 'idle' } @@ -30,6 +31,7 @@ let availableReleaseUrl: string | null = null let pendingCheckFailureKey: string | null = null let pendingCheckFailurePromise: Promise | null = null let autoUpdateCheckTimer: ReturnType | null = null +let pendingQuitAndInstallTimer: ReturnType | null = null let persistLastUpdateCheckAt: ((timestamp: number) => void) | null = null /** Guards against the macOS `activate` handler re-opening the old version * while Squirrel's ShipIt is replacing the .app bundle. */ @@ -78,6 +80,11 @@ function getPendingInstallVersion(): string { } function performQuitAndInstall(): void { + if (pendingQuitAndInstallTimer) { + clearTimeout(pendingQuitAndInstallTimer) + pendingQuitAndInstallTimer = null + } + markMacQuitAndInstallInFlight() // Set this BEFORE anything else so the `activate` handler in index.ts @@ -233,6 +240,10 @@ export function isQuittingForUpdate(): boolean { } export function quitAndInstall(): void { + if (pendingQuitAndInstallTimer) { + return + } + if ( deferMacQuitUntilInstallerReady( currentStatus, @@ -244,7 +255,13 @@ export function quitAndInstall(): void { return } - performQuitAndInstall() + // Why: every renderer entrypoint reaches this IPC handler from an in-flight + // click or toast callback. Deferring the actual quit here gives the renderer + // a moment to flush dismissals/state updates before windows start closing, + // and centralizing it avoids drift between the toast flow and settings UI. + pendingQuitAndInstallTimer = setTimeout(() => { + performQuitAndInstall() + }, QUIT_AND_INSTALL_DELAY_MS) } export function setupAutoUpdater(