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
This commit is contained in:
Jinjing
2026-04-05 13:34:25 -07:00
committed by GitHub
parent 6754fa22f9
commit 6b18a1ce28
2 changed files with 52 additions and 1 deletions
+34
View File
@@ -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()
+18 -1
View File
@@ -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<void> | null = null
let autoUpdateCheckTimer: ReturnType<typeof setTimeout> | null = null
let pendingQuitAndInstallTimer: ReturnType<typeof setTimeout> | 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(