Skip to content

fix: ANRs in main thread#632

Open
martinpelli wants to merge 1 commit into
deepak786:masterfrom
martinpelli:fix-android-anrs
Open

fix: ANRs in main thread#632
martinpelli wants to merge 1 commit into
deepak786:masterfrom
martinpelli:fix-android-anrs

Conversation

@martinpelli

Copy link
Copy Markdown

This PR attempts to fix ANRs issues mentioned in #629

Description

Fixes ANR triggered during app startup on Android when Intercom.initialize is called from Application.onCreate.

Intercom.initialize internally calls UserIdentityStore.loadUserFields using runBlocking over DataStore/Keystore operations. When invoked on the main thread, this blocks it for >5s on low-end devices (Tecno, Infinix), causing an ANR.

Changes

  • initSdk now spawns a daemon background thread "intercom-init" to run Intercom.initialize off the main thread
  • Added a CountDownLatch(1) that gates all method calls — any call arriving before init completes waits on a background thread, then posts back to main once the latch releases
  • Fast-path added: once initialized, latch.count == 0L short-circuits the wait with zero overhead
  • onMethodCall now delegates to a private dispatch(call, result) wrapped in runAfterInit — no per-method changes needed
  • Added Intercom.initialized (Future<void>) on the Dart side that completes when initialize() finishes, for clients that want to explicitly await readiness

@deepak786

Copy link
Copy Markdown
Owner

@martinpelli Thank you for the PR. I will check.

Since you have put the initialization on a separate thread, I'm worried that it could trigger another issue

Intercom was not initialized correctly, Intercom.initialize() needs to be called in onCreate() in your Application class.

@martinpelli

Copy link
Copy Markdown
Author

@deepak786 thanks! The thread is spawned from within Application.onCreate() and receives the Application instance directly. The Intercom SDK check validates that a valid Application context is passed, not that initialize() is called on the main thread. I have tested it and I don't see any problem, but I think until I test in production we don't really know.

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.

2 participants