Baseflow / flutter-geolocator

Android and iOS Geolocation plugin for Flutter
https://baseflow.com/
MIT License
1.25k stars 662 forks source link

[Bug]: FusedLocationClient.isLocationServiceEnabled crashes #1366

Open greg-tekai-com opened 1 year ago

greg-tekai-com commented 1 year ago

Please check the following before submitting a new issue.

Please select affected platform(s)

Steps to reproduce

I'm unable to reproduce a disconnect locally, but we have thousands of crash reports with the stack trace captured in the actual results when we get disconnected from play services.

We're pretty sure that the bug is within FusedLocationClient.isLocationServiceEnabled. If the response is not successful, it calls listener.onLocationServiceError(), but doesn't exit, and all other paths from this point are attempting to return a second result which would also crash.

In our case, after returning an error via onLocationServiceError, the method calls response.getResult which throws an exception if the result was not successful as per docs. This exception is not caught and causes the thread to crash.

Interestingly, it's primarily Amazon fire devices (~90%) that get disconnected, but we do see Pixels and Galaxy phones along with some others.

Expected results

If we get disconnected from services, it should simply call result.error once and return.

Actual results

The thread crashes with:

Stack trace

Fatal Exception: com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ApiException: 22: API failed to connect while resuming due to an unknown error.
       at com.google.android.gms.tasks.zzw.getResult(com.google.android.gms:play-services-tasks@@18.0.2:3)
       at com.baseflow.geolocator.location.FusedLocationClient.lambda$isLocationServiceEnabled$0(FusedLocationClient.java:142)
       at com.google.android.gms.tasks.zzi.run(com.google.android.gms:play-services-tasks@@18.0.2:1)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:223)
       at android.app.ActivityThread.main(ActivityThread.java:7719)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Caused by com.google.android.gms.common.api.ApiException: 22: API failed to connect while resuming due to an unknown error.
       at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@18.1.0:3)
       at com.google.android.gms.common.api.internal.ApiExceptionMapper.getException(com.google.android.gms:play-services-base@@18.1.0:1)
       at com.google.android.gms.common.api.internal.zag.zad(com.google.android.gms:play-services-base@@18.1.0:1)
       at com.google.android.gms.common.api.internal.zabq.zaE(com.google.android.gms:play-services-base@@18.1.0:1)
       at com.google.android.gms.common.api.internal.zabq.zaD(com.google.android.gms:play-services-base@@18.1.0:2)
       at com.google.android.gms.common.api.internal.zabq.zaw(com.google.android.gms:play-services-base@@18.1.0:6)
       at com.google.android.gms.common.api.internal.GoogleApiManager.handleMessage(com.google.android.gms:play-services-base@@18.1.0:47)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:223)
       at android.os.HandlerThread.run(HandlerThread.java:67)

Code sample

Code sample ```dart final isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled(); ```

Screenshots or video

Screenshots or video demonstration [Upload media here]

Version

10.1.0

Flutter Doctor output

Doctor output ```console Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.13.8, on Ubuntu 22.04.3 LTS 5.15.0-86-generic, locale en_CA.UTF-8) [!] Android toolchain - develop for Android devices (Android SDK version 34.0.0) ✗ Android SDK file not found: /media/greg/sdks/android/platforms/android-34/android.jar. [✓] Chrome - develop for the web [✓] Linux toolchain - develop for Linux desktop [✓] Android Studio (version 2022.3) [✓] Android Studio (version 2022.2) [✓] VS Code (version unknown) ✗ Unable to determine VS Code version. [✓] Connected device (3 available) [✓] Network resources ! Doctor found issues in 1 category. ```
AvidanGC commented 10 months ago

E/AndroidRuntime(12981): FATAL EXCEPTION: main E/AndroidRuntime(12981): Process: io.app.example, PID: 12981 E/AndroidRuntime(12981): java.lang.IncompatibleClassChangeError: Found class com.google.android.gms.location.FusedLocationProviderClient, but interface was expected (declaration of 'com.google.android.gms.location.FusedLocationProviderClient' appears in /data/app/~~8o8e888GaypcD02QVX9q2w==/io.app.example-4ohfhzzJfudc8cAh8t6Ccw==/base.apk) E/AndroidRuntime(12981): at com.lyokone.location.FlutterLocation.createLocationCallback(FlutterLocation.java:220) E/AndroidRuntime(12981): at com.lyokone.location.FlutterLocation.changeSettings(FlutterLocation.java:198) E/AndroidRuntime(12981): at com.lyokone.location.MethodCallHandlerImpl.onChangeSettings(MethodCallHandlerImpl.java:106) E/AndroidRuntime(12981): at com.lyokone.location.MethodCallHandlerImpl.onMethodCall(MethodCallHandlerImpl.java:40) E/AndroidRuntime(12981): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/AndroidRuntime(12981): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/AndroidRuntime(12981): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) E/AndroidRuntime(12981): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/AndroidRuntime(12981): at android.os.Handler.handleCallback(Handler.java:942) E/AndroidRuntime(12981): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(12981): at android.os.Looper.loopOnce(Looper.java:226) E/AndroidRuntime(12981): at android.os.Looper.loop(Looper.java:313) E/AndroidRuntime(12981): at android.app.ActivityThread.main(ActivityThread.java:8741) E/AndroidRuntime(12981): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(12981): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) E/AndroidRuntime(12981): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) I/Process (12981): Sending signal. PID: 12981 SIG: 9. need help please, any idea ?

huvi8161 commented 9 months ago

I have the same error, @TimHoogstrate is your PR a solution for this error, do you plan to send it for merge?

jozefvodicka commented 8 months ago

Still happening... Is it possible to look into it, test the proposed solution and merge it?

tentenponce commented 7 months ago

hi @greg-tekai-com @TimHoogstrate is there any way to reproduce this? Does the PR has been tested to fix the issue? We have similar reports to this, a little bit different but the same reason which is related to connectivity issues.

Fatal Exception: com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.b: 20: The connection to Google Play services was lost due to service disconnection. Last reason for disconnect: Timing out service connection.
       at com.google.android.gms.tasks.zzw.getResult(:19)
       at com.baseflow.geolocator.location.FusedLocationClient.lambda$isLocationServiceEnabled$0(FusedLocationClient.java:11)
       at com.google.android.gms.tasks.zzi.run(:21)
       at android.os.Handler.handleCallback(Handler.java:942)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:240)
       at android.os.Looper.loop(Looper.java:351)
       at android.app.ActivityThread.main(ActivityThread.java:8370)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:568)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)

Caused by com.google.android.gms.common.api.b: 20: The connection to Google Play services was lost due to service disconnection. Last reason for disconnect: Timing out service connection.
       at com.google.android.gms.common.api.internal.zaad.zah(zaad.java:108)
       at com.google.android.gms.common.api.internal.zaad.zae(zaad.java:43)
       at com.google.android.gms.common.api.internal.zabq.zaH(zabq.java:14)
       at com.google.android.gms.common.api.internal.zabq.onConnectionSuspended(zabq.java:16)
       at com.google.android.gms.common.api.internal.zabq.zaJ(zabq.java:14)
       at com.google.android.gms.common.api.internal.zabq.zaL(zabq.java:22)
       at com.google.android.gms.common.api.internal.zabq.zap(zabq.java:17)
       at com.google.android.gms.common.api.internal.GoogleApiManager.handleMessage(GoogleApiManager.java:641)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loopOnce(Looper.java:240)
       at android.os.Looper.loop(Looper.java:351)
       at android.os.HandlerThread.run(HandlerThread.java:67)
tentenponce commented 7 months ago

attaching here also the similar issue with react native, and with their solution. Actually, its not a solution but just hides the actual error. But since the error is coming from Google itself, what we can do is to wait for them to fix the actual issue.

similar RN issue: https://github.com/Agontuk/react-native-geolocation-service/issues/137 fix: https://github.com/Agontuk/react-native-geolocation-service/pull/147/files

mofogasy commented 6 months ago

We encounter the same problem when calling Geolocator.isLocationServiceEnabled if the location services are disabled on the device.

E/AndroidRuntime(23245): FATAL EXCEPTION: main
E/AndroidRuntime(23245): Process: xxx, PID: 23245
E/AndroidRuntime(23245): com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED
E/AndroidRuntime(23245):    at com.google.android.gms.tasks.zzw.getResult(com.google.android.gms:play-services-tasks@@18.0.2:3)
E/AndroidRuntime(23245):    at com.baseflow.geolocator.location.FusedLocationClient.lambda$isLocationServiceEnabled$0(FusedLocationClient.java:161)
E/AndroidRuntime(23245):    at com.baseflow.geolocator.location.FusedLocationClient$$ExternalSyntheticLambda0.onComplete(D8$$SyntheticClass:0)
E/AndroidRuntime(23245):    at com.google.android.gms.tasks.zzi.run(com.google.android.gms:play-services-tasks@@18.0.2:1)
E/AndroidRuntime(23245):    at android.os.Handler.handleCallback(Handler.java:958)
E/AndroidRuntime(23245):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(23245):    at android.os.Looper.loopOnce(Looper.java:230)
E/AndroidRuntime(23245):    at android.os.Looper.loop(Looper.java:319)
E/AndroidRuntime(23245):    at android.app.ActivityThread.main(ActivityThread.java:8893)
E/AndroidRuntime(23245):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(23245):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
E/AndroidRuntime(23245):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
E/AndroidRuntime(23245): Caused by: com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED
E/AndroidRuntime(23245):    at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@18.1.0:2)
E/AndroidRuntime(23245):    at com.google.android.gms.common.api.internal.TaskUtil.setResultOrApiException(com.google.android.gms:play-services-base@@18.1.0:4)
E/AndroidRuntime(23245):    at com.google.android.gms.internal.location.zzcq.zzb(com.google.android.gms:play-services-location@@21.0.1:2)
E/AndroidRuntime(23245):    at com.google.android.gms.internal.location.zzr.zza(com.google.android.gms:play-services-location@@21.0.1:3)
E/AndroidRuntime(23245):    at com.google.android.gms.internal.location.zzb.onTransact(com.google.android.gms:play-services-location@@21.0.1:3)
E/AndroidRuntime(23245):    at android.os.Binder.execTransactInternal(Binder.java:1380)
E/AndroidRuntime(23245):    at android.os.Binder.execTransact(Binder.java:1311)
I/flutter (23245): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (23245): │ PlatformException(LOCATION_SERVICES_DISABLED, Location services are disabled. To receive location updates the location services should be enabled., null, null)
I/flutter (23245): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (23245): │ #0   StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
I/flutter (23245): │ #1   MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
I/flutter (23245): │ #2   <asynchronous suspension>
I/flutter (23245): │ #3   GeolocatorAndroid.isLocationServiceEnabled.<anonymous closure> (package:geolocator_android/src/geolocator_android.dart:73:13)
I/flutter (23245): │ #4   <asynchronous suspension>
I/flutter (23245): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (23245): │ Error caught in guarded zone
I/flutter (23245): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
wzslr321 commented 3 months ago

I am also encountering it on Android. Is there any work planned regarding resolving that?

tentenponce commented 3 months ago

hi @greg-tekai-com , I forcefully replicated the issue by simply intentionally throwing the same error that we're encountering. I managed to successfully add a proper error handling, and was able to handle it on the Flutter side.

For the mean time while waiting for the contributors to review the PR, I forked the library, specifically the geolocator_android, apply the fix and overrides the geolocator_android dependency in our pubspec.yaml.

kazbek-sultanov-t commented 1 month ago

Is there any update related to this issue?