react-native-background-geolocation: Android: java.lang.RuntimeException Could not invoke HeadlessJsTaskSupport.notifyTaskFinished
Required Reading
- Confirmed
Plugin Version
4.18.3
Mobile operating-system(s)
- iOS
- Android
Device Manufacturer(s) and Model(s)
Google Pixel 6, Samsung, Moto G Power
Device operating-systems(s)
Android 12, 13, 14, 15
React Native / Expo version
0.72.17
What happened?
After upgrading our package from 4.17.6 to 4.18.3, we started seeing the following error in our error tracking tools:
java.lang.RuntimeException: Could not invoke HeadlessJsTaskSupport.notifyTaskFinished
java.lang.AssertionError: Tried to finish non-existent task with id 133.
assertCondition(Assertions.java:88)
finishTask(HeadlessJsTaskContext.java:179)
notifyTaskFinished(HeadlessJsTaskSupportModule.java:53)
Our headless task registration (just for completeness):
BackgroundGeolocation.registerHeadlessTask(
throttle(async (event: HeadlessTaskEvent) => {
if (event.name === BackgroundGeolocation.EVENT_LOCATION) {
// Persist location to DB and save the last position
}
}, FIFTEEN_SECONDS),
)
It looks like a significant refactor was made to headless task management between 4.18.0 and 4.18.1, but we’re unsure what specifically is causing the crash on affected devices.
We haven’t been able to reproduce this issue locally, but we’ve observed around 100 occurrences in production over the past 15 days. Any insights or guidance on potential causes would be greatly appreciated.
Plugin Code and/or Config
const geolocationSettings: Config = {
desiredAccuracy:
Platform.OS === 'ios'
? BackgroundGeolocation.DESIRED_ACCURACY_NAVIGATION
: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: DISTANCE_METERS,
disableElasticity: true,
enableHeadless: true,
fastestLocationUpdateInterval: INTERVAL_MILLISECONDS,
foregroundService: true,
notification: {
title: 'App',
text: 'Tracking your location',
smallIcon: 'ic_notification',
},
allowIdenticalLocations: true,
disableStopDetection: false,
stopOnTerminate: false,
startOnBoot: true,
maxRecordsToPersist: 1,
disableMotionActivityUpdates: Platform.OS !== 'ios',
reset: true,
locationAuthorizationRequest: 'Always',
stopTimeout: STOP_TIMEOUT,
batchSync: false,
autoSync: true,
url: '',
httpRootProperty: '',
locationTemplate: '',
headers: {},
}
Relevant log output
java.lang.RuntimeException: Could not invoke HeadlessJsTaskSupport.notifyTaskFinished
handleUncaughtException(MainApplication.java:87)
uncaughtException(MainApplication.java:75)
uncaughtException(BackgroundGeolocation.java:98)
uncaughtException(ar.java:9)
uncaughtException(ThreadGroup.java:1071)
uncaughtException(ThreadGroup.java:1066)
dispatchUncaughtException(Thread.java:2306)
java.lang.reflect.InvocationTargetException: null
invoke(Method.java:-2)
invoke(JavaMethodWrapper.java:372)
invoke(JavaModuleWrapper.java:188)
run(NativeRunnable.java:-2)
handleCallback(Handler.java:958)
dispatchMessage(Handler.java:99)
dispatchMessage(MessageQueueThreadHandler.java:27)
loopOnce(Looper.java:230)
loop(Looper.java:319)
run(MessageQueueThreadImpl.java:228)
run(Thread.java:1012)
java.lang.AssertionError: Tried to finish non-existent task with id 133.
assertCondition(Assertions.java:88)
finishTask(HeadlessJsTaskContext.java:179)
notifyTaskFinished(HeadlessJsTaskSupportModule.java:53)
invoke(Method.java:-2)
invoke(JavaMethodWrapper.java:372)
invoke(JavaModuleWrapper.java:188)
run(NativeRunnable.java:-2)
handleCallback(Handler.java:958)
dispatchMessage(Handler.java:99)
dispatchMessage(MessageQueueThreadHandler.java:27)
loopOnce(Looper.java:230)
loop(Looper.java:319)
run(MessageQueueThreadImpl.java:228)
run(Thread.java:1012)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (11 by maintainers)
(note that I found this by just doing this search, and following the trail to the commit then to see the release tag on the commit - it took me about a minute --> https://github.com/facebook/react-native/issues?q=Tried to finish non-existent task with id – well worth the effort)
You may be able to build rn from source to get that patch in there, I’ve had success that way before. Arduous but doable. As to the responses you’re not wrong, but it is kind of shocking the lack of effort people put in when asking for help. Occasionally my frustration seeps out. Fir the record Cris is the one and only maintainer here, I just drop occasionally annoying comments now and again when I see things related or that might affect my projects as well. Cheers
We are also seeing this issue, though I don’t believe it is tied to React Native Background Geolocation as we are currently on version 4.16.3.
We also will be staying on RN 0.74.5 until react-native-maps supports the new architecture.
Our attempt to solve this is by using patch-package in combination with the commit @mikehardy found.
Since there is no clear recreation steps, we will have to cross our fingers and wait to see the crash log reports once this goes live.
Hope this helps anyone else who stumbles across this issue.
Hey evereyone 👋 there is a lesson here. What is the lesson? Logging issues against old versions of software is an unproductive use of time when contrasted with updating your versions then attempting to reproduce then spending time tracking the issue.
https://github.com/facebook/react-native/commit/b4532adad4e38e1237631778f7b8c917a9ccf746
react-native 0.77 is out and contains the fix for this
it’s upstream, not related to this repository