flutter-geolocator icon indicating copy to clipboard operation
flutter-geolocator copied to clipboard

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

Open jbxbergdev opened this issue 10 months ago • 0 comments

Please check the following before submitting a new issue.

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
  Stream<Position> _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
[✓] 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!

jbxbergdev avatar Dec 18 '24 19:12 jbxbergdev