[GH-ISSUE #3602] [Issue]: Graceful shutdown can drop writes queued during an active file-store flush #1948

Closed
opened 2026-10-21 05:44:04 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @Xelvanis on GitHub (Jul 14, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3602

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Severity: Medium — rare data-loss race. Graceful shutdown can finish while an asynchronous file-store flush is still running and a newer dirty write is waiting behind it. The process then exits successfully, but the latest mutation may not have reached disk.

Expected behavior

FileBackedStore.close() should wait for any active flush to finish, then flush changes that accumulated during that write before reporting that shutdown is complete.

Actual behavior

When flush() is called while this.saving is true, it marks the store dirty and returns immediately. close() clears the timers and calls await this.flush(true), but that await also returns immediately if a flush is already active. The active flush does not schedule or drain a follow-up pass when it finishes, and the normal SIGINT/SIGTERM path then exits the process.

This leaves a narrow sequence where a mutation made after the active flush captured its snapshot is absent from the file even though graceful shutdown completed without an error.

Steps to reproduce

  1. Use the file-native database backend and delay or block atomicWriteFile so a flush remains active after the target table has been serialized.
  2. Mutate the table and allow the 750 ms debounce to start that flush.
  3. Mutate the same table again while the first write is still active.
  4. Send SIGINT/SIGTERM or otherwise trigger the graceful app.close() path.
  5. Reopen the store and inspect the table. The second mutation is absent.

Environment

  • Marinara Engine version: 2.2.2 (bcd4e836)
  • Install type: source
  • OS + version: Windows 11 Pro 10.0.26200 (build 26200)
  • Browser or app shell: N/A (storage-layer deterministic reproduction)

Logs, screenshots, or video

No server error is emitted because close() resolves successfully.

Deterministic ephemeral reproduction output:

{"closeMs":0,"diskIds":["before-active-flush"]}

The second insert occurred during the active flush before close(), but was still absent from disk after the original flush was allowed to finish. A negative control with the same two inserts followed by an ordinary close, with no active flush, persisted both rows.

Additional context

  • Related: #2527 changed the in-app updater to use graceful app.close() so the file-backed store would be flushed before exit.
  • Related: #2528 moved file-store persistence off the event loop. The current residual race exists in the asynchronous flush lifecycle.
  • The relevant async flush behavior was introduced in 58ffdfd7 and adjusted in 55aac5cb.
  • There are no known user reports for this exact case. It requires a mutation during a narrow active-write window followed immediately by shutdown, so it is likely uncommon despite the data-loss impact.
  • A focused fix would track the active flush promise, have close() await it, then drain any dirty tables accumulated during that flush. Persistent I/O errors should still propagate rather than causing an endless retry loop. No file-format or public API change should be required.
  • Verified with an ephemeral deterministic reproduction; no test file needs to be committed.

Template check

Please uncheck (untick) the checkbox below before submitting this issue.

  • I have removed any checkmark from this box, and I understand that leaving it checked may cause the issue to be closed automatically.
Originally created by @Xelvanis on GitHub (Jul 14, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3602 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Severity: **Medium — rare data-loss race.** Graceful shutdown can finish while an asynchronous file-store flush is still running and a newer dirty write is waiting behind it. The process then exits successfully, but the latest mutation may not have reached disk. ## Expected behavior `FileBackedStore.close()` should wait for any active flush to finish, then flush changes that accumulated during that write before reporting that shutdown is complete. ## Actual behavior When `flush()` is called while `this.saving` is true, it marks the store dirty and returns immediately. `close()` clears the timers and calls `await this.flush(true)`, but that await also returns immediately if a flush is already active. The active flush does not schedule or drain a follow-up pass when it finishes, and the normal SIGINT/SIGTERM path then exits the process. This leaves a narrow sequence where a mutation made after the active flush captured its snapshot is absent from the file even though graceful shutdown completed without an error. ## Steps to reproduce 1. Use the file-native database backend and delay or block `atomicWriteFile` so a flush remains active after the target table has been serialized. 2. Mutate the table and allow the 750 ms debounce to start that flush. 3. Mutate the same table again while the first write is still active. 4. Send SIGINT/SIGTERM or otherwise trigger the graceful `app.close()` path. 5. Reopen the store and inspect the table. The second mutation is absent. ## Environment - Marinara Engine version: 2.2.2 (`bcd4e836`) - Install type: source - OS + version: Windows 11 Pro 10.0.26200 (build 26200) - Browser or app shell: N/A (storage-layer deterministic reproduction) ## Logs, screenshots, or video No server error is emitted because `close()` resolves successfully. Deterministic ephemeral reproduction output: ```json {"closeMs":0,"diskIds":["before-active-flush"]} ``` The second insert occurred during the active flush before `close()`, but was still absent from disk after the original flush was allowed to finish. A negative control with the same two inserts followed by an ordinary close, with no active flush, persisted both rows. ## Additional context - Related: #2527 changed the in-app updater to use graceful `app.close()` so the file-backed store would be flushed before exit. - Related: #2528 moved file-store persistence off the event loop. The current residual race exists in the asynchronous flush lifecycle. - The relevant async flush behavior was introduced in `58ffdfd7` and adjusted in `55aac5cb`. - There are no known user reports for this exact case. It requires a mutation during a narrow active-write window followed immediately by shutdown, so it is likely uncommon despite the data-loss impact. - A focused fix would track the active flush promise, have `close()` await it, then drain any dirty tables accumulated during that flush. Persistent I/O errors should still propagate rather than causing an endless retry loop. No file-format or public API change should be required. - Verified with an ephemeral deterministic reproduction; no test file needs to be committed. ## Template check Please **uncheck (untick)** the checkbox below before submitting this issue. - [ ] I have removed any checkmark from this box, and I understand that leaving it checked may cause the issue to be closed automatically.
bsakaguchi 2026-10-21 05:44:04 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Jul 14, 2026):

Claimed by @SpicyMarinara. I’m implementing a tracked active-flush promise and close-time dirty drain, with deterministic regression coverage for a write queued during an in-flight flush. I’ll preserve I/O error propagation and avoid retry loops.

<!-- gh-comment-id:4968989971 --> @SpicyMarinara commented on GitHub (Jul 14, 2026): Claimed by @SpicyMarinara. I’m implementing a tracked active-flush promise and close-time dirty drain, with deterministic regression coverage for a write queued during an in-flight flush. I’ll preserve I/O error propagation and avoid retry loops.
Author
Owner

@SpicyMarinara commented on GitHub (Jul 14, 2026):

Fixed in #3605 and merged to staging as acc3d3b22. Graceful close now awaits the active flush, drains writes queued after snapshot capture, and reports persistent close-time I/O failures. Deterministic regression coverage, full checks, UI smoke, container build, and CodeRabbit review passed.

<!-- gh-comment-id:4969072455 --> @SpicyMarinara commented on GitHub (Jul 14, 2026): Fixed in #3605 and merged to `staging` as `acc3d3b22`. Graceful close now awaits the active flush, drains writes queued after snapshot capture, and reports persistent close-time I/O failures. Deterministic regression coverage, full checks, UI smoke, container build, and CodeRabbit review passed.
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#1948
No description provided.