Skip to content

[FCM] Fix ANR in SharedPreferencesQueue by reducing lock contention#8068

Merged
rlazo merged 3 commits intofirebase:mainfrom
xiaohegong:xiaohegong/fix-anr
Apr 28, 2026
Merged

[FCM] Fix ANR in SharedPreferencesQueue by reducing lock contention#8068
rlazo merged 3 commits intofirebase:mainfrom
xiaohegong:xiaohegong/fix-anr

Conversation

@xiaohegong
Copy link
Copy Markdown
Contributor

@xiaohegong xiaohegong commented Apr 27, 2026

Change SharedPreferences.Editor.commit() to apply() in SharedPreferencesQueue.syncState().

Previously, the synchronized block held the lock while performing a synchronous commit(), which could block the background thread on disk I/O. If the main thread attempted to access the queue (e.g., in add()), it would block waiting for the lock, leading to ANRs.

By switching to apply(), the operation updates the in-memory SharedPreferences immediately and schedules the disk write asynchronously. This avoids blocking on disk I/O while holding the lock, significantly reducing lock contention and preventing ANRs. Keeping it inside the synchronized block ensures that updates are applied in the correct order even if called from multiple threads.

@xiaohegong xiaohegong requested a review from gsakakihara April 27, 2026 22:33
@gemini-code-assist
Copy link
Copy Markdown
Contributor

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

Customization

To customize 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 counter productive. 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.

@xiaohegong xiaohegong requested a review from rlazo April 27, 2026 22:44
@xiaohegong xiaohegong enabled auto-merge (squash) April 28, 2026 17:54
@xiaohegong xiaohegong disabled auto-merge April 28, 2026 17:55
@xiaohegong xiaohegong changed the title Fix ANR in SharedPreferencesQueue by reducing lock contention [FCM] Fix ANR in SharedPreferencesQueue by reducing lock contention Apr 28, 2026
@rlazo rlazo merged commit b396cfb into firebase:main Apr 28, 2026
22 of 24 checks passed
@xiaohegong xiaohegong deleted the xiaohegong/fix-anr branch April 28, 2026 23:01
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.

3 participants