Android Webview - chromium 在调用方法 webView.loadUrl 时显示 ANR(使用 headers)
Android Webview - chromium shows ANR when method webView.loadUrl is called (with headers)
我们有一个使用 Android webview 的应用程序,我们注意到自 04/07/2021 以来调用以下代码时出现 ANR(该代码在 UIThread 中调用):
public void onLoadUrlWithHeaders(final String url, final Map<String, String> headers) {
Log.d(TAG, "onLoadUrl with headers : " + url);
webView.loadUrl(url, headers);
}
问题是间歇性的,只发生在某些设备上。例如在我们的测试设备上:
摩托罗拉 Moto X (4) Android 9 (SDK 28)
三星 SM - A520F Android 8 (SDK 26) - Webview Chrome 90.0.4430.82
问题出现在我们 2020 年 12 月发布的应用程序版本中,该版本直到 4 月才出现上述 ANR。
ANR
后A520F的/data/anr/traces.txt文件摘录
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)
在 Activity onDestroy 上调用了遗留代码。
@Override
protected void onDestroy() {
super.onDestroy();
WebView webView = getWebView();
if (webView != null) {
webView.setWebViewClient(null);
webView.setWebChromeClient(null);
webView.loadUrl("about:blank");
}
}
此代码可以在 webview 中发出注销请求后立即执行。当响应仍未决时。
在某个时候,在 chromium 更新后,当这种情况发生时(onDestroy 有待处理的响应)在下一次执行 webView.loadUrl 时观察到 ANR。
我们创建一个控件来等待注销响应完成 activity。 Play 商店中注册的 ANR 数量减少了 99% 以上。或者你可以简单地从 ondestroy 中删除上面的代码。
我们有一个使用 Android webview 的应用程序,我们注意到自 04/07/2021 以来调用以下代码时出现 ANR(该代码在 UIThread 中调用):
public void onLoadUrlWithHeaders(final String url, final Map<String, String> headers) {
Log.d(TAG, "onLoadUrl with headers : " + url);
webView.loadUrl(url, headers);
}
问题是间歇性的,只发生在某些设备上。例如在我们的测试设备上: 摩托罗拉 Moto X (4) Android 9 (SDK 28)
三星 SM - A520F Android 8 (SDK 26) - Webview Chrome 90.0.4430.82
问题出现在我们 2020 年 12 月发布的应用程序版本中,该版本直到 4 月才出现上述 ANR。
ANR
后A520F的/data/anr/traces.txt文件摘录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)
在 Activity onDestroy 上调用了遗留代码。
@Override
protected void onDestroy() {
super.onDestroy();
WebView webView = getWebView();
if (webView != null) {
webView.setWebViewClient(null);
webView.setWebChromeClient(null);
webView.loadUrl("about:blank");
}
}
此代码可以在 webview 中发出注销请求后立即执行。当响应仍未决时。
在某个时候,在 chromium 更新后,当这种情况发生时(onDestroy 有待处理的响应)在下一次执行 webView.loadUrl 时观察到 ANR。
我们创建一个控件来等待注销响应完成 activity。 Play 商店中注册的 ANR 数量减少了 99% 以上。或者你可以简单地从 ondestroy 中删除上面的代码。