Skip to content

fix(common): Resolve thread deadlock in HeartBeatInfoStorage#8182

Open
milaGGL wants to merge 8 commits into
mainfrom
mila/fix-HeartBeatInfoStorage-deadlock
Open

fix(common): Resolve thread deadlock in HeartBeatInfoStorage#8182
milaGGL wants to merge 8 commits into
mainfrom
mila/fix-HeartBeatInfoStorage-deadlock

Conversation

@milaGGL
Copy link
Copy Markdown
Contributor

@milaGGL milaGGL commented May 19, 2026

The Bug
The deadlock was caused by mixing legacy Java synchronized monitor locks with modern asynchronous Kotlin Coroutines inside the JavaDataStorage wrapper.

  1. Lock Acquisition & Thread Blocking: A calling thread (Thread A) enters the synchronized storeHeartBeat(...) method, acquiring the monitor lock on HeartBeatInfoStorage.this. It then blocks inside runBlocking waiting for the Jetpack DataStore preference transaction (JavaDataStorage.editSync(...)) to complete.
  2. Background Execution: Jetpack DataStore handles file edits asynchronously, scheduling and executing the transaction callback on its own internal background worker thread (Thread B).
  3. Lock Collision (Deadlock): Inside the transaction callback, Thread B calls private helper methods (e.g., getStoredUserAgentString, updateStoredUserAgent, cleanUpStoredHeartBeats). Because these helper methods were also declared as synchronized, Thread B attempts to acquire the monitor lock on HeartBeatInfoStorage.this, causing a permanent thread deadlock.

The Fix
Removed the synchronized keyword from all private helper methods and utility methods.

These methods operate exclusively on thread-local transaction parameters and do not access shared mutable instance state, making synchronization unnecessary. This allows the background thread to execute them safely without acquiring the class monitor.

Verification
Added the regression unit test storeHeartBeat_whenCalledOnSeparateThread_doesNotDeadlock to reproduce the deadlock condition and verify the fix.

Fixes #8016

milaGGL added 2 commits May 19, 2026 13:38
Remove the legacy `synchronized` keyword from private helper methods and pure utility methods inside `HeartBeatInfoStorage` (such as `getStoredUserAgentString`, `updateStoredUserAgent`, `removeStoredDate`, `getFormattedDate`, `cleanUpStoredHeartBeats`, and `isSameDateUtc`).

Under the bug, `storeHeartBeat()` locked the class monitor while blocking in `runBlocking` for Jetpack DataStore's `editSync()` to complete. Because DataStore schedules transactions on a background thread, helper methods called inside the transaction block that were also synchronized on the same monitor resulted in a permanent deadlock. Since these helper methods operate exclusively on thread-local transaction parameters and do not access shared mutable instance state, they do not require synchronization.

Add regression unit test `storeHeartBeat_whenCalledOnSeparateThread_doesNotDeadlock` to verify the fix.

Fixes #8016
@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
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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 19, 2026

📝 PRs merging into main branch

Our main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released.

assertThat(heartBeatDataStore.getSync(GLOBAL, -1L)).isEqualTo(currentTime);
}

/**
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.

Move this tests to a kolin file so there's no need to use language bridging features, like kotlin.jvm.functions.Function1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

create a kotlin file to put the test case in

@milaGGL milaGGL requested a review from rlazo May 19, 2026 22:15
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.

Deadlock in firebase-common heartbeat DataStore path can wedge Firebase Installations getToken()/getId() forever

2 participants