atn832 / fake_cloud_firestore

BSD 2-Clause "Simplified" License
118 stars 88 forks source link

ConcurrentModificationError in QuerySnapshotStreamManager.fireSnapshotUpdate #263

Closed kinex closed 7 months ago

kinex commented 1 year ago

Exception: ConcurrentModificationError (Concurrent modification during iteration: _LinkedHashMap len:3.)

Happens here: ..\pub.dartlang.org\fake_cloud_firestore-2.3.0+3\lib\src\query_snapshot_stream_manager.dart:91)

kinex commented 1 year ago

The exception happens because the await in line 92 makes it possible to call other methods for this instance while the loop is still being executed. Possible fix:

    final exactPathCache = _streamCache[firestore]![path];
    if (exactPathCache != null) {
      for (final query in exactPathCache.keys.toList()) {
        final streamController = exactPathCache[query];
        if (streamController != null) {
          await query.get().then(streamController!.add);
        }
      }
    }
atn832 commented 1 year ago

Amazing that you were able to track down this issue. Would you be able to contribute a PR? The hard part would be to write a test that reproduces the issue.

atn832 commented 7 months ago

You can now get the fix at https://pub.dev/packages/fake_cloud_firestore/changelog#244. Thank you for your patience!