Skip to content

Commit

Permalink
Fix GC stop handling
Browse files Browse the repository at this point in the history
1. Protect m_bClosing by m_GCStopLock
2. Fix the issue that m_GCStopLock can be setup multiple times and
never be released. srt_startup()/srt_cleanup() meant to be called
only once, but it doesn't be used this way in FFmpeg/VLC/GStreamer.
  • Loading branch information
quink-black committed Apr 15, 2021
1 parent 9442870 commit 8a7ef48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
36 changes: 16 additions & 20 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ m_ClosedSockets()
// XXX An unlikely exception thrown from the below calls
// might destroy the application before `main`. This shouldn't
// be a problem in general.
setupMutex(m_GCStopLock, "GCStop");
setupCond(m_GCStopCond, "GCStop");
setupMutex(m_GlobControlLock, "GlobControl");
setupMutex(m_IDLock, "ID");
setupMutex(m_InitLock, "Init");
Expand All @@ -235,6 +237,16 @@ CUDTUnited::~CUDTUnited()
releaseMutex(m_GlobControlLock);
releaseMutex(m_IDLock);
releaseMutex(m_InitLock);
// XXX There's some weird bug here causing this
// to hangup on Windows. This might be either something
// bigger, or some problem in pthread-win32. As this is
// the application cleanup section, this can be temporarily
// tolerated with simply exit the application without cleanup,
// counting on that the system will take care of it anyway.
#ifndef _WIN32
releaseCond(m_GCStopCond);
#endif
releaseMutex(m_GCStopLock);

delete m_pCache;
}
Expand Down Expand Up @@ -273,15 +285,6 @@ int CUDTUnited::startup()

m_bClosing = false;

try
{
setupMutex(m_GCStopLock, "GCStop");
setupCond(m_GCStopCond, "GCStop");
}
catch (...)
{
return -1;
}
if (!StartThread(m_GCThread, garbageCollect, this, "SRT:GC"))
return -1;

Expand All @@ -302,7 +305,10 @@ int CUDTUnited::cleanup()
if (!m_bGCStatus)
return 0;

m_bClosing = true;
{
UniqueLock gclock(m_GCStopLock);
m_bClosing = true;
}
HLOGC(inlog.Debug, log << "GarbageCollector: thread EXIT");
// NOTE: we can do relaxed signaling here because
// waiting on m_GCStopCond has a 1-second timeout,
Expand All @@ -312,16 +318,6 @@ int CUDTUnited::cleanup()
CSync::signal_relaxed(m_GCStopCond);
m_GCThread.join();

// XXX There's some weird bug here causing this
// to hangup on Windows. This might be either something
// bigger, or some problem in pthread-win32. As this is
// the application cleanup section, this can be temporarily
// tolerated with simply exit the application without cleanup,
// counting on that the system will take care of it anyway.
#ifndef _WIN32
releaseCond(m_GCStopCond);
#endif

m_bGCStatus = false;

// Global destruction code
Expand Down
2 changes: 1 addition & 1 deletion srtcore/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ friend class CRendezvousQueue;
CCache<CInfoBlock>* m_pCache; // UDT network information cache

private:
volatile bool m_bClosing;
bool m_bClosing;
srt::sync::Mutex m_GCStopLock;
srt::sync::Condition m_GCStopCond;

Expand Down

0 comments on commit 8a7ef48

Please sign in to comment.