[GH-ISSUE #2319] [Issue]: No flush-on-quit — writes within the 750ms storage debounce window are lost when the app is closed #1146

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

Originally created by @Romuromylus on GitHub (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2319

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — Data-loss / durability. Ordinary writes (new message, edit, swipe, settings) go through the debounced FileStorage path and sit only in the in-memory cache for up to STORAGE_SAVE_DEBOUNCE_MS = 750ms before reaching disk. There is no flush-on-quit anywhere in src-tauri, and the only Drop backstop is gated on Arc::strong_count == 1, which the in-flight debounce thread defeats. So quitting (or crashing) within ~750ms of the last edit silently loses that write.

Expected behavior

On app shutdown the storage layer should synchronously flush all pending dirty collections to disk before the process exits, independent of Drop/strong_count timing.

Actual behavior

  • Writes are debounced: write_collection (src-tauri/crates/storage/src/lib.rs:1718-1722) → cache_collection(dirty=true) + schedule_dirty_flush.
  • schedule_dirty_flush (lib.rs:820-835) does let storage = self.clone(); then spawns a detached thread that sleeps 750ms before flushing — holding a cache-Arc clone alive for the whole window.
  • Drop for FileStorage (lib.rs:1954-1960) only flushes when Arc::strong_count(&self.cache) == 1; the in-flight thread's clone keeps it ≥ 2 during exactly the pending window, so Drop skips the flush.
  • No exit-time flush exists: a repo-wide search for RunEvent / on_window_event / ExitRequested / WindowEvent::CloseRequested returns zero matches; the builder ends at .run(tauri::generate_context!()) (src/lib.rs:281) with no event callback. flush() is public but only called from backup, gallery recovery, and import rollback — never on quit.

On a normal Tauri window-close the underlying event loop typically exits the process without unwinding, so Drop very likely never runs at all — either path yields the same loss.

Steps to reproduce

  1. Send or edit a message (or change a setting).
  2. Close the app window (or the process exits/crashes) within ~750ms, before the debounce timer fires.
  3. The pending dirty collection (e.g. messages.json, settings.json) is never written; the change is gone on next launch.

Impact area

  • App shell, navigation, startup, or providers
  • Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime

Environment

  • Branch or commit: refactor @ b0e5561d
  • Run mode: Tauri desktop
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? not relevant
  • Provider, model, or integration, if relevant: n/a

Logs, screenshots, or video

Static source audit; cited to file:line above.

Regression notes

Found via static audit of the refactor branch; not verified against main. Loss window is bounded (~750ms, single last action) and recoverable by re-typing, hence medium.

Additional context

Suggested fix: switch to .build(ctx)? + app.run(move |app_handle, event| { if let RunEvent::ExitRequested { .. } | RunEvent::Exit = event { if let Some(state) = app_handle.try_state::<AppState>() { let _ = state.storage.flush(); } } }). flush() already takes the write lock and calls flush_dirty_collections_locked, so it is safe from the run callback. Optionally also flush on WindowEvent::CloseRequested. Keep Drop as a secondary backstop but remove its strong_count blind spot.

Template check

Please uncheck 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 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2319 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — Data-loss / durability.** Ordinary writes (new message, edit, swipe, settings) go through the debounced FileStorage path and sit only in the in-memory cache for up to `STORAGE_SAVE_DEBOUNCE_MS = 750ms` before reaching disk. There is no flush-on-quit anywhere in `src-tauri`, and the only Drop backstop is gated on `Arc::strong_count == 1`, which the in-flight debounce thread defeats. So quitting (or crashing) within ~750ms of the last edit silently loses that write. ## Expected behavior On app shutdown the storage layer should synchronously flush all pending dirty collections to disk before the process exits, independent of `Drop`/`strong_count` timing. ## Actual behavior - Writes are debounced: `write_collection` (`src-tauri/crates/storage/src/lib.rs:1718-1722`) → `cache_collection(dirty=true)` + `schedule_dirty_flush`. - `schedule_dirty_flush` (`lib.rs:820-835`) does `let storage = self.clone();` then spawns a detached thread that sleeps 750ms before flushing — holding a cache-Arc clone alive for the whole window. - `Drop for FileStorage` (`lib.rs:1954-1960`) only flushes when `Arc::strong_count(&self.cache) == 1`; the in-flight thread's clone keeps it ≥ 2 during exactly the pending window, so Drop skips the flush. - No exit-time flush exists: a repo-wide search for `RunEvent` / `on_window_event` / `ExitRequested` / `WindowEvent::CloseRequested` returns zero matches; the builder ends at `.run(tauri::generate_context!())` (`src/lib.rs:281`) with no event callback. `flush()` is public but only called from backup, gallery recovery, and import rollback — never on quit. On a normal Tauri window-close the underlying event loop typically exits the process without unwinding, so `Drop` very likely never runs at all — either path yields the same loss. ## Steps to reproduce 1. Send or edit a message (or change a setting). 2. Close the app window (or the process exits/crashes) within ~750ms, before the debounce timer fires. 3. The pending dirty collection (e.g. `messages.json`, `settings.json`) is never written; the change is gone on next launch. ## Impact area - [x] App shell, navigation, startup, or providers - [x] Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime ## Environment - Branch or commit: refactor @ b0e5561d - Run mode: Tauri desktop - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? not relevant - Provider, model, or integration, if relevant: n/a ## Logs, screenshots, or video Static source audit; cited to file:line above. ## Regression notes Found via static audit of the refactor branch; not verified against main. Loss window is bounded (~750ms, single last action) and recoverable by re-typing, hence medium. ## Additional context Suggested fix: switch to `.build(ctx)?` + `app.run(move |app_handle, event| { if let RunEvent::ExitRequested { .. } | RunEvent::Exit = event { if let Some(state) = app_handle.try_state::<AppState>() { let _ = state.storage.flush(); } } })`. `flush()` already takes the write lock and calls `flush_dirty_collections_locked`, so it is safe from the run callback. Optionally also flush on `WindowEvent::CloseRequested`. Keep Drop as a secondary backstop but remove its `strong_count` blind spot. ## Template check Please uncheck 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:27:50 +07:00
Author
Owner

@Romuromylus commented on GitHub (Jun 6, 2026):

Fixed by #2379, merged into refactor (squash 2e2225cd).

<!-- gh-comment-id:4637085015 --> @Romuromylus commented on GitHub (Jun 6, 2026): Fixed by #2379, merged into `refactor` (squash 2e2225cd).
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#1146
No description provided.