Skip to content

[LokiJS] Fix unsafeResetDatabase failing while an autosave is in progress - #8

Merged
joker-777 merged 1 commit into
kenhubfrom
fix/loki-reset-during-autosave
Oct 31, 2026
Merged

joker-777 merged 1 commit into
kenhubfrom
fix/loki-reset-during-autosave

Conversation

@joker-777

Copy link
Copy Markdown

Problem

unsafeResetDatabase() on the LokiJS adapter calls deleteDatabase(loki), which does loki.close(() => loki.deleteDatabase(...)).

close() only waits for a save if Loki is dirty. An autosave clears the dirty flags when it starts (in getLokiCopy), so while one is in flight Loki looks clean. close() then emits 'close' synchronously (Loki's listeners are synchronous), and IncrementalIndexedDBAdapter.deleteDatabase throws Error while deleting database - another operation is already in progress because operationInProgress is still true. The throw comes out of loki.close() inside the Promise executor, so the reset rejects.

Fix

After close(), wait for Loki's throttled save queue to drain (throttledSaveDrain) before calling deleteDatabase. Two small guards:

  • throttledSaveDrain calls its callback twice when throttledSaves is disabled (a lokijs bug: missing return), so the drain is skipped in that case.
  • The delete now runs in an async callback, where a throw would no longer reject the promise and would leave the reset hanging. It's wrapped in try/catch so it still rejects.

Note: when a save is pending, Loki's drain runs one more save before calling back. That save finishes before the delete starts, so it's harmless.

Test

New Jest test in src/adapters/lokijs/test.js. A fake incremental persistence adapter passed through _testLokiAdapter mimics IncrementalIndexedDBAdapter: it throws on save/delete while another operation is in progress and can keep a save in flight. The test starts a save, calls unsafeResetDatabase(), checks that nothing is deleted while the save runs, then finishes the save and checks that the reset completes and the data is gone. Without the fix it fails with "another operation is already in progress".

  • jest src/adapters/lokijs src/adapters/__tests__: 140 passed
  • eslint, tslint and flow focus-check on the changed file: clean

This is independent of #7.

🤖 Generated with Claude Code

unsafeResetDatabase() closes Loki and then deletes the database. close()
only waits for a save when Loki is dirty, but an autosave clears the dirty
flags as soon as it starts. If one was still running, close() emitted
'close' synchronously and IncrementalIndexedDBAdapter.deleteDatabase threw
"another operation is already in progress", rejecting the reset.

Drain Loki's throttled save queue after close() and before deleting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 08:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the fix addressing LokiJS database resets during in-progress saves.

No open issues 🎉

What changed in this PR

Fixes LokiJS unsafeResetDatabase() failing when an autosave is already in progress by waiting for pending saves before deleting the database.

Changes:

  • Drains the throttled save queue after closing Loki, skips draining when throttling is disabled, and catches deletion errors.
  • Adds a regression test using a persistence adapter that holds saves in progress.
  • Documents the fix in the unreleased changelog.
File Description
src/​adapters/​lokijs/​worker/​lokiExtensions.js Waits for pending saves before deletion and preserves error rejection.
src/​adapters/​lokijs/​test.js Tests resetting the database while a save is in progress.
CHANGELOG-Unreleased.md Records the LokiJS reset fix.

Review effort: Balanced

@joker-777
joker-777 merged commit 62991b7 into kenhub Oct 31, 2026
1 check passed
@joker-777

Copy link
Copy Markdown
Author

Offered upstream as Nozbe#1980, rebuilt on upstream master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants