[GH-ISSUE #2527] [Issue]: In-app updater exits without flushing the file-backed store (data loss) #1255

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/2527

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — data loss. The in-app updater exits via process.exit(0) without flushing the file-backed store, so any writes still in the 750 ms debounce window (or awaiting the 10 s safety flush) are silently lost when a user applies an update. The graceful shutdown path flushes correctly; the updater bypasses it.

Expected behavior

Before exiting, the "Apply update" route should perform the same durable flush the graceful shutdown path uses — await flushDB() (or await app.close() so the onClose hooks run) — so no pending writes are dropped.

Actual behavior

packages/server/src/routes/updates.routes.ts:702-705:

// Give Fastify time to flush the response, then exit
setTimeout(() => {
  logger.info("[Update] Shutting down after update...");
  process.exit(0);   // bypasses onClose/beforeExit; no flushDB()/closeDB()
}, 500);

The store is a debounced write-back cache: markDirty() arms a 750 ms debounce (SAVE_DEBOUNCE_MS) and a 10 s safety interval (SAFETY_SAVE_MS) before flush() touches disk (file-backed-store.ts). Durable flushes happen only via the graceful path (SIGTERM/SIGINT → shutdown() → app.close() → Fastify onClose at app.ts:75-83 → closeDB() → fileStore.close() → flush(true)) or an explicit flushDB(). process.exit(0) runs neither — Node does not execute Fastify onClose hooks on an explicit exit, and the beforeExit autosave handler does not fire on process.exit(). The 500 ms timer is shorter than the 750 ms debounce, so a write landing during the install/rebuild step (an autonomous agent turn, an in-progress user message persist, a swipe patch) is dropped. No flushDB()/closeDB()/app.close() appears anywhere in updates.routes.ts.

Steps to reproduce

  1. While any chat/message/game-state write is within the 750 ms debounce window (or dirty awaiting the 10 s safety flush), click "Apply update" in the UI.
  2. The route exits via process.exit(0); the unflushed dirty tables (messages.json, chats.json, game_state_snapshots.json, etc.) never reach disk and the writes are lost after relaunch.

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: before process.exit(0), await flushDB() (import { flushDB } from "../db/connection.js"), or better await app.close() / closeDB() so the onClose hooks, sidecar stop, and the durable flush(true) all run — mirroring index.ts shutdown(). flushDB() is the in-repo reference pattern, already used in backup.routes.ts:678/1585/1637 before reading storage files.

This is bounded to the rare, user-initiated update event (not an every-turn path), hence Medium.

Regression notes: found via static audit; the identical no-flush process.exit(0) exists on main (65a0395, updates.routes.ts:704), so this is a long-standing shared bug, 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/2527 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — data loss.** The in-app updater exits via `process.exit(0)` without flushing the file-backed store, so any writes still in the 750 ms debounce window (or awaiting the 10 s safety flush) are silently lost when a user applies an update. The graceful shutdown path flushes correctly; the updater bypasses it. ## Expected behavior Before exiting, the "Apply update" route should perform the same durable flush the graceful shutdown path uses — `await flushDB()` (or `await app.close()` so the `onClose` hooks run) — so no pending writes are dropped. ## Actual behavior `packages/server/src/routes/updates.routes.ts:702-705`: ```ts // Give Fastify time to flush the response, then exit setTimeout(() => { logger.info("[Update] Shutting down after update..."); process.exit(0); // bypasses onClose/beforeExit; no flushDB()/closeDB() }, 500); ``` The store is a debounced write-back cache: `markDirty()` arms a 750 ms debounce (`SAVE_DEBOUNCE_MS`) and a 10 s safety interval (`SAFETY_SAVE_MS`) before `flush()` touches disk (`file-backed-store.ts`). Durable flushes happen only via the graceful path (`SIGTERM`/`SIGINT` → `shutdown()` → `app.close()` → Fastify `onClose` at `app.ts:75-83` → `closeDB()` → `fileStore.close()` → `flush(true)`) or an explicit `flushDB()`. `process.exit(0)` runs neither — Node does not execute Fastify `onClose` hooks on an explicit exit, and the `beforeExit` autosave handler does not fire on `process.exit()`. The 500 ms timer is shorter than the 750 ms debounce, so a write landing during the install/rebuild step (an autonomous agent turn, an in-progress user message persist, a swipe patch) is dropped. No `flushDB()`/`closeDB()`/`app.close()` appears anywhere in `updates.routes.ts`. ## Steps to reproduce 1. While any chat/message/game-state write is within the 750 ms debounce window (or dirty awaiting the 10 s safety flush), click "Apply update" in the UI. 2. The route exits via `process.exit(0)`; the unflushed dirty tables (`messages.json`, `chats.json`, `game_state_snapshots.json`, etc.) never reach disk and the writes are lost after relaunch. ## 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: before `process.exit(0)`, `await flushDB()` (`import { flushDB } from "../db/connection.js"`), or better `await app.close()` / `closeDB()` so the `onClose` hooks, sidecar stop, and the durable `flush(true)` all run — mirroring `index.ts` `shutdown()`. `flushDB()` is the in-repo reference pattern, already used in `backup.routes.ts:678/1585/1637` before reading storage files. This is bounded to the rare, user-initiated update event (not an every-turn path), hence Medium. Regression notes: found via static audit; the identical no-flush `process.exit(0)` exists on `main` (`65a0395`, `updates.routes.ts:704`), so this is a long-standing shared bug, 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 #2528 — both concern the file-backed store. This one is the missing durable flush on the updater exit path; #2528 is the synchronous cost of each flush that does fire. A fix here (await flushDB()/app.close() before process.exit(0)) and the async-flush change in #2528 touch the same flush()/atomicWriteFile machinery, so they're worth handling together.

<!-- gh-comment-id:4659026576 --> @Romuromylus commented on GitHub (Jun 9, 2026): Related to #2528 — both concern the file-backed store. This one is the missing durable flush on the updater exit path; #2528 is the synchronous cost of each flush that does fire. A fix here (await `flushDB()`/`app.close()` before `process.exit(0)`) and the async-flush change in #2528 touch the same `flush()`/`atomicWriteFile` machinery, so they're worth handling together.
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#1255
No description provided.