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

Fix DepUsrSCTP::Checker::timer not being freed (#576)

上级 6b6d8cec
...@@ -31,6 +31,8 @@ private: ...@@ -31,6 +31,8 @@ private:
public: public:
static void ClassInit(); static void ClassInit();
static void ClassDestroy(); static void ClassDestroy();
static void CreateChecker();
static void CloseChecker();
static uintptr_t GetNextSctpAssociationId(); static uintptr_t GetNextSctpAssociationId();
static void RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation); static void RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation);
static void DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation); static void DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation);
......
...@@ -9,6 +9,27 @@ ...@@ -9,6 +9,27 @@
thread_local uv_loop_t* DepLibUV::loop{ nullptr }; thread_local uv_loop_t* DepLibUV::loop{ nullptr };
/* Static methods for UV callbacks. */
inline static void onClose(uv_handle_t* handle)
{
delete handle;
}
inline static void onWalk(uv_handle_t* handle, void* arg)
{
// Must use MS_ERROR_STD since at this point the Channel is already closed.
MS_ERROR_STD(
"alive UV handle found (this shouldn't happen) [type:%d, active:%d, closing:%d, has_ref:%d]",
handle->type,
uv_is_active(handle),
uv_is_closing(handle),
uv_has_ref(handle));
if (!uv_is_closing(handle))
uv_close(handle, onClose);
}
/* Static methods. */ /* Static methods. */
void DepLibUV::ClassInit() void DepLibUV::ClassInit()
...@@ -20,18 +41,37 @@ void DepLibUV::ClassInit() ...@@ -20,18 +41,37 @@ void DepLibUV::ClassInit()
int err = uv_loop_init(DepLibUV::loop); int err = uv_loop_init(DepLibUV::loop);
if (err != 0) if (err != 0)
MS_ABORT("libuv initialization failed"); MS_ABORT("libuv loop initialization failed");
} }
void DepLibUV::ClassDestroy() void DepLibUV::ClassDestroy()
{ {
MS_TRACE(); MS_TRACE();
if (DepLibUV::loop != nullptr) // Here we should not have any UV handle left. All them should have been
// already closed+freed. However, in order to not introduce regressions
// in the future, we check this anyway.
// More context: https://github.com/versatica/mediasoup/pull/576
int err;
uv_stop(DepLibUV::loop);
uv_walk(DepLibUV::loop, onWalk, nullptr);
while (true)
{ {
uv_loop_close(DepLibUV::loop); err = uv_loop_close(DepLibUV::loop);
delete DepLibUV::loop;
if (err != UV_EBUSY)
break;
uv_run(DepLibUV::loop, UV_RUN_NOWAIT);
} }
if (err != 0)
MS_ERROR_STD("failed to close libuv loop: %s", uv_err_name(err));
delete DepLibUV::loop;
} }
void DepLibUV::PrintVersion() void DepLibUV::PrintVersion()
......
...@@ -82,8 +82,6 @@ void DepUsrSCTP::ClassInit() ...@@ -82,8 +82,6 @@ void DepUsrSCTP::ClassInit()
++globalInstances; ++globalInstances;
} }
DepUsrSCTP::checker = new DepUsrSCTP::Checker();
} }
void DepUsrSCTP::ClassDestroy() void DepUsrSCTP::ClassDestroy()
...@@ -97,13 +95,31 @@ void DepUsrSCTP::ClassDestroy() ...@@ -97,13 +95,31 @@ void DepUsrSCTP::ClassDestroy()
if (globalInstances == 0) if (globalInstances == 0)
{ {
usrsctp_finish(); usrsctp_finish();
// TODO: This cleanup currently causes assertion errors in
// DepUsrSCTP::DeregisterSctpAssociation()
// TODO: This cleanup currently causes assertion errors in
// DepUsrSCTP::DeregisterSctpAssociation() when using mediasoup-worker
// in thread mode (Rust).
//
// numSctpAssociations = 0u; nextSctpAssociationId = 0u; // numSctpAssociations = 0u; nextSctpAssociationId = 0u;
// DepUsrSCTP::mapIdSctpAssociation.clear(); // DepUsrSCTP::mapIdSctpAssociation.clear();
} }
} }
}
void DepUsrSCTP::CreateChecker()
{
MS_TRACE();
MS_ASSERT(DepUsrSCTP::checker == nullptr, "Checker already created");
DepUsrSCTP::checker = new DepUsrSCTP::Checker();
}
void DepUsrSCTP::CloseChecker()
{
MS_TRACE();
MS_ASSERT(DepUsrSCTP::checker != nullptr, "Checker not created");
delete DepUsrSCTP::checker; delete DepUsrSCTP::checker;
} }
...@@ -138,6 +154,8 @@ void DepUsrSCTP::RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation) ...@@ -138,6 +154,8 @@ void DepUsrSCTP::RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation)
std::lock_guard<std::mutex> lock(globalSyncMutex); std::lock_guard<std::mutex> lock(globalSyncMutex);
MS_ASSERT(DepUsrSCTP::checker != nullptr, "Checker not created");
auto it = DepUsrSCTP::mapIdSctpAssociation.find(sctpAssociation->id); auto it = DepUsrSCTP::mapIdSctpAssociation.find(sctpAssociation->id);
MS_ASSERT( MS_ASSERT(
...@@ -156,6 +174,8 @@ void DepUsrSCTP::DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation ...@@ -156,6 +174,8 @@ void DepUsrSCTP::DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation
std::lock_guard<std::mutex> lock(globalSyncMutex); std::lock_guard<std::mutex> lock(globalSyncMutex);
MS_ASSERT(DepUsrSCTP::checker != nullptr, "Checker not created");
auto found = DepUsrSCTP::mapIdSctpAssociation.erase(sctpAssociation->id); auto found = DepUsrSCTP::mapIdSctpAssociation.erase(sctpAssociation->id);
MS_ASSERT(found > 0, "SctpAssociation not found"); MS_ASSERT(found > 0, "SctpAssociation not found");
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Worker.hpp" #include "Worker.hpp"
#include "DepLibUV.hpp" #include "DepLibUV.hpp"
#include "DepUsrSCTP.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Settings.hpp" #include "Settings.hpp"
...@@ -32,6 +33,9 @@ Worker::Worker(::Channel::ChannelSocket* channel, PayloadChannel::PayloadChannel ...@@ -32,6 +33,9 @@ Worker::Worker(::Channel::ChannelSocket* channel, PayloadChannel::PayloadChannel
} }
#endif #endif
// Create the Checker instance in DepUsrSCTP.
DepUsrSCTP::CreateChecker();
// Tell the Node process that we are running. // Tell the Node process that we are running.
Channel::ChannelNotifier::Emit(std::to_string(Logger::pid), "running"); Channel::ChannelNotifier::Emit(std::to_string(Logger::pid), "running");
...@@ -75,6 +79,9 @@ void Worker::Close() ...@@ -75,6 +79,9 @@ void Worker::Close()
} }
this->mapRouters.clear(); this->mapRouters.clear();
// Close the Checker instance in DepUsrSCTP.
DepUsrSCTP::CloseChecker();
// Close the Channel. // Close the Channel.
this->channel->Close(); this->channel->Close();
......
...@@ -33,6 +33,7 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]") ...@@ -33,6 +33,7 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]")
keyFrameRequestManager.KeyFrameNeeded(1111); keyFrameRequestManager.KeyFrameNeeded(1111);
// Must run the loop here to consume the timer before doing the check.
DepLibUV::RunLoop(); DepLibUV::RunLoop();
REQUIRE(listener.onKeyFrameNeededTimesCalled == 2); REQUIRE(listener.onKeyFrameNeededTimesCalled == 2);
...@@ -48,6 +49,7 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]") ...@@ -48,6 +49,7 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]")
keyFrameRequestManager.KeyFrameNeeded(1111); keyFrameRequestManager.KeyFrameNeeded(1111);
keyFrameRequestManager.KeyFrameNeeded(1111); keyFrameRequestManager.KeyFrameNeeded(1111);
// Must run the loop here to consume the timer before doing the check.
DepLibUV::RunLoop(); DepLibUV::RunLoop();
REQUIRE(listener.onKeyFrameNeededTimesCalled == 2); REQUIRE(listener.onKeyFrameNeededTimesCalled == 2);
...@@ -61,6 +63,7 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]") ...@@ -61,6 +63,7 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]")
keyFrameRequestManager.KeyFrameNeeded(1111); keyFrameRequestManager.KeyFrameNeeded(1111);
keyFrameRequestManager.KeyFrameReceived(1111); keyFrameRequestManager.KeyFrameReceived(1111);
// Must run the loop here to consume the timer before doing the check.
DepLibUV::RunLoop(); DepLibUV::RunLoop();
REQUIRE(listener.onKeyFrameNeededTimesCalled == 1); REQUIRE(listener.onKeyFrameNeededTimesCalled == 1);
...@@ -74,6 +77,7 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]") ...@@ -74,6 +77,7 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]")
keyFrameRequestManager.KeyFrameNeeded(1111); keyFrameRequestManager.KeyFrameNeeded(1111);
keyFrameRequestManager.ForceKeyFrameNeeded(1111); keyFrameRequestManager.ForceKeyFrameNeeded(1111);
// Must run the loop here to consume the timer before doing the check.
DepLibUV::RunLoop(); DepLibUV::RunLoop();
REQUIRE(listener.onKeyFrameNeededTimesCalled == 3); REQUIRE(listener.onKeyFrameNeededTimesCalled == 3);
...@@ -88,8 +92,14 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]") ...@@ -88,8 +92,14 @@ SCENARIO("KeyFrameRequestManager", "[rtp][keyframe]")
keyFrameRequestManager.ForceKeyFrameNeeded(1111); keyFrameRequestManager.ForceKeyFrameNeeded(1111);
keyFrameRequestManager.KeyFrameReceived(1111); keyFrameRequestManager.KeyFrameReceived(1111);
// Must run the loop here to consume the timer before doing the check.
DepLibUV::RunLoop(); DepLibUV::RunLoop();
REQUIRE(listener.onKeyFrameNeededTimesCalled == 2); REQUIRE(listener.onKeyFrameNeededTimesCalled == 2);
} }
// Must run the loop to wait for UV timers and close them.
// NOTE: Not really needed in this file since we run it in each SECTION in
// purpose.
DepLibUV::RunLoop();
} }
#include "common.hpp" #include "common.hpp"
#include "DepLibUV.hpp"
#include "RTC/Codecs/PayloadDescriptorHandler.hpp" #include "RTC/Codecs/PayloadDescriptorHandler.hpp"
#include "RTC/NackGenerator.hpp" #include "RTC/NackGenerator.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
...@@ -278,4 +279,7 @@ SCENARIO("NACK generator", "[rtp][rtcp]") ...@@ -278,4 +279,7 @@ SCENARIO("NACK generator", "[rtp][rtcp]")
validate(inputs); validate(inputs);
} }
// Must run the loop to wait for UV timers and close them.
DepLibUV::RunLoop();
} }
#include "common.hpp" #include "common.hpp"
#include "DepLibUV.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
#include "RTC/RtpStream.hpp" #include "RTC/RtpStream.hpp"
#include "RTC/RtpStreamRecv.hpp" #include "RTC/RtpStreamRecv.hpp"
...@@ -201,5 +202,8 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]") ...@@ -201,5 +202,8 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]")
rtpStream.ReceivePacket(packet); rtpStream.ReceivePacket(packet);
} }
// Must run the loop to wait for UV timers and close them.
DepLibUV::RunLoop();
delete packet; delete packet;
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论