Singleton BackgroundServer causes cross-task contention when two independent features call start()/stop() concurrently

Open
#272 0 comments 0 reactions 0 assignees View on GitHub

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
Domain
backend, mobile

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/_isRunning fields, so stop() can target a specific task; or
  • Document explicitly that only one BackgroundService task 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rapsssito/react-native-background-actions

All issues in Rapsssito/react-native-background-actions

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.