fix: impossible to configure task if previous task has call stop(). (Android + Headless) #17
+4
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I noticed that in my app:
IF
configure()
a background-taskTHEN
Reproducing the bug
I created a simple react-native app with minimal code to reproduce the bug
https://github.com/ACHP/rn-background-fetch-bug-repro
Run the app using react-native (checkout + yarn + yarn start + yarn android)
adb shell cmd jobscheduler run -f com.rnbgfetchbugrepro 999
adb shell cmd jobscheduler run -f com.rnbgfetchbugrepro 999
[BGTask] failed to load config for taskId: react-native-background-fetch
Video of this process
rnbgfetch-repro.mp4
What is happening
From my research, I noticed that the second time we register the background task, it is not properly saved into the shared-preferences (We can see it in the video).
This happens because on this line, the mConfig hashmap still contains the taskId when we relaunch the app the second time.
transistor-background-fetch/android/tsbackgroundfetch/src/main/java/com/transistorsoft/tsbackgroundfetch/BackgroundFetch.java
Line 88 in 7ad81a8
And the hashmap still contains the taskId because when we call
stop()
, it clean the shared-preferences, but not the mConfig hashmap.transistor-background-fetch/android/tsbackgroundfetch/src/main/java/com/transistorsoft/tsbackgroundfetch/BackgroundFetch.java
Lines 146 to 170 in 7ad81a8
That is why we see a
Re-configured existing task
, instead of a fresh "configure".Then when we try to run the background task, it loads the config from the shared-preference, but it can't find nothing. So it fails with
[BGTask] failed to load config for taskId: react-native-background-fetch
Suggestion
We should probably clear the mConfig object when we stop the backgroundTask, so that there is no "desynchronisation" between the sharedPreference