[PR #2537] [MERGED] fix(updates): flush the file-backed store before exiting after an in-app update #5640

Closed
opened 2026-10-22 04:18:03 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2537
Author: @Romuromylus
Created: 6/9/2026
Status: ✅ Merged
Merged: 6/9/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/updater-flush-before-exit


📝 Commits (2)

  • 8579238 fix(updates): flush the file-backed store before exiting after an in-app update
  • 7bae4bb Merge branch 'staging' into fix/updater-flush-before-exit

📊 Changes

1 file changed (+21 additions, -4 deletions)

View changed files

📝 packages/server/src/routes/updates.routes.ts (+21 -4)

📄 Description

Linked issue

Closes #2527

Why this change

The in-app updater's POST /api/updates/apply route exited via setTimeout(..., 500) → process.exit(0). The file-backed store is a debounced write-back cache: a write arms a 750ms debounce (SAVE_DEBOUNCE_MS) before it touches disk. Durable flushes only happen via the graceful path (app.close() → Fastify onClose → closeDB() → fileStore.close() → flush(true)) or an explicit flushDB(). A bare process.exit(0) runs neither — Node does not execute Fastify onClose hooks or the store's beforeExit autosave on an explicit exit — and the 500ms timer is shorter than the 750ms debounce, so any write landing during the install/rebuild step (an in-progress message persist, swipe patch, autonomous-agent turn) is silently dropped on update.

What changed

packages/server/src/routes/updates.routes.ts:

  • Made the exit-timer callback async and await app.close() before process.exit(0), so the same onClose → closeDB → fileStore.close → flush(true) chain that runs on SIGTERM/SIGINT (index.ts shutdown()) now runs here, plus a clean sidecar stop.
  • try/catch always reaches process.exit(0) so the process still halts for the user's relaunch even if the flush fails; the failure is surfaced via err-first Pino logging (exit code kept at 0 so a supervisor doesn't misread a post-successful-update teardown as a crash).
  • Bumped the timer 500ms → 1000ms to clear the 750ms debounce window (belt-and-suspenders; app.close() forces flush(true) regardless).

The HTTP response is already returned before the timer fires, so making the callback async does not delay or alter the response. Not a hot path — apply is a rare, privileged/loopback-gated user action.

Not a regression: byte-identical on main; long-standing shared bug.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

pnpm check passes locally. The change reuses the exact graceful-shutdown path already exercised on every SIGTERM/SIGINT via index.ts shutdown() (await app.close() then process.exit(0)), so the flush-on-close chain is pre-existing behavior — this PR only routes the updater's exit through it.

Still to verify manually against the deploy (timing-sensitive, hard to hit by hand):

  • Make a write (send a message / edit game state) so a write enters the 750ms debounce window, then immediately trigger the in-app "Apply update". After the app exits and relaunches, confirm the just-made write survived (on an unpatched build it is lost).
  • Confirm the server log shows "[Update] Shutting down after update..." appearing after app.close() resolves, and no "Graceful shutdown failed" line.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2537 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/9/2026 **Status:** ✅ Merged **Merged:** 6/9/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/updater-flush-before-exit` --- ### 📝 Commits (2) - [`8579238`](https://github.com/Pasta-Devs/Marinara-Engine/commit/85792388b744e29d5baa72c8fc26817032050be4) fix(updates): flush the file-backed store before exiting after an in-app update - [`7bae4bb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7bae4bb5c019c0f61dafd5e5ae19bb32889e98d5) Merge branch 'staging' into fix/updater-flush-before-exit ### 📊 Changes **1 file changed** (+21 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/updates.routes.ts` (+21 -4) </details> ### 📄 Description ## Linked issue Closes #2527 ## Why this change The in-app updater's `POST /api/updates/apply` route exited via `setTimeout(..., 500)` → `process.exit(0)`. The file-backed store is a debounced write-back cache: a write arms a 750ms debounce (`SAVE_DEBOUNCE_MS`) before it touches disk. Durable flushes only happen via the graceful path (`app.close()` → Fastify `onClose` → `closeDB()` → `fileStore.close()` → `flush(true)`) or an explicit `flushDB()`. A bare `process.exit(0)` runs **neither** — Node does not execute Fastify `onClose` hooks or the store's `beforeExit` autosave on an explicit exit — and the 500ms timer is shorter than the 750ms debounce, so any write landing during the install/rebuild step (an in-progress message persist, swipe patch, autonomous-agent turn) is silently dropped on update. ## What changed `packages/server/src/routes/updates.routes.ts`: - Made the exit-timer callback `async` and `await app.close()` before `process.exit(0)`, so the same `onClose → closeDB → fileStore.close → flush(true)` chain that runs on SIGTERM/SIGINT (`index.ts` `shutdown()`) now runs here, plus a clean sidecar stop. - `try/catch` always reaches `process.exit(0)` so the process still halts for the user's relaunch even if the flush fails; the failure is surfaced via err-first Pino logging (exit code kept at 0 so a supervisor doesn't misread a post-successful-update teardown as a crash). - Bumped the timer 500ms → 1000ms to clear the 750ms debounce window (belt-and-suspenders; `app.close()` forces `flush(true)` regardless). The HTTP response is already returned before the timer fires, so making the callback async does not delay or alter the response. Not a hot path — `apply` is a rare, privileged/loopback-gated user action. Not a regression: byte-identical on `main`; long-standing shared bug. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes `pnpm check` passes locally. The change reuses the exact graceful-shutdown path already exercised on every SIGTERM/SIGINT via `index.ts shutdown()` (`await app.close()` then `process.exit(0)`), so the flush-on-close chain is pre-existing behavior — this PR only routes the updater's exit through it. Still to verify manually against the deploy (timing-sensitive, hard to hit by hand): - Make a write (send a message / edit game state) so a write enters the 750ms debounce window, then immediately trigger the in-app "Apply update". After the app exits and relaunches, confirm the just-made write survived (on an unpatched build it is lost). - Confirm the server log shows "[Update] Shutting down after update..." appearing **after** `app.close()` resolves, and no "Graceful shutdown failed" line. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:18:03 +07:00
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#5640
No description provided.