Skip to content

fix(android): run getContext() on IO thread to prevent main-thread ANR#205

Open
Priyanshu-Mallick wants to merge 1 commit into
segmentio:mainfrom
Priyanshu-Mallick:fix/android-getContext-anr
Open

fix(android): run getContext() on IO thread to prevent main-thread ANR#205
Priyanshu-Mallick wants to merge 1 commit into
segmentio:mainfrom
Priyanshu-Mallick:fix/android-getContext-anr

Conversation

@Priyanshu-Mallick

Copy link
Copy Markdown

When collectDeviceId: true is configured, the native Android plugin calls MediaDrm(WIDEVINE_UUID) and getPropertyByteArray(PROPERTY_DEVICE_UNIQUE_ID) synchronously on the main thread inside getContext().

Since this involves a slow binder IPC call into the DRM service (taking hundreds of milliseconds to seconds on some OEMs), it frequently triggers an ANR in production:
com.segment.analytics.AnalyticsPlugin.getUniqueId Input dispatching timed out (No focused window)

This change mirrors the fix implemented in the native analytics-android SDK in version 4.10.4 (Feb 2022).

Changes:

  1. Wrapped the entire body of getContext() in a coroutine launched on Dispatchers.IO to ensure all slow operations (DRM query, package manager lookups, connectivity manager requests, and display metrics) run off the main thread.
  2. Switched back to Dispatchers.Main before calling the final callback to maintain thread-safe communication with the Flutter platform channel.
  3. Added the kotlinx-coroutines-android dependency to packages/core/android/build.gradle.

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.

1 participant