Skip to content

Commit

Permalink
fix missing increment of the reference count on every call to startup()
Browse files Browse the repository at this point in the history
also 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.

This also adds handling for the case where the user code fails to call
srt_startup when initially using the library, but does later on.
It better matches up the implicit startup() with an implicit cleanup().

fixes #3098
  • Loading branch information
mbechard committed Jan 6, 2025
1 parent 8a89a3a commit 20def32
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
32 changes: 23 additions & 9 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ srt::CUDTUnited::CUDTUnited()
, m_InitLock()
, m_iInstanceCount(0)
, m_bGCStatus(false)
, m_bImplicitStartup(false)
, m_ClosedSockets()
{
// Socket ID MUST start from a random value
Expand All @@ -198,12 +199,15 @@ 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();
// If an implicit startup was called, then call a matching cleanup
// This will happen at the end of main() of the application,
// when the user didn't call srt_cleanup() before using the library.
ScopedLock gcinit(m_InitLock);
if (m_bImplicitStartup)
{
cleanup();
}
}

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

int srt::CUDTUnited::startup()
void srt::CUDTUnited::implicitStartup()
{
ScopedLock gcinit(m_InitLock);
if (m_bGCStatus)
return 1;
return;
m_bImplicitStartup = true;
startup();
}

int srt::CUDTUnited::startup()
{
ScopedLock gcinit(m_InitLock);
if (m_iInstanceCount++ > 0)
return 1;

if (m_bGCStatus)
return 1;

// Global initialization code
#ifdef _WIN32
WORD wVersionRequested;
Expand Down Expand Up @@ -299,6 +312,7 @@ int srt::CUDTUnited::cleanup()
m_GCThread.join();

m_bGCStatus = false;
m_bImplicitStartup = false;

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

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

try
{
Expand Down Expand Up @@ -3519,7 +3533,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
3 changes: 3 additions & 0 deletions srtcore/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,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 Expand Up @@ -542,6 +543,8 @@ class CUDTUnited
int m_iInstanceCount; // number of startup() called by application
SRT_ATTR_GUARDED_BY(m_InitLock)
bool m_bGCStatus; // if the GC thread is working (true)
SRT_ATTR_GUARDED_BY(m_InitLock)
bool m_bImplicitStartup; // if startup was implicitly called

SRT_ATTR_GUARDED_BY(m_InitLock)
sync::CThread m_GCThread;
Expand Down

0 comments on commit 20def32

Please sign in to comment.