Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in production #112

Closed
OsamaQureshi796 opened this issue Apr 21, 2024 · 7 comments
Closed

Crash in production #112

OsamaQureshi796 opened this issue Apr 21, 2024 · 7 comments

Comments

@OsamaQureshi796
Copy link

OsamaQureshi796 commented Apr 21, 2024

Hello @X-SLAYER ,

Thank you soo much for having such an awesome plugin for flutter.

I want to bring one issue to your attention that is happening in the production.

Here's the stack-trace from firebase-crashlytics.

# Crashlytics - Stack trace
# Platform: android

This happens mostly in Oppo devices.

Fatal Exception: java.lang.RuntimeException
Unable to create service flutter.overlay.window.flutter_overlay_window.OverlayService: java.lang.NullPointerException: Attempt to invoke virtual method 'kd.a io.flutter.embedding.engine.a.j()' on a null object reference



Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'kd.a io.flutter.embedding.engine.a.j()' on a null object reference
       at flutter.overlay.window.flutter_overlay_window.OverlayService.<init>(:35)
       at java.lang.Class.newInstance(Class.java)
       at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:129)
       at androidx.core.app.CoreComponentFactory.instantiateService()
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:5073)
       at android.app.ActivityThread.-$$Nest$mhandleCreateService()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2506)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:230)
       at android.os.Looper.loop(Looper.java:319)
       at android.app.ActivityThread.main(ActivityThread.java:8893)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

Caused by java.lang.NullPointerException
Attempt to invoke virtual method 'kd.a io.flutter.embedding.engine.a.j()' on a null object reference

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'kd.a io.flutter.embedding.engine.a.j()' on a null object reference
       at flutter.overlay.window.flutter_overlay_window.OverlayService.<init>(:35)
       at java.lang.Class.newInstance(Class.java)
       at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:129)
       at androidx.core.app.CoreComponentFactory.instantiateService()
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:4731)
       at android.app.ActivityThread.-$$Nest$mhandleCreateService()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2335)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:240)
       at android.os.Looper.loop(Looper.java:351)
       at android.app.ActivityThread.main(ActivityThread.java:8436)
       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)

please let me know if you need further information from my side.

thanks

@chinmay4github1987
Copy link

chinmay4github1987 commented Apr 23, 2024

/MethodChannel#x-slayer/overlay_channel( 6714): Failed to handle method call E/MethodChannel#x-slayer/overlay_channel( 6714): java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x30000000 cmp=com.qspiders.flutter_project/flutter.overlay.window.flutter_overlay_window.OverlayService (has extras) }: app is in background uid UidRecord{ef07ad0 u0a168 LAST bg:+27m44s110ms idle change:cached procs:1 seq(0,0,0)} E/MethodChannel#x-slayer/overlay_channel( 6714): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1616) E/MethodChannel#x-slayer/overlay_channel( 6714): at android.app.ContextImpl.startService(ContextImpl.java:1571) E/MethodChannel#x-slayer/overlay_channel( 6714): at android.content.ContextWrapper.startService(ContextWrapper.java:669) E/MethodChannel#x-slayer/overlay_channel( 6714): at flutter.overlay.window.flutter_overlay_window.FlutterOverlayWindowPlugin.onMethodCall(FlutterOverlayWindowPlugin.java:110) E/MethodChannel#x-slayer/overlay_channel( 6714): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/MethodChannel#x-slayer/overlay_channel( 6714): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/MethodChannel#x-slayer/overlay_channel( 6714): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) E/MethodChannel#x-slayer/overlay_channel( 6714): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/MethodChannel#x-slayer/overlay_channel( 6714): at android.os.Handler.handleCallback(Handler.java:883) E/MethodChannel#x-slayer/overlay_channel( 6714): at android.os.Handler.dispatchMessage(Handler.java:100) E/MethodChannel#x-slayer/overlay_channel( 6714): at android.os.Looper.loop(Looper.java:214) E/MethodChannel#x-slayer/overlay_channel( 6714): at android.app.ActivityThread.main(ActivityThread.java:7356) E/MethodChannel#x-slayer/overlay_channel( 6714): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#x-slayer/overlay_channel( 6714): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491) E/MethodChannel#x-slayer/overlay_channel( 6714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:940) E/flutter ( 6714): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, Not allowed to start service Intent { flg=0x30000000 cmp=com.qspiders.flutter_project/flutter.overlay.window.flutter_overlay_window.OverlayService (has extras) }: app is in background uid UidRecord{ef07ad0 u0a168 LAST bg:+27m44s110ms idle change:cached procs:1 seq(0,0,0)}, null, java.lang.IllegalStateException: Not allowed to start service Intent { flg=0x30000000 cmp=com.qspiders.flutter_project/flutter.overlay.window.flutter_overlay_window.OverlayService (has extras) }: app is in background uid UidRecord{ef07ad0 u0a168 LAST bg:+27m44s110ms idle change:cached procs:1 seq(0,0,0)} E/flutter ( 6714): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1616) E/flutter ( 6714): at android.app.ContextImpl.startService(ContextImpl.java:1571) E/flutter ( 6714): at android.content.ContextWrapper.startService(ContextWrapper.java:669) E/flutter ( 6714): at flutter.overlay.window.flutter_overlay_window.FlutterOverlayWindowPlugin.onMethodCall(FlutterOverlayWindowPlugin.java:110) E/flutter ( 6714): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/flutter ( 6714): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/flutter ( 6714): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) E/flutter ( 6714): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/flutter ( 6714): at android.os.Handler.handleCallback(Handler.java:883) E/flutter ( 6714): at android.os.Handler.dispatchMessage(Handler.java:100) E/flutter ( 6714): at android.os.Looper.loop(Looper.java:214) E/flutter ( 6714): at android.app.ActivityThread.main(ActivityThread.java:7356) E/flutter ( 6714): at java.lang.reflect.Method.invoke(Native Method) E/flutter ( 6714): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491) E/flutter ( 6714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:940) E/flutter ( 6714): ) E/flutter ( 6714): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7) E/flutter ( 6714): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:322:18) E/flutter ( 6714): <asynchronous suspension> E/flutter ( 6714): #2 FlutterOverlayWindow.showOverlay (package:flutter_overlay_window/src/overlay_window.dart:54:5) E/flutter ( 6714): <asynchronous suspension> E/flutter ( 6714): #3 phoneStateBackgroundCallbackHandler (package:flutter_project/main.dart:279:7) E/flutter ( 6714): <asynchronous suspension>
Getting error whenever an inocming call comes in background.

@OsamaQureshi796
Copy link
Author

Hi @X-SLAYER ,

Do you have any plan on this to resolve.

Thanks

@s4-ali
Copy link

s4-ali commented Jun 3, 2024

Happened on debug as well, I have google pixel 7

@rorystephenson
Copy link

Android may decide to kill the overlay service for various reasons (e.g. low ram), and since START_STICKY is used it may later decide to restart the service. When this happens OverlayService.java tries to access variables which were created outside of the service and are no longer set, for example FlutterEngineCache.getInstance().get(OverlayConstants.CACHED_TAG) returns null and that leads to a NPE when we try to use that value.

You have two options to avoid these crashes:

  • Change START_STICKY to START_NOT_STICKY. This will prevent the crash but it also means the overlay won't be restarted if Android decides to close it.
  • Re-architect OverlayService.java to manage all of the variables it needs access to so that when it is restarted it doesn't refer to variables from other classes that may no longer exist. I chose this path in my own fork and made it so the flutter engine is only created when the overlay starts and is managed totally by the overlay. Unfortunately my fork was already heavily modified to suit my own needs so upstreaming a fix is not feasible. It also changes how the overlay system works (one engine per overlay, created when the overlay service is started, rather than a single engine created at app start and shared across overlays).

Apart from explaining why this happens the only other help I can really offer is a suggestion of how to reproduce the error if you want to keep START_STICKY and try to fix it yourself:

  1. Run your app and start the overlay.
  2. Use the Fill Memory app or a similar app to fill up your device memory. I selected the "FILL UNTIL MEM LOW" option. For me around 3/4 times Android would kill the overlay service and then when Fill Memory stopped filling the memory Android would try to restart the overlay service and it would fail with a NPE.

@youssefm15
Copy link

@X-SLAYER Do you plan to release a fix to this issue as it's still reproduceable in the latest version?

@X-SLAYER
Copy link
Owner

Done! the package has been updated with the instructions for android 14 compatibility

@youssefm15
Copy link

youssefm15 commented Jul 30, 2024

@X-SLAYER Thanks for your quick reply but the issue is not related to android 14 only as i can reproduce it on more than android version like android 13 and android 12 (these are the devices that i have and it may be reproduced on more).
The problem is when you run on release mode and then terminate the app you'll get FATAL EXCEPTION as the the logs shown below.
Also the needed permissions for android 14 is already added.
After debugging, i think FlutterEngine is not properly initialized or is null at this case.

FATAL EXCEPTION: main
E/AndroidRuntime(11963): Process: com.example.app, PID: 11963
E/AndroidRuntime(11963): java.lang.RuntimeException: Unable to create service flutter.overlay.window.flutter_overlay_window.OverlayService: java.lang.NullPointerException: Attempt to invoke virtual method 'a2.a io.flutter.embedding.engine.a.j()' on a null object reference
E/AndroidRuntime(11963): 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:4498)
E/AndroidRuntime(11963): 	at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
E/AndroidRuntime(11963): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2161)
E/AndroidRuntime(11963): 	at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(11963): 	at android.os.Looper.loopOnce(Looper.java:201)
E/AndroidRuntime(11963): 	at android.os.Looper.loop(Looper.java:288)
E/AndroidRuntime(11963): 	at android.app.ActivityThread.main(ActivityThread.java:7872)
E/AndroidRuntime(11963): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(11963): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime(11963): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/AndroidRuntime(11963): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'a2.a io.flutter.embedding.engine.a.j()' on a null object reference
E/AndroidRuntime(11963): 	at flutter.overlay.window.flutter_overlay_window.OverlayService.<init>(Unknown Source:35)
E/AndroidRuntime(11963): 	at java.lang.Class.newInstance(Native Method)
E/AndroidRuntime(11963): 	at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:129)
E/AndroidRuntime(11963): 	at androidx.core.app.CoreComponentFactory.instantiateService(Unknown Source:0)
E/AndroidRuntime(11963): 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:4467)
E/AndroidRuntime(11963): 	... 9 more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants