Title: [251443] trunk/Source/WebKit
Revision
251443
Author
[email protected]
Date
2019-10-22 12:40:45 -0700 (Tue, 22 Oct 2019)

Log Message

Simplify "Unexpectedly Resumed" assertion handling
https://bugs.webkit.org/show_bug.cgi?id=203254

Reviewed by Geoffrey Garen.

When the WebContent process gets resumed from suspension, it now unconditionally takes a
process assertion on behalf on the UIProcess and sends a ProcessDidResume IPC to the
UIProcess. The UIProcess then sends a DidHandleProcessWasResumed IPC back after handing
the ProcessDidResume IPC allowing the WebContent process to release its assertion on
behalf on the UIProcess.

The previous code was racy because it relied on the m_processIsSuspended flag, which was
queried and set from different threads. Also, the 'unexpectedly resumed' naming was
confusing since we'd often take this assertion whenever the WebProcess got resumed,
wether unexpected or not, simply because the processTaskStateDidChange IPC won the race
with the ProcessDidResume IPC from the UIProcess.

* UIProcess/Cocoa/WebProcessProxyCocoa.mm:
(WebKit::WebProcessProxy::processWasResumed):
(WebKit::WebProcessProxy::processWasUnexpectedlyUnsuspended): Deleted.
* UIProcess/WebProcessProxy.h:
* UIProcess/WebProcessProxy.messages.in:
* WebProcess/WebProcess.cpp:
(WebKit::WebProcess::processDidResume):
* WebProcess/WebProcess.h:
* WebProcess/WebProcess.messages.in:
* WebProcess/cocoa/WebProcessCocoa.mm:
(WebKit::WebProcess::processTaskStateDidChange):
(WebKit::WebProcess::didHandleProcessWasResumed):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (251442 => 251443)


--- trunk/Source/WebKit/ChangeLog	2019-10-22 19:03:44 UTC (rev 251442)
+++ trunk/Source/WebKit/ChangeLog	2019-10-22 19:40:45 UTC (rev 251443)
@@ -1,3 +1,35 @@
+2019-10-22  Chris Dumez  <[email protected]>
+
+        Simplify "Unexpectedly Resumed" assertion handling
+        https://bugs.webkit.org/show_bug.cgi?id=203254
+
+        Reviewed by Geoffrey Garen.
+
+        When the WebContent process gets resumed from suspension, it now unconditionally takes a
+        process assertion on behalf on the UIProcess and sends a ProcessDidResume IPC to the
+        UIProcess. The UIProcess then sends a DidHandleProcessWasResumed IPC back after handing
+        the ProcessDidResume IPC allowing the WebContent process to release its assertion on
+        behalf on the UIProcess.
+
+        The previous code was racy because it relied on the m_processIsSuspended flag, which was
+        queried and set from different threads. Also, the 'unexpectedly resumed' naming was
+        confusing since we'd often take this assertion whenever the WebProcess got resumed,
+        wether unexpected or not, simply because the processTaskStateDidChange IPC won the race
+        with the ProcessDidResume IPC from the UIProcess.
+
+        * UIProcess/Cocoa/WebProcessProxyCocoa.mm:
+        (WebKit::WebProcessProxy::processWasResumed):
+        (WebKit::WebProcessProxy::processWasUnexpectedlyUnsuspended): Deleted.
+        * UIProcess/WebProcessProxy.h:
+        * UIProcess/WebProcessProxy.messages.in:
+        * WebProcess/WebProcess.cpp:
+        (WebKit::WebProcess::processDidResume):
+        * WebProcess/WebProcess.h:
+        * WebProcess/WebProcess.messages.in:
+        * WebProcess/cocoa/WebProcessCocoa.mm:
+        (WebKit::WebProcess::processTaskStateDidChange):
+        (WebKit::WebProcess::didHandleProcessWasResumed):
+
 2019-10-22  youenn fablet  <[email protected]>
 
         Remove mayHaveServiceWorkerRegisteredForOrigin

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebProcessProxyCocoa.mm (251442 => 251443)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebProcessProxyCocoa.mm	2019-10-22 19:03:44 UTC (rev 251442)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebProcessProxyCocoa.mm	2019-10-22 19:40:45 UTC (rev 251443)
@@ -37,6 +37,7 @@
 #import "WebProcessPool.h"
 #import <sys/sysctl.h>
 #import <wtf/NeverDestroyed.h>
+#import <wtf/Scope.h>
 #import <wtf/spi/darwin/SandboxSPI.h>
 
 namespace WebKit {
@@ -190,19 +191,22 @@
 #endif
 
 #if PLATFORM(IOS_FAMILY)
-void WebProcessProxy::processWasUnexpectedlyUnsuspended()
+void WebProcessProxy::processWasResumed()
 {
+    auto exitScope = makeScopeExit([this] {
+        send(Messages::WebProcess::ParentProcessDidHandleProcessWasResumed(), 0);
+    });
+
     if (m_throttler.shouldBeRunnable()) {
-        // The process becoming unsuspended was not unexpected; it likely was notified of its running state
-        // before receiving a processDidResume() message from the UIProcess.
+        // The process becoming unsuspended was not unexpected.
         return;
     }
 
     // The WebProcess was awakened by something other than the UIProcess. Take out an assertion for a
     // limited duration to allow whatever task needs to be accomplished time to complete.
-    RELEASE_LOG(ProcessSuspension, "%p - WebProcessProxy::processWasUnexpectedlyUnsuspended()", this);
+    RELEASE_LOG(ProcessSuspension, "%p - WebProcessProxy::processWasResumed() Process was unexpectedly resumed, starting background activity", this);
     auto backgroundActivityTimeoutHandler = [activityToken = m_throttler.backgroundActivityToken(), weakThis = makeWeakPtr(this)] {
-        RELEASE_LOG(ProcessSuspension, "%p - WebProcessProxy::processWasUnexpectedlyUnsuspended() - lambda, background activity timed out", weakThis.get());
+        RELEASE_LOG(ProcessSuspension, "%p - WebProcessProxy::processWasResumed() - lambda, background activity timed out", weakThis.get());
     };
     m_unexpectedActivityTimer = makeUnique<WebCore::DeferrableOneShotTimer>(WTFMove(backgroundActivityTimeoutHandler), unexpectedActivityDuration);
 }

Modified: trunk/Source/WebKit/UIProcess/WebProcessProxy.h (251442 => 251443)


--- trunk/Source/WebKit/UIProcess/WebProcessProxy.h	2019-10-22 19:03:44 UTC (rev 251442)
+++ trunk/Source/WebKit/UIProcess/WebProcessProxy.h	2019-10-22 19:40:45 UTC (rev 251443)
@@ -326,7 +326,7 @@
 #endif
 
 #if PLATFORM(IOS_FAMILY)
-    void processWasUnexpectedlyUnsuspended();
+    void processWasResumed();
 #endif
 
     void webPageMediaStateDidChange(WebPageProxy&);

Modified: trunk/Source/WebKit/UIProcess/WebProcessProxy.messages.in (251442 => 251443)


--- trunk/Source/WebKit/UIProcess/WebProcessProxy.messages.in	2019-10-22 19:03:44 UTC (rev 251442)
+++ trunk/Source/WebKit/UIProcess/WebProcessProxy.messages.in	2019-10-22 19:40:45 UTC (rev 251443)
@@ -71,7 +71,7 @@
 #endif
 
 #if PLATFORM(IOS_FAMILY)
-    ProcessWasUnexpectedlyUnsuspended()
+    ProcessWasResumed()
 #endif
 
     # Plug-in messages.

Modified: trunk/Source/WebKit/WebProcess/WebProcess.cpp (251442 => 251443)


--- trunk/Source/WebKit/WebProcess/WebProcess.cpp	2019-10-22 19:03:44 UTC (rev 251442)
+++ trunk/Source/WebKit/WebProcess/WebProcess.cpp	2019-10-22 19:40:45 UTC (rev 251443)
@@ -1539,13 +1539,6 @@
     m_webSQLiteDatabaseTracker.setIsSuspended(false);
     SQLiteDatabase::setIsDatabaseOpeningForbidden(false);
     accessibilityProcessSuspendedNotification(false);
-    {
-        LockHolder holder(m_unexpectedlyResumedUIAssertionLock);
-        if (m_unexpectedlyResumedUIAssertion) {
-            RELEASE_LOG(ProcessSuspension, "%p - WebProcess::processDidResume() Releasing 'Unexpectedly resumed' assertion", this);
-            m_unexpectedlyResumedUIAssertion = nullptr;
-        }
-    }
 #endif
 
 #if ENABLE(VIDEO)

Modified: trunk/Source/WebKit/WebProcess/WebProcess.h (251442 => 251443)


--- trunk/Source/WebKit/WebProcess/WebProcess.h	2019-10-22 19:03:44 UTC (rev 251442)
+++ trunk/Source/WebKit/WebProcess/WebProcess.h	2019-10-22 19:40:45 UTC (rev 251443)
@@ -455,6 +455,7 @@
 
 #if PLATFORM(IOS_FAMILY)
     void processTaskStateDidChange(ProcessTaskStateObserver::TaskState) final;
+    void parentProcessDidHandleProcessWasResumed();
     bool shouldFreezeOnSuspension() const;
     void updateFreezerStatus();
 #endif
@@ -535,8 +536,8 @@
 #if PLATFORM(IOS_FAMILY)
     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
     Ref<ProcessTaskStateObserver> m_taskStateObserver;
-    Lock m_unexpectedlyResumedUIAssertionLock;
-    RetainPtr<BKSProcessAssertion> m_unexpectedlyResumedUIAssertion;
+    Lock m_processWasResumedUIAssertionLock;
+    RetainPtr<BKSProcessAssertion> m_processWasResumedUIAssertion;
 #endif
 
     enum PageMarkingLayersAsVolatileCounterType { };

Modified: trunk/Source/WebKit/WebProcess/WebProcess.messages.in (251442 => 251443)


--- trunk/Source/WebKit/WebProcess/WebProcess.messages.in	2019-10-22 19:03:44 UTC (rev 251442)
+++ trunk/Source/WebKit/WebProcess/WebProcess.messages.in	2019-10-22 19:40:45 UTC (rev 251443)
@@ -156,6 +156,7 @@
 
 #if PLATFORM(IOS_FAMILY)
     UnblockAccessibilityServer(WebKit::SandboxExtension::Handle handle)
+    ParentProcessDidHandleProcessWasResumed();
 #endif
 
 #if PLATFORM(GTK) || PLATFORM(WPE)

Modified: trunk/Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm (251442 => 251443)


--- trunk/Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm	2019-10-22 19:03:44 UTC (rev 251442)
+++ trunk/Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm	2019-10-22 19:40:45 UTC (rev 251443)
@@ -297,43 +297,41 @@
 {
     // NOTE: This will be called from a background thread.
     RELEASE_LOG(ProcessSuspension, "%p - WebProcess::processTaskStateDidChange() - taskState(%d)", this, taskState);
-    if (taskState == ProcessTaskStateObserver::None)
+    if (taskState != ProcessTaskStateObserver::Running)
         return;
 
-    if (taskState == ProcessTaskStateObserver::Suspended) {
-        if (m_processIsSuspended)
-            return;
-
-        RELEASE_LOG(ProcessSuspension, "%p - WebProcess::processTaskStateChanged() - unexpectedly entered Suspended state", this);
+    LockHolder holder(m_processWasResumedUIAssertionLock);
+    if (m_processWasResumedUIAssertion)
         return;
-    }
 
-    if (!m_processIsSuspended)
-        return;
-
-    LockHolder holder(m_unexpectedlyResumedUIAssertionLock);
-    if (m_unexpectedlyResumedUIAssertion)
-        return;
-
     // We were awakened from suspension unexpectedly. Notify the WebProcessProxy, but take a process assertion on our parent PID
     // to ensure that it too is awakened.
-    RELEASE_LOG(ProcessSuspension, "%p - WebProcess::processTaskStateChanged() Taking 'Unexpectedly resumed' assertion", this);
-    m_unexpectedlyResumedUIAssertion = adoptNS([[BKSProcessAssertion alloc] initWithPID:parentProcessConnection()->remoteProcessID() flags:BKSProcessAssertionPreventTaskSuspend reason:BKSProcessAssertionReasonFinishTask name:@"Unexpectedly resumed" withHandler:nil]);
+    RELEASE_LOG(ProcessSuspension, "%p - WebProcess::processTaskStateChanged() Taking 'WebProcess was resumed' assertion on behalf on UIProcess", this);
+    m_processWasResumedUIAssertion = adoptNS([[BKSProcessAssertion alloc] initWithPID:parentProcessConnection()->remoteProcessID() flags:BKSProcessAssertionPreventTaskSuspend reason:BKSProcessAssertionReasonFinishTask name:@"WebProcess was resumed" withHandler:nil]);
 
-    auto releaseAssertion = [this] {
-        LockHolder holder(m_unexpectedlyResumedUIAssertionLock);
-        if (!m_unexpectedlyResumedUIAssertion)
-            return;
-
-        RELEASE_LOG(ProcessSuspension, "%p - WebProcess::processTaskStateChanged() Releasing 'Unexpectedly resumed' assertion due to time out", this);
-        [m_unexpectedlyResumedUIAssertion invalidate];
-        m_unexpectedlyResumedUIAssertion = nullptr;
+    m_processWasResumedUIAssertion.get().invalidationHandler = [this] {
+        LockHolder holder(m_processWasResumedUIAssertionLock);
+        RELEASE_LOG(ProcessSuspension, "%p - WebProcess::processTaskStateChanged() Releasing 'WebProcess was resumed' assertion on behalf on UIProcess due invalidation", this);
+        [m_processWasResumedUIAssertion invalidate];
+        m_processWasResumedUIAssertion = nullptr;
     };
 
-    m_unexpectedlyResumedUIAssertion.get().invalidationHandler = releaseAssertion;
-    parentProcessConnection()->send(Messages::WebProcessProxy::ProcessWasUnexpectedlyUnsuspended(), 0);
-    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), releaseAssertion);
+    // This will cause the parent process to send a ParentProcessDidHandleProcessWasResumed IPC back, so that we can release our assertion on its behalf.
+    // We are not using sendWithAsyncReply() because it would not be thread-safe.
+    parentProcessConnection()->send(Messages::WebProcessProxy::ProcessWasResumed(), 0);
 }
+
+void WebProcess::parentProcessDidHandleProcessWasResumed()
+{
+    LockHolder holder(m_processWasResumedUIAssertionLock);
+    ASSERT(m_processWasResumedUIAssertion);
+
+    RELEASE_LOG(ProcessSuspension, "%p - WebProcess::parentProcessDidHandleProcessWasResumed() Releasing 'WebProcess was resumed' assertion on behalf on UIProcess", this);
+
+    [m_processWasResumedUIAssertion invalidate];
+    m_processWasResumedUIAssertion = nullptr;
+}
+
 #endif
 
 #if PLATFORM(IOS_FAMILY)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to