Unverified 提交 3b8c06ed authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi 提交者: GitHub

Fix memory leaks on error exit, minor tweaks (#581)

* Fix memory leaks in case worker exits with error

* Skip delay on shutdown in library mode

* Unlock SCTP cleanup, it no longer causes issues

* Remove output from build cache to fix CI
上级 070643c7
......@@ -32,8 +32,6 @@ jobs:
with:
path: |
~/.npm
worker/deps
worker/out
key: ${{ matrix.os }}-node-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ matrix.os }}-node-
......
......@@ -61,8 +61,6 @@ jobs:
with:
path: |
~/.npm
worker/deps
worker/out
key: ${{ matrix.build.os }}-node-${{matrix.build.cc}}-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ matrix.build.os }}-node-${{matrix.build.cc}}-
......
......@@ -20,8 +20,8 @@ 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,
"alive UV handle found (this shouldn't happen) [type:%s, active:%d, closing:%d, has_ref:%d]",
uv_handle_type_name(handle->type),
uv_is_active(handle),
uv_is_closing(handle),
uv_has_ref(handle));
......
......@@ -96,12 +96,10 @@ void DepUsrSCTP::ClassDestroy()
{
usrsctp_finish();
// TODO: This cleanup currently causes assertion errors in
// DepUsrSCTP::DeregisterSctpAssociation() when using mediasoup-worker
// in thread mode (Rust).
//
// numSctpAssociations = 0u; nextSctpAssociationId = 0u;
// DepUsrSCTP::mapIdSctpAssociation.clear();
numSctpAssociations = 0u;
nextSctpAssociationId = 0u;
DepUsrSCTP::mapIdSctpAssociation.clear();
}
}
}
......
......@@ -48,12 +48,6 @@ Worker::~Worker()
{
MS_TRACE();
// Delete the Channel.
delete this->channel;
// Delete the PayloadChannel.
delete this->payloadChannel;
if (!this->closed)
Close();
}
......
......@@ -40,37 +40,44 @@ extern "C" int run_worker(
// Initialize libuv stuff (we need it for the Channel).
DepLibUV::ClassInit();
// Channel socket (it will be handled and deleted by the Worker).
Channel::ChannelSocket* channel{ nullptr };
// Channel socket.
std::unique_ptr<Channel::ChannelSocket> channel{ nullptr };
// PayloadChannel socket (it will be handled and deleted by the Worker).
PayloadChannel::PayloadChannelSocket* payloadChannel{ nullptr };
// PayloadChannel socket.
std::unique_ptr<PayloadChannel::PayloadChannelSocket> payloadChannel{ nullptr };
try
{
channel = new Channel::ChannelSocket(consumerChannelFd, producerChannelFd);
channel.reset(new Channel::ChannelSocket(consumerChannelFd, producerChannelFd));
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("error creating the Channel: %s", error.what());
DepLibUV::RunLoop();
DepLibUV::ClassDestroy();
return 1;
}
try
{
payloadChannel =
new PayloadChannel::PayloadChannelSocket(payloadConsumeChannelFd, payloadProduceChannelFd);
payloadChannel.reset(
new PayloadChannel::PayloadChannelSocket(payloadConsumeChannelFd, payloadProduceChannelFd));
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("error creating the RTC Channel: %s", error.what());
channel->Close();
DepLibUV::RunLoop();
DepLibUV::ClassDestroy();
return 1;
}
// Initialize the Logger.
Logger::ClassInit(channel);
Logger::ClassInit(channel.get());
try
{
......@@ -80,6 +87,11 @@ extern "C" int run_worker(
{
MS_ERROR_STD("settings error: %s", error.what());
channel->Close();
payloadChannel->Close();
DepLibUV::RunLoop();
DepLibUV::ClassDestroy();
// 42 is a custom exit code to notify "settings error" to the Node library.
return 42;
}
......@@ -87,6 +99,11 @@ extern "C" int run_worker(
{
MS_ERROR_STD("unexpected settings error: %s", error.what());
channel->Close();
payloadChannel->Close();
DepLibUV::RunLoop();
DepLibUV::ClassDestroy();
return 1;
}
......@@ -121,18 +138,16 @@ extern "C" int run_worker(
Utils::Crypto::ClassInit();
RTC::DtlsTransport::ClassInit();
RTC::SrtpSession::ClassInit();
Channel::ChannelNotifier::ClassInit(channel);
PayloadChannel::PayloadChannelNotifier::ClassInit(payloadChannel);
Channel::ChannelNotifier::ClassInit(channel.get());
PayloadChannel::PayloadChannelNotifier::ClassInit(payloadChannel.get());
#ifdef MS_EXECUTABLE
{
// Ignore some signals.
IgnoreSignals();
}
// Ignore some signals.
IgnoreSignals();
#endif
// Run the Worker.
Worker worker(channel, payloadChannel);
Worker worker(channel.get(), payloadChannel.get());
// Free static stuff.
DepLibSRTP::ClassDestroy();
......@@ -142,9 +157,11 @@ extern "C" int run_worker(
DepUsrSCTP::ClassDestroy();
DepLibUV::ClassDestroy();
#ifdef MS_EXECUTABLE
// Wait a bit so pending messages to stdout/Channel arrive to the Node
// process.
uv_sleep(200);
#endif
return 0;
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论