twilio / audioswitch

An Android audio management library for real-time communication apps.
Apache License 2.0
163 stars 50 forks source link

Crash by ConcurrentModificationException #75

Closed guness closed 3 years ago

guness commented 3 years ago

Describe the bug I don't have a prior logs on clue how did this happened. Stack trace does not show my code and I am not able to reproduce the issue, so sorry for that.

Stack Trace

Caused by java.util.ConcurrentModificationException
       at java.util.ArrayList$Itr.next(ArrayList.java:860)
       at java.util.AbstractCollection.toString(AbstractCollection.java:461)
       at java.lang.String.valueOf(String.java:2924)
       at java.lang.StringBuilder.append(StringBuilder.java:132)
       at com.twilio.audioswitch.AudioSwitch.enumerateDevices(AudioSwitch.java:274)
       at com.twilio.audioswitch.AudioSwitch.access$setLogger$p(AudioSwitch.java:29)
       at com.twilio.audioswitch.AudioSwitch$bluetoothDeviceConnectionListener$1.onBluetoothHeadsetStateChanged(AudioSwitch.java:47)
       at com.twilio.audioswitch.bluetooth.BluetoothHeadsetConnectionListener$DefaultImpls.onBluetoothHeadsetStateChanged$default(BluetoothHeadsetConnectionListener.java:4)
       at com.twilio.audioswitch.bluetooth.BluetoothHeadsetManager.onReceive(BluetoothHeadsetManager.java:140)
       at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1588)
       at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(-.java:2)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7615)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

and second one, probably on another thread or worker:

Fatal Exception: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.media.ACTION_SCO_AUDIO_STATE_UPDATED flg=0x10000010 (has extras) } in com.twilio.audioswitch.bluetooth.BluetoothHeadsetManager@df0a9a7
       at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1606)
       at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(-.java:2)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7615)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

Expected behavior Probably some thread protection should be there or maybe immutable list should be used. you know the best.

Android Device (please complete the following information):

Version

Additional context Happens rarely.

Alton09 commented 3 years ago

Hi @guness . From the ConcurrentModificationException it appears that your application is accessing an AudioSwitch instance from multiple threads. We currently only support access to this class from a single thread as specified in the documentation here. Please make sure that access to this class in your application is single threaded. Thanks!

guness commented 3 years ago

hey Alton, I will make this single threaded. thanks for the tip.

filipebatista commented 3 years ago

Hey, I think the README should refer to this situation of only supporting access from a single thread. Besides that I think there are two situations that should also be informed:

Thanks

Alton09 commented 3 years ago

Hi @filipebatista . Thanks for the recommendation! We will add this one to our backlog to address your feedback. We accept pull requests from the community as well so feel free do to so if you'd like.