mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #2540] [MERGED] perf(db): make the file-backed store flush async so it stops blocking the event loop #5643
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#5643
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2540
Author: @Romuromylus
Created: 6/9/2026
Status: ✅ Merged
Merged: 6/9/2026
Merged by: @SpicyMarinara
Base:
staging← Head:fix/file-store-async-flush📝 Commits (3)
58ffdfdperf(db): make the file-backed store flush async so it stops blocking the event loop55aac5cfix(db): snapshot the dirty set before the async flush so interleaved writes aren't loste5dd8d0Merge branch 'staging' into fix/file-store-async-flush📊 Changes
1 file changed (+32 additions, -36 deletions)
View changed files
📝
packages/server/src/db/file-backed-store.ts(+32 -36)📄 Description
Linked issue
Closes #2528
Why this change
The file-backed store's flush path was fully synchronous and ran O(total table size) work on the Node event loop:
atomicWriteFiledidcopyFileSync(a whole-file.bakcopy),writeFileSync, and threefsyncSynccalls.messagesis a single global file-backed table thatmarkDirtys on every insert/update/delete, so during active chat each debounced flush synchronously re-serialized and whole-file-copied the entiremessagestable (multi-MB on a long transcript), blocking every other request on the shared server. The cost grows linearly with accumulated chat history; the 750ms debounce only prevents a write per op, not the O(table size) cost per flush.What changed
packages/server/src/db/file-backed-store.ts:flushFileandatomicWriteFiletofs/promises(open+FileHandle.sync(),copyFile,writeFile,rename,unlink).saveFileSnapshotsasync andawaiteachatomicWriteFile(sequential — preserves ordering and bounds concurrent handles to one, matching prior behavior).awaitthe now-asyncsaveFileSnapshots()in the already-asyncflush().fsimports (copyFileSync/fsyncSync/writeFileSync/renameSync/unlinkSync), keeping the ones still used bylooksNulFilled/loadFileSnapshots/etc.Crash-safety is preserved exactly: same tmp-path naming, same
.bakrefresh gating (refreshBackup && existsSync && !looksNulFilled), same fsync-before-rename on the.baktmp / main tmp / parent dir, same cleanup-tmp-on-error then rethrow. The only behavioral change is that the multi-MB serialize→copy→fsync now yields the event loop instead of blocking it.JSON.stringify(rows)stays synchronous (the deeper per-chat-partitioning win is out of scope here).All three
atomicWriteFile/saveFileSnapshotscall sites are awaited;flush()'s fire-and-forget callers (markDirty debounce, safety timer, beforeExit handler, controller) already usedvoid this.flush()and still return a Promise, so no signature change is needed at any of them.Not a regression: byte-identical on
main; shared-engine perf characteristic.Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
pnpm checkpasses locally. Added a local-only node:test (file-backed-store-flush.test.ts, gitignored) driving the store throughcreateFileNativeDBagainst a tempFILE_STORAGE_DIR:flush()returns a Promise, the table JSON contains the row, and no.tmp-*files remain (atomic rename completed)..bakholding the prior version (provescopyFile → fsync → renameworks throughfs/promises).Local run: 3/3 pass.
Still to verify manually against the deploy (non-regression smoke, not the gate): create a character + chat, send several messages, refresh and restart the service, and confirm all data persists (durable flush on shutdown via
close() → flush(true)), withtables/messages.jsonvalid and a.bakpresent after activity.Docs and release impact
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.