Bug 142583 - The app with several WKWebView freezes when restoring from background
Summary: The app with several WKWebView freezes when restoring from background
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit2 (show other bugs)
Version: 528+ (Nightly build)
Hardware: iPhone / iPad iOS 8.2
: P2 Normal
Assignee: Nobody
URL: https://yadi.sk/d/Pi04ndh9f5TWh
Keywords: InRadar
Depends on:
Reported: 2015-03-11 09:24 PDT by Alexander Denisov
Modified: 2015-05-12 06:57 PDT (History)
2 users (show)

See Also:

Sample project (536.44 KB, application/octet-stream)
2015-03-11 09:24 PDT, Alexander Denisov
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Denisov 2015-03-11 09:24:40 PDT
Created attachment 248424 [details]
Sample project

The bug is related to WKWebView iOS component, actual version on iOS 8.2 is 600.1.4

Short description:
Having many WKWebView instances the restoration from background can freeze for several seconds, while waiting for IPC (this behaviour is not expected and is much different from UIWebView. I should mention here that the bug is also reproducable on mobile Safari).
In WKContentView.mm we can see the code:

- (void)_applicationWillEnterForeground:(NSNotification*)notification
    _page->viewStateDidChange(ViewState::AllFlags & ~ViewState::IsInWindow, ***true***, WebPageProxy::ViewStateChangeDispatchMode::Immediate);

where the boolean parameter indicates that the IPC call should be handled synchronously.

There is a bugreport to Apple describing the same problem: http://openradar.appspot.com/20068553

In the apps that use many WKWebView instances there is a problem with restoration from background which leads to UI freezing or even killing by system (because of not resuming in certain time interval). The problem affects Safari on iOS 8, as well.

If most of web view instances are not in view hierarchy when entering background and restoring back - restoration is always slow, even if you push "Home" and return back to your app without switching to another tasks.
If you switch to another apps, especially memory-demanding or using WKWebViews such as Safari and then return back to you app (make sure it was not unloaded by the system) - you get main thread completely freezed by communicating with WKWebView system process. This problem has a huge user experience impact and also can lead to a crash due to not resuming in time.

Steps to Reproduce:
Use the attached sample project

Case 1.
1. Load 5-10 WKWebView instances using "Create" button.
2. Press "Home", wait several seconds
3. Return to the app and see a restoration time - should be 1-4 seconds with the linear dependence on the web view instances count.

Case 2.
1. Same as before
2. Tap the "Workaround" button. This puts all WKWebView instances into view hierarchy.
2. Press "Home", wait several seconds
3. Return to the app and see a restoration time - should less than a second.
4. Switch to another apps, browse sites in Safari, etc.
5. Switch to the sample app again - restoration time should be several seconds again.

Expected Results:
Main thread is not blocked or blocked only for the active WKWebView instance

Actual Results:
Inter-process communication freezing main thread is performed for all WKWebView instances, so the restoration time is completely unacceptable (if keeping in mind a UI responsiveness principle).

iOS 8.1.3 [12B466], 8.2 [12D508], 8.3 beta 2 [12F5037c]


iPhone 5s, iPhone 6, iPad mini

'WKWebViewRestorationFromBackground.zip': https://yadi.sk/d/Pi04ndh9f5TWh
Comment 1 Alexey Proskuryakov 2015-03-11 23:19:01 PDT