Crash because of the foreground service did not start in time

See original GitHub issue

Media3 Version

1.0.0-beta01

Devices that reproduce the issue

Google Pixel 6, Android 13 Samsung Galaxy A32 5G, Android 12 Samsung Galaxy A13 5G, Android 12 Xiaomi POCO X3 Pro, Android 12 Samsung Galaxy A12, Android 11 Samsung Galaxy A10s, Android 11

Devices that do not reproduce the issue

Can’t reproduce manually, got those stats from Crashlytics. Not sure how we can deal with this crash, do you have any idea why this crash may happen?

Reproducible in the demo app?

Not tested

Reproduction steps

We extended our playback service by your MediaSessionService, the crash happened approximately 70% in the background (stats in Crashlytics), we can’t manage to reproduce it in any way.

Expected result

Actual result

There are several types of exception grouped by the Android OS, but the reason looks exactly the same

Android 11

Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{bc3065 u0 com.bandlab.bandlab/com.bandlab.media.player.notification.MediaPlaybackService}
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2269)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:246)
       at android.app.ActivityThread.main(ActivityThread.java:8653)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Android 12

Fatal Exception: android.app.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{782cd47 u0 com.bandlab.bandlab/com.bandlab.media.player.notification.MediaPlaybackService}
       at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2147)
       at android.app.ActivityThread.access$2800(ActivityThread.java:310)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2376)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8669)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

Android 13

Fatal Exception: android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{2c98e95 u0 com.bandlab.bandlab/com.bandlab.media.player.notification.MediaPlaybackService}
       at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2013)
       at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:1984)
       at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2249)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:201)
       at android.os.Looper.loop(Looper.java:288)
       at android.app.ActivityThread.main(ActivityThread.java:7929)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Media

Nothing particular, just normal audio streaming

Bug Report

  • You will email the zip file produced by adb bugreport to dev.exoplayer@gmail.com after filing this issue.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
kvn-stglcommented, Jul 19, 2022

I have the same issue in my app (at least I can see it in crashlytics). But in my case, the issue is a bit stranger: It only happens at Samsung Devices and only with Android 12. The other things are identical (we can not reproduce it manually and it happens around 70% in the background). Also, the crashlog is (almost) identical (Android 12):

Fatal Exception: android.app.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{4e97eb7 u0 ***}
       at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2147)
       at android.app.ActivityThread.access$2900(ActivityThread.java:310)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2376)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8669)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
2reactions
christostscommented, Aug 2, 2022

I spotted a bug that occurs after the player has finished playback. The notification still shows a pause button (which does nothing) and the MediaSessionService remains in the foreground.

I don’t think this is the root cause for the exceptions you are seeing reported, but I’m going to link the commit to this issue anyway since I spotted it while investigating this issue. Thank you for reports and for helping us.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Context.startForegroundService() did not then call Service ...
I have an app that has 30k active users on Play Store. And I have the same issue: ~30 daily crashes on Android...
Read more >
The “misbehaving” foreground service in Android
The solution​​ We need to synchronise with the main thread and start the service only if main becomes free again. So, the only...
Read more >
Context.startForegroundService() Did Not Then Call Service ...
The reason for this crash is “From Android 9 Pie if your service does not call startForeground within 5 seconds after it has…...
Read more >
Context.startForegroundService() did not then call Service ...
When I start the service on 8.0 I see *BOTH* of the above messages in the log but then 5 seconds later, I...
Read more >
Crashes - Android Developers
An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found