From 4fea27e9e5ba34be07a061e4e9ceba7367440ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Fri, 3 May 2024 13:30:30 +0300 Subject: [PATCH 1/2] Fix Worker crash when closing WebRtcServer with active WebRtcTransports Fixes #1388 ### Details - `WebRtcServer` destructor must first close its `WebRtcTransports` and then the UDP sockets and TCP servers (so also its TCP connections). Otherwise those `WebRtcTransports` will try to send DTLS Close Alert on an already closed/freed `UdpSocketHandle` or `TcpConnectionHandle`. ### Steps to verify 1. Have `WebRtcServer` running and a client connected. 2. In server side call `webRtcServer.close()`. 3. It should not crash anymore. --- worker/src/RTC/WebRtcServer.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/worker/src/RTC/WebRtcServer.cpp b/worker/src/RTC/WebRtcServer.cpp index 274391d922..c088564546 100644 --- a/worker/src/RTC/WebRtcServer.cpp +++ b/worker/src/RTC/WebRtcServer.cpp @@ -236,6 +236,15 @@ namespace RTC this->shared->channelMessageRegistrator->UnregisterHandler(this->id); + // NOTE: We need to close WebRtcTransports first since they may need to + // send DTLS Close Alert so UDP sockets and TCP connections must remain + // open. + for (auto* webRtcTransport : this->webRtcTransports) + { + webRtcTransport->ListenServerClosed(); + } + this->webRtcTransports.clear(); + for (auto& item : this->udpSocketOrTcpServers) { delete item.udpSocket; @@ -245,12 +254,6 @@ namespace RTC item.tcpServer = nullptr; } this->udpSocketOrTcpServers.clear(); - - for (auto* webRtcTransport : this->webRtcTransports) - { - webRtcTransport->ListenServerClosed(); - } - this->webRtcTransports.clear(); } flatbuffers::Offset WebRtcServer::FillBuffer( From 8aaff4e483565f3e4af97560fbddc0c56e0ae9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Fri, 3 May 2024 13:31:47 +0300 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f38e77021..8d335b3c08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### NEXT - Worker: Fix memory leak when using `WebRtcServer` with TCP enabled ([PR #1389](https://github.com/versatica/mediasoup/pull/1389)). +- Worker: Fix crash when closing `WebRtcServer` with active `WebRtcTransports` ([PR #1390](https://github.com/versatica/mediasoup/pull/1390)). ### 3.14.4