diff --git a/worker/include/RTC/WebRtcServer.hpp b/worker/include/RTC/WebRtcServer.hpp index 96bde6e277..11f3901dfb 100644 --- a/worker/include/RTC/WebRtcServer.hpp +++ b/worker/include/RTC/WebRtcServer.hpp @@ -106,6 +106,8 @@ namespace RTC absl::flat_hash_map mapLocalIceUsernameFragmentWebRtcTransport; // Map of WebRtcTransports indexed by TransportTuple.hash. absl::flat_hash_map mapTupleWebRtcTransport; + // Whether the destructor has been called. + bool closing{ false }; }; } // namespace RTC diff --git a/worker/src/RTC/WebRtcServer.cpp b/worker/src/RTC/WebRtcServer.cpp index 1cd27188b5..274391d922 100644 --- a/worker/src/RTC/WebRtcServer.cpp +++ b/worker/src/RTC/WebRtcServer.cpp @@ -232,6 +232,8 @@ namespace RTC { MS_TRACE(); + this->closing = true; + this->shared->channelMessageRegistrator->UnregisterHandler(this->id); for (auto& item : this->udpSocketOrTcpServers) @@ -497,7 +499,14 @@ namespace RTC this->webRtcTransports.find(webRtcTransport) != this->webRtcTransports.end(), "WebRtcTransport not handled"); - this->webRtcTransports.erase(webRtcTransport); + // NOTE: If WebRtcServer is closing then do not remove the transport from + // the set since it would modify the set while the WebRtcServer destructor + // is iterating it. + // See: https://github.com/versatica/mediasoup/pull/1369#issuecomment-2044672247 + if (!this->closing) + { + this->webRtcTransports.erase(webRtcTransport); + } } inline void WebRtcServer::OnWebRtcTransportLocalIceUsernameFragmentAdded(