Skip to content

fix(sync): flush pending CRDT write-backs on shutdown instead of dropping them#554

Merged
h4yfans merged 1 commit into
mainfrom
fix/flush-writebacks-on-shutdown
Jun 12, 2026
Merged

fix(sync): flush pending CRDT write-backs on shutdown instead of dropping them#554
h4yfans merged 1 commit into
mainfrom
fix/flush-writebacks-on-shutdown

Conversation

@h4yfans

@h4yfans h4yfans commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

On shutdown, CrdtProvider.destroy() called cancelPendingWritebacks(), which cleared the debounced (500 ms) markdown write-back timers without running them. Any note/journal edit made within ~500 ms of quit never reached the on-disk .md file or the search index. The CRDT state is persisted (so the edit isn't permanently lost), but the file silently lagged the true content and did not self-heal — only a new edit re-triggered a write-back. For a "files on disk, not a database" product, that on-disk/UI divergence (which also propagates stale via folder sync and stale search results) is a real correctness gap.

This flushes pending write-backs on shutdown instead of cancelling them.

Closes #543.

Changes

  • crdt-writeback.tspendingTimers now stores { timer, doc } per pending note (was just the timer handle, which a flush couldn't reach). New exported async flushPendingWritebacks(): snapshots pending entries, clears their timers, and runs every performWriteback to completion; per-note failures are caught and logged, so the flush never rejects. cancelPendingWritebacks() is retained (still used elsewhere).
  • crdt-provider.tsdestroy() now await flushPendingWritebacks() as its first line, before the entry.doc.destroy() loop (the flush reads from the live Y.Docs). destroy() is already awaited from stopSyncRuntime() → the Electron before-quit handler, so the async flush is awaited on the real shutdown path.

Tests

Added 4 cases to crdt-writeback.test.ts (fake timers, so the only way a write happens is the flush):

  1. flush runs a scheduled write-back without advancing the debounce timer
  2. flush clears pending timers so they do not fire a second time
  3. flush with nothing pending is a no-op
  4. a failing write-back during flush is logged, not thrown (flush still resolves)

Two existing test files that mock ./crdt-writeback and exercise destroy() were updated to add flushPendingWritebacks to their module mocks (crdt-provider.test.ts also flips its destroy() assertion to the new function).

Verification

  • pnpm --filter @memry/desktop typecheck:node — exit 0
  • pnpm exec vitest run --config config/vitest.config.ts --project main for the 3 sync test files — 44 passed (incl. the 4 new)
  • pnpm lint — exit 0 (0 errors)

Notes

  • Docs gate skipped (MEMRY_DOCS_IMPACT_SKIP=1): internal shutdown-sequencing correctness fix; no public API or user-facing config, and no existing doc describes the prior drop-on-quit behavior.
  • Deferred (filed separately if found): the same drop-on-quit shape may exist for other debounced projections; this PR covers only the markdown write-back.

@vercel

vercel Bot commented Jun 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
memry-docs Building Building Preview, Comment Jun 12, 2026 9:59pm
memrynote-landing Building Building Preview, Comment Jun 12, 2026 9:59pm

@h4yfans h4yfans marked this pull request as ready for review June 12, 2026 22:01
@h4yfans h4yfans merged commit ff12ee6 into main Jun 12, 2026
10 of 16 checks passed
@h4yfans h4yfans deleted the fix/flush-writebacks-on-shutdown branch June 12, 2026 22:01
@github-actions github-actions Bot added bug Something isn't working documentation Improvements or additions to documentation test labels Jun 12, 2026
@github-actions

Copy link
Copy Markdown

React Doctor

No React Doctor issues found in this scan.

Score Issues Errors Warnings Affected Files Scope
100 / 100 (Great) 0 0 0 0 6 files changed on fix/flush-writebacks-on-shutdown vs. main

View workflow run

Generated by React Doctor. Questions? Contact founders@million.dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flush pending CRDT markdown write-backs on shutdown instead of dropping them

1 participant