Skip to content

fix(firestore, android): firestore instance cache key mismatch#9097

Open
russellwheatley wants to merge 4 commits into
mainfrom
firestore-8981
Open

fix(firestore, android): firestore instance cache key mismatch#9097
russellwheatley wants to merge 4 commits into
mainfrom
firestore-8981

Conversation

@russellwheatley

@russellwheatley russellwheatley commented Jul 10, 2026

Copy link
Copy Markdown
Member

Description

UniversalFirebaseFirestoreCommon.getFirestoreForApp() on Android read the instance cache using the key appName:databaseId but wrote it back using only appName, so the cache lookup could never hit — for the default database or any custom database.

Because of this, every native Firestore call re-derived and reapplied settings via setFirestoreSettings() against the same underlying FirebaseFirestore instance. Once that instance had already been used, reapplying settings that differ from what's currently active throws:

IllegalStateException: FirebaseFirestore has already been started and its settings can no longer be changed.
This surfaced in production (Crashlytics) when firestore.settings(...) was called during app startup while document listeners were mounting concurrently. Fix: write the cache entry using the same firestoreKey that's used to read it, matching the (already-correct) iOS implementation.

Related issues

fixes #8981

Release Summary

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
    • Yes
  • My change supports the following platforms;
    • Android
    • iOS
    • Other (macOS, web)
  • My change includes tests;
    • e2e tests added or updated in packages/\*\*/e2e
    • jest tests added or updated in packages/\*\*/__tests__
  • I have updated TypeScript types that are affected by my change.
  • This is a breaking change;
    • Yes
    • No

Test Plan


Think react-native-firebase is great? Please consider supporting the project with any of the below:

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a cache key mismatch issue on Android that prevented the Firestore instance cache from being correctly utilized. By aligning the cache storage key with the lookup key, the fix ensures that subsequent operations on an initialized Firestore instance do not trigger redundant settings applications, which previously caused runtime exceptions.

Highlights

  • Firestore Instance Cache Fix: Updated the Firestore instance cache key in UniversalFirebaseFirestoreCommon.java to use the unique firestoreKey instead of just the appName, ensuring correct cache hits.
  • Regression Testing: Added an end-to-end test case to verify that updating Firestore settings on an already-started instance no longer throws an IllegalStateException.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an Android Firestore instance cache key mismatch (issue 8981) by updating the cache write key to use firestoreKey instead of appName, and adds a corresponding E2E regression test. However, because instanceCache is a WeakHashMap, using dynamically created String keys will cause the keys to be garbage collected almost immediately, evicting the cache entries. It is recommended to change instanceCache to a standard ConcurrentHashMap since the values are already wrapped in WeakReference.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.67%. Comparing base (3933b45) to head (cc8659c).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9097      +/-   ##
============================================
+ Coverage     58.61%   58.67%   +0.06%     
- Complexity     1616     1620       +4     
============================================
  Files           503      503              
  Lines         39178    39180       +2     
  Branches       5807     5743      -64     
============================================
+ Hits          22962    22983      +21     
+ Misses        14811    14739      -72     
- Partials       1405     1458      +53     
Flag Coverage Δ
android-native 53.61% <100.00%> (+0.05%) ⬆️
e2e-ts-android 50.03% <100.00%> (+0.06%) ⬆️
e2e-ts-ios 53.95% <ø> (+0.06%) ⬆️
e2e-ts-macos 41.48% <ø> (ø)
ios-native 53.95% <ø> (+0.06%) ⬆️
jest 49.58% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@russellwheatley russellwheatley marked this pull request as ready for review July 10, 2026 15:56
@cursor

cursor Bot commented Jul 10, 2026

Copy link
Copy Markdown

Bugbot is not enabled for this team, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@mikehardy mikehardy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix looks good - one question on the test which cuts to the problem I had with it - can you trigger an expected failure in e2e bet altering settings in an already started database?

+1 because the fix is the obvious fix, a negative probe is just a hope

Comment on lines +189 to +191
fail(
`Regression in issue 8981: switching database on the same app and reapplying settings should not throw, but got: ${e}`,
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so here's the question - if you specifically do the pathological thing - now taking an already started firestore database and try to change it's settings in a way that should throw, does it throw?

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.

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

2 participants