From 6b18a1ce28db6f25cefeea44de3e171902fc127b Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sun, 5 Apr 2026 13:34:25 -0700 Subject: [PATCH] fix: centralize updater restart deferral in main process (#313) * fix: defer updater quitAndInstall to allow React/UI state to settle PR #299 introduced a regression where the one-click auto-update flow would synchronously invoke `quitAndInstall` immediately upon receiving the `downloaded` state transition. This triggered a synthetic `beforeunload` event (to capture terminal buffers) and an IPC call to quit in the exact same event loop tick that React and the Toast library were processing state updates. This disrupted the renderer's synchronous shutdown and flush logic, causing the app to fail to quit and install properly. This adds a `setTimeout` to defer `quitAndInstall`, pushing it to the bottom of the event queue. This allows the UI state to settle and ensures the `beforeunload` flush sequence executes cleanly. Test cases have also been updated to use fake timers. * fix: centralize updater restart deferral in main process --- src/main/updater.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/main/updater.ts | 19 ++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) 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(