[GH-ISSUE #2528] [Issue]: File-store flush synchronously serializes whole table + copies file to .bak on the event loop #1256

Closed
opened 2026-10-21 05:30:02 +07:00 by bsakaguchi · 1 comment
Owner

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 several fsyncSync calls, all on the Node event loop. messages is 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 async fs/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 — saveFileSnapshots only writes dirty tables). But each flush that does fire pays O(rows in the dirty table) synchronously. atomicWriteFile (packages/server/src/db/file-backed-store.ts:329-376, driven by saveFileSnapshots at :1226):

if (refreshBackup && existsSync(path) && !looksNulFilled(path)) {
  copyFileSync(path, bakTmpPath);   // whole-file copy, synchronous
  flushFile(bakTmpPath);            // fsyncSync
  renameSync(bakTmpPath, bakPath);
}
writeFileSync(tmpPath, content);     // content = JSON.stringify(entire table), synchronous
flushFile(tmpPath);                  // fsyncSync
renameSync(tmpPath, path);
flushFile(dirname(path));            // fsyncSync on dir

messages is a single global table (:154); rows("messages") returns the entire in-memory array, and markDirty("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-MB messages.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

  1. Accumulate a large messages table (a long transcript or many chats; multi-MB messages.json).
  2. Chat actively (or run autonomous agents writing per turn) so flushes fire on the debounce.
  3. Observe each flush stringify + whole-file-copy the entire table synchronously; on a multi-user server, one user's flush blocks the event loop for all others.

Environment

  • Marinara Engine version: 1.6.1 (staging @ a9fdae8)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above.

Additional context

Fix: swap the *Sync fs calls in atomicWriteFile for fs/promises async variants so the flush yields the event loop, and/or replace the whole-file copyFileSync backup 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 hot messages table 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.ts is byte-identical on main (65a0395, git diff empty), so this is a shared-engine performance characteristic, not a main→staging regression.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
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 `copyFileSync`s the whole previous file to a `.bak`, plus several `fsyncSync` calls, all on the Node event loop. `messages` is 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 async `fs/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 — `saveFileSnapshots` only writes dirty tables). But each flush that *does* fire pays `O(rows in the dirty table)` synchronously. `atomicWriteFile` (`packages/server/src/db/file-backed-store.ts:329-376`, driven by `saveFileSnapshots` at `:1226`): ```ts if (refreshBackup && existsSync(path) && !looksNulFilled(path)) { copyFileSync(path, bakTmpPath); // whole-file copy, synchronous flushFile(bakTmpPath); // fsyncSync renameSync(bakTmpPath, bakPath); } writeFileSync(tmpPath, content); // content = JSON.stringify(entire table), synchronous flushFile(tmpPath); // fsyncSync renameSync(tmpPath, path); flushFile(dirname(path)); // fsyncSync on dir ``` `messages` is a single global table (`:154`); `rows("messages")` returns the entire in-memory array, and `markDirty("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-MB `messages.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 1. Accumulate a large `messages` table (a long transcript or many chats; multi-MB `messages.json`). 2. Chat actively (or run autonomous agents writing per turn) so flushes fire on the debounce. 3. Observe each flush stringify + whole-file-copy the entire table synchronously; on a multi-user server, one user's flush blocks the event loop for all others. ## Environment - Marinara Engine version: 1.6.1 (`staging @ a9fdae8`) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. ## Additional context Fix: swap the `*Sync` fs calls in `atomicWriteFile` for `fs/promises` async variants so the flush yields the event loop, and/or replace the whole-file `copyFileSync` backup 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 hot `messages` table 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.ts` is byte-identical on `main` (`65a0395`, `git diff` empty), so this is a shared-engine performance characteristic, not a `main`→`staging` regression. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:02 +07:00
Author
Owner

@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 synchronous O(DB size) cost of each flush. Making atomicWriteFile async here and adding the durable flush in #2527 both land in file-backed-store.ts/connection.ts.

<!-- gh-comment-id:4659026770 --> @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 synchronous `O(DB size)` cost of each flush. Making `atomicWriteFile` async here and adding the durable flush in #2527 both land in `file-backed-store.ts`/`connection.ts`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1256
No description provided.