androidwebviewchromiumandroid-anr-dialog

Android Webview - chromium shows ANR when method webView.loadUrl is called (with headers)


We have an application that uses Android webview and we have noticed since 04/07/2021 ANRs when call the code below (The code is called in UIThread):

public void onLoadUrlWithHeaders(final String url, final Map<String, String> headers) {
        Log.d(TAG, "onLoadUrl with headers : " + url);
        webView.loadUrl(url, headers);
    }

The problem is intermittent, and only happens on some devices. For exemple on our test devices: Motorola Moto X (4) Android 9 (SDK 28)

Samsung SM - A520F Android 8 (SDK 26) - Webview Chrome 90.0.4430.82

The problem is happening in a version of our app released in December 2020 and that version had not been presenting the mentioned ANR until April.

excerpt from the /data/anr/traces.txt file of A520F after the ANR

suspend all histogram:  Sum: 340.474ms 99% C.I. 0.042ms-187.678ms Avg: 4.365ms Max: 283.301ms
DALVIK THREADS (69):
"Signal Catcher" daemon prio=5 tid=3 Runnable
  | group="system" sCount=0 dsCount=0 flags=0 obj=0x13680248 self=0x7d302c1400
  | sysTid=15932 nice=0 cgrp=default sched=0/0 handle=0x7d254fe4f0
  | state=R schedstat=( 0 0 0 ) utm=2 stm=3 core=3 HZ=100
  | stack=0x7d25404000-0x7d25406000 stackSize=1005KB
  | held mutexes= "mutator lock"(shared held)
  native: #00 pc 0000000000397510  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+212)
  native: #01 pc 000000000045dce8  /system/lib64/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMapb+348)
  native: #02 pc 000000000047549c  /system/lib64/libart.so (_ZN3art14DumpCheckpoint3RunEPNS_6ThreadE+880)
  native: #03 pc 000000000046d774  /system/lib64/libart.so (_ZN3art10ThreadList13RunCheckpointEPNS_7ClosureES2_+480)
  native: #04 pc 000000000046d17c  /system/lib64/libart.so (_ZN3art10ThreadList4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEb+796)
  native: #05 pc 000000000046cd1c  /system/lib64/libart.so (_ZN3art10ThreadList14DumpForSigQuitERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+920)
  native: #06 pc 00000000004426f0  /system/lib64/libart.so (_ZN3art7Runtime14DumpForSigQuitERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+196)
  native: #07 pc 000000000044bb88  /system/lib64/libart.so (_ZN3art13SignalCatcher13HandleSigQuitEv+1676)
  native: #08 pc 000000000044a9c0  /system/lib64/libart.so (_ZN3art13SignalCatcher3RunEPv+388)
  native: #09 pc 0000000000067d04  /system/lib64/libc.so (_ZL15__pthread_startPv+200)
  native: #10 pc 000000000001f348  /system/lib64/libc.so (__start_thread+68)
  (no managed stack frames)

"main" prio=5 tid=1 Native
  | group="main" sCount=1 dsCount=0 flags=1 obj=0x731fda50 self=0x7d302c0a00
  | sysTid=15926 nice=-10 cgrp=default sched=0/0 handle=0x7d353e19c8
  | state=R schedstat=( 0 0 0 ) utm=4634 stm=638 core=2 HZ=100
  | stack=0x7fd505c000-0x7fd505e000 stackSize=8MB
  | held mutexes=
  kernel: (couldn't read /proc/self/task/15926/stack)
  native: #00 pc 00000000035e867c  /data/app/com.android.chrome-ZK4DDobzNyyzf7J8pPGLPg==/base.apk (???)
  native: #01 pc 00000000033e32d8  /data/app/com.android.chrome-ZK4DDobzNyyzf7J8pPGLPg==/base.apk (???)
  at J.N.MAqmDh4t(Native method)
  **at org.chromium.content.browser.framehost.NavigationControllerImpl.c(chromium-Monochrome.aab-stable-443008223:17)
  at org.chromium.android_webview.AwContents.w(chromium-Monochrome.aab-stable-443008223:35)
  at org.chromium.android_webview.AwContents.v(chromium-Monochrome.aab-stable-443008223:14)
  at com.android.webview.chromium.WebViewChromium.loadUrl(chromium-Monochrome.aab-stable-443008223:5)**
  at android.webkit.WebView.loadUrl(WebView.java:927)
  at br.com.gabba.box.l.o(:-1)
  at br.com.gabba.box.model.BO.k$a.c(:-1)
  at br.com.gabba.box.model.BO.k$a.b(:-1)
  at br.com.gabba.box.model.BO.LoginAuthBO.i(:-1)
  at br.com.gabba.box.model.BO.LoginAuthBO_.H(:-1)
  at br.com.gabba.box.model.BO.LoginAuthBO_$a.run(:-1)
  at android.os.Handler.handleCallback(Handler.java:789)
  at android.os.Handler.dispatchMessage(Handler.java:98)
  at android.os.Looper.loop(Looper.java:164)
  at android.app.ActivityThread.main(ActivityThread.java:6944)
  at java.lang.reflect.Method.invoke(Native method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Here is the entire traces.txt

Similar problem in Google Mobile Ads SDK Developers


Solution

  • There was a legacy code called on Activity onDestroy.

     @Override
        protected void onDestroy() {
            super.onDestroy();
    
            WebView webView = getWebView();
    
            if (webView != null) {
                webView.setWebViewClient(null);
                webView.setWebChromeClient(null);
                webView.loadUrl("about:blank");
            }
        }
    

    This code could be executed immediately after a logout request in webview. When the response was still pending.

    At some point, after a chromium update, when this happened (onDestroy with pending response) an ANR was observed in the next execution of webView.loadUrl.

    We create a control to wait the logout response to finish the activity. The number of ANRs registered in the Play Store decreased more than 99%. Or you could simply remove the above code from ondestroy.