Skip to content

FIX dexie storage: close Dexie connections and evict state cache on last close (#8793)#8797

Open
Cr4ftingMine wants to merge 2 commits into
pubkey:masterfrom
Cr4ftingMine:fix-dexie-connection-refcount
Open

FIX dexie storage: close Dexie connections and evict state cache on last close (#8793)#8797
Cr4ftingMine wants to merge 2 commits into
pubkey:masterfrom
Cr4ftingMine:fix-dexie-connection-refcount

Conversation

@Cr4ftingMine

Copy link
Copy Markdown

Fixes #8793, as requested there, with a regression test based on test/unit/bug-report.test.ts.

The PR is structured in two commits so the reproduction stays verifiable:

  1. The first commit contains only the test. On that commit the test fails with Dexie's DatabaseClosedError ("Database has been closed").
  2. The second commit contains the fix (plus changelog entry). From here on the test passes.

Test case

Reproduces the scenario from the issue: create a database with the dexie storage, close it, delete the underlying per-collection IndexedDB databases (like an application does on logout to remove user data from a shared device), then re-create the same database name in the same JS context. The test runs for the dexie storage only and returns early for other storages, since the defect is located in dexie-helper.ts.

Fix

Repairs the reference counting in dexie-helper.ts:

  • key the initial count on value instead of the still-unassigned state (the factory callback runs synchronously inside the assignment, so the count was stored under undefined and closeDexieDb() computed undefined - 1 = NaN),
  • count one reference per storage instance in getDexieDbWithTables() (there was no increment anywhere, so the close branch was unreachable),
  • when the last reference is released, also evict the DEXIE_STATE_DB_BY_NAME entry, otherwise the next open of the same name still receives the closed Dexie instance.

Validation

  • new test fails on master with "Database has been closed" and passes with the fix
  • npm run test:fast:dexie: 1404 passing
  • npm run check-types: clean
  • changelog entry added under orga/changelog/

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

Labels

None yet

Projects

None yet

1 participant