Title: [277114] trunk/Source/WebKit
Revision
277114
Author
achristen...@apple.com
Date
2021-05-06 13:40:56 -0700 (Thu, 06 May 2021)

Log Message

Fix crash when WebsiteDataStore is destroyed with outstanding getNetworkProcessConnection request
https://bugs.webkit.org/show_bug.cgi?id=225478
<rdar://77576148>

Reviewed by Chris Dumez.

In WebsiteDataStore::getNetworkProcessConnection if we don't get a connection the first time, we terminate the network process
and try again.  This greatly increases our success rate.  However, if we are cancelling the reply because of the destruction
of the WebsiteDataStore, we will end up doing bad things with partially destroyed objects, which ends up crashing.
In order to prevent this, use RunLoop::main.dispatch to retry on the next runloop iteration so that we will never be inside the
stack of WebsiteDataStore::~WebsiteDataStore when retrying.  In that case, we will find that weakThis is null and send an empty
reply.

* UIProcess/WebsiteData/WebsiteDataStore.cpp:
(WebKit::WebsiteDataStore::getNetworkProcessConnection):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (277113 => 277114)


--- trunk/Source/WebKit/ChangeLog	2021-05-06 20:26:56 UTC (rev 277113)
+++ trunk/Source/WebKit/ChangeLog	2021-05-06 20:40:56 UTC (rev 277114)
@@ -1,3 +1,21 @@
+2021-05-06  Alex Christensen  <achristen...@webkit.org>
+
+        Fix crash when WebsiteDataStore is destroyed with outstanding getNetworkProcessConnection request
+        https://bugs.webkit.org/show_bug.cgi?id=225478
+        <rdar://77576148>
+
+        Reviewed by Chris Dumez.
+
+        In WebsiteDataStore::getNetworkProcessConnection if we don't get a connection the first time, we terminate the network process
+        and try again.  This greatly increases our success rate.  However, if we are cancelling the reply because of the destruction
+        of the WebsiteDataStore, we will end up doing bad things with partially destroyed objects, which ends up crashing.
+        In order to prevent this, use RunLoop::main.dispatch to retry on the next runloop iteration so that we will never be inside the
+        stack of WebsiteDataStore::~WebsiteDataStore when retrying.  In that case, we will find that weakThis is null and send an empty
+        reply.
+
+        * UIProcess/WebsiteData/WebsiteDataStore.cpp:
+        (WebKit::WebsiteDataStore::getNetworkProcessConnection):
+
 2021-05-06  Sihui Liu  <sihui_...@apple.com>
 
         Drop some unnecessary code in LocalStorageDatabase

Modified: trunk/Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp (277113 => 277114)


--- trunk/Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp	2021-05-06 20:26:56 UTC (rev 277113)
+++ trunk/Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp	2021-05-06 20:40:56 UTC (rev 277114)
@@ -1720,11 +1720,17 @@
 
 void WebsiteDataStore::getNetworkProcessConnection(WebProcessProxy& webProcessProxy, CompletionHandler<void(const NetworkProcessConnectionInfo&)>&& reply)
 {
-    networkProcess().getNetworkProcessConnection(webProcessProxy, [this, weakThis = makeWeakPtr(*this), webProcessProxy = makeWeakPtr(webProcessProxy), reply = WTFMove(reply)] (auto& connectionInfo) mutable {
-        if (UNLIKELY(!IPC::Connection::identifierIsValid(connectionInfo.identifier()) && webProcessProxy && weakThis)) {
-            terminateNetworkProcess();
-            RELEASE_LOG_IF(isPersistent(), Process, "getNetworkProcessConnection: Failed first attempt, retrying");
-            networkProcess().getNetworkProcessConnection(*webProcessProxy, WTFMove(reply));
+    networkProcess().getNetworkProcessConnection(webProcessProxy, [weakThis = makeWeakPtr(*this), webProcessProxy = makeWeakPtr(webProcessProxy), reply = WTFMove(reply)] (auto& connectionInfo) mutable {
+        if (UNLIKELY(!IPC::Connection::identifierIsValid(connectionInfo.identifier()))) {
+            // Retry on the next RunLoop iteration because we may be inside the WebsiteDataStore destructor.
+            RunLoop::main().dispatch([weakThis = WTFMove(weakThis), webProcessProxy = WTFMove(webProcessProxy), reply = WTFMove(reply)] () mutable {
+                if (RefPtr<WebsiteDataStore> strongThis = weakThis.get(); strongThis && webProcessProxy) {
+                    strongThis->terminateNetworkProcess();
+                    RELEASE_LOG_IF(strongThis->isPersistent(), Process, "getNetworkProcessConnection: Failed first attempt, retrying");
+                    strongThis->networkProcess().getNetworkProcessConnection(*webProcessProxy, WTFMove(reply));
+                } else
+                    reply({ });
+            });
             return;
         }
         reply(connectionInfo);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to