fix(desktop): solve #6048 — update button closes the app without installing the update

Merged
GI
3mo ago
+208-15

Root cause

app.on("before-quit") in apps/desktop/src/main/index.ts unconditionally ended with app.exit(0).

On macOS the install is a two-phase handoff, not a synchronous operation:

  1. installUpdate() → autoUpdater.quitAndInstall() → MacUpdater.handleUpdateDownloaded() → Electron's native autoUpdater.quitAndInstall().
  2. That call only starts the Squirrel.Mac handoff. ShipIt is launched asynchronously and does the real work — swap the app bundle, then relaunch — after the app process terminates through the normal termination path.

app.exit() "exits immediately… before-quit and will-quit will not be emitted" (Electron docs). So our handler was hard-killing the browser process out from under the Squirrel handoff and skipping will-quit entirely. Result is exactly what the issue reports: the app closes, nothing relaunches it, and on manual reopen it's still on the old version with the same update prompt.

The same hazard applied to a plain quit while an update was staged, since autoUpdater.autoInstallOnAppQuit = true (auto-updater.ts:309) relies on a normal termination to install on quit.

The fix

  • Extracted the before-quit cleanup into apps/desktop/src/main/lib/quit-sequence.ts (runQuitCleanup) with injected dependencies. main/index.ts has heavy import-time side effects (local DB, shell env, protocol registration), so the quit logic was untestable while inline.
  • When the quit is an update handoff (isUpdateReadyToInstall() — covers both the Update button and quitting while an update is staged), skip the forced app.exit(0) and let Electron's normal termination run so ShipIt can complete the swap and relaunch.
  • The forced exit is retained as a watchdog on a 15s timer, so the original reason for app.exit(0) — a wedged quit hanging forever on a lingering handle — is still covered.
  • All existing cleanup (host services, terminal-host detach, persistence, tray, network logger) is unchanged, including the important detail that terminal-host is detached, never torn down, on an update quit so PTY sessions reattach after the relaunch.

Tests

apps/desktop/src/main/lib/quit-sequence.test.ts (6 tests, bun:test).

Reproduction — with the pre-fix logic extracted verbatim, two tests fail:

(fail) runQuitCleanup > does not force-exit while an update install is in flight
  expect(received).not.toHaveBeenCalled()
  Expected number of calls: 0
  Received number of calls: 1
(fail) runQuitCleanup > arms a watchdog exit if Squirrel never terminates the app
  Expected length: 1 / Received length: 0
 4 pass, 2 fail

After the fix: 6 pass, 0 fail. Full bun test src/main/lib in apps/desktop is green (490 pass, 0 fail), and bun run --filter @superset/desktop typecheck plus bun run lint both exit 0.

Verification scope

The failing→passing evidence above is a unit test of the quit sequencing, which is where the defect lives. The Squirrel.Mac/ShipIt side can't be exercised in CI; it was verified against the real pipeline instead:

E2E baseline (2026-08-01, pre-fix signed canary, arm64 macOS): drove the actual UpdatesPill click via CDP on the production feed and captured the full handoff in the unified log. Click 16:46:43.401 PT → renderer gone +9ms → ShipIt spawned +110ms ("Detected this as an install request") → "Beginning installation" +216ms → bundle swapped → auto-relaunch on the new version at +10s (main.log: Updated: …183003 → …230704).

This confirms the mechanism: ShipIt's spawn is an async XPC-triggered launchd job racing the app's teardown. On this machine cleanup + app.exit(0) takes longer than the spawn, so the race always wins (updates worked); #6048 hits machines where the forced exit lands inside that ~100ms window and ShipIt never spawns. The failure is therefore hardware/timing-dependent and not reproducible here — fix-leg verification is a rerun of the same harness on the first post-merge canary (updating from a build containing this fix), plus reporter confirmation on the next stable release.

Closes #6048


Open in Stage

Summary by cubic

Fixes #6048: pressing Update on macOS closed the app without installing or relaunching. We now let the normal quit path complete during the update handoff and add a 15s watchdog to avoid hangs.

  • Bug Fixes

    • Skip forced app.exit(0) while an update is installing so Squirrel.Mac/ShipIt can swap and relaunch.
    • Add a 15s watchdog to force-exit if the handoff stalls.
    • Keep existing cleanup; detach terminal host on update quits so PTYs reattach after relaunch.
  • Refactors

    • Extracted quit logic to apps/desktop/src/main/lib/quit-sequence.ts (runQuitCleanup) with injected deps; before-quit now calls it.
    • Added unit tests in quit-sequence.test.ts (6 passing) to cover the update install path.