cordova-plugin-nativeaudio icon indicating copy to clipboard operation
cordova-plugin-nativeaudio copied to clipboard

got `ConcurrentModificationException` exception

Open achmadk opened this issue 7 years ago • 1 comments

Excuse me, Mr. @floatinghotpot . I use your plugin to play or stop audio files in my game project. Almost users who installed that game get ConcurrentModificationException. This is one of the stack trace reported from Google Play console.

java.lang.RuntimeException: 
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3616)
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3571)
  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3546)
  at android.app.ActivityThread.access$1100(ActivityThread.java:188)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1548)
  at android.os.Handler.dispatchMessage(Handler.java:111)
  at android.os.Looper.loop(Looper.java:210)
  at android.app.ActivityThread.main(ActivityThread.java:5833)
  at java.lang.reflect.Method.invoke(Native Method:0)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
Caused by: java.util.ConcurrentModificationException: 
  at java.util.HashMap$HashIterator.nextEntry(HashMap.java:787)
  at java.util.HashMap$EntryIterator.next(HashMap.java:824)
  at java.util.HashMap$EntryIterator.next(HashMap.java:822)
  at com.rjfun.cordova.plugin.nativeaudio.NativeAudio.onPause(NativeAudio.java:318)
  at org.apache.cordova.PluginManager.onPause(PluginManager.java:211)
  at org.apache.cordova.CordovaWebViewImpl.handlePause(CordovaWebViewImpl.java:431)
  at org.apache.cordova.CordovaActivity.onPause(CordovaActivity.java:245)
  at android.app.Activity.performPause(Activity.java:6288)
  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1321)
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3598)

achmadk avatar May 18 '17 07:05 achmadk

We believe that we're encountering this issue intermittently when we unload sounds just before dismissing our Cordova MainActivity; it seems like doing that dismissal causes the Native Audio plugin's onPause method to fire, so we might be unloading sounds while some other process leads to the pausing of sounds.

Calls to unload can take some time - we temporarily moved unload calls outside of the cordova pooled threads, which logs out a delay warning and we were able to see that the unload calls were taking 100ms each for us. This could easily have been enough to make the process overlap the implicit onPause that comes soon afterwards in our app.

I was able to simply change the type of access to the HashMap of sounds in the onPause method. It seems like using entrySet() counts as concurrent modification, but I didn't investigate why - I simply created a copy of the original HashMap to work through in onPause(), since all that's being done is pausing the sounds - no actual modification to the original set. This seems to have resolved the issue for us. Perhaps creating a copy is wrong, and perhaps using something other than entrySet() to iterate over that HashMap would also have been sufficient as a solution.

Cloov avatar Mar 14 '18 14:03 Cloov