Repository navigation
fix(persistence): make the renderer unload checkpoint durably flush before reporting success - #12387
Conversation
…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.
|
Warning Review limit reached
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 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 configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (12)
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. Comment |
…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.
Summary
The renderer's before-unload checkpoint reported success before the state it staged was on disk.
app:stage-before-unload-syncstaged sessions/scrollback/UI synchronously, then queuedstore.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 thewill-quitstore.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:
app:await-before-unload-checkpointinvoke channel. The flush usesflushPendingOrThrowAsync(honest failures) instead of the failure-swallowingflushPendingAsync.prepareRendererForAppRestartawaits 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.app.restart,updater.quitAndInstall, and the lazy-chunk recovery reload. The browser fallback resolves immediately because its staging already writes through to browser storage.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.tsand thewill-quitbarrier are unchanged.Screenshots
No visual change.
Testing
pnpm lintpnpm typecheckpnpm 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 exceptsrc/main/ipc/pty.test.ts,src/main/ipc/worktree-base-directory-poller.test.tsandsrc/shared/setup-agent-sequencing.test.ts, which fail identically on the base commit in this environment.pnpm buildNew/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 returnscheckpoint-refusedwithout navigating when the write fails, and navigates only after it lands.src/main/quit-path-durable-write-blocking.test.tspasses 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:
awaitis in the renderer/preload preparation path, not in the sync IPC reply, andquit-path-durable-write-blocking.test.tsstill asserts no sync fs syscalls on either path.awaiton a stalled write could strand a restart or an update install — mitigated by the 20s deadline plus anAbortSignal, which reports an honest failure instead of hanging. Flagged and fixed during review.flushPendingOrThrowAsyncdrains to a stable write generation by default, which a live app (streaming PTY output) can keep re-dirtying. The checkpoint passesdrainToStableGeneration: false, matching the oldflushPendingAsyncsemantics — the staged snapshot lands in the first write. Covered by an assertion.app:relaunch/app:restart/will-quitordering,quit-teardown-deadline.ts, and the finalstore.flushAsync()barrier are unchanged. One deliberate difference: once the quit flush has started,flushPendingOrThrowAsyncrejects whereflushPendingAsyncwould have started another write; the staged state is still in memory, so the final quit flush persists it — no loss, one fewer racing write.ipcMain.handlethrows on a second registration, andopenMainWindow()can run again on macOSactivate.registerCoreHandlershas aregisteredguard, so the new handler registers exactly once.hostId) and local staged sessions go through the same store write, and folder workspaces are unaffected — no git or worktree assumptions are involved.Security Audit
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 siblingapp:*handlers it does not pin a sender; the worst a renderer can do is read a boolean-shaped status.stage-before-unload-sync(unchanged in reachability) can start one.Notes
app.reload()(the bare "reload window" IPC, used by the GitHub auth error helper) andapp.relaunch()(app.exit(0), which skipsbefore-quitand 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.WILL_QUIT_TEARDOWN_DEADLINE_MS; on a stalled network profile mount a restart now refuses after 20s rather than proceeding with unpersisted state.