Skip to content

[firestore][android] instance cache key mismatch causes repeated setFirestoreSettings / IllegalStateException #8981

@michaelpomogajko

Description

@michaelpomogajko

Issue

In the Android Firestore native module, UniversalFirebaseFirestoreCommon.getFirestoreForApp() reads from instanceCache using firestoreKey = appName + ":" + databaseId, but writes back using only appName.

That means the cache lookup can never hit for the default database or any custom database:

String firestoreKey = createFirestoreKey(appName, databaseId);
WeakReference<FirebaseFirestore> cachedInstance = instanceCache.get(firestoreKey);
...
instanceCache.put(appName, new WeakReference<FirebaseFirestore>(instance));

The write should use firestoreKey, not appName.

Why this matters

On Android, this can cause setFirestoreSettings(...) to be called repeatedly on an already-started Firestore instance, which crashes with:

java.lang.IllegalStateException: FirebaseFirestore has already been started and its settings can no longer be changed. You can only modify settings before calling any methods on a FirebaseFirestore object.

I hit this in production via Crashlytics while using @react-native-firebase/firestore 22.4.0, but I also checked the currently published npm release 24.0.0 and the current main branch, and both still contain the same cache write bug.

Current affected code

As of npm 24.0.0 and main:

packages/firestore/android/src/main/java/io/invertase/firebase/firestore/UniversalFirebaseFirestoreCommon.java

instanceCache.put(appName, new WeakReference<FirebaseFirestore>(instance));

Expected fix

instanceCache.put(firestoreKey, new WeakReference<FirebaseFirestore>(instance));

Local confirmation

Applying the one-line change above removed the Firestore initialization crash in my app.

Environment

  • @react-native-firebase/firestore: observed on 22.4.0
  • latest npm at time of writing: 24.0.0
  • platform: Android
  • app type: React Native / Expo dev client app using firestore.settings(...) during initialization

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions