Skip to content

fix(persistence): make the renderer unload checkpoint durably flush before reporting success - #12387

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/fix-unload-checkpoint
Aug 4, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/fix-unload-checkpoint

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Summary

The renderer's before-unload checkpoint reported success before the state it staged was on disk. app:stage-before-unload-sync staged sessions/scrollback/UI synchronously, then queued store.flushPendingAsync() fire-and-forget (void + .catch(console.error), and that method swallows failures) and immediately returned { ok: true }. The renderer guard marked the checkpoint complete, so restart, update-install and lazy-chunk recovery reload navigated while the snapshot still existed only in memory. A normal quit is covered by the will-quit store.flushAsync() barrier; those paths have no such barrier, so a crash, power loss or failed write in that window silently lost state the old synchronous path had already persisted.

This keeps staging synchronous — no durable writes back on the main thread (that is exactly what #11931 removed) — and fixes the ordering instead:

  • The main handler now keeps the staged flush's outcome and exposes it on a new app:await-before-unload-checkpoint invoke channel. The flush uses flushPendingOrThrowAsync (honest failures) instead of the failure-swallowing flushPendingAsync.
  • prepareRendererForAppRestart awaits that durable write after a beforeunload attempt is allowed to navigate. On failure it dispatches the abort event and throws, so the renderer resets its checkpoint guard and the navigation is cancelled and retryable — the same contract a vetoed unload already had.
  • Wired through all three preparation paths: app.restart, updater.quitAndInstall, and the lazy-chunk recovery reload. The browser fallback resolves immediately because its staging already writes through to browser storage.
  • The wait is bounded by a 20s deadline (matching WILL_QUIT_TEARDOWN_DEADLINE_MS) so a stalled disk or network profile mount fails the checkpoint instead of stranding a restart forever.

User-visible: a restart/update/reload no longer silently discards the last session, scrollback and UI state when the process dies right after navigating; if the state genuinely cannot be written, the restart is refused instead of proceeding and losing it.

The quit path is untouched: the sync reply still returns as soon as staging completes, nothing on the quit path awaits the new channel, and quit-teardown-deadline.ts and the will-quit barrier are unchanged.

Screenshots

No visual change.

Testing

  • pnpm lint
  • pnpm typecheck
  • pnpm test — ran the affected suites instead of the full matrix: src/main/ipc, src/preload, src/shared, src/main/quit-path-durable-write-blocking.test.ts, src/main/quit-teardown-deadline.test.ts, src/main/persistence-async-write-syscalls.test.ts, src/main/agent-auth-restart-preservation.test.ts, src/renderer/src/lib/lazy-with-retry.test.ts, src/renderer/src/web/web-preload-api.test.ts, src/renderer/src/app-startup-routing.test.ts. All green except src/main/ipc/pty.test.ts, src/main/ipc/worktree-base-directory-poller.test.ts and src/shared/setup-agent-sequencing.test.ts, which fail identically on the base commit in this environment.
  • pnpm build
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

New/updated coverage:

  • src/main/ipc/renderer-shutdown-checkpoint.test.ts — the checkpoint stays pending until the durable write settles; a rejected write reports { ok: false } instead of success; a write that outlives the deadline reports failure and aborts the flush signal; staging failure short-circuits the flush; staging itself still does not wait on the write.
  • src/shared/renderer-restart-preparation.test.ts — restart preparation blocks on the durable write and aborts (dispatching the abort event) when it fails.
  • src/preload/renderer-restart-wiring.test.ts — an update install is never invoked and never marked prepared when the checkpoint fails to persist.
  • src/renderer/src/lib/lazy-chunk-recovery-reload.test.ts (new) — the recovery reload returns checkpoint-refused without navigating when the write fails, and navigates only after it lands.
  • src/main/quit-path-durable-write-blocking.test.ts passes unchanged: staging and the quit flush still issue zero synchronous fs syscalls.

AI Review Report

Self-review with Claude (Opus) over the full diff. Risks checked:

  • Reintroducing main-thread blocking (the fix(quit): stop durable state writes from parking the main thread on quit #11931 regression). Verified staging remains synchronous-in-memory only; the added await is in the renderer/preload preparation path, not in the sync IPC reply, and quit-path-durable-write-blocking.test.ts still asserts no sync fs syscalls on either path.
  • New hang surface. An unbounded await on a stalled write could strand a restart or an update install — mitigated by the 20s deadline plus an AbortSignal, which reports an honest failure instead of hanging. Flagged and fixed during review.
  • Flush livelock. flushPendingOrThrowAsync drains to a stable write generation by default, which a live app (streaming PTY output) can keep re-dirtying. The checkpoint passes drainToStableGeneration: false, matching the old flushPendingAsync semantics — the staged snapshot lands in the first write. Covered by an assertion.
  • Quit-path drift. app:relaunch/app:restart/will-quit ordering, quit-teardown-deadline.ts, and the final store.flushAsync() barrier are unchanged. One deliberate difference: once the quit flush has started, flushPendingOrThrowAsync rejects where flushPendingAsync would have started another write; the staged state is still in memory, so the final quit flush persists it — no loss, one fewer racing write.
  • Double IPC registration. ipcMain.handle throws on a second registration, and openMainWindow() can run again on macOS activate. registerCoreHandlers has a registered guard, so the new handler registers exactly once.
  • Cross-platform (macOS, Linux, Windows). No platform branches, no shortcuts, labels, paths or shell invocations added or changed; the diff is promise ordering plus one Electron IPC channel, which behaves identically on all three. The macOS-specific window-reopen lifecycle is covered above. Both remote (SSH/runtime hostId) and local staged sessions go through the same store write, and folder workspaces are unaffected — no git or worktree assumptions are involved.
  • Paired web / non-Electron hosts. The browser fallback implements the new API as an immediate resolve (its staging is already durable in browser storage), and the recovery-reload default tolerates a host with no preload bridge.

Security Audit

  • New IPC channel app:await-before-unload-checkpoint: invoke-only, accepts no arguments, performs no work, and returns only { ok: boolean } — no state, paths or error text cross the boundary (failures are logged main-side only). Like the sibling app:* handlers it does not pin a sender; the worst a renderer can do is read a boolean-shaped status.
  • No new input handling, command execution, path handling, auth, secrets or dependencies. The staged payload, its validation and its write path are unchanged; only the ordering of an existing write is.
  • Denial-of-service consideration: repeated calls cannot pile up work — the handler returns the already-running promise rather than starting a flush, and only stage-before-unload-sync (unchanged in reachability) can start one.
  • No follow-up required.

Notes

  • Out of scope: app.reload() (the bare "reload window" IPC, used by the GitHub auth error helper) and app.relaunch() (app.exit(0), which skips before-quit and beforeunload entirely) still do no checkpoint preparation of their own. Routing them through preparation would add hot-exit backup and unload-veto semantics to those buttons, which is a behavior change beyond this fix. Main-initiated reloads (View → Reload, Cmd+R) likewise stage during the native beforeunload with no barrier — the main process keeps the staged state in memory there, so only a crash in that window loses it.
  • The 20s checkpoint deadline is deliberately the same budget as WILL_QUIT_TEARDOWN_DEADLINE_MS; on a stalled network profile mount a restart now refuses after 20s rather than proceeding with unpersisted state.

…efore reporting success

The sync before-unload checkpoint staged renderer state and then queued
store.flushPendingAsync() fire-and-forget, so reload/restart/update paths
navigated while the staged session, scrollback and UI state were still
only in memory. Quit is covered by the will-quit flush barrier; those
paths were not.

Keep staging synchronous (no sync durable writes), but record the flush
outcome and expose it on app:await-before-unload-checkpoint. Restart,
updater install and lazy-chunk recovery reload now join that write before
navigating and abort the attempt when it fails or outlives a 20s deadline.
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@brennanb2025, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 8 seconds

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cde48961-8c4b-472b-8ad4-25d5de83911c

📥 Commits

Reviewing files that changed from the base of the PR and between 73e7df7 and e158224.

📒 Files selected for processing (12)
  • src/main/ipc/renderer-shutdown-checkpoint.test.ts
  • src/main/ipc/renderer-shutdown-checkpoint.ts
  • src/main/persistence.ts
  • src/preload/api-types.ts
  • src/preload/index.ts
  • src/preload/renderer-restart-wiring.test.ts
  • src/preload/renderer-restart-wiring.ts
  • src/renderer/src/lib/lazy-chunk-recovery-reload.test.ts
  • src/renderer/src/lib/lazy-chunk-recovery-reload.ts
  • src/renderer/src/web/web-preload-api.ts
  • src/shared/renderer-restart-preparation.test.ts
  • src/shared/renderer-restart-preparation.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@brennanb2025
brennanb2025 merged commit 194e1a8 into main Aug 4, 2026
43 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…efore reporting success (stablyai#12387)

The sync before-unload checkpoint staged renderer state and then queued
store.flushPendingAsync() fire-and-forget, so reload/restart/update paths
navigated while the staged session, scrollback and UI state were still
only in memory. Quit is covered by the will-quit flush barrier; those
paths were not.

Keep staging synchronous (no sync durable writes), but record the flush
outcome and expose it on app:await-before-unload-checkpoint. Restart,
updater install and lazy-chunk recovery reload now join that write before
navigating and abort the attempt when it fails or outlives a 20s deadline.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant