Repository navigation
fix(android-ble): prevent ANR by moving blocking Bluetooth calls off main thread - #1186
Merged
Merged
Conversation
Contributor
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses a Background ANR issue (COLUMBA-DY) where blocking Android Bluetooth framework calls were executed on the main thread. On Android 13+, calls like
startScan,stopScan,discoverServices,requestConnectionPriority, advertising, and GATT server responses can block synchronously for extended periods when the Bluetooth stack is congested, leading to ANRs.The solution involves:
BleDispatchers.ble: A dedicated single-threadedCoroutineDispatcher(Dispatchers.IO.limitedParallelism(1)) to handle all blocking Bluetooth framework calls. This maintains the serialized execution semantics previously provided by the main thread but prevents blocking the UI.withContext(Dispatchers.Main)blocks wrapping Bluetooth framework calls inBleScanner,BleGattClient,BleAdvertiser, andBleGattServerhave been replaced withwithContext(bleDispatcher).bleDispatcheris now injected via the constructor in these classes, allowing tests to provide aTestDispatcher(e.g.,UnconfinedTestDispatcher) for predictable timing and control, addressing feedback from code review.This change prevents ANRs by ensuring that slow Bluetooth operations do not freeze the main thread, while preserving the intended sequential execution of these calls and improving test isolation.
Fixes COLUMBA-DY
@sentry <feedback>: Autofix iterates on these changes@sentry stop iterating: Autofix stops iterating on this runThis PR was automatically generated by Sentry. You can adjust this setting at any time.