Singleton BackgroundServer causes cross-task contention when two independent features call start()/stop() concurrently
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- javascript, react-native
Research direction
Start with src/index.js, which the issue identifies as holding the singleton BackgroundServer and its shared state; trace how start() and stop() use _isRunning, _stopTask, and _currentOptions. Compare the keyed task tracking and single task documentation options before deciding the intended API. Done means independent callers can stop their own task safely, or the README clearly states the single task limit and caller coordination requirement.
Written by the indexing model from the issue text.
Description
Description
src/index.js exports a single module-level BackgroundServer instance (const backgroundServer = new BackgroundServer(); export default backgroundServer;). All state - _isRunning, _stopTask, _currentOptions - lives on that one instance, with no per-caller isolation and no reference counting.
In an app where two independent features each call BackgroundService.start() / BackgroundService.stop() (e.g. a long-running import task and an unrelated playback keep-alive task), the second start() silently overwrites _currentOptions and _stopTask from the first. Calling stop() from either caller then stops/resolves the other caller's task too - there's no way for one feature to stop its own task without risking it stop a task started by a completely different part of the app, and no way to know from the caller side whether the "current" task is actually the one you started.
Repro sketch
// featureA.js
await BackgroundService.start(taskA, optionsA);
// featureB.js, called while featureA's task is still running
await BackgroundService.start(taskB, optionsB); // overwrites _currentOptions/_stopTask
// featureA.js
await BackgroundService.stop(); // actually resolves/stops featureB's task, not A's
Impact
We hit this in production: one module used BackgroundService for a dataSync keep-alive task and a separate module used it for dataSync import progress. Because both go through the same singleton, stop() calls from one could tear down the other's foreground-service notification/state unexpectedly. We worked around it by removing one of the two callers so only a single feature in the app touches BackgroundService, but that's not viable for apps that genuinely need more than one independent background task tracked at once.
Suggested fix
Either:
- Track running tasks in a map keyed by
taskName(or a caller-supplied id) instead of single_currentOptions/_stopTask/_isRunningfields, sostop()can target a specific task; or - Document explicitly that only one
BackgroundServicetask may be "owned" by the app at a time, and that callers must coordinate start/stop externally (a note in the README's current single-task examples doesn't call this out).
Happy to open a PR for the ref-counted/keyed approach if that's the preferred direction - wanted to confirm intended design first since it's a bigger API surface change.
Environment
- react-native-background-actions: (as vendored in
node_modules,src/index.js) - Platform: Android
- Dominant language
- Java
- Stars
- 941
- Forks
- 145
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 3
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rapsssito/react-native-background-actions
-
iOS CocoaPods build fails: Package.swift included in sources causes No such module 'PackageDescription'Possibly taken @dattm97 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 4/5 3-5 days Newbie friendliness 18/100
Rapsssito/react-native-background-actions#255 · 4 comments ·
-
enhancement help wanted
Difficulty 5/5 Over a week Newbie friendliness 20/100
Rapsssito/react-native-background-actions#249 · 16 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Rapsssito/react-native-background-actions#246 · 5 comments · 2 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Rapsssito/react-native-background-actions#238 · 5 comments ·
All issues in Rapsssito/react-native-background-actions
Similar issues
-
bug verbose GC
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
IBM/javacore-analyser#677 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
liberation-data/fornax4j#302 ·
Maintainers usually reply within 1 day
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 61/100
grimmory-tools/grimmory#3297 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
SmartPack/PackageManager#186 ·
-
Area: Java feature request needs triage OS: Windows
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
actions/runner-images#15457 ·
Maintainers usually reply within 3 days