Skip to content

Commit

Permalink
[core] Fixed group lock-order-inversion (#3013).
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko authored Aug 21, 2024
1 parent c10d55a commit 2fff34a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions srtcore/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ class CUDTUnited
// This is only for a use together with an empty constructor.
bool acquire(CUDTUnited& glob, CUDTSocket* s)
{
if (s == NULL)
return false;
const bool caught = glob.acquireSocket(s);
socket = caught ? s : NULL;
return caught;
Expand Down
11 changes: 8 additions & 3 deletions srtcore/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,18 @@ void CUDTGroup::getOpt(SRT_SOCKOPT optname, void* pw_optval, int& w_optlen)

bool is_set_on_socket = false;
{
ScopedLock cg(m_GroupLock);
// Can't have m_GroupLock locked while calling getOpt on a member socket
// because the call will acquire m_ControlLock leading to a lock-order-inversion.
enterCS(m_GroupLock);
gli_t gi = m_Group.begin();
if (gi != m_Group.end())
CUDTSocket* const ps = (gi != m_Group.end()) ? gi->ps : NULL;
CUDTUnited::SocketKeeper sk(CUDT::uglobal(), ps);
leaveCS(m_GroupLock);
if (sk.socket)
{
// Return the value from the first member socket, if any is present
// Note: Will throw exception if the request is wrong.
gi->ps->core().getOpt(optname, (pw_optval), (w_optlen));
sk.socket->core().getOpt(optname, (pw_optval), (w_optlen));
is_set_on_socket = true;
}
}
Expand Down

0 comments on commit 2fff34a

Please sign in to comment.