Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix missing increment of the reference count on every call to startup() #3100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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