Baseflow / Baseflow/flutter-geolocator

[Bug]: Possible race condition when restarting position updates after timeout

Open
#1,621 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Dart
Stars
1.3k
Forks
803
Avg merge
8h 16m
Merged PRs (30d)
1

Description

### Please check the following before submitting a new issue.

- [X] I have searched the [existing issues](https://github.com/baseflow/flutter-geolocator/issues).
- [X] I have carefully [read the documentation](https://github.com/Baseflow/flutter-geolocator/blob/main/geolocator/README.md) and verified I have added the required platform specific configuration.

### Please select affected platform(s)

- [X] Android
- [ ] iOS
- [ ] Linux
- [ ] macOS
- [ ] Web
- [ ] Windows

### Steps to reproduce

My app uses Geolocator in a background isolate (with flutter_foreground_task). In the main Isolate, Geolocator is used to request/check permissions. In the background isolate, the position stream is used. To handle possible stale states, the position stream is requested with a timeout, and the position stream is restarted immediately after timeout occurs. For details, see code sample.

### Expected results

Location stream restarts as expected.

### Actual results

The location stream starts, but doesn't emit any positions. The following series of log messages occurs:
```
2024-12-18 17:54:36.747 4341-4341 FlutterGeolocator Geolocator position updates started
2024-12-18 17:54:36.751 4341-4341 FlutterGeolocator Geolocator position updates stopped
2024-12-18 17:54:36.751 4341-4341 FlutterGeolocator There is still another flutter engine connected, not stopping location service
```

### Code sample

Code sample

```dart
Stream _startGeoLocator() async* {
late final LocationSettings locationSettings;
if(Platform.isAndroid) {
locationSettings = AndroidSettings(
accuracy: LocationAccuracy.best,
forceLocationManager: true,
intervalDuration: Duration(seconds: 1),
timeLimit: _gpsTimeout, // 10 minutes
);
} else {
locationSettings = AppleSettings(
accuracy: LocationAccuracy.bestForNavigation,
activityType: ActivityType.automotiveNavigation,
allowBackgroundLocationUpdates: true,
showBackgroundLocationIndicator: true,
pauseLocationUpdatesAutomatically: false,
timeLimit: null, // no timeout on iOS, as unsubscribing from location updates may allow the device to sleep
);
}

bool timeout = false;
do {
timeout = false;
try {
await for (final position in Geolocator.getPositionStream(locationSettings: locationSettings)) {
yield position;
}
} on TimeoutException {
timeout = true;
// with this delay enabled, the problem doesn't occur and the Geolocator stream emits positions as expected.
// await Future.delayed(Duration(seconds: 5));
}
} while (timeout);
}
```

### Screenshots or video

Screenshots or video demonstration

[Upload media here]

### Version

latest

### Flutter Doctor output

Doctor output

```console
[✓] Flutter (Channel stable, 3.24.5, on Linux Mint 20.3 5.15.0-117-generic, locale de_DE.UTF-8)
• Flutter version 3.24.5 on channel stable at ***
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision dec2ee5c1f (vor 5 Wochen), 2024-11-13 11:13:06 -0800
• Engine revision a18df97ca5
• Dart version 3.5.4
• DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
• Android SDK at ***
• Platform android-35, build-tools 35.0.0
• ANDROID_HOME = ***
• Java binary at: ***
• Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
• All Android licenses accepted.

[✓] Chrome - develop for the web
• Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
• clang version 10.0.0-4ubuntu1
• cmake version 3.16.3
• ninja version 1.10.0
• pkg-config version 0.29.1

[✓] Android Studio (version 2024.1)
• Android Studio at ***
• Flutter plugin version 81.0.2
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)

[✓] IntelliJ IDEA Community Edition (version 2024.3)
• IntelliJ at ***
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.92.0)
• VS Code at ***
• Flutter extension can be installed from:
🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)
• sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64 • Android 14 (API 34) (emulator)
• Linux (desktop) • linux • linux-x64 • Linux Mint 20.3 5.15.0-117-generic
• Chrome (web) • chrome • web-javascript • Google Chrome 127.0.6533.88

[✓] Network resources
• All expected network resources are available.

• No issues found!

```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with getPositionStream, AndroidSettings, a timeout, and an immediate restart in a background isolate. Inspect the Android position-update lifecycle around the logged start/stop messages and the connected-engine handling. Done means the restarted stream emits positions without requiring an artificial delay.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, dart
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.