ANR on player release
Issue description
According to log reports from users, after upgrade to r2.8.1 (from r2.6.1), on about 1% of app sessions we have ANR on SimpleExoPlayer.release(). Never seen this issue (reports) on versions <=r2.6.1.
Reproduction steps
Play some stream and then call SimpleExoPlayer.release(), called in main thread.
Link to test content
Unknown for a while.
Version of ExoPlayer being used
r2.8.1.
Device(s) and version(s) of Android being used
Different devices and Android versions: Nvidia Shield TV, Xiaomi Mi-Box, etc.
A full bug report captured from the device
"main" tid=1 Waiting
"main" prio=5 tid=1 Waiting | group="main" sCount=1 dsCount=0 obj=0x762777f0 self=0x7527e95a00 | sysTid=32258 nice=-10 cgrp=default sched=0/0 handle=0x752beaea98 | state=S schedstat=( 264497782805 29710697109 1267932 ) utm=22158 stm=4291 core=7 HZ=100 | stack=0x7ff8a69000-0x7ff8a6b000 stackSize=8MB | held mutexes=
at java.lang.Object.wait! (Native method)
- waiting on <0x0225fb39> (a com.google.android.exoplayer2.ExoPlayerImplInternal)
at com.google.android.exoplayer2.ExoPlayerImplInternal.release (unavailable)
- locked <0x0225fb39> (a com.google.android.exoplayer2.ExoPlayerImplInternal)
com.google.android.exoplayer2.ExoPlayerImpl.release (unavailable)
com.google.android.exoplayer2.SimpleExoPlayer.release (unavailable)
...
- It would be useful to see a bug report so we can check what the playback thread is doing (it is responsible for notifying the thread waiting in
releasethat resources have been returned to the system). If you have one, please could you attach it here or email it to[email protected]with subject "Issue #4352"? - Do you have any information on whether all devices are affected equally, or certain devices are affected more than others?
- To help narrow down which changes between 2.6.1 and 2.8.1 might have caused this, it would be good to know if the app is doing anything that would exercise recently added functionality like: setting/clearing the player's surface during playback, or using blocking message sending (we couldn't see any issues with this, but thought it was worth checking just in case). Thanks!
Agreed we need a full bug report (or at least the stack traces of all of the threads) to debug this.
-
Could this "bug report" be get from Google Play Console's ANR reports? And will it has enough data to analyze playback thread?
-
Looks like, all devices are affected equally. It will be more precise when more users install app update.
-
We did not added any new features when upgraded from 2.6.1 to 2.8.1. No manual message sending. But surface changing during playback had been added a long time ago. For example, when Surface occasionally happens to be recreated during playback.
@andrewlewis sent you an e-mail with ANR bug reports from different devices.
Looking at the stack traces, it looks like release is blocked on the playback thread, which is typical for this kind of ANR. The playback thread is blocked on various different codec related platform calls (e.g. MediaCodec.native_stop), without a clear pattern.
We do see this kind of ANR happen occasionally on devices, when the underlying platform media server gets into a bad state. This is not something we'd expect to occur more frequently with 2.8.0 than earlier releases, however, and I'm not aware of anything that we did that's likely to have exacerbated the issue.
Are you certain these ANRs are increased as of 2.8.0, rather than some other change, such as them being better reported than they were previously? What ANR rate did you see when releasing the player previously? It should have been higher than 0 if ANRs were being reported correctly, since we've been aware of rare/sporadic ANRs when calling release for quite a long time.
@ojw28 Thanks!
We have not seen any of ExoPlayer related ANR reports before. Even can't find any of them. It appears in the last version which uses upgraded 2.8.1 version. The app is on beta stage currently and not looks like it's too much of them but comparable value with the others. We'll put the app with 2.8.1 as a release and then inform you of the result ANR rate.
Do you have any ideas why could we have not seen ExoPlayer related ANRs before?
That's (unfortunately!) strange, unless you were previously not calling release on the player. Is that possible? If not, then I think I would expect at least the odd ANR to be reported. Please do let us know what you discover.
Hey,
We are using the 2.7.3 and we are facing the same issue.
So this is not specific to 2.8.X version.
After the final release we do not see any valuable ANR growth. However, we saw it (ExoPlayer-related ANRs) in reports of Beta-versions. Probably, was caught "problematc" beta testers.
Also, during research we've found some specific devices on which changing Surface on-the-fly causes ANRs. Will open separate issue on this. Thanks!
Hi,
I'm seeing the same ANR issue on both the release() method of the exoplayer and also the blockingSendMessages of the exoplayer. Am on exoplayer 2.2.0. Seeing that this issue has still not been resolved, is there good reason to update to the latest version and try? Also, any workarounds for this issue? I have attached stack traces for both issues. blockingSendMessages.txt release_trace.txt
We are experiencing this issue in 2.7.1 as well, stacktrace from the Google Play Console, as you can see we call SimpleExoPlayer.release.
at java.lang.Object.wait (Object.java)
- waiting on <0x057d9834> (a com.google.android.exoplayer2.k)
at com.google.android.exoplayer2.ExoPlayerImplInternal.release (ExoPlayerImplInternal.java:232)
- locked <0x057d9834> (a com.google.android.exoplayer2.k)
at com.google.android.exoplayer2.ExoPlayerImpl.release (ExoPlayerImpl.java:342)
at com.google.android.exoplayer2.SimpleExoPlayer.release (SimpleExoPlayer.java:681)
at com.plexapp.plex.videoplayer.local.v2.Exo2VideoPlayer.disconnect
This looks to be only picked up now because of the changes to ANR's on the Google Play Console, rather than an issue in a new version of ExoPlayer v2 (at least from what we can tell).
@ojw28 / @tonihei After all discussions about threading and the adding of your internal stat handler that creates the race conditions https://github.com/google/ExoPlayer/issues/4278.
I finally followed the recommendations and switched to all access on mainthread and I'm now facing this ANR too. (Google Vitals, unable to reproduce yet)
"main" prio=5 tid=1 Waiting
| group="main" sCount=1 dsCount=0 obj=0x749478d0 self=0x7f92095a00
| sysTid=9432 nice=0 cgrp=default sched=0/0 handle=0x7f95f53a98
| state=S schedstat=( 0 0 0 ) utm=1130 stm=86 core=4 HZ=100
| stack=0x7fd81f0000-0x7fd81f2000 stackSize=8MB
| held mutexes=
at java.lang.Object.wait! (Native method)
- waiting on <0x026f6e6f> (a com.google.android.exoplayer2.l)
at com.google.android.exoplayer2.l.a (ExoPlayerImplInternal.java:228)
- locked <0x026f6e6f> (a com.google.android.exoplayer2.l)
at com.google.android.exoplayer2.j.c (ExoPlayerImpl.java:372)
at com.google.android.exoplayer2.al.c (SimpleExoPlayer.java:788)
at org.leetzone.android.yatsewidget.f.c.e$b.run (MusicPlayer.kt:200)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6186)
at java.lang.reflect.Method.invoke! (Native method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:779)
Related code is;
handler.sendEmptyMessage(MSG_RELEASE);
boolean wasInterrupted = false;
while (!released) {
try {
wait();
} catch (InterruptedException e) {
wasInterrupted = true;
}
}
IMO you can't ask people to call that on main thread as there's so many possible reasons for a message sent to a thread to be delayed or not arriving at all.
Hard to choose between very rare race conditions or rare ANR.
And a little disappointed that the issue was known yet pushing toward main thread :(
since we've been aware of rare/sporadic ANRs when calling release for quite a long time.
I should have rewritten the code to do everything on a background thread instead.
Is there a way to reduce risk of race conditions with your stat handler when moving back the release call to background thread?
IMO you can't ask people to call that on main thread as there's so many possible reasons for a message sent to a thread to be delayed or not arriving at all.
The rare/sporadic ANRs occur when MediaCodec gets into a bad state, causing one of ExoPlayer's calls into it to hang indefinitely. I'm not sure that we've ever seen a reported occurrence that's been anything other than this, so I disagree with the statement that there are many possible reasons. In practice we only see one.
Hard to choose between very rare race conditions or rare ANR.
I don't think this is an accurate reflection of the choice. It's more like choosing between:
- Rare ANRs, which result in your process being killed. This is bad, but at least make it likely that your app will be in a good state when the user re-opens it.
- Releasing the player on a background thread, which is being a bad citizen. By doing this you hold on to limited resources (e.g. hardware codecs, possibly the secure video output path) for an uncontrolled period of time that likely extends beyond the point at which your activity is stopped. Whatever activity is coming into the foreground may try and acquire those resources before you release them, and so may end up failing to do so. On some older devices you can break the camera by holding onto the secure video output path beyond the point at which your activity is stopped, for example. Also, whilst you avoid your process being killed, it's quite likely that subsequent playbacks within your own process will fail due to the bad state of the underlying media components. So whilst you've made the problem less visible from the point of view of your dashboards / automatic feedback, you might have actually made the problem worse for your users because your app may just remain in a broken state until the process is killed via some other means.
You send a message to a thread and wait for something that is supposed to be done there for infinite time. Maybe currently only MediaCodec bad state generate that, but message queue could be full or take time to manage all and all those will generate lag on main thread. Wait on main thread is being a bad citizen on Android, and Google says that nearly everywhere ;) Specially when it's done inside a library without users having a way to know that this will happens and with said library urging users to only use main thread. I'm happy to have seen that during beta phase.
About the choice, currently the app only use AudioPlayer part, so being a bad citizen is less an issue, and this is just a small part of the app. Release occurs when the user changes it's player to something not ExoPlayer related so hopefully won't reuse the player until the codec is released. If it's never released then maybe there's something to address in ExoPlayer too if possible, else in all cases, crash or issue user is not happy about the app.
And currently app is rated 4.75 and in top 2% of all Play Store apps about crash and 7% for ANR (Both remaining issues being due to bugs in Support Library that should be fixed in 28 and Android 8 bug with startForeground). I'm lucky to have an active community that report issues and allow building such app, so everything I can do on my side to ensure them a proper experience is important. 0,001% of crashed MediaCodec is better than 0,01% of ANR or Lag as both result in reduced experience but one case impact less users.
but message queue could be full or take time to manage all and all those will generate lag on main thread
This is hypothetical and not true in practice. The message queue is completely under our control, and will normally only have one or two messages queued at any given point in time.
Wait on main thread is being a bad citizen on Android, and Google says that nearly everywhere ;)
There are some cases where not waiting is significantly worse, as is the case here. There are other cases where waiting on the main thread is unavoidable. For example if you've passed a Surface reference to a non-main thread and that surface is being destroyed. You're contractually obliged to block the main thread until the non-main thread no longer holds a reference to the Surface in that case. This is the one other case where we block the main thread until a message on the playback thread has been processed.
@ojw28 So the question is how to avoid the MediaCodec infinite wait so :)
Is there a way to detect that? Is there a way to kill the ExoPlayer thread without the process? (If that reset the MediaCodec of course) Is there a way to detect blocked mediacodecs on new ExoPlayer creation to warn the user and propose a restart of the process?
I don't mind showing a wait dialog or anything useful to the users but randoms ANR or app killing for P without a message or anything to warn the user for something that is known and I hope can be detected does not sounds OK to provide quality app.
I'm open to any suggestion or workaround. Just don't want ANR / app killing without proper user message.
PS: Not wanting to argue more but
This is hypothetical and not true in practice. The message queue is completely under our control, and will normally only have one or two messages queued at any given point in time.
Just 2 messages and a wait is some ms and dropped frames, of course it's not that awful and very far from the ANRs, but it's still dropped frames so lag to the user. And just saying normally implies that there could be anormal cases. Just adding this as note for others that will read, yes it may be unavoidable, but yes it will implies some impact that users should know, as small as they normally are. With audio players, users are probably doing something else in the app and will feel the lag when scrolling, it's a little different from a video player where there can be some delay when they press stop.
Unfortunately the only real fix for the MediaCodec issue is to better prevent devices from shipping with the underlying issues that cause it in the first place. Which we've been working on, but it's a hard problem. The percentages are so low and the failures so sporadic, that it's not possible to write a test that will reliably detect such issues during Android device certification.
I'm not aware of a way to kill the ExoPlayer thread without killing the process. I don't think that's possible.
Just 2 messages and a wait is some ms and dropped frames
Whether there are dropped frames depends entirely on how long those messages take to process, so this in itself does not seem like a strong argument. I'm not saying there are never dropped frames, but I am saying (a) it's not inevitable that the piece of code in question will cause dropped frames, and (b) the benefit of blocking until we've released the limited resources is more important.
The ANR issue affects our own applications (e.g. YouTube) as well. If we thought there were a better solution then we would have implemented it.
Well there was no offense I'm not native English speaker so often sound harsh. I totally suppose that you'll have fixed it if it was possible. All I'm saying is that ANR / crash are bad, a message saying there's a problem press ok to restart the app is better. I can understand it does not fit Youtube, but for my user target it's a better option.
About sporadic, I only have 7500 beta testers and the audio player is used by 5% max, beta have run for 3 days and already happened twice on an S6 edge and Moto X.
So for my use case the remaining solution would be to propose to the user to restart the app when there's the lock.
The best solution (for my need) would be that my app only propose that when the next player start and can't access the MediaCodec since it's still locked by previous unreleased one. (That I'll release in background thread). Is there a way to detect that on next exoplayer creation? Or maybe a specific error when the track will try to play? (Don't know when they are initialized)
For more background, my app is not primarily a player, it's more a global controller, that connect and control things. At some point user might be playing audio from multiple source to local device and that's the part where ExoPlayer kicks in. User can then stop playing locally and start playing on Chromecast or anything else. Having the app hang and ANR because of ExoPlayer that is no more used and will probably won't until next app start, and have the user loose it's music and queue running on the other player is not something cool if it can be avoided.
Facing the same issue exoplayer version 2.9.1 This is the top cause of ANRs in my app
Affected android versions 8.0,8.1,9.0 Affected devices from crash clusters of play console.
| One (A0001) | 5 | 29.4% |
|---|---|---|
| Nokia 6.1 Plus (DRG_sprout) | 3 | 17.6% |
| OnePlus3T (OnePlus3T) | 2 | 11.8% |
| V30 (joan) | 2 | 11.8% |
| Others | 5 | 29.4% |
| DROID Turbo 2 (kinzie) | 4 | 13.8% |
|---|---|---|
| Moto E4 (perry_f) | 3 | 10.3% |
| Galaxy S3 (m0) | 3 | 10.3% |
| Moto G(4) Plus (athene_f) | 3 | 10.3% |
| Others | 16 | 55.2% |
| MIX 2S (polaris) | 8 | 20.5% |
|---|---|---|
| Nokia 7 plus (B2N_sprout) | 6 | 15.4% |
| OnePlus5T (OnePlus5T) | 3 | 7.7% |
| Moto Z (2) Play (albus) | 3 | 7.7% |
| Others | 19 | 48.7% |
stack trace
"main" prio=5 tid=1 Waiting
| group="main" sCount=1 dsCount=0 flags=1 obj=0x73446330 self=0x7d3a8c2a00
| sysTid=30940 nice=-10 cgrp=default sched=0/0 handle=0x7dbf7b29a8
| state=S schedstat=( 13621696685 2308186531 21699 ) utm=1102 stm=260 core=4 HZ=100
| stack=0x7ff9c6a000-0x7ff9c6c000 stackSize=8MB
| held mutexes=
at java.lang.Object.wait (Object.java)
- waiting on <0x0cf8f9d3> (a com.google.android.exoplayer2.ExoPlayerImplInternal)
at com.google.android.exoplayer2.ExoPlayerImplInternal.release (ExoPlayerImplInternal.java:229)
- locked <0x0cf8f9d3> (a com.google.android.exoplayer2.ExoPlayerImplInternal)
at com.google.android.exoplayer2.ExoPlayerImpl.release (ExoPlayerImpl.java:393)
at com.google.android.exoplayer2.SimpleExoPlayer.release (SimpleExoPlayer.java:979)
I can reproduce it quite regularly with the attached sample after a few "play + exit the player" action on a Nexus 6 - API 7.1.1. The sample recording shows no problems in other devices and Android versions.
About the attached sample, it isn't a regular video. Don't expect any image or audio. The video content is dark and is just for testing purposes. It has been recorded using the Android MediaRecorder with a time-lapse Camcorder profile, setting the recording rate at 30fps and a capture rate at 90fps.