Title: [284956] branches/safari-612-branch/Source/WebKit
Revision
284956
Author
[email protected]
Date
2021-10-27 14:06:22 -0700 (Wed, 27 Oct 2021)

Log Message

Cherry-pick r283797. rdar://problem/81171560

    Bind the number of WebRTC sockets opened per process
    https://bugs.webkit.org/show_bug.cgi?id=231171

    Reviewed by Alex Christensen.

    Migrate from HashMap to StdMap so that we can keep the order based on socket identifiers.
    Take benefit of the ordering so that, above a certain size, we get the oldest socket, close it and remove it from the map.
    We set the maximum size to 256 sockets per process.
    Fix a potential bug in NetworkRTCTCPSocketCocoa to ensure we remove the socket from the map even if the nw connection is not live.
    Manually tested.

    * NetworkProcess/webrtc/NetworkRTCProvider.cpp:
    * NetworkProcess/webrtc/NetworkRTCProvider.h:
    * NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm:

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

Modified Paths

Diff

Modified: branches/safari-612-branch/Source/WebKit/ChangeLog (284955 => 284956)


--- branches/safari-612-branch/Source/WebKit/ChangeLog	2021-10-27 21:06:19 UTC (rev 284955)
+++ branches/safari-612-branch/Source/WebKit/ChangeLog	2021-10-27 21:06:22 UTC (rev 284956)
@@ -1,5 +1,44 @@
 2021-10-26  Russell Epstein  <[email protected]>
 
+        Cherry-pick r283797. rdar://problem/81171560
+
+    Bind the number of WebRTC sockets opened per process
+    https://bugs.webkit.org/show_bug.cgi?id=231171
+    
+    Reviewed by Alex Christensen.
+    
+    Migrate from HashMap to StdMap so that we can keep the order based on socket identifiers.
+    Take benefit of the ordering so that, above a certain size, we get the oldest socket, close it and remove it from the map.
+    We set the maximum size to 256 sockets per process.
+    Fix a potential bug in NetworkRTCTCPSocketCocoa to ensure we remove the socket from the map even if the nw connection is not live.
+    Manually tested.
+    
+    * NetworkProcess/webrtc/NetworkRTCProvider.cpp:
+    * NetworkProcess/webrtc/NetworkRTCProvider.h:
+    * NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm:
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@283797 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2021-10-08  Youenn Fablet  <[email protected]>
+
+            Bind the number of WebRTC sockets opened per process
+            https://bugs.webkit.org/show_bug.cgi?id=231171
+
+            Reviewed by Alex Christensen.
+
+            Migrate from HashMap to StdMap so that we can keep the order based on socket identifiers.
+            Take benefit of the ordering so that, above a certain size, we get the oldest socket, close it and remove it from the map.
+            We set the maximum size to 256 sockets per process.
+            Fix a potential bug in NetworkRTCTCPSocketCocoa to ensure we remove the socket from the map even if the nw connection is not live.
+            Manually tested.
+
+            * NetworkProcess/webrtc/NetworkRTCProvider.cpp:
+            * NetworkProcess/webrtc/NetworkRTCProvider.h:
+            * NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm:
+
+2021-10-26  Russell Epstein  <[email protected]>
+
         Cherry-pick r283604. rdar://problem/81171560
 
     Close NetworkRTCProvider sockets explicitly when closing NetworkRTCProvider

Modified: branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.cpp (284955 => 284956)


--- branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.cpp	2021-10-27 21:06:19 UTC (rev 284955)
+++ branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.cpp	2021-10-27 21:06:22 UTC (rev 284956)
@@ -127,9 +127,9 @@
 
     callOnRTCNetworkThread([this]() {
         auto sockets = WTFMove(m_sockets);
-        for (auto& socket : m_sockets.values())
-            socket->close();
-        ASSERT(sockets.isEmpty());
+        for (auto& socket : m_sockets)
+            socket.second->close();
+        ASSERT(sockets.empty());
 #if PLATFORM(COCOA)
         m_attributedBundleIdentifiers.clear();
 #endif
@@ -140,7 +140,7 @@
 {
     ASSERT(m_rtcNetworkThread.IsCurrent());
     if (!socket) {
-        RTC_RELEASE_LOG_ERROR("createSocket with %u sockets is unable to create a new socket", m_sockets.size());
+        RTC_RELEASE_LOG_ERROR("createSocket with %lu sockets is unable to create a new socket", m_sockets.size());
         connection->send(Messages::LibWebRTCNetwork::SignalClose(identifier, 1), 0);
         return;
     }
@@ -249,27 +249,28 @@
 void NetworkRTCProvider::sendToSocket(LibWebRTCSocketIdentifier identifier, const IPC::DataReference& data, RTCNetwork::SocketAddress&& address, RTCPacketOptions&& options)
 {
     ASSERT(m_rtcNetworkThread.IsCurrent());
-    auto* socket = m_sockets.get(identifier);
-    if (!socket)
+    auto iterator = m_sockets.find(identifier);
+    if (iterator == m_sockets.end())
         return;
-
-    socket->sendTo(data.data(), data.size(), address.value, options.options);
+    iterator->second->sendTo(data.data(), data.size(), address.value, options.options);
 }
 
 void NetworkRTCProvider::closeSocket(LibWebRTCSocketIdentifier identifier)
 {
     ASSERT(m_rtcNetworkThread.IsCurrent());
-    auto* socket = m_sockets.get(identifier);
-    if (!socket)
+    auto iterator = m_sockets.find(identifier);
+    if (iterator == m_sockets.end())
         return;
-    socket->close();
+    iterator->second->close();
 }
 
 void NetworkRTCProvider::doSocketTaskOnRTCNetworkThread(LibWebRTCSocketIdentifier identifier, Function<void(Socket&)>&& callback)
 {
     callOnRTCNetworkThread([this, identifier, callback = WTFMove(callback)]() mutable {
-        if (auto* socket = m_sockets.get(identifier))
-            callback(*socket);
+        auto iterator = m_sockets.find(identifier);
+        if (iterator == m_sockets.end())
+            return;
+        callback(*iterator->second);
     });
 }
 
@@ -276,22 +277,34 @@
 void NetworkRTCProvider::setSocketOption(LibWebRTCSocketIdentifier identifier, int option, int value)
 {
     ASSERT(m_rtcNetworkThread.IsCurrent());
-    auto* socket = m_sockets.get(identifier);
-    if (!socket)
+    auto iterator = m_sockets.find(identifier);
+    if (iterator == m_sockets.end())
         return;
-    socket->setOption(option, value);
+    iterator->second->setOption(option, value);
 }
 
 void NetworkRTCProvider::addSocket(LibWebRTCSocketIdentifier identifier, std::unique_ptr<Socket>&& socket)
 {
     ASSERT(m_rtcNetworkThread.IsCurrent());
-    m_sockets.add(identifier, WTFMove(socket));
+    ASSERT(socket);
+    m_sockets.emplace(identifier, WTFMove(socket));
+    if (m_sockets.size() > maxSockets) {
+        auto socketIdentifierToClose = m_sockets.begin()->first;
+        closeSocket(socketIdentifierToClose);
+        ASSERT(m_sockets.find(socketIdentifierToClose) == m_sockets.end());
+    }
 }
 
 std::unique_ptr<NetworkRTCProvider::Socket> NetworkRTCProvider::takeSocket(LibWebRTCSocketIdentifier identifier)
 {
     ASSERT(m_rtcNetworkThread.IsCurrent());
-    return m_sockets.take(identifier);
+    auto iterator = m_sockets.find(identifier);
+    if (iterator == m_sockets.end())
+        return nullptr;
+
+    auto socket = WTFMove(iterator->second);
+    m_sockets.erase(iterator);
+    return socket;
 }
 
 void NetworkRTCProvider::newConnection(Socket& serverSocket, std::unique_ptr<rtc::AsyncPacketSocket>&& newSocket)
@@ -375,11 +388,11 @@
     callOnRTCNetworkThread([this, completionHandler = WTFMove(completionHandler)]() mutable {
         Vector<LibWebRTCSocketIdentifier> listeningSocketIdentifiers;
         for (auto& keyValue : m_sockets) {
-            if (keyValue.value->type() == Socket::Type::ServerTCP)
-                listeningSocketIdentifiers.append(keyValue.key);
+            if (keyValue.second->type() == Socket::Type::ServerTCP)
+                listeningSocketIdentifiers.append(keyValue.first);
         }
         for (auto id : listeningSocketIdentifiers)
-            m_sockets.get(id)->close();
+            m_sockets[id]->close();
 
         callOnMainRunLoop([provider = makeRef(*this), listeningSocketIdentifiers = WTFMove(listeningSocketIdentifiers), completionHandler = WTFMove(completionHandler)] {
             if (provider->m_connection) {

Modified: branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.h (284955 => 284956)


--- branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.h	2021-10-27 21:06:19 UTC (rev 284955)
+++ branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.h	2021-10-27 21:06:22 UTC (rev 284956)
@@ -38,6 +38,7 @@
 #include <webrtc/p2p/base/basic_packet_socket_factory.h>
 #include <webrtc/rtc_base/third_party/sigslot/sigslot.h>
 #include <wtf/HashMap.h>
+#include <wtf/StdMap.h>
 #include <wtf/ThreadSafeRefCounted.h>
 #include <wtf/UniqueRef.h>
 #include <wtf/text/WTFString.h>
@@ -63,6 +64,13 @@
 class NetworkSession;
 struct RTCPacketOptions;
 
+struct SocketComparator {
+    bool operator()(const WebCore::LibWebRTCSocketIdentifier& a, const WebCore::LibWebRTCSocketIdentifier& b) const
+    {
+        return a.toUInt64() < b.toUInt64();
+    }
+};
+
 class NetworkRTCProvider : public rtc::MessageHandler, public IPC::Connection::ThreadMessageReceiverRefCounted {
 public:
     static Ref<NetworkRTCProvider> create(NetworkConnectionToWebProcess& connection)
@@ -142,8 +150,10 @@
     const String& attributedBundleIdentifierFromPageIdentifier(WebPageProxyIdentifier);
 #endif
 
+    static constexpr size_t maxSockets { 256 };
+
     HashMap<LibWebRTCResolverIdentifier, std::unique_ptr<NetworkRTCResolver>> m_resolvers;
-    HashMap<WebCore::LibWebRTCSocketIdentifier, std::unique_ptr<Socket>> m_sockets;
+    StdMap<WebCore::LibWebRTCSocketIdentifier, std::unique_ptr<Socket>, SocketComparator> m_sockets;
     NetworkConnectionToWebProcess* m_connection;
     Ref<IPC::Connection> m_ipcConnection;
     bool m_isStarted { true };

Modified: branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm (284955 => 284956)


--- branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm	2021-10-27 21:06:19 UTC (rev 284955)
+++ branches/safari-612-branch/Source/WebKit/NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm	2021-10-27 21:06:22 UTC (rev 284956)
@@ -138,9 +138,8 @@
 
 void NetworkRTCTCPSocketCocoa::close()
 {
-    if (!m_nwConnection)
-        return;
-    nw_connection_cancel(m_nwConnection.get());
+    if (m_nwConnection)
+        nw_connection_cancel(m_nwConnection.get());
     m_rtcProvider.takeSocket(m_identifier);
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to