Unverified 提交 34336429 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Worker: Fix memory leak when using WebRtcServer with TCP enabled (#1389)

上级 98cf79eb
# Changelog # Changelog
### NEXT
- Worker: Fix memory leak when using `WebRtcServer` with TCP enabled ([PR #1389](https://github.com/versatica/mediasoup/pull/1389)).
### 3.14.4 ### 3.14.4
- Worker: Fix crash. RtcpFeedback parameter is optional ([PR #1387](https://github.com/versatica/mediasoup/pull/1387), credits to @Lynnworld). - Worker: Fix crash. `RtcpFeedback` parameter is optional ([PR #1387](https://github.com/versatica/mediasoup/pull/1387), credits to @Lynnworld).
### 3.14.3 ### 3.14.3
......
...@@ -131,6 +131,7 @@ namespace RTC ...@@ -131,6 +131,7 @@ namespace RTC
std::string password; std::string password;
uint16_t consentTimeoutMs{ 30000u }; uint16_t consentTimeoutMs{ 30000u };
// Others. // Others.
bool destroying{ false };
std::string oldUsernameFragment; std::string oldUsernameFragment;
std::string oldPassword; std::string oldPassword;
IceState state{ IceState::NEW }; IceState state{ IceState::NEW };
......
...@@ -51,29 +51,7 @@ namespace RTC ...@@ -51,29 +51,7 @@ namespace RTC
} }
public: public:
void Close() void CloseTcpConnection();
{
if (this->protocol == Protocol::UDP)
{
this->udpSocket->Close();
}
else
{
this->tcpConnection->Close();
}
}
bool IsClosed()
{
if (this->protocol == Protocol::UDP)
{
return this->udpSocket->IsClosed();
}
else
{
return this->tcpConnection->IsClosed();
}
}
flatbuffers::Offset<FBS::Transport::Tuple> FillBuffer(flatbuffers::FlatBufferBuilder& builder) const; flatbuffers::Offset<FBS::Transport::Tuple> FillBuffer(flatbuffers::FlatBufferBuilder& builder) const;
......
...@@ -22,9 +22,11 @@ public: ...@@ -22,9 +22,11 @@ public:
~SignalHandle(); ~SignalHandle();
public: public:
void Close();
void AddSignal(int signum, const std::string& name); void AddSignal(int signum, const std::string& name);
private:
void InternalClose();
/* Callbacks fired by UV events. */ /* Callbacks fired by UV events. */
public: public:
void OnUvSignal(int signum); void OnUvSignal(int signum);
......
...@@ -49,7 +49,7 @@ public: ...@@ -49,7 +49,7 @@ public:
virtual ~TcpConnectionHandle(); virtual ~TcpConnectionHandle();
public: public:
void Close(); void TriggerClose();
bool IsClosed() const bool IsClosed() const
{ {
return this->closed; return this->closed;
...@@ -110,6 +110,7 @@ public: ...@@ -110,6 +110,7 @@ public:
} }
private: private:
void InternalClose();
bool SetPeerAddress(); bool SetPeerAddress();
/* Callbacks fired by UV events. */ /* Callbacks fired by UV events. */
......
...@@ -17,7 +17,6 @@ public: ...@@ -17,7 +17,6 @@ public:
~TcpServerHandle() override; ~TcpServerHandle() override;
public: public:
void Close();
void Dump() const; void Dump() const;
const struct sockaddr* GetLocalAddress() const const struct sockaddr* GetLocalAddress() const
{ {
...@@ -48,6 +47,7 @@ protected: ...@@ -48,6 +47,7 @@ protected:
void AcceptTcpConnection(TcpConnectionHandle* connection); void AcceptTcpConnection(TcpConnectionHandle* connection);
private: private:
void InternalClose();
bool SetLocalAddress(); bool SetLocalAddress();
/* Pure virtual methods that must be implemented by the subclass. */ /* Pure virtual methods that must be implemented by the subclass. */
......
...@@ -23,7 +23,6 @@ public: ...@@ -23,7 +23,6 @@ public:
~TimerHandle(); ~TimerHandle();
public: public:
void Close();
void Start(uint64_t timeout, uint64_t repeat = 0); void Start(uint64_t timeout, uint64_t repeat = 0);
void Stop(); void Stop();
void Reset(); void Reset();
...@@ -41,6 +40,9 @@ public: ...@@ -41,6 +40,9 @@ public:
return uv_is_active(reinterpret_cast<uv_handle_t*>(this->uvHandle)) != 0; return uv_is_active(reinterpret_cast<uv_handle_t*>(this->uvHandle)) != 0;
} }
private:
void InternalClose();
/* Callbacks fired by UV events. */ /* Callbacks fired by UV events. */
public: public:
void OnUvTimer(); void OnUvTimer();
......
...@@ -42,7 +42,6 @@ public: ...@@ -42,7 +42,6 @@ public:
virtual ~UdpSocketHandle(); virtual ~UdpSocketHandle();
public: public:
void Close();
bool IsClosed() const bool IsClosed() const
{ {
return this->closed; return this->closed;
...@@ -80,6 +79,7 @@ public: ...@@ -80,6 +79,7 @@ public:
void SetRecvBufferSize(uint32_t size); void SetRecvBufferSize(uint32_t size);
private: private:
void InternalClose();
bool SetLocalAddress(); bool SetLocalAddress();
/* Callbacks fired by UV events. */ /* Callbacks fired by UV events. */
......
...@@ -247,7 +247,7 @@ namespace Channel ...@@ -247,7 +247,7 @@ namespace Channel
return free != nullptr; return free != nullptr;
} }
inline void ChannelSocket::SendImpl(const uint8_t* payload, uint32_t payloadLen) void ChannelSocket::SendImpl(const uint8_t* payload, uint32_t payloadLen)
{ {
MS_TRACE_STD(); MS_TRACE_STD();
......
...@@ -112,6 +112,8 @@ namespace RTC ...@@ -112,6 +112,8 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
this->destroying = true;
// Here we must notify the listener about the removal of current // Here we must notify the listener about the removal of current
// usernameFragments (and also the old one if any) and all tuples. // usernameFragments (and also the old one if any) and all tuples.
...@@ -223,6 +225,13 @@ namespace RTC ...@@ -223,6 +225,13 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// While IceServer is being destroyed, it may call listener methods that may
// end calling RemoveTuple(). We must ignore it to avoid double-free issues.
if (this->destroying)
{
return;
}
RTC::TransportTuple* removedTuple{ nullptr }; RTC::TransportTuple* removedTuple{ nullptr };
// Find the removed tuple. // Find the removed tuple.
......
...@@ -11,6 +11,8 @@ namespace RTC ...@@ -11,6 +11,8 @@ namespace RTC
TransportTuple::Protocol TransportTuple::ProtocolFromFbs(FBS::Transport::Protocol protocol) TransportTuple::Protocol TransportTuple::ProtocolFromFbs(FBS::Transport::Protocol protocol)
{ {
MS_TRACE();
switch (protocol) switch (protocol)
{ {
case FBS::Transport::Protocol::UDP: case FBS::Transport::Protocol::UDP:
...@@ -23,6 +25,8 @@ namespace RTC ...@@ -23,6 +25,8 @@ namespace RTC
FBS::Transport::Protocol TransportTuple::ProtocolToFbs(TransportTuple::Protocol protocol) FBS::Transport::Protocol TransportTuple::ProtocolToFbs(TransportTuple::Protocol protocol)
{ {
MS_TRACE();
switch (protocol) switch (protocol)
{ {
case TransportTuple::Protocol::UDP: case TransportTuple::Protocol::UDP:
...@@ -35,6 +39,18 @@ namespace RTC ...@@ -35,6 +39,18 @@ namespace RTC
/* Instance methods. */ /* Instance methods. */
void TransportTuple::CloseTcpConnection()
{
MS_TRACE();
if (this->protocol == Protocol::UDP)
{
MS_ABORT("cannot delete a UDP socket");
}
this->tcpConnection->TriggerClose();
}
flatbuffers::Offset<FBS::Transport::Tuple> TransportTuple::FillBuffer( flatbuffers::Offset<FBS::Transport::Tuple> TransportTuple::FillBuffer(
flatbuffers::FlatBufferBuilder& builder) const flatbuffers::FlatBufferBuilder& builder) const
{ {
......
...@@ -1180,9 +1180,9 @@ namespace RTC ...@@ -1180,9 +1180,9 @@ namespace RTC
} }
// If this is a TCP tuple, close its underlaying TCP connection. // If this is a TCP tuple, close its underlaying TCP connection.
if (tuple->GetProtocol() == RTC::TransportTuple::Protocol::TCP && !tuple->IsClosed()) if (tuple->GetProtocol() == RTC::TransportTuple::Protocol::TCP)
{ {
tuple->Close(); tuple->CloseTcpConnection();
} }
} }
......
...@@ -31,24 +31,7 @@ SignalHandle::~SignalHandle() ...@@ -31,24 +31,7 @@ SignalHandle::~SignalHandle()
if (!this->closed) if (!this->closed)
{ {
Close(); InternalClose();
}
}
void SignalHandle::Close()
{
MS_TRACE();
if (this->closed)
{
return;
}
this->closed = true;
for (auto* uvHandle : this->uvHandles)
{
uv_close(reinterpret_cast<uv_handle_t*>(uvHandle), static_cast<uv_close_cb>(onCloseSignal));
} }
} }
...@@ -86,7 +69,24 @@ void SignalHandle::AddSignal(int signum, const std::string& name) ...@@ -86,7 +69,24 @@ void SignalHandle::AddSignal(int signum, const std::string& name)
this->uvHandles.push_back(uvHandle); this->uvHandles.push_back(uvHandle);
} }
inline void SignalHandle::OnUvSignal(int signum) void SignalHandle::InternalClose()
{
MS_TRACE();
if (this->closed)
{
return;
}
this->closed = true;
for (auto* uvHandle : this->uvHandles)
{
uv_close(reinterpret_cast<uv_handle_t*>(uvHandle), static_cast<uv_close_cb>(onCloseSignal));
}
}
void SignalHandle::OnUvSignal(int signum)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -90,13 +90,13 @@ TcpConnectionHandle::~TcpConnectionHandle() ...@@ -90,13 +90,13 @@ TcpConnectionHandle::~TcpConnectionHandle()
if (!this->closed) if (!this->closed)
{ {
Close(); InternalClose();
} }
delete[] this->buffer; delete[] this->buffer;
} }
void TcpConnectionHandle::Close() void TcpConnectionHandle::TriggerClose()
{ {
MS_TRACE(); MS_TRACE();
...@@ -105,41 +105,9 @@ void TcpConnectionHandle::Close() ...@@ -105,41 +105,9 @@ void TcpConnectionHandle::Close()
return; return;
} }
int err; InternalClose();
this->closed = true;
// Tell the UV handle that the TcpConnectionHandle has been closed.
this->uvHandle->data = nullptr;
// Don't read more.
err = uv_read_stop(reinterpret_cast<uv_stream_t*>(this->uvHandle));
if (err != 0)
{
MS_ABORT("uv_read_stop() failed: %s", uv_strerror(err));
}
// If there is no error and the peer didn't close its connection side then close gracefully.
if (!this->hasError && !this->isClosedByPeer)
{
// Use uv_shutdown() so pending data to be written will be sent to the peer
// before closing.
auto* req = new uv_shutdown_t;
req->data = static_cast<void*>(this);
err = uv_shutdown(
req, reinterpret_cast<uv_stream_t*>(this->uvHandle), static_cast<uv_shutdown_cb>(onShutdown));
if (err != 0) this->listener->OnTcpConnectionClosed(this);
{
MS_ABORT("uv_shutdown() failed: %s", uv_strerror(err));
}
}
// Otherwise directly close the socket.
else
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onCloseTcp));
}
} }
void TcpConnectionHandle::Dump() const void TcpConnectionHandle::Dump() const
...@@ -366,11 +334,57 @@ void TcpConnectionHandle::ErrorReceiving() ...@@ -366,11 +334,57 @@ void TcpConnectionHandle::ErrorReceiving()
{ {
MS_TRACE(); MS_TRACE();
Close(); InternalClose();
this->listener->OnTcpConnectionClosed(this); this->listener->OnTcpConnectionClosed(this);
} }
void TcpConnectionHandle::InternalClose()
{
MS_TRACE();
if (this->closed)
{
return;
}
int err;
this->closed = true;
// Tell the UV handle that the TcpConnectionHandle has been closed.
this->uvHandle->data = nullptr;
// Don't read more.
err = uv_read_stop(reinterpret_cast<uv_stream_t*>(this->uvHandle));
if (err != 0)
{
MS_ABORT("uv_read_stop() failed: %s", uv_strerror(err));
}
// If there is no error and the peer didn't close its connection side then close gracefully.
if (!this->hasError && !this->isClosedByPeer)
{
// Use uv_shutdown() so pending data to be written will be sent to the peer
// before closing.
auto* req = new uv_shutdown_t;
req->data = static_cast<void*>(this);
err = uv_shutdown(
req, reinterpret_cast<uv_stream_t*>(this->uvHandle), static_cast<uv_shutdown_cb>(onShutdown));
if (err != 0)
{
MS_ABORT("uv_shutdown() failed: %s", uv_strerror(err));
}
}
// Otherwise directly close the socket.
else
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onCloseTcp));
}
}
bool TcpConnectionHandle::SetPeerAddress() bool TcpConnectionHandle::SetPeerAddress()
{ {
MS_TRACE(); MS_TRACE();
...@@ -450,7 +464,7 @@ inline void TcpConnectionHandle::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/ ...@@ -450,7 +464,7 @@ inline void TcpConnectionHandle::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/
this->isClosedByPeer = true; this->isClosedByPeer = true;
// Close server side of the connection. // Close server side of the connection.
Close(); InternalClose();
// Notify the listener. // Notify the listener.
this->listener->OnTcpConnectionClosed(this); this->listener->OnTcpConnectionClosed(this);
...@@ -463,7 +477,7 @@ inline void TcpConnectionHandle::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/ ...@@ -463,7 +477,7 @@ inline void TcpConnectionHandle::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/
this->hasError = true; this->hasError = true;
// Close server side of the connection. // Close server side of the connection.
Close(); InternalClose();
// Notify the listener. // Notify the listener.
this->listener->OnTcpConnectionClosed(this); this->listener->OnTcpConnectionClosed(this);
...@@ -497,7 +511,7 @@ inline void TcpConnectionHandle::OnUvWrite(int status, TcpConnectionHandle::onSe ...@@ -497,7 +511,7 @@ inline void TcpConnectionHandle::OnUvWrite(int status, TcpConnectionHandle::onSe
(*cb)(false); (*cb)(false);
} }
Close(); InternalClose();
this->listener->OnTcpConnectionClosed(this); this->listener->OnTcpConnectionClosed(this);
} }
......
...@@ -65,34 +65,10 @@ TcpServerHandle::~TcpServerHandle() ...@@ -65,34 +65,10 @@ TcpServerHandle::~TcpServerHandle()
if (!this->closed) if (!this->closed)
{ {
Close(); InternalClose();
} }
} }
void TcpServerHandle::Close()
{
MS_TRACE();
if (this->closed)
{
return;
}
this->closed = true;
// Tell the UV handle that the TcpServerHandle has been closed.
this->uvHandle->data = nullptr;
MS_DEBUG_DEV("closing %zu active connections", this->connections.size());
for (auto* connection : this->connections)
{
delete connection;
}
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onCloseTcp));
}
void TcpServerHandle::Dump() const void TcpServerHandle::Dump() const
{ {
MS_DUMP("<TcpServerHandle>"); MS_DUMP("<TcpServerHandle>");
...@@ -219,6 +195,30 @@ void TcpServerHandle::AcceptTcpConnection(TcpConnectionHandle* connection) ...@@ -219,6 +195,30 @@ void TcpServerHandle::AcceptTcpConnection(TcpConnectionHandle* connection)
this->connections.insert(connection); this->connections.insert(connection);
} }
void TcpServerHandle::InternalClose()
{
MS_TRACE();
if (this->closed)
{
return;
}
this->closed = true;
// Tell the UV handle that the TcpServerHandle has been closed.
this->uvHandle->data = nullptr;
MS_DEBUG_DEV("closing %zu active connections", this->connections.size());
for (auto* connection : this->connections)
{
delete connection;
}
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onCloseTcp));
}
bool TcpServerHandle::SetLocalAddress() bool TcpServerHandle::SetLocalAddress()
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -43,24 +43,10 @@ TimerHandle::~TimerHandle() ...@@ -43,24 +43,10 @@ TimerHandle::~TimerHandle()
if (!this->closed) if (!this->closed)
{ {
Close(); InternalClose();
} }
} }
void TimerHandle::Close()
{
MS_TRACE();
if (this->closed)
{
return;
}
this->closed = true;
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onCloseTimer));
}
void TimerHandle::Start(uint64_t timeout, uint64_t repeat) void TimerHandle::Start(uint64_t timeout, uint64_t repeat)
{ {
MS_TRACE(); MS_TRACE();
...@@ -154,6 +140,20 @@ void TimerHandle::Restart() ...@@ -154,6 +140,20 @@ void TimerHandle::Restart()
} }
} }
void TimerHandle::InternalClose()
{
MS_TRACE();
if (this->closed)
{
return;
}
this->closed = true;
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onCloseTimer));
}
inline void TimerHandle::OnUvTimer() inline void TimerHandle::OnUvTimer()
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -103,35 +103,10 @@ UdpSocketHandle::~UdpSocketHandle() ...@@ -103,35 +103,10 @@ UdpSocketHandle::~UdpSocketHandle()
if (!this->closed) if (!this->closed)
{ {
Close(); InternalClose();
} }
} }
void UdpSocketHandle::Close()
{
MS_TRACE();
if (this->closed)
{
return;
}
this->closed = true;
// Tell the UV handle that the UdpSocketHandle has been closed.
this->uvHandle->data = nullptr;
// Don't read more.
const int err = uv_udp_recv_stop(this->uvHandle);
if (err != 0)
{
MS_ABORT("uv_udp_recv_stop() failed: %s", uv_strerror(err));
}
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onCloseUdp));
}
void UdpSocketHandle::Dump() const void UdpSocketHandle::Dump() const
{ {
MS_DUMP("<UdpSocketHandle>"); MS_DUMP("<UdpSocketHandle>");
...@@ -338,6 +313,31 @@ void UdpSocketHandle::SetRecvBufferSize(uint32_t size) ...@@ -338,6 +313,31 @@ void UdpSocketHandle::SetRecvBufferSize(uint32_t size)
} }
} }
void UdpSocketHandle::InternalClose()
{
MS_TRACE();
if (this->closed)
{
return;
}
this->closed = true;
// Tell the UV handle that the UdpSocketHandle has been closed.
this->uvHandle->data = nullptr;
// Don't read more.
const int err = uv_udp_recv_stop(this->uvHandle);
if (err != 0)
{
MS_ABORT("uv_udp_recv_stop() failed: %s", uv_strerror(err));
}
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onCloseUdp));
}
bool UdpSocketHandle::SetLocalAddress() bool UdpSocketHandle::SetLocalAddress()
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -126,14 +126,16 @@ UnixStreamSocketHandle::~UnixStreamSocketHandle() ...@@ -126,14 +126,16 @@ UnixStreamSocketHandle::~UnixStreamSocketHandle()
{ {
MS_TRACE_STD(); MS_TRACE_STD();
delete[] this->buffer;
if (!this->closed) if (!this->closed)
{ {
Close(); Close();
} }
delete[] this->buffer;
} }
// NOTE: In UnixStreamSocketHandle we need a poublic Close() method and cannot
// just rely on the destructor plus a private InternalClose() method.
void UnixStreamSocketHandle::Close() void UnixStreamSocketHandle::Close()
{ {
MS_TRACE_STD(); MS_TRACE_STD();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论