Title: [283797] trunk/Source/WebKit
Revision
283797
Author
[email protected]
Date
2021-10-08 05:30:02 -0700 (Fri, 08 Oct 2021)

Log Message

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:

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (283796 => 283797)


--- trunk/Source/WebKit/ChangeLog	2021-10-08 10:02:49 UTC (rev 283796)
+++ trunk/Source/WebKit/ChangeLog	2021-10-08 12:30:02 UTC (rev 283797)
@@ -1,5 +1,22 @@
 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-08  Youenn Fablet  <[email protected]>
+
         Add support for processing push events without service worker clients
         https://bugs.webkit.org/show_bug.cgi?id=231285
 

Modified: trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.cpp (283796 => 283797)


--- trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.cpp	2021-10-08 10:02:49 UTC (rev 283796)
+++ trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.cpp	2021-10-08 12:30:02 UTC (rev 283797)
@@ -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 = Ref { *this }, listeningSocketIdentifiers = WTFMove(listeningSocketIdentifiers), completionHandler = WTFMove(completionHandler)] {
             if (provider->m_connection) {

Modified: trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.h (283796 => 283797)


--- trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.h	2021-10-08 10:02:49 UTC (rev 283796)
+++ trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCProvider.h	2021-10-08 12:30:02 UTC (rev 283797)
@@ -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: trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm (283796 => 283797)


--- trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm	2021-10-08 10:02:49 UTC (rev 283796)
+++ trunk/Source/WebKit/NetworkProcess/webrtc/NetworkRTCTCPSocketCocoa.mm	2021-10-08 12:30:02 UTC (rev 283797)
@@ -136,9 +136,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