react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

ForegroundServiceDidNotStartInTimeException crashes

Open suresh-mojro opened this issue 4 months ago • 2 comments

Summary We are seeing recurring, fatal ForegroundServiceDidNotStartInTimeException crashes originating inside the plugin's native service-start path (FgsLaunchGate → AbstractService.startForegroundService() → TrackingService/LocationRequestService). Alongside the crash reports, we captured an ANR trace that pinpoints one specific blocking call: TSLocalNotification.build() calling PendingIntent.getActivity(), which blocks on a binder IPC to system_server (IActivityManager.getIntentSenderWithFeature) — synchronously, on the main thread, inside onStartCommand(), in the same code path that must complete before Android's 5-second foreground-service promotion deadline expires. We're also seeing a related but structurally different pattern (Observation 3 below) on older/OEM-throttled devices, where the same timeout occurs with no thread contention at all — suggesting a second, independent contributing mechanism worth being aware of alongside the binder-blocking one. Environment

  • Plugin: react-native-background-geolocation (Android) — 5.3.0
  • React Native version: 0.79.6
  • targetSdkVersion: 35
  • compileSdkVersion: 35
  • Affected Android versions: 8.1, 13, 15, 16
  • Device manufacturers observed: Oppo, Realme, OnePlus, Moto, Vivo, CPH-series devices
  • App config relevant to this plugin: { desiredOdometerAccuracy: 30, speedJumpFilter: 50, logger: { debug: false, logLevel: BackgroundGeolocation.LogLevel.Verbose, }, geolocation: { desiredAccuracy: BackgroundGeolocation.DesiredAccuracy.Navigation, elasticityMultiplier: 1.5, distanceFilter: 7, allowIdenticalLocations: false, disableStopDetection: false, pausesLocationUpdatesAutomatically: false, stopTimeout: 3, stopOnStationary: false, locationAuthorizationRequest: "Always", geofenceModeHighAccuracy: true, }, activity: { disableStopDetection: false, motionTriggerDelay: 2000, }, app: { heartbeatInterval: 60, enableHeadless: true, stopOnTerminate: false, startOnBoot: true, preventSuspend: false, notification: { title: "Mojro Partner", text: "Location tracking active...", priority: BackgroundGeolocation.NotificationPriority.High, }, }, http: { autoSync: true, autoSyncThreshold: 2, batchSync: true, maxBatchSize: 5, rootProperty: ".", }, persistence: { maxDaysToPersist: 3, maxRecordsToPersist: 3000, },}

Crash Trace 1 — LocationRequestService (via FgsLaunchGate, broadcast-triggered warm launch) Fatal Exception: android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{... com.mojro.partner/com.transistorsoft.locationmanager.service.LocationRequestService} android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException ... Caused by android.app.StackTrace Last startServiceCommon() call for this service was made here android.content.ContextWrapper.startForegroundService com.transistorsoft.locationmanager.service.FgsLaunchGate.startService com.transistorsoft.locationmanager.service.FgsLaunchGate.handleWarmLaunch com.transistorsoft.locationmanager.service.FgsLaunchGate.onReceive android.app.ActivityThread.handleReceiver

Crash Trace 2

Fatal Exception: android.app.RemoteServiceException Context.startForegroundService() did not then call Service.startForeground()

android.app.ActivityThread$H.handleMessage (ActivityThread.java:1964)

android.os.Handler.dispatchMessage (Handler.java:106)

android.os.Looper.loop (Looper.java:214)

android.app.ActivityThread.main (ActivityThread.java:6977)

java.lang.reflect.Method.invoke (Method.java)

com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:528)

com.android.internal.os.ZygoteInit.main (ZygoteInit.java:910)

ANR Trace — pinpoints the actual blocking call for Crash Traces 1–2 Captured for TrackingService.onStartCommand(), on the same FgsLaunchGate.handleWarmLaunch code path as Crash Trace 2 above. This is the "root blocking" thread at the moment of the ANR: main (native) tid=1 Triggered ANR Root blocking 0 libc.so syscall + 32 1 libart.so art::ConditionVariable::WaitHoldingLocks + 140 2 libart.so artQuickGenericJniEndTrampoline + 876 3 libart.so art_quick_generic_jni_trampoline + 160 4 [jit-zygote-cache] android.os.BinderProxy.transact + 2796 5 [jit-zygote-cache] android.app.IActivityManager$Stub$Proxy.getIntentSenderWithFeature + 976 6 [jit-zygote-cache] android.app.PendingIntent.getActivityAsUser + 532 7 [jit-zygote-cache] android.app.PendingIntent.getActivity + 148

android.os.BinderProxy.transactNative (Native method) "This Binder call may be taking too long, causing the main thread to wait, and triggering the ANR." android.os.BinderProxy.transact (BinderProxy.java:635) android.app.IActivityManager$Stub$Proxy.getIntentSenderWithFeature (IActivityManager.java:6913) android.app.PendingIntent.getActivityAsUser (PendingIntent.java:466) android.app.PendingIntent.getActivity (PendingIntent.java:447) android.app.PendingIntent.getActivity (PendingIntent.java:411) com.transistorsoft.locationmanager.notification.TSLocalNotification.build (SourceFile:31) com.transistorsoft.locationmanager.service.ForegroundNotification.build (SourceFile:1) com.transistorsoft.locationmanager.service.AbstractService.c (SourceFile:22) com.transistorsoft.locationmanager.service.AbstractService.a (SourceFile:38) com.transistorsoft.locationmanager.service.TrackingService.onStartCommand (SourceFile:1) android.app.ActivityThread.handleServiceArgs (ActivityThread.java:4807) This is, as far as we can tell, the mechanism behind Crash Traces 1–2 above: TSLocalNotification.build() synchronously constructs a PendingIntent via PendingIntent.getActivity(), which requires a binder round-trip to ActivityManagerService (getIntentSenderWithFeature). This call sits directly inside onStartCommand(), on the same path that must complete and call Service.startForeground() before Android's ~5-second deadline. If system_server is even briefly busy — plausible immediately after a broadcast-triggered process wake, exactly the trigger visible in FgsLaunchGate.onReceive in both traces above — this synchronous, un-timed-out binder call is enough on its own to blow the deadline.

Questions for Transistorsoft

  1. Does TSLocalNotification/ForegroundNotification cache and reuse a PendingIntent/Notification object across repeated service starts, or is a fresh one (with a fresh binder round-trip) constructed on every single onStartCommand()? If it's rebuilt every time, this looks like an avoidable synchronous IPC sitting directly inside the FGS-start deadline window, and caching it seems like it would meaningfully reduce exposure to this crash.
  2. Is there a known issue or fix — in this version or a newer release — addressing binder-call latency inside the FGS-start path, particularly for FgsLaunchGate's warm-launch flow?
  3. Are you aware of any additional mitigation for the OEM-throttling case described in Observation 3 (i.e., cases where the process isn't scheduled in time at all, independent of any blocking call inside the plugin's own code)?

What we've already ruled out on our side

  • Not caused by JS-thread/bridge contention — Crash Traces 1–2 originate entirely in native code (BroadcastReceiver.onReceive), with no JS/bridge frames in the call chain.
  • Not caused by anything in our own app initialization code — we tested with and without an app-side native pre-warming mitigation, and this crash recurred in both cases at a similar rate.
  • We previously also saw this same exception triggered from our own JS calling stopWatchPosition() in rapid succession (via TSWatchPositionRequest/AbstractService.stop()); adding backoff/debounce on our side around repeated restarts resolved that variant. Crash Traces 1–2 (and Observation 3) are unrelated to that path — they originate from native broadcast receivers/OS scheduling with no JS involvement, which is why we're raising them here.

Attachments available on request

  • Full (untruncated) stack traces for all crash instances above, including the complete thread dump for Observation 3.

com.mojro.partner_issue_0055b5565d49825afb5c19d78ab58fd4_crash_session_6A719078023800016CEED1955DA0BC63_DNE_0_v2_stacktrace.txt com.mojro.partner_issue_995d2990e0b45c803706b37798be7481_crash_session_6A71947802ED00011661CF5AC7982AE6_DNE_0_v2_stacktrace.txt com.mojro.partner_issue_a3e17f2db8a78a3ff1ebc1da78db0b4d_ANR_session_6A710726020C0001576EF4568624AED0_DNE_0_v2_stacktrace.txt

suresh-mojro avatar Aug 04 '26 07:08 suresh-mojro

Plugin: react-native-background-geolocation (Android) — 5.3.0

Upgrade to 5.4.0

christocracy avatar Aug 04 '26 11:08 christocracy

this crash is no happening on 5.4.0. thank you

suresh-mojro avatar Aug 24 '26 05:08 suresh-mojro