JulianAssmann / flutter_background

A flutter plugin to keep apps running in the background via foreground services. Android only.
https://pub.dev/packages/flutter_background
MIT License
88 stars 51 forks source link

[BUG] App crashes on popping route programmatically #58

Closed sprimm98 closed 2 years ago

sprimm98 commented 2 years ago

Hi Julian, first of all thanks for your amazing package. It enables me to keep a bluetooth connection and data synchronization process running in the background. Before describing the bug, a few things on the general usage: If a bluetooth connection is established, the app pushes a screen for real-time viewing of incoming data and the underlying handler enables background execution. When the user leaves the screen, the handler disables background execution again. Additionally, if the bluetooth connection gets interrupted (e.x. by turning off the bluetooth service or by getting out of reach), background execution will be disabled and the route will be popped.

Describe the bug Popping the route after registering an interrupted bluetooth connection will cause the app to crash if background execution was enabled. Please see the logs below. This does not happen if the user manually leaves the screen or background execution was not enabled.

To Reproduce

  1. Use a bluetooth package like flutter_blue_plus to establish a connection to a bluetooth device
  2. When connected, push a new route and enable background execution
  3. Register a listener, so that the route gets popped when the connection is interrupted
  4. Interrupt the connection, e.x. by disabling the bluetooth service in the control center
  5. The app should show the previous screen for a moment before crashing

Expected behavior The app pops the route and keeps running.

Smartphone:

Logs ``` E/AndroidRuntime(10900): FATAL EXCEPTION: main E/AndroidRuntime(10900): Process: de.sprimm.app, PID: 10900 E/AndroidRuntime(10900): android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{a515b44 u0 de.sprimm.app/de.julianassmann.flutter_background.IsolateHolderService} E/AndroidRuntime(10900): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2005) E/AndroidRuntime(10900): at android.os.Handler.dispatchMessage(Handler.java:106) E/AndroidRuntime(10900): at android.os.Looper.loop(Looper.java:223) E/AndroidRuntime(10900): at android.app.ActivityThread.main(ActivityThread.java:7656) E/AndroidRuntime(10900): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(10900): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) E/AndroidRuntime(10900): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) ``` ``` [✓] Flutter (Channel stable, 3.0.1, on macOS 12.3 21E230 darwin-arm, locale de-DE) • Flutter version 3.0.1 at ... • Upstream repository https://github.com/flutter/flutter.git • Framework revision fb57da5f94 (3 weeks ago), 2022-05-19 15:50:29 -0700 • Engine revision caaafc5604 • Dart version 2.17.1 • DevTools version 2.12.2 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at ... • Platform android-31, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 13.4) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.11.0 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.1) • Android Studio at /Applications/Android Studio.app/Contents • 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 • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763) [✓] VS Code (version 1.67.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.42.0 [✓] Connected device (3 available) • Pixel 2 XL (mobile) • 711KPYR0576642 • android-arm64 • Android 11 (API 30) • macOS (desktop) • macos • darwin-arm64 • macOS 12.3 21E230 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 102.0.5005.61 [✓] HTTP Host Availability • All required HTTP hosts are available • No issues found! ```
sprimm98 commented 2 years ago

I'm sorry, closing this issue for now, as minimal code change and reinstalling fixed the crash.

tronje commented 2 years ago

@sprimm98 can you elaborate how you fixed this? I'm getting a similar crash:

D/AndroidRuntime(21730): Shutting down VM
E/AndroidRuntime(21730): FATAL EXCEPTION: main
E/AndroidRuntime(21730): Process: com.example.myapp, PID: 21730
E/AndroidRuntime(21730): android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{80be3c5 u0 com.example.myapp/de.julianassmann.flutter_background.IsolateHolderService}
E/AndroidRuntime(21730):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2185)
E/AndroidRuntime(21730):    at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(21730):    at android.os.Looper.loop(Looper.java:233)
E/AndroidRuntime(21730):    at android.app.ActivityThread.main(ActivityThread.java:8010)
E/AndroidRuntime(21730):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(21730):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
E/AndroidRuntime(21730):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
I/Process (21730): Sending signal. PID: 21730 SIG: 9

Edit: it looks like I was calling FlutterBackground.disableBackgroundExecution() twice in a row, which caused the crash. If I keep track of my background execution enabled-ness, and only ever call this once before enabling again, the crash does not occur.

sprimm98 commented 2 years ago

@tronje Yes, it was quite similar. I called the dispose method of the underlying controller twice, and therefore the mentioned method was also called twice resulting in the crash. Calling it just once fixed it.

JulianAssmann commented 2 years ago

Thank you for your comments, I will probably update the FlutterBackground.disableBackgroundExecution() method to not crash when called multiple times.