mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2528] [Issue]: File-store flush synchronously serializes whole table + copies file to .bak on the event loop #1256
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#1256
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?
Originally created by @Romuromylus on GitHub (Jun 9, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2528
Originally assigned to: @Romuromylus on GitHub.
Summary
Severity: Medium — performance. Each file-store flush synchronously serializes the entire dirty table and
copyFileSyncs the whole previous file to a.bak, plus severalfsyncSynccalls, all on the Node event loop.messagesis a single global table holding every message across every chat, so on a large transcript each flush blocks the loop for a multi-MB serialize + multi-MB file copy, stalling concurrent requests on the shared server. Cost grows linearly with accumulated chat history.Expected behavior
Flush should not block the event loop with
O(total DB size)synchronous work on a hot write path. The serialize + backup copy + fsync should run via asyncfs/promises(so the flush yields), and/or the whole-file backup copy should be replaced with rename-based double-buffering.Actual behavior
The 750 ms debounce correctly prevents a write per op (the naive "rewrites whole file per op" claim is false —
saveFileSnapshotsonly writes dirty tables). But each flush that does fire paysO(rows in the dirty table)synchronously.atomicWriteFile(packages/server/src/db/file-backed-store.ts:329-376, driven bysaveFileSnapshotsat:1226):messagesis a single global table (:154);rows("messages")returns the entire in-memory array, andmarkDirty("messages")fires on every insert/update/delete. So during active chatting each debounced flush re-serializes and whole-file-copies the entire messages table. For a long transcript (multi-MBmessages.json), that is a multi-MB synchronous serialize + multi-MB synchronous copy + multiple fsyncs blocking the loop for every other user's request, growing with one user's chat history.Steps to reproduce
messagestable (a long transcript or many chats; multi-MBmessages.json).Environment
staging @ a9fdae8)Logs, screenshots, or video
Static source audit, cited to file:line above.
Additional context
Fix: swap the
*Syncfs calls inatomicWriteFileforfs/promisesasync variants so the flush yields the event loop, and/or replace the whole-filecopyFileSyncbackup with rename-based double-buffering (keep the just-replaced file as the backup) so each flush is one serialize + one write rather than copy-then-write. Partitioning the hotmessagestable per chat would cap serialize/copy cost to the active chat, but that is a larger change.Related: this compounds with the updater-flush data-loss issue (the same store), but is a distinct cost (synchronous flush vs missing flush).
Regression notes: found via static audit;
file-backed-store.tsis byte-identical onmain(65a0395,git diffempty), so this is a shared-engine performance characteristic, not amain→stagingregression.Template check
Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:
@Romuromylus commented on GitHub (Jun 9, 2026):
Related to #2527 — same subsystem (the file-backed write-back cache). #2527 is the missing flush on the in-app updater's
process.exit(0); this issue is the synchronousO(DB size)cost of each flush. MakingatomicWriteFileasync here and adding the durable flush in #2527 both land infile-backed-store.ts/connection.ts.