react-native-background-geolocation: Fatal Exception: android.app.RemoteServiceException Context.startForegroundService() did not then call Service.startForeground()

Your Environment

  • Plugin version: 3.4.2
  • Platform: Android
  • OS version: 8, 9
  • Device manufacturer / model: Samsung mostly and others
  • React Native version (react-native -v): 0.61.2
  • Plugin config
            {
                locationAuthorizationRequest: "Any",
                disableLocationAuthorizationAlert: true,
                desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_LOW,
                distanceFilter: 5,            // Must be 0 or locationUpdateInterval is ignored!
                locationUpdateInterval: 1000,  // Get a location every 5 seconds
                disableElasticity: true,
                desiredOdometerAccuracy: 50,
                fastestLocationUpdateInterval: 5000,
                locationAuthorizationAlert: {
                    titleWhenNotEnabled: "Location service is not enabled",
                    titleWhenOff: "Location service is OFF",
                    instructions: "You must enable 'Always' in location-services",
                    cancelButton: "Cancel",
                    settingsButton: "Settings"
                },
                minimumActivityRecognitionConfidence: 15,
                // Activity Recognition
                stopTimeout: 1,
                // Application config
                debug: false, 
                logLevel: BackgroundGeolocation.LOG_LEVEL_OFF,
                logMaxDays: 0,
                stopOnTerminate: false,
                startOnBoot: true,        
                foregroundService: true,

                url: 'http://yourserver.com/locations',
                batchSync: false,  
                autoSync: false,    
                headers: { "X-FOO": "bar" },
                params: { "auth_token": "maybe_your_server_authenticates_via_token_YES?" },
                enableHeadless: true,
            }

Expected Behavior

Not crash

Actual Behavior

Sometimes the App crash

Following exceptions thrown:

#0. Crashed: main at android.app.ActivityThread$H.handleMessage + 1855(ActivityThread.java:1855) at android.os.Handler.dispatchMessage + 106(Handler.java:106) at android.os.Looper.loop + 214(Looper.java:214) at android.app.ActivityThread.main + 6986(ActivityThread.java:6986) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 494(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main + 1445(ZygoteInit.java:1445)

–

Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{f637c22 u0 com.myapp/com.transistorsoft.locationmanager.service.TrackingService} at android.app.ActivityThread$H.handleMessage + 1855(ActivityThread.java:1855) at android.os.Handler.dispatchMessage + 106(Handler.java:106) at android.os.Looper.loop + 214(Looper.java:214) at android.app.ActivityThread.main + 6986(ActivityThread.java:6986) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 494(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main + 1445(ZygoteInit.java:1445)

#0. Crashed: main at android.app.ActivityThread$H.handleMessage + 1855(ActivityThread.java:1855) at android.os.Handler.dispatchMessage + 106(Handler.java:106) at android.os.Looper.loop + 214(Looper.java:214) at android.app.ActivityThread.main + 6986(ActivityThread.java:6986) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 494(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main + 1445(ZygoteInit.java:1445)

#1. OkHttp Dispatcher at java.lang.Object.wait(Object.java) at java.lang.Thread.parkFor$ + 2137(Thread.java:2137) at sun.misc.Unsafe.park + 358(Unsafe.java:358) at java.util.concurrent.locks.LockSupport.parkNanos + 230(LockSupport.java:230) at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill + 461(SynchronousQueue.java:461) at java.util.concurrent.SynchronousQueue$TransferStack.transfer + 362(SynchronousQueue.java:362) at java.util.concurrent.SynchronousQueue.poll + 937(SynchronousQueue.java:937) at java.util.concurrent.ThreadPoolExecutor.getTask + 1091(ThreadPoolExecutor.java:1091) at java.util.concurrent.ThreadPoolExecutor.runWorker + 1152(ThreadPoolExecutor.java:1152) at java.util.concurrent.ThreadPoolExecutor$Worker.run + 641(ThreadPoolExecutor.java:641) at java.lang.Thread.run + 764(Thread.java:764)

Steps to Reproduce

No steps to reproduce, the crash recorded by Google Play Vital and Firebase Crashlytics on remote phone devices

Context

Capture user’s location via geolocation plugin

Debug logs

Logs

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

This is generally caused by your app taking too long to boot, where too many tasks are blocking the UI Thread, preventing the plugin’s foreground-services from executing startForeground in time, causing Android to induce this error upon your app.

You gain insight into which tasks are taking too long by enabling Android StrictMode in your Application.java.

See the SampleApp for an example of enabling StrictMode.

We observe the same crashes on com.transistorsoft.locationmanager.service.GeofencingService, com.transistorsoft.locationmanager.service.ActivityRecognitionService, com.transistorsoft.locationmanager.service.LocationRequestService. Plugin version 3.4.2 too.

@macielrsf have you solved, I’m facing similar issue

do you think our config is optimal for minimizing disk workload ?

Yes, you’ve configured maxRecordsToPersist: 0

Hello @christocracy , we still see big amount of crashes caused by this so I researched this little bit deeper… It looks like e.g. registering the service after phone is rebooted can easily cause these crashes as the phone is overloaded with all the starting apps etc. I also found a suggestion to use JobScheduler and JobService instead of foreground services to solve tasks like this (https://stackoverflow.com/a/56534432/77160) and prevent the crash as there’s a timeout of just 5 seconds for the app to react which often can be a problem on lowend devices or after the restart - it’s just out of control of the developer.

What’s your opinion on this, please - would it make sense to switch to JobScheduler and JobService? This is unfortunately most frequent crash in our app so for us it’s quite a pain - we are over 1% crash rate just because of this issue. image