transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.64k stars 425 forks source link

Context.startForegroundService() did not then call Service.startForeground() #1243

Closed tankers746 closed 3 years ago

tankers746 commented 3 years ago

My exception logging service and google play are reporting a large number of crashes after implementing this library. I've not been able to reproduce the issue myself but it has greatly affected the crash rate in the Google Play console.

Your Environment

I have enabled strict mode after seeing the issue below but I couldn't see any violations. https://github.com/transistorsoft/react-native-background-geolocation/issues/976

I enabled detectAll() in strict mode which produced the violation below however after some googling that appears to be a common error.

Debug logs

Logs ``` 2021-02-15 20:02:34.414 23875-24238/com.fleeteng.spatulago D/StrictMode: StrictMode policy violation: android.os.strictmode.UntaggedSocketViolation: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2124) at com.android.server.NetworkManagementSocketTagger.tag(NetworkManagementSocketTagger.java:82) at libcore.io.BlockGuardOs.tagSocket(BlockGuardOs.java:52) at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:372) at libcore.io.ForwardingOs.socket(ForwardingOs.java:217) at libcore.io.IoBridge.socket(IoBridge.java:658) at java.net.PlainSocketImpl.socketCreate(PlainSocketImpl.java:128) at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:128) at java.net.Socket.createImpl(Socket.java:489) at java.net.Socket.getImpl(Socket.java:552) at java.net.Socket.setSoTimeout(Socket.java:1180) at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:245) at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:167) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:258) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:201) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:919) ```
christocracy commented 3 years ago

This issue is well-known among Android developers and there are plenty of other existing issues reported here (search the issues).

This is not directly related to this plugin. Something else in your app is blocking the main thread, preventing background-geolocation from executing startForeground within 5 seconds android provides.

tankers746 commented 3 years ago

I couldn't see any solutions in the existing issues. Any tips on where to start?

christocracy commented 3 years ago

So you found all these?

There is no quick solution, but a process of discovering which of your plugins are consuming too much time on the main-thread while your app boots by enabling Android StrictMode. Examples of "consuming too much time on the main thread" typically come from file or database operations performed on the main-thread.

christocracy commented 3 years ago

Duplicate #976

christocracy commented 3 years ago

I no longer experience these in the SampleApp on Play Store. I used to but since I took measures within the plugin's code back in version 3.3.0 to ensure file/database operations are performed in background threads, my problems went away.

So with the SampleApp running the background-geolocation plugin in the absence of other plugins, there are no ANR.

tankers746 commented 3 years ago

When is Context.startForegroundService() called? is it on app load or BackgroundGeolocation.start() or BackgroundGeolocation.ready()?

christocracy commented 3 years ago

The plugin has many foreground-services which are capable of launching a terminated app to service a request.

Eg, if you terminate your app then start physically moving, the plugin's ActivityRecognitionService will cause your app to re-launch headless in the background and that service will be wanting to call Context.startForegroundService.

The next step for you is to enable Android StrictMode and observe your logs for StrictMode warnings.

tankers746 commented 3 years ago

I see, I was able to detect some DiskReadViolation by moving the strict mode statement from MainActivity to MainApplication.

1. android.graphics.Typeface.setFlipFonts(Typeface.java:1568) ``` android.os.strictmode.DiskReadViolation at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1556) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:247) at libcore.io.ForwardingOs.open(ForwardingOs.java:167) at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7825) at libcore.io.IoBridge.open(IoBridge.java:482) at java.io.FileInputStream.(FileInputStream.java:159) at android.graphics.Typeface.getFullFlipFont(Typeface.java:1518) at android.graphics.Typeface.getFontPathFlipFont(Typeface.java:1479) at android.graphics.Typeface.setFlipFonts(Typeface.java:1568) at android.app.Activity.onCreate(Activity.java:1482) at androidx.core.app.ComponentActivity.onCreate(ComponentActivity.java:85) at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:154) at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:312) at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115) at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:44) at com.fleeteng.spatulago.MainActivity.onCreate(MainActivity.java:49) at android.app.Activity.performCreate(Activity.java:7955) at android.app.Activity.performCreate(Activity.java:7944) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3531) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7948) at java.lang.reflect.Method.invoke(Method.java:-2) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) ```
2. com.microsoft.codepush.react.CodePush.(CodePush.java:88) ``` android.os.strictmode.DiskReadViolation at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1556) at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:261) at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:287) at com.microsoft.codepush.react.SettingsManager.getPendingUpdate(SettingsManager.java:35) at com.microsoft.codepush.react.CodePush.initializeUpdateAfterRestart(CodePush.java:296) at com.microsoft.codepush.react.CodePush.(CodePush.java:88) at com.facebook.react.PackageList.getPackages(PackageList.java:105) at com.fleeteng.spatulago.MainApplication$1.getPackages(MainApplication.java:53) at com.facebook.react.ReactNativeHost.createReactInstanceManager(ReactNativeHost.java:77) at com.facebook.react.ReactNativeHost.getReactInstanceManager(ReactNativeHost.java:39) at com.fleeteng.spatulago.MainApplication.onCreate(MainApplication.java:101) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1190) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6993) at android.app.ActivityThread.access$1300(ActivityThread.java:270) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2051) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7948) at java.lang.reflect.Method.invoke(Method.java:-2) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) ```
3. com.microsoft.codepush.react.CodePush.getJSBundleFile(CodePush.java:246) ``` android.os.strictmode.DiskReadViolation at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1556) at libcore.io.BlockGuardOs.access(BlockGuardOs.java:69) at libcore.io.ForwardingOs.access(ForwardingOs.java:73) at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7816) at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:281) at java.io.File.exists(File.java:815) at com.microsoft.codepush.react.FileUtils.fileAtPathExists(FileUtils.java:90) at com.microsoft.codepush.react.CodePushUpdateManager.getCurrentPackageInfo(CodePushUpdateManager.java:54) at com.microsoft.codepush.react.CodePushUpdateManager.getCurrentPackageFolderPath(CodePushUpdateManager.java:76) at com.microsoft.codepush.react.CodePushUpdateManager.getCurrentPackageBundlePath(CodePushUpdateManager.java:86) at com.microsoft.codepush.react.CodePush.getJSBundleFileInternal(CodePush.java:255) at com.microsoft.codepush.react.CodePush.getJSBundleFile(CodePush.java:246) at com.microsoft.codepush.react.CodePush.getJSBundleFile(CodePush.java:238) at com.fleeteng.spatulago.MainApplication$1.getJSBundleFile(MainApplication.java:41) at com.facebook.react.ReactNativeHost.createReactInstanceManager(ReactNativeHost.java:81) at com.facebook.react.ReactNativeHost.getReactInstanceManager(ReactNativeHost.java:39) at com.fleeteng.spatulago.MainApplication.onCreate(MainApplication.java:101) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1190) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6993) at android.app.ActivityThread.access$1300(ActivityThread.java:270) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2051) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7948) at java.lang.reflect.Method.invoke(Method.java:-2) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) ```
4. com.facebook.react.modules.i18nmanager.I18nUtil.allowRTL(I18nUtil.java:61) ``` android.os.strictmode.DiskReadViolation at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1556) at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:261) at android.app.SharedPreferencesImpl.edit(SharedPreferencesImpl.java:354) at com.facebook.react.modules.i18nmanager.I18nUtil.setPref(I18nUtil.java:94) at com.facebook.react.modules.i18nmanager.I18nUtil.allowRTL(I18nUtil.java:61) at com.fleeteng.spatulago.MainApplication.onCreate(MainApplication.java:99) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1190) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6993) at android.app.ActivityThread.access$1300(ActivityThread.java:270) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2051) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7948) at java.lang.reflect.Method.invoke(Method.java:-2) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) ```
  1. Seems to be part of creating an activity and unavoidable.
  2. The two codepush ones are perhaps an issue, have you had anyone else face issues with codepush?
  3. This is a built in react native method, I'm not sure how I can enable RTL any other way.
angelo-hub commented 1 year ago

@christocracy We're having the same issue here, most of our strictMode violations take place from the same DiskReadViolation/DiskWriteViolation in codepush

christocracy commented 1 year ago

Perhaps this “code push” should be consulted about clearing up their StrictMode violations.