Skip to content

Commit

Permalink
Fix missing increment of the instance count on every call to startup()
Browse files Browse the repository at this point in the history
Change how the implicit startup is tracked.
The extra change is needed since the startup() calls in
srt::CUDT::socket()
and
srt::CUDT::createGroup
would endlessly increment the startup counter as sockets were created
otherwise.

Force a clean() when the singleton is getting destructed.
Previously it just did a single decrement of the instance count,
which could result in attempting to exit without actually cleaning up.

fixes #3098
  • Loading branch information
mbechard committed Jan 10, 2025
1 parent 8a89a3a commit 6e4537c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
40 changes: 26 additions & 14 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,8 @@ srt::CUDTUnited::CUDTUnited()

srt::CUDTUnited::~CUDTUnited()
{
// Call it if it wasn't called already.
// This will happen at the end of main() of the application,
// when the user didn't call srt_cleanup().
if (m_bGCStatus)
{
cleanup();
}
// force cleanup, even if the instance count is > 1
cleanup(true);

releaseMutex(m_GlobControlLock);
releaseMutex(m_IDLock);
Expand Down Expand Up @@ -233,15 +228,24 @@ string srt::CUDTUnited::CONID(SRTSOCKET sock)
return os.str();
}

int srt::CUDTUnited::startup()
void srt::CUDTUnited::implicitStartup()
{
startup(true);
}

int srt::CUDTUnited::startup(bool implicit)
{
ScopedLock gcinit(m_InitLock);
if (m_bGCStatus)

if (implicit && m_iInstanceCount > 0)
return 1;

if (m_iInstanceCount++ > 0)
return 1;

if (m_bGCStatus)
return 1;

// Global initialization code
#ifdef _WIN32
WORD wVersionRequested;
Expand All @@ -268,7 +272,7 @@ int srt::CUDTUnited::startup()
return 0;
}

int srt::CUDTUnited::cleanup()
int srt::CUDTUnited::cleanup(bool force)
{
// IMPORTANT!!!
// In this function there must be NO LOGGING AT ALL. This function may
Expand All @@ -280,8 +284,14 @@ int srt::CUDTUnited::cleanup()
// executing this procedure.
ScopedLock gcinit(m_InitLock);

if (--m_iInstanceCount > 0)
return 0;
if (!force)
{
if (m_iInstanceCount == 0)
return 0;

if (--m_iInstanceCount > 0)
return 0;
}

if (!m_bGCStatus)
return 0;
Expand All @@ -298,6 +308,8 @@ int srt::CUDTUnited::cleanup()
CSync::notify_one_relaxed(m_GCStopCond);
m_GCThread.join();

if (force)
m_iInstanceCount = 0;
m_bGCStatus = false;

// Global destruction code
Expand Down Expand Up @@ -3469,7 +3481,7 @@ int srt::CUDT::cleanup()

SRTSOCKET srt::CUDT::socket()
{
uglobal().startup();
uglobal().implicitStartup();

try
{
Expand Down Expand Up @@ -3519,7 +3531,7 @@ srt::CUDTGroup& srt::CUDT::newGroup(const int type)
SRTSOCKET srt::CUDT::createGroup(SRT_GROUP_TYPE gt)
{
// Doing the same lazy-startup as with srt_create_socket()
uglobal().startup();
uglobal().implicitStartup();

try
{
Expand Down
7 changes: 5 additions & 2 deletions srtcore/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,14 @@ class CUDTUnited
static std::string CONID(SRTSOCKET sock);

/// initialize the UDT library.
/// @param [in] implicit should be set to true for internal implicit startup() calls.
/// @return 0 if success, otherwise -1 is returned.
int startup();
int startup(bool implicit = false);

/// release the UDT library.
/// @param [in] force cleanup regardless of the instance count.
/// @return 0 if success, otherwise -1 is returned.
int cleanup();
int cleanup(bool force = false);

/// Create a new UDT socket.
/// @param [out] pps Variable (optional) to which the new socket will be written, if succeeded
Expand Down Expand Up @@ -428,6 +430,7 @@ class CUDTUnited
CUDTSocket* locateSocket_LOCKED(SRTSOCKET u);
CUDTSocket* locatePeer(const sockaddr_any& peer, const SRTSOCKET id, int32_t isn);

void implicitStartup();
#if ENABLE_BONDING
CUDTGroup* locateAcquireGroup(SRTSOCKET u, ErrorHandling erh = ERH_RETURN);
CUDTGroup* acquireSocketsGroup(CUDTSocket* s);
Expand Down

0 comments on commit 6e4537c

Please sign in to comment.