fix: impossible to configure task if previous task has call stop(). (Android + Headless)#17
Conversation
This commit ensures tasks are properly removed from the configuration map when they are cancelled. Previously, some tasks were still lingering in the map, leading to potential inconsistencies. Now, the tasks are consistently
|
Have you actually tested this? |
|
TBH I tested by hand only 2 scenarios:
Here is a video where we see these 2 scenarios seems to works bg-fetch.mp4I imported the module via android studio and the I didn't test using the |
|
@christocracy Have you had a chance to review this PR? It's not urgent since we're currently using a patch, but if there are any major issues with my fix or if I overlooked something that makes the pull request unnecessary, I'd appreciate your feedback. |
|
Hello @christocracy, could you please take a moment to review this pull request ? Maybe I'm missing something and all this is not necessary, but if that is the case I'd like to know what I missed and we can close it. I've described the issue and investigate it as much as I could and done a minimal reproduction example to make it easy to try. |
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 999adb shell cmd jobscheduler run -f com.rnbgfetchbugrepro 999[BGTask] failed to load config for taskId: react-native-background-fetchVideo 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-fetchSuggestion
We should probably clear the mConfig object when we stop the backgroundTask, so that there is no "desynchronisation" between the sharedPreference