Skip to content

fix(idxdb-store): synchronize openDatabase to prevent worker reset race condition (#287) - #299

Open
Lesnak1 wants to merge 1 commit into
0xMiden:mainfrom
Lesnak1:fix/idxdb-store-init-race-condition
Open

Lesnak1 wants to merge 1 commit into
0xMiden:mainfrom
Lesnak1:fix/idxdb-store-init-race-condition

Conversation

@Lesnak1

@Lesnak1 Lesnak1 commented Aug 17, 2026

Copy link
Copy Markdown

Summary

Fixes #287.

Initializing WebClient with the default useWorker: true starts WASM both on the main thread and in the web worker, calling openDatabase(storeName, clientVersion) concurrently against the same database.

When ensureClientVersion triggers a reset path (e.g. major/minor version mismatch or invalid semver), dexie.delete() emits a versionchange event that closes the peer connection. Whichever thread reaches delete() second deletes the freshly reopened database while settings reads/writes are in flight, rejecting with DatabaseClosedError and throwing Failed to initialize IdxdbStore.

Changes

  • Web Locks Synchronization (crates/idxdb-store/src/ts/schema.ts):
    • Added acquireDbInitLock wrapping openDatabase using the standard Web Locks API (navigator.locks.request("miden-db-init:${name}", ...)).
    • Added an in-process promise queue fallback for non-browser/test environments without navigator.locks.
  • Race-Free Store Resets:
    • The first worker/thread acquires the lock, performs the reset/nuke, persists the new version, and opens the DB.
    • The second instance waits for the lock, reads the updated getStoredClientVersion(), recognizes the version matches, and proceeds without closing or re-deleting the database.
  • Compiled JS:
    • Synchronized crates/idxdb-store/src/js/schema.js.
  • Testing:
    • Added test suite in crates/idxdb-store/src/ts/schema.test.ts asserting concurrent openDatabase calls during a major version reset complete cleanly without errors or dangling connections.
    • Added test asserting navigator.locks integration when present.

Verification

  • All 12 test files (318 tests) in crates/idxdb-store pass cleanly (vitest run).
  • Full React SDK test suite (62 files, 845 tests) passes.
  • Pre-commit hooks (eslint --fix, prettier --write) passed.

…ce condition (0xMiden#287)

- Synchronize openDatabase calls per network using Web Locks API (navigator.locks) with an in-process queue fallback
- Prevents concurrent store resets and DatabaseClosedError when WebClient initializes on main thread and web worker simultaneously
- Add concurrent openDatabase and version reset race tests
@Lesnak1
Lesnak1 force-pushed the fix/idxdb-store-init-race-condition branch from f124e48 to 5977bab Compare September 1, 2026 11:11
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.

Failed to initialize IdxdbStore: main thread and worker race each other during the client-version store reset

1 participant