[firestore][android] fix instance cache key for Firestore initialization#8982
Conversation
|
@michaelpomogajko is attempting to deploy a commit to the Invertase Team on Vercel. A member of the Team first needs to authorize it. |
|
|
There was a problem hiding this comment.
Code Review
This pull request corrects a cache key mismatch in UniversalFirebaseFirestoreCommon.java by using firestoreKey instead of appName for storage. While this fixes the immediate logic error, the reviewer pointed out that the current cache implementation using WeakHashMap is problematic because the locally generated string keys are subject to immediate garbage collection, making the cache ineffective. Additionally, the reviewer highlighted concerns regarding thread safety and potential null pointer exceptions, suggesting a move to ConcurrentHashMap for a more robust implementation.
| setFirestoreSettings(instance, firestoreKey); | ||
|
|
||
| instanceCache.put(appName, new WeakReference<FirebaseFirestore>(instance)); | ||
| instanceCache.put(firestoreKey, new WeakReference<FirebaseFirestore>(instance)); |
There was a problem hiding this comment.
While this change correctly fixes the key mismatch between lookup and storage, the underlying cache implementation remains problematic: (1) Key Reachability: WeakHashMap removes entries when the key object is no longer strongly reachable. Since firestoreKey is a local string created via concatenation, it will be garbage collected quickly, making the cache ineffective and leading to the repeated setFirestoreSettings calls this PR aims to avoid. (2) Thread Safety: WeakHashMap is not thread-safe. Concurrent access to this static map from multiple threads can cause race conditions or internal corruption. (3) WeakReference Handling: The retrieval logic at line 43 (visible in context) returns cachedInstance.get() without checking for null. If the WeakReference is cleared, the method returns null, which may cause a NullPointerException in the caller. Consider replacing instanceCache with a ConcurrentHashMap to ensure persistence, thread safety, and reliability.
Summary
firestoreKeysetFirestoreSettings(...)callsDetails
getFirestoreForApp()reads frominstanceCacheusingappName + ":" + databaseId, but was writing back using onlyappName.That makes the cache internally inconsistent:
firestoreKeyappNameThis change makes the cache write use the same key as the lookup.
Fixes #8981
Note
Low Risk
Small, localized change to cache key selection; main risk is altered instance reuse behavior for apps using multiple Firestore database IDs.
Overview
Fixes an Android Firestore caching bug where
getFirestoreForApp()stored instances ininstanceCacheunderappNameinstead of the compositefirestoreKey(appName:databaseId).This aligns cache writes with cache reads, preventing cache misses and avoiding repeated
setFirestoreSettings(...)application for the same app/database pair.Reviewed by Cursor Bugbot for commit 4f5ead2. Bugbot is set up for automated code reviews on this repo. Configure here.