Protect globalWebSocketStreamMap with a Lock
authorachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Feb 2019 15:38:20 +0000 (15:38 +0000)
committerachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Feb 2019 15:38:20 +0000 (15:38 +0000)
https://bugs.webkit.org/show_bug.cgi?id=194224
<rdar://problem/47581081>

Reviewed by Ryosuke Niwa.

* WebProcess/Network/WebSocketStream.cpp:
(WebKit::WebSocketStream::streamWithIdentifier):
(WebKit::WebSocketStream::networkProcessCrashed):
(WebKit::WebSocketStream::WebSocketStream):
(WebKit::WebSocketStream::~WebSocketStream):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@240974 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/WebProcess/Network/WebSocketStream.cpp

index 65706bb..9a66bc1 100644 (file)
@@ -1,3 +1,17 @@
+2019-02-05  Alex Christensen  <achristensen@webkit.org>
+
+        Protect globalWebSocketStreamMap with a Lock
+        https://bugs.webkit.org/show_bug.cgi?id=194224
+        <rdar://problem/47581081>
+
+        Reviewed by Ryosuke Niwa.
+
+        * WebProcess/Network/WebSocketStream.cpp:
+        (WebKit::WebSocketStream::streamWithIdentifier):
+        (WebKit::WebSocketStream::networkProcessCrashed):
+        (WebKit::WebSocketStream::WebSocketStream):
+        (WebKit::WebSocketStream::~WebSocketStream):
+
 2019-02-04  Alex Christensen  <achristensen@webkit.org>
 
         Add Networking Daemon skeleton
index 52bc385..1fb6961 100644 (file)
@@ -41,6 +41,7 @@
 namespace WebKit {
 using namespace WebCore;
 
+static Lock globalWebSocketStreamMapLock;
 static HashMap<uint64_t, WebSocketStream*>& globalWebSocketStreamMap()
 {
     static NeverDestroyed<HashMap<uint64_t, WebSocketStream*>> globalMap;
@@ -49,19 +50,30 @@ static HashMap<uint64_t, WebSocketStream*>& globalWebSocketStreamMap()
 
 WebSocketStream* WebSocketStream::streamWithIdentifier(uint64_t identifier)
 {
+    LockHolder locker(globalWebSocketStreamMapLock);
     return globalWebSocketStreamMap().get(identifier);
 }
 
 void WebSocketStream::networkProcessCrashed()
 {
-    for (auto& stream : globalWebSocketStreamMap().values()) {
+    Vector<RefPtr<WebSocketStream>> sockets;
+    {
+        LockHolder locker(globalWebSocketStreamMapLock);
+        sockets.reserveInitialCapacity(globalWebSocketStreamMap().size());
+        for (auto& stream : globalWebSocketStreamMap().values())
+            sockets.uncheckedAppend(stream);
+    }
+
+    for (auto& stream : sockets) {
         for (auto& callback : stream->m_sendDataCallbacks.values())
             callback(false);
         for (auto& callback : stream->m_sendHandshakeCallbacks.values())
             callback(false, false);
         stream->m_client.didFailSocketStream(*stream, SocketStreamError(0, { }, "Network process crashed."));
+        stream = nullptr;
     }
 
+    LockHolder locker(globalWebSocketStreamMapLock);
     globalWebSocketStreamMap().clear();
 }
 
@@ -76,12 +88,14 @@ WebSocketStream::WebSocketStream(const URL& url, WebCore::SocketStreamHandleClie
 {
     WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::CreateSocketStream(url, sessionID, cachePartition, identifier()), 0);
 
+    LockHolder locker(globalWebSocketStreamMapLock);
     ASSERT(!globalWebSocketStreamMap().contains(identifier()));
     globalWebSocketStreamMap().set(identifier(), this);
 }
 
 WebSocketStream::~WebSocketStream()
 {
+    LockHolder locker(globalWebSocketStreamMapLock);
     ASSERT(globalWebSocketStreamMap().contains(identifier()));
     globalWebSocketStreamMap().remove(identifier());
 }