doublesymmetry / react-native-track-player

A fully fledged audio module created for music apps. Provides audio playback, external media controls, background mode and more!
https://rntp.dev/
Apache License 2.0
3.25k stars 1.01k forks source link

android.app.ForegroundServiceDidNotStartInTimeException - Context.startForegroundService() did not then call Service.startForeground() #2155

Closed harshil-gosquire closed 11 months ago

harshil-gosquire commented 12 months ago

Describe the Bug We have app live in playstore with crashlytics implemented and we are keep getting this crash on crashlytics with live users

ActivityThread.throwRemoteServiceException: android.app.ForegroundServiceDidNotStartInTimeException - Context.startForegroundService() did not then call Service.startForeground(

Full log below:

Fatal Exception: android.app.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{41247aa u0 com.ourapppackagename/com.doublesymmetry.trackplayer.service.MusicService}
       at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2134)
       at android.app.ActivityThread.access$2900(ActivityThread.java:309)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2363)
       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:8582)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)

Other crashlytics stats for better understanding

Observed to be happening only on samsung devices + 50% of user are facing when app is in background state

crash

Steps To Reproduce We are not able to reproduce at our end but live users are facing it

Code To Reproduce We are not able to reproduce at our end but live users are facing it

Replicable on Example App? We are not able to reproduce at our end but live users are facing it

Environment Info:

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M1 Pro
    Memory: 202.58 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.16.0/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.12.0 - /opt/homebrew/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 23.0, iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0
    Android SDK: Not Found
  IDEs:
    Android Studio: 2022.1 AI-221.6008.13.2211.9477386
    Xcode: 15.0/15A240d - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.17 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2 
    react-native: 0.67.4 => 0.67.4 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

"react-native-track-player": "^3.2.0",

Real device? Or simulator?

What OS are you running?

How I can Help What can you do to help resolve this? We have tried solutions from existing similar issues on here github but still crash log pops up with same error, Please let us know what we can do to resolve this crash.

arthurgeron-work commented 12 months ago

I'm getting a similar error related to startForegroundService, here's my stacktrace:

Exception java.lang.RuntimeException:
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5261)
  at android.app.ActivityThread.-$$Nest$mhandleServiceArgs
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2447)
  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:8757)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1067)
Caused by android.app.ForegroundServiceStartNotAllowedException:
  at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)
  at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)
  at android.os.Parcel.readParcelableInternal (Parcel.java:4787)
  at android.os.Parcel.readParcelable (Parcel.java:4755)
  at android.os.Parcel.createExceptionOrNull (Parcel.java:3018)
  at android.os.Parcel.createException (Parcel.java:3007)
  at android.os.Parcel.readException (Parcel.java:2990)
  at android.os.Parcel.readException (Parcel.java:2932)
  at android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:6978)
  at android.app.Service.startForeground (Service.java:743)
  at com.doublesymmetry.trackplayer.service.MusicService.onStartCommand (MusicService.kt:69)
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5243)
jorgerojas26 commented 12 months ago

Exact same problem here. Can't reproduce by our side but i can see in Datadog that our users are facing this issue.

Package version: "react-native-track-player": "3.2.0"

Package initialization:

image

Player setup in App.jsx:

image

Here is the stack strace:

android.app.RemoteServiceException.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{2d8b624 u0 com.companyname.tila/com.doublesymmetry.trackplayer.service.MusicService} android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{2d8b624 u0 com.companyname.tila/com.doublesymmetry.trackplayer.service.MusicService} at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2245) at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2216) at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2508) 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:8762) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) Caused by: android.app.StackTrace: Last startServiceCommon() call for this service was made here at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1988) at android.app.ContextImpl.startForegroundService(ContextImpl.java:1933) at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:839) at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:839) at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:1091) at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:749) at androidx.media.session.MediaButtonReceiver.onReceive(MediaButtonReceiver.java:115) at android.app.ActivityThread.handleReceiver(ActivityThread.java:4894) at android.app.ActivityThread.-$$Nest$mhandleReceiver(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2420) ... 7 more

In Datadog, none of the reported errors are being referenced as a RUM session, so i think this is only happening when the app is in background state.

takacskalman commented 12 months ago

Maybe it's related to this: https://github.com/doublesymmetry/react-native-track-player/issues/2159 We get the same error in background on Android 12+ and the app crashes. Check out the issue linked, we have the reproduction steps listed

dcvz commented 11 months ago

It seems like mostly everyone here is on version 3.2.0 - a lot of changes happened in v4 to try to mitigate this class of issues. Please first try on those RC's - full release coming soon, then raise an issue if still present 🙏

harshil-gosquire commented 11 months ago

@dcvz https://github.com/doublesymmetry/react-native-track-player/issues/2159#issuecomment-1784227897