Sendbird.connect sometimes blocks UI thread and produces ANRs

Our users experience ANRs frequently, when opening the app, however we cannot reproduce them. There are about 800 events monthly which leads to a poor user experience.
Those are the logs we are provided with by Firebase Crashlytics.
Currently on 3.1.0, however this is an old issue, it’s been happening for months, but wasn’t tracked 'till recently by crashlytics.

main (unknown): tid=1 systid=28362        --- DEADLOCKED --- TRIGGERED ANR ---
       at com.sendbird.android.SendBird.isInitialized(SendBird.java:18446744073709552000)
       at com.sendbird.android.SocketManager.connect(SocketManager.java:349)
       at com.sendbird.android.SendBird._connect(SendBird.java:1187)
       at com.sendbird.android.SendBird.connect(SendBird.java:1159)
       at com.flipsidegroup.killingkittens.bll.ConnectionManager.connect(ConnectionManager.kt:10)
       at com.flipsidegroup.killingkittens.bll.KKSyncManager$init$2.invokeSuspend(KKSyncManager.kt:43)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:246)
       at android.app.ActivityThread.main(ActivityThread.java:8653)
       at java.lang.reflect.Method.invoke(Native method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
pool-65-thread-428 (unknown): tid=244 systid=25861      --- DEADLOCKED ---
       at com.sendbird.android.SocketManager.reconnect(SocketManager.java:18446744073709552000)
       at com.sendbird.android.SendBird.reconnect(SendBird.java:1207)
       at com.sendbird.android.APIRequest.makeRequestBuilder(APIRequest.java:78)
       at com.sendbird.android.APIRequest.POST(APIRequest.java:129)
       at com.sendbird.android.APIClient.requestPOST(APIClient.java:3309)
       at com.sendbird.android.APIClient.registerPushToken(APIClient.java:2104)
       at com.sendbird.android.SendBird$14.call(SendBird.java:2020)
       at com.sendbird.android.SendBird$14.call(SendBird.java:2009)
       at com.sendbird.android.JobResultTask$callable$1.call(JobResultTask.kt:16)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:923)

We tried to move the execution to another thread and upgraded the SDK version to 3.1.1, however this still happens:

main (unknown): tid=1 systid=7689     --- TRIGGERED ANR ---
       at com.sendbird.android.SendBird.startWatchdog(SendBird.java)
       at com.sendbird.android.SocketManager.notifyConnectionComplete(SocketManager.java:197)
       at com.sendbird.android.SocketManager.connectionComplete(SocketManager.java:189)
       at com.sendbird.android.SocketManager.access$1100(SocketManager.java:36)
       at com.sendbird.android.SocketManager$10.onResultForUiThread(SocketManager.java:519)
       at com.sendbird.android.SocketManager$10.onResultForUiThread(SocketManager.java:459)
       at com.sendbird.android.JobResultTask$callable$1$1.run(JobResultTask.kt:22)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8582)
       at java.lang.reflect.Method.invoke(Native method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)
DefaultDispatcher-worker-4 (unknown): tid=32 systid=8341        --- DEADLOCKED ---
       at com.sendbird.android.SendBird.isInitialized(SendBird.java)
       at com.sendbird.android.SocketManager.connect(SocketManager.java:375)
       at com.sendbird.android.SendBird._connect(SendBird.java:1164)
       at com.sendbird.android.SendBird.connect(SendBird.java:1136)
       at com.flipsidegroup.killingkittens.bll.ConnectionManager.connect(ConnectionManager.kt:13)
       at com.flipsidegroup.killingkittens.bll.KKSyncManager$init$2$user$1.invokeSuspend(KKSyncManager.kt:44)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
pool-32-thread-41 (unknown): tid=208 systid=30513      --- DEADLOCKED ---
       at com.sendbird.android.SocketManager.reconnect(SocketManager.java)
       at com.sendbird.android.SendBird.reconnect(SendBird.java:1184)
       at com.sendbird.android.APIRequest.makeRequestBuilder(APIRequest.java:78)
       at com.sendbird.android.APIRequest.GET(APIRequest.java:122)
       at com.sendbird.android.APIClient.requestGET(APIClient.java:3304)
       at com.sendbird.android.APIClient.messageChangeLogs(APIClient.java:770)
       at com.sendbird.android.BaseChannel.getMessageChangeLogsBlocking(BaseChannel.java:594)
       at com.sendbird.android.BaseChannel$1.call(BaseChannel.java:555)
       at com.sendbird.android.BaseChannel$1.call(BaseChannel.java:551)
       at com.sendbird.android.JobTask$callable$1.call(JobTask.kt:9)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:920)

Hi @George_Fodor , sorry for the late response. And thanks for your report.
We’ll investigate it but I think it will take some time because we can’t reproduce it now.
I’ll let you know if there’s an update. I really appreciate it for providing error logs.

Hi, @George_Fodor

We’re having a progress on debugging this issue.
Can you give us more detail on when you are calling the getMessageChangelogs API and SendBird.connect()?
We’re trying to narrow down on which scenario this could happen.

Thank you.

Hi @Jerry_Jeon

SendBird.connect is always called when the app comes back from the background (in onResume), ONLY when SendBird.getConnectionState() != SendBird.ConnectionState.OPEN

getMessageChangelogs is also called when the app comes back from background, in our MediaGallery to update the reaction counter.

Thank you.

Hi @George_Fodor .
Thank you for sharing a detail. According to your commend, you’re calling getMessageChangelog without waiting for the connection to be completed, right?
Would you give it a try for SDK version 3.1.4? We fixed some points that can cause ANR.

@Jerry_Jeon Possible. We’ll give it a try, as we cannot reproduce it we’ll have to roll-out an update and see if our users experience this again. I’ll come back to you in a few days when I have the results.

1 Like

Hi there - we have had a similar issue that I was able to reproduce with the following steps using uikit sdk 2.2.2
→ open a channel and repeat many times:
1. type & send a message
2. take app to background
3. bring app back to foreground
4. repeat 1-3 until at some point you get ANR.

@berry Could you give us a log for ANR?

So the log was the exact same log that was provided here. I realized though that the way I suggested to reproduce it was only reproducing the ANR in a specific version of how we implemented chat in our app. In that specific version we were checking whether other user in the channel was online just as we opened the chat. We added a delay to checking for online and that seems to have solved the ANR for now.

1 Like

Thank you @berry
As you said, it will not be generated if you implement your API call with some delay. Maybe, we will fix this issue next release!

@Doo_Rim Unfortunately this ANR still happens occasionally. I do not know exactly how to reproduce it.
Please add this to your priority for next release

1 Like

@berry Okay, After releasing the fixed version, I will let you know!

Hey, @Jerry_Jeon. Sorry for the late reply, we just managed to release the new version a couple of days ago and I’m super happy to tell you that the ANRs have completely disappeared. Admittedly only about 30% of our users are on the latest version, but that’s still about 1k on the latest Android release. I thank you very much for your cooperation and the help you provided.