Skip to content

Commit

Permalink
properly close the uv_async_t handle
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Mar 6, 2024
1 parent a62fe3c commit 1c2b236
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
18 changes: 3 additions & 15 deletions worker/src/DepUsrSCTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ static size_t GlobalInstances{ 0u };
inline static void onAsync(uv_async_t* handle)
{
MS_TRACE();
MS_DUMP_STD("---------- onAsync!!");

const std::lock_guard<std::mutex> lock(GlobalSyncMutex);

Expand All @@ -41,8 +40,6 @@ inline static void onAsync(uv_async_t* handle)
auto* data = store->data;
auto len = store->len;

MS_DUMP_STD("---------- onAsync, sending SCTP data!!");

sctpAssociation->OnUsrSctpSendSctpData(data, len);

// Must delete the mem copied data once sent.
Expand All @@ -55,8 +52,6 @@ inline static int onSendSctpData(void* addr, void* data, size_t len, uint8_t /*t
{
MS_TRACE();

MS_DUMP_STD("---------- onSendSctpData!!");

auto* sctpAssociation = DepUsrSCTP::RetrieveSctpAssociation(reinterpret_cast<uintptr_t>(addr));

if (!sctpAssociation)
Expand Down Expand Up @@ -104,7 +99,6 @@ void DepUsrSCTP::ClassInit()
{
MS_TRACE();

MS_DUMP_STD("---------- DepUsrSCTP::ClassInit()");
MS_DEBUG_TAG(info, "usrsctp");

const std::lock_guard<std::mutex> lock(GlobalSyncMutex);
Expand All @@ -128,8 +122,6 @@ void DepUsrSCTP::ClassDestroy()
{
MS_TRACE();

MS_DUMP_STD("---------- DepUsrSCTP::ClassDestroy()");

const std::lock_guard<std::mutex> lock(GlobalSyncMutex);

--GlobalInstances;
Expand Down Expand Up @@ -196,8 +188,6 @@ void DepUsrSCTP::RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation)
{
MS_TRACE();

MS_DUMP_STD("------ DepUsrSCTP::RegisterSctpAssociation()");

const std::lock_guard<std::mutex> lock(GlobalSyncMutex);

MS_ASSERT(DepUsrSCTP::checker != nullptr, "Checker not created");
Expand All @@ -212,7 +202,7 @@ void DepUsrSCTP::RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation)
it2 == DepUsrSCTP::mapAsyncHandlerSendSctpData.end(),
"the id of the SctpAssociation is already in the mapAsyncHandlerSendSctpData map");

DepUsrSCTP::mapIdSctpAssociation[sctpAssociation->id] = sctpAssociation;
DepUsrSCTP::mapIdSctpAssociation[sctpAssociation->id] = sctpAssociation;
DepUsrSCTP::mapAsyncHandlerSendSctpData[sctpAssociation->GetAsyncHandle()] = { sctpAssociation };

sctpAssociation->InitializeSyncHandle(onAsync);
Expand All @@ -227,8 +217,6 @@ void DepUsrSCTP::DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation
{
MS_TRACE();

MS_DUMP_STD("------ DepUsrSCTP::DeregisterSctpAssociation() !!!!!!!!");

const std::lock_guard<std::mutex> lock(GlobalSyncMutex);

MS_ASSERT(DepUsrSCTP::checker != nullptr, "Checker not created");
Expand Down Expand Up @@ -283,8 +271,8 @@ void DepUsrSCTP::SendSctpData(RTC::SctpAssociation* sctpAssociation, uint8_t* da
// the callback execution finishes. So we have to mem copy it.
// TODO: This must be freed, but I'd prefer if we used a static thread_local
// buffer, but I don't know max size of this (if any).
store.data = new uint8_t[len];
store.len = len;
store.data = new uint8_t[len];
store.len = len;

std::memcpy(store.data, data, len);

Expand Down
14 changes: 10 additions & 4 deletions worker/src/RTC/SctpAssociation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const uint16_t EventTypes[] =
};
/* clang-format on */

/* Static methods for UV callbacks. */

inline static void onCloseAsync(uv_handle_t* handle)
{
delete reinterpret_cast<uv_async_t*>(handle);
}

/* Static methods for usrsctp callbacks. */

inline static int onRecvSctpData(
Expand Down Expand Up @@ -287,7 +294,6 @@ namespace RTC
SctpAssociation::~SctpAssociation()
{
MS_TRACE();
MS_DUMP_STD("----------------- SctpAssociation destructor!!!");

usrsctp_set_ulpinfo(this->socket, nullptr);
usrsctp_close(this->socket);
Expand All @@ -298,9 +304,9 @@ namespace RTC
// Register the SctpAssociation from the global map.
DepUsrSCTP::DeregisterSctpAssociation(this);

MS_DUMP_STD("---- HERE we should delete the handler but if so the app crashes");
// TODO: We should delete teh async handle, but then a crash happens.
// delete this->uvAsyncHandle;
uv_close(
reinterpret_cast<uv_handle_t*>(this->uvAsyncHandle), static_cast<uv_close_cb>(onCloseAsync));

delete[] this->messageBuffer;
}

Expand Down

0 comments on commit 1c2b236

Please sign in to comment.