Skip to content

fix(desktop): make crash-recovery buffer writes durable against power loss - #4864

Merged
Jocs merged 2 commits into
developfrom
fix/buffer-store-durable
Jul 5, 2026
Merged

Jocs merged 2 commits into
developfrom
fix/buffer-store-durable

Conversation

@Jocs

@Jocs Jocs commented Jul 5, 2026

Copy link
Copy Markdown
Member

Problem

Follow-up to the #3786 / #3828 review (#4852): the atomic-save discussion noted that the crash-recovery buffer store — the precedent that change cited — shares the same durability gap.

EditorBufferStore.writeBufferStoreFile persists unsaved tab content (the buffer restored after a crash) with a temp-file + renameSync. That is namespace-atomic, so it survives an application crash, but it omits fsync. After a power loss / OS reboot the kernel can commit the new directory entry (rename) while the temp file's data blocks were never flushed — leaving the buffer truncated or full-length zero-filled. Same failure class as the document save path in #3786.

Fix

Write through write-file-atomic's sync API (writeFileAtomic.sync), which fsyncs the temp file before the atomic rename. This also removes the hand-rolled temp/rename/cleanup block and the writeSequence counter — write-file-atomic handles unique temp naming and cleanup internally.

Notes

  • Scoped to the buffer store only. The other main-process writers (generic mt::fs::* IPC handlers, keybindings/recents config, the debug dump, the upload temp file) aren't document-critical or are inherently temporary, so they're intentionally left as-is.
  • write-file-atomic (+ @types) is also added by fix(desktop): save files atomically to prevent data loss on crash #4852 for the document save path; whichever merges first, the other's dependency addition is a no-op.

Tests

test/unit/specs/buffer-store-durable.spec.ts: asserts writeBufferStoreFile writes the state as JSON, overwrites cleanly, and leaves no temp file behind. typecheck, eslint, and build:unpack (confirming the new main-process import externalizes) all pass.

Jocs and others added 2 commits July 5, 2026 10:29
Adds write-file-atomic (+ @types) for durable, atomic file writes. Used by
the crash-recovery buffer store; the document save path adopts it separately
in #4852.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… loss

writeBufferStoreFile persists unsaved tab content (the crash-recovery
buffer) with a temp-file + rename. That is namespace-atomic — safe against
an application crash — but omits fsync, so a power loss / OS reboot can
still commit the new directory entry while the data blocks were never
flushed, leaving a truncated or full-length zero-filled buffer. This is the
same gap the document save path had (#3786).

Write through write-file-atomic's sync API (fsync before the atomic rename),
which also removes the hand-rolled temp/rename/cleanup and the writeSequence
counter (its unique temp naming is handled internally).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Website preview ready: https://pr-4864-marktext-website.ransixi.workers.dev

Built from 2a0512506ebf572c4dfe62769d2cc25c5239ce98 · Worker: marktext-website · Version: 4e461d8c-752b-4283-bc75-01f1305b188b · Alias: pr-4864

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Build artifacts for PR #4864:

Run: https://github.com/marktext/marktext/actions/runs/28727006163

Artifact Size Link
marktext-windows-arm64 275.7 MB Download
marktext-macos-x64 293.1 MB Download
marktext-linux 627.7 MB Download
marktext-windows-x64 283.9 MB Download
marktext-macos-arm64 282.7 MB Download

@Jocs
Jocs merged commit ac273f4 into develop Jul 5, 2026
18 checks passed
@Jocs
Jocs deleted the fix/buffer-store-durable branch July 5, 2026 02:59
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