flutter-ml / google_ml_kit_flutter

A flutter plugin that implements Google's standalone ML Kit
MIT License
974 stars 742 forks source link

ANR when checking or downloading model on Android >=11 #504

Closed baraghiffari closed 3 weeks ago

baraghiffari commented 1 year ago

I have this issue for quite some time, I'm not sure if this come because of this plugin or there's something wrong that might make it happens in my app since I don't run any native function at the same time when doing that and I can't seem to find the same issue on internet. When checking if model is present or downloading it might make the app freeze for a while, and it only happens on Android >= 11 (11, 12 and 13) according to Crashlytics. Does anyone face the same thing as me? or know how to fix this or the cause of it? I use the latest plugin, but it also happens on previous version.

Here's the stack trace (It's tagged as Triggered ANR and Root Blocking) and I think it also triggers Native Method (android.os.MessageQueue.nativePollOnce) ANR too. The title issue title is com.google_mlkit_commons.GenericModelManager.isModelDownloaded on Crashlytics

main (waiting):tid=1 systid=17546 at sun.misc.Unsafe.park(Native method) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:190) at java.util.concurrent.FutureTask.awaitDone(FutureTask.java:450) at java.util.concurrent.FutureTask.get(FutureTask.java:192) at com.google_mlkit_commons.GenericModelManager.isModelDownloaded(GenericModelManager.java:73) at com.google_mlkit_commons.GenericModelManager.manageModel(GenericModelManager.java:44) at com.google_mlkit_digital_ink_recognition.DigitalInkRecognizer.manageModel(DigitalInkRecognizer.java:148) at com.google_mlkit_digital_ink_recognition.DigitalInkRecognizer.onMethodCall(DigitalInkRecognizer.java:44) at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258) at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(D8$$SyntheticClass) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8625) at java.lang.reflect.Method.invoke(Native method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

fbernaly commented 1 year ago

@baraghiffari : have you tried our example app? try our example app and share detailed steps to reproduce, otherwise I wont be able to help. I have not seen that issue before.

lurongshuang commented 1 year ago

Encountered the same problem at: _modelManager.isModelDownloaded(language);

D/CrashReport( 8240): main stack:sun.misc.Unsafe.park(Native Method) D/CrashReport( 8240): java.util.concurrent.locks.LockSupport.park(LockSupport.java:190) D/CrashReport( 8240): java.util.concurrent.FutureTask.awaitDone(FutureTask.java:450) D/CrashReport( 8240): java.util.concurrent.FutureTask.get(FutureTask.java:192) D/CrashReport( 8240): com.google_mlkit_commons.GenericModelManager.isModelDownloaded(GenericModelManager.java:73) D/CrashReport( 8240): com.google_mlkit_commons.GenericModelManager.downloadModel(GenericModelManager.java:54) D/CrashReport( 8240): com.google_mlkit_commons.GenericModelManager.manageModel(GenericModelManager.java:38) D/CrashReport( 8240): com.google_mlkit_digital_ink_recognition.DigitalInkRecognizer.manageModel(DigitalInkRecognizer.java:148) D/CrashReport( 8240): com.google_mlkit_digital_ink_recognition.DigitalInkRecognizer.onMethodCall(DigitalInkRecognizer.java:44) D/CrashReport( 8240): io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258) D/CrashReport( 8240): io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) D/CrashReport( 8240): io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) D/CrashReport( 8240): io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) D/CrashReport( 8240): android.os.Handler.handleCallback(Handler.java:883) D/CrashReport( 8240): android.os.Handler.dispatchMessage(Handler.java:100) D/CrashReport( 8240): android.os.Looper.loop(Looper.java:238) D/CrashReport( 8240): android.app.ActivityThread.main(ActivityThread.java:7820) D/CrashReport( 8240): java.lang.reflect.Method.invoke(Native Method) D/CrashReport( 8240): com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) D/CrashReport( 8240): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995) D/CrashReport( 8240): D/CrashReport( 8240): short msg:Find process anr, but unable to get anr message. D/CrashReport( 8240): long msg:null D/CrashReport( 8240): threads:34 I/CrashReport( 8240): found visible anr , start to upload! D/CrashReport( 8240): trace file:/data/user/0/tech.qqss.wordflows/app_bugly/manual_bugly_trace_1694058424055.txt D/CrashReport( 8240): trace file exists I/CrashReport( 8240): trace file rename :true D/CrashReport( 8240): save main stack trace D/CrashReport( 8240): isAppForeground:true D/CrashReport( 8240): [Database] insert t_cr success. D/CrashReport( 8240): insert t_cr success! I/CrashReport( 8240): backup anr record success!

lurongshuang commented 1 year ago

@fbernaly

lurongshuang commented 1 year ago

At present, I can only modify the code as follows to ensure normal operation;

Annotate all if (! GenericModelManager. isModelDownloaded (model))

Only judge on the Flutter platform first

public void isModelDownloaded(RemoteModel model, @NonNull final MethodChannel.Result result) {

// IsModelDownloaded myCallable = new IsModelDownloaded(remoteModelManager.isModelDownloaded(model)); // Future taskResult = executorService.submit(myCallable); // try { // return taskResult.get(); // } catch (InterruptedException | ExecutionException e) { // e.printStackTrace(); // } // return null; try { remoteModelManager.isModelDownloaded(model) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(@NonNull Boolean isDownloaded) { result.success(isDownloaded); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { result.error("Check model failed!", e.getMessage(), e); } }); } catch (Exception e) { result.error("Exception", e.getMessage(), e); } }

github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 3 weeks ago

This issue was closed because it has been inactive for 14 days since being marked as stale.