Skip to content

Fix: close the collection of a failed or interrupted addCollections() call - #9660

Open
pubkey wants to merge 2 commits into
masterfrom
claude/vigilant-newton-8rqsab-close-collection-of-failed-or-interrupted-add-collections
Open

pubkey wants to merge 2 commits into
masterfrom
claude/vigilant-newton-8rqsab-close-collection-of-failed-or-interrupted-add-collections

Conversation

@pubkey

@pubkey pubkey commented Nov 12, 2026

Copy link
Copy Markdown
Owner

This PR contains:

  • A BUGFIX

Describe the problem you have without this PR

a collection that was created by an addCollections() call which failed (for example with DM4 from the autoMigrate migration) or which was still running when database.close() was called, was never closed, so its timeouts (like the 60 seconds wait of the cleanup plugin) kept the JavaScript process alive

When the creation of a collection failed after the collection object existed, only the storage instance was closed. The collection stayed open with the timers that plugins start for it, and the 60 second timer of the cleanup plugin kept the process alive. The same happened when database.close() ran while addCollections() waited for an automatic migration, because the collection was not registered in the database yet. A failed creation now closes the collection, and database.close() also closes the collections of the database that are still being created.

  • The rejection is unchanged. With a close during the automatic migration it is a plain RxJS EmptyError on Dexie and localStorage, and the call stays pending on memory. pubkey/rxdb#9565 fixes that with DM8. Both together give DM8, a process that exits, and no unhandled rejection.
  • Merge problem: the rollback of pubkey/rxdb#9595 reads the internal store after the database was closed. Together with this PR and pubkey/rxdb#9565, Dexie then rejects with a plain storage error. That rollback has to be skipped when the database is closing.
  • A failed creation now emits a CLOSED event on collections$ for a collection that was never added.
  • Not covered: a database.close() while the collection is still being prepared.

Changed files:

  • orga/changelog/fix-close-collection-of-failed-or-interrupted-add-collections.json
  • src/rx-collection.ts
  • src/rx-database.ts
  • test/unit/migration-schema.test.ts

Test output without the fix

The changed test file run on the memory storage with src/ from master:

  52 passing (3s)
  1 failing

  1) migration-schema.test.ts
       issues
         collection of a not finished addCollections() call
           should close the collection when the automatic migration fails:

      AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

false !== true

      + expected - actual

      -false
      +true
      
      at Context.<anonymous> (./test_tmp/unit/migration-schema.test.js:2619:16)

Test output with the fix

56 passing (3s)

Todos

  • Tests
  • Changelog

Assisted by

YES, CLAUDE CODE


Generated by Claude Code

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

✅ Verify Test Reproduction: Tests FAILED without the fix (expected)

This confirms the changed tests correctly reproduce the bug that the source changes fix.

This workflow runs the changed tests without the source fix to verify they reproduce the bug.

Show output
...(truncated, showing last 200 of 4102 lines)
      �[32m✓ �[39mshould save one encrypted value (object)
    replication
      �[32m✓ �[39mreplication state meta should not contain a secret in cleartext
    ISSUES
      �[32m✓ �[39m#837 Recover from wrong database password
      �[32m✓ �[39m#917 Unexpected end of JSON input
      �[32m✓ �[39mshould work with encrypted fields that have maxLength in schema
      �[32m✓ �[39mshould work with encrypted object fields that have nested properties
      �[32m✓ �[39mshould correctly encrypt and decrypt nested fields with dot-notation paths and non-string types
      �[32m✓ �[39mshould throw when encrypted contains overlapping parent and child paths
      �[32m✓ �[39mshould throw when an index contains an encrypted field
      #157 Cannot sort on field(s) "XXX" when using the default index
        �[32m✓ �[39mschema example 1
        �[32m✓ �[39mschema example 2
    SECURITY
      �[32m✓ �[39mshould not expose the database password as an enumerable property
      �[32m✓ �[39mshould not leak the password in error parameters when password is too short
      �[32m✓ �[39mshould report the correct minimum password length when the password is too short

  rx-state.test.ts (useSchemaValidator: true)
    helper
      .nextRxStateId()
        �[32m✓ �[39mshould increment in sort order
    creation
      �[32m✓ �[39mcalling addState twice should give the same instance
    write state data
      �[32m✓ �[39mshould write without error
      �[32m✓ �[39mwrite multiple times at once
      �[32m✓ �[39mupdate nested
      �[32m✓ �[39mupdate complete state at once
      �[32m✓ �[39mdoing many writes should end up in a single persistence write to the storage
    .get()
      �[32m✓ �[39mshould get root state
      �[32m✓ �[39mshould get the updated value
      �[32m✓ �[39mshould get nested values
      �[32m✓ �[39mshould not throw on undefined values
    .get$()
      �[32m✓ �[39mshould emit the correct data
      �[32m✓ �[39mshould emit the correct data via proxy-getter a$
    .get$$()
      �[32m✓ �[39mshould get the correct object
    cleanup
      �[32m✓ �[39mshould merge the state documents data on cleanup
    multiInstance
      �[32m✓ �[39mwrite with two states at once
      �[32m✓ �[39mwrite with two states to nested at once
      �[32m✓ �[39mget changes from other state
      �[32m✓ �[39mclose() and a pending set() must settle when the other instance wrote at the same time
      �[32m✓ �[39mshould have a deterministic output when 2 instances write at the same time
      �[32m✓ �[39mshould have a deterministic output when 2 instances write to different fields
      �[32m✓ �[39mshould recover the same state from disc on the other side
      �[32m✓ �[39mshould emit the correct data for all states
      �[32m✓ �[39mshould not lose a write when eventReduce is disabled and the events of the other instance arrive delayed
    issues
      �[32m✓ �[39mRxState.property$ should be stable for initial synchronous get and subsequent subscription
      �[32m✓ �[39mset() with empty path should pass the current state to the modifier
      �[32m✓ �[39mshould recover correct state from disk after full-state replacement with set('')
      �[32m✓ �[39mbad rx-state after cleanup
      �[32m✓ �[39m$ observable should not emit duplicate values on a single write
      �[32m✓ �[39mget$() should emit the current value when subscribed, not a stale value from creation time
      �[32m✓ �[39m_cleanup() should return true to signal completion
      �[32m✓ �[39mwrite queue should recover after a modifier throws

  rx-state.test.ts (useSchemaValidator: false)
    helper
      .nextRxStateId()
        �[32m✓ �[39mshould increment in sort order
    creation
      �[32m✓ �[39mcalling addState twice should give the same instance
    write state data
      �[32m✓ �[39mshould write without error
      �[32m✓ �[39mwrite multiple times at once
      �[32m✓ �[39mupdate nested
      �[32m✓ �[39mupdate complete state at once
      �[32m✓ �[39mdoing many writes should end up in a single persistence write to the storage
    .get()
      �[32m✓ �[39mshould get root state
      �[32m✓ �[39mshould get the updated value
      �[32m✓ �[39mshould get nested values
      �[32m✓ �[39mshould not throw on undefined values
    .get$()
      �[32m✓ �[39mshould emit the correct data
      �[32m✓ �[39mshould emit the correct data via proxy-getter a$
    .get$$()
      �[32m✓ �[39mshould get the correct object
    cleanup
      �[32m✓ �[39mshould merge the state documents data on cleanup
    multiInstance
      �[32m✓ �[39mwrite with two states at once
      �[32m✓ �[39mwrite with two states to nested at once
      �[32m✓ �[39mget changes from other state
      �[32m✓ �[39mclose() and a pending set() must settle when the other instance wrote at the same time
      �[32m✓ �[39mshould have a deterministic output when 2 instances write at the same time
      �[32m✓ �[39mshould have a deterministic output when 2 instances write to different fields
      �[32m✓ �[39mshould recover the same state from disc on the other side
      �[32m✓ �[39mshould emit the correct data for all states
      �[32m✓ �[39mshould not lose a write when eventReduce is disabled and the events of the other instance arrive delayed
    issues
      �[32m✓ �[39mRxState.property$ should be stable for initial synchronous get and subsequent subscription
      �[32m✓ �[39mset() with empty path should pass the current state to the modifier
      �[32m✓ �[39mshould recover correct state from disk after full-state replacement with set('')
      �[32m✓ �[39mbad rx-state after cleanup
      �[32m✓ �[39m$ observable should not emit duplicate values on a single write
      �[32m✓ �[39mget$() should emit the current value when subscribed, not a stale value from creation time
      �[32m✓ �[39m_cleanup() should return true to signal completion
      �[32m✓ �[39mwrite queue should recover after a modifier throws

  migration-schema.test.ts
    .create() with migrationStrategies
      positive
        �[32m✓ �[39mok to create with strategies
        �[32m✓ �[39mcreate same collection with different schema-versions
      negative
        �[32m✓ �[39mshould throw when array
        �[32m✓ �[39mshould throw when property no number
        �[32m✓ �[39mshould throw when property no non-float-number
        �[32m✓ �[39mshould throw when property-value no function
        �[32m✓ �[39mshould report the type of the non-function strategy value
        �[32m✓ �[39mthrow when strategy missing
    getOldCollectionMeta()
      �[32m✓ �[39mshould NOT get an older version
      �[32m✓ �[39mshould get an older version
    migration basics
      .remove()
        �[32m✓ �[39mshould delete the old storage instance with all its content
      .migrate()
        �[32m✓ �[39mshould resolve finished when no docs
        �[32m✓ �[39mshould resolve finished when some docs are in the collection
        �[32m✓ �[39mshould emit status updates
        �[32m✓ �[39mshould remove the document when migration-strategy returns null
        �[32m✓ �[39mshould throw when document cannot be migrated
      .migratePromise()
        positive
          �[32m✓ �[39mshould resolve when nothing to migrate
          �[32m✓ �[39mshould resolve when migrating data
        negative
          �[32m✓ �[39mshould reject when migration fails
      writes during migration
        �[32m✓ �[39mshould block writes while a migration is running and allow them again after it finishes
        �[32m✓ �[39mshould block bulkInsert that races with a starting migration (no waitUntil)
        �[32m✓ �[39mshould block writes when a migration is needed but not yet started
        �[32m✓ �[39mshould reset migrationInProgress on migration error
    integration into collection
      run
        �[32m✓ �[39mshould auto-run on creation
        �[32m✓ �[39mshould be able to change the primary key during migration
        �[32m✓ �[39mshould auto-run on creation (async)
        �[32m✓ �[39mshould keep the _meta.lwt value of the documents
      .migrationNeeded()
        �[32m✓ �[39mreturn true if schema-version is 0
        �[32m✓ �[39mreturn false if nothing to migrate
        �[32m✓ �[39mreturn true if something to migrate
    RxDatabase.migrationStates()
      �[32m✓ �[39mshould emit the ongoing migration state
    migration and replication
      �[32m✓ �[39mshould have migrated the replication state
      �[32m✓ �[39missue #7204 schema migration failing when returning null
    issues
      �[32m✓ �[39m#7226 db.addCollections fails after it failed for a missing migration strategy
      �[32m✓ �[39mduplicate meta states found if migration is canceled in between and restarted with a new version
      �[32m✓ �[39m#7791 should delete old collection meta doc even when _rev has changed
      �[32m✓ �[39m#7786 should properly cancel internal replication after migratePromise completes
      �[32m✓ �[39m#7827 should close the broadcastChannel/leader-election when the migration errors
      �[32m✓ �[39m#9191 must not block addCollections() forever when the migration leader is frozen
      �[32m✓ �[39m#7008 migrate schema with multiple connected storages
      �[32m✓ �[39m#212 migration runs into infinity-loop
      �[32m✓ �[39m#3460 migrate attachments
      �[32m✓ �[39mopening an older RxDB database state with a new major version should throw an error
      �[32m✓ �[39mversion data should be compatible with next version code
      �[32m✓ �[39mmigratePromise() should return 100 percent when collection has 0 documents
      �[32m✓ �[39mshould not resurrect deleted documents when migration strategy returns a new object
      �[32m✓ �[39mmigratePromise() should return percent 100 when status is DONE
      �[32m✓ �[39mshould NOT auto-apply schema default values during migration
      �[32m✓ �[39mshould keep attachments when migration strategy returns a new object
      �[32m✓ �[39mshould preserve _attachments across chained migration strategies
      �[32m✓ �[39m#8607 should throw/reject when migration produces a document that does not match the schema
      �[32m✓ �[39m#9145 must keep all documents when the migration needs multiple batches with multiInstance: false
      collection of a not finished addCollections() call
        �[31m✗ �[39m�[31mshould close the collection when the automatic migration fails�[39m
	AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
	
	false !== true
	
	    at Context.<anonymous> (/home/runner/work/rxdb/rxdb/test/unit/migration-schema.test.ts:2851:17 <- /tmp/_karma_webpack_rxdb_5865/commons.js:208686:45)


Chrome Headless 157.0.0.0 (Linux 0.0.0): Executed 1299 of 1508�[31m (1 FAILED)�[39m (58.388 secs / 57.23 secs)
�[31mTOTAL: 1 FAILED, 1298 SUCCESS�[39m


�[31m1) should close the collection when the automatic migration fails
�[39m�[31m     migration-schema.test.ts issues collection of a not finished addCollections() call
�[39m�[90m     AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

false !== true

    at Context.<anonymous> (/home/runner/work/rxdb/rxdb/test/unit/migration-schema.test.ts:2851:17 <- /tmp/_karma_webpack_rxdb_5865/commons.js:208686:45)
�[39m



View full workflow run

@pubkey
pubkey force-pushed the claude/vigilant-newton-8rqsab-close-collection-of-failed-or-interrupted-add-collections branch 2 times, most recently from 4af0961 to 6ff9cff Compare November 13, 2026 12:55
… call

A collection that was created by addCollections() but never reached
database.collections was not closed when its creation failed (for example
DM4 from the autoMigrate migration) or when database.close() was called
while the automatic migration was still running. Its timeouts, like the
60 seconds wait of the cleanup plugin, kept the process alive.

createRxCollection() now closes the collection when the creation fails and
database.close() also closes the collections of this database that are
still being created.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6hnnyVPXwotts6YQ2F
@pubkey
pubkey force-pushed the claude/vigilant-newton-8rqsab-close-collection-of-failed-or-interrupted-add-collections branch from 6ff9cff to b2c5fbe Compare November 14, 2026 13:20
close() collected the collections from OPEN_COLLECTIONS and from
this.collections. A normal collection is in both, so its close() ran
twice in the same tick and the onClose handlers ran twice. The GraphQL
replication then released its websocket reference twice and threw
"missing value from map ws://..." as an unhandled rejection. Both lists
are now merged into a Set so that each collection is closed once.

The test 'should close the collection when the database is closed
during the automatic migration' awaited database.close() before it
released the migration strategy. Since #9661 closing waits for the
running batch of the canceled migration, so the test never ended. It now
releases the strategy after the migration was canceled and then awaits
the close.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6hnnyVPXwotts6YQ2F

This branch has not been deployed

No deployments
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