Skip to content

Commit

Permalink
[core] Set CLOSING state when closing a socket (#2643).
Browse files Browse the repository at this point in the history
Signaling the GC prematurely after closing a socket to reduce closing time.


Co-authored-by: Maxim Sharabayko <maxlovic@gmail.com>
  • Loading branch information
ethouris and maxsharabayko authored Feb 8, 2023
1 parent 02cba9e commit 65bef37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1990,12 +1990,14 @@ int srt::CUDTUnited::close(CUDTSocket* s)

HLOGC(smlog.Debug, log << s->core().CONID() << "CLOSING (removing listener immediately)");
s->core().notListening();
s->m_Status = SRTS_CLOSING;

// broadcast all "accept" waiting
CSync::lock_notify_all(s->m_AcceptCond, s->m_AcceptLock);
}
else
{
s->m_Status = SRTS_CLOSING;
// Note: this call may be done on a socket that hasn't finished
// sending all packets scheduled for sending, which means, this call
// may block INDEFINITELY. As long as it's acceptable to block the
Expand Down Expand Up @@ -2118,6 +2120,7 @@ int srt::CUDTUnited::close(CUDTSocket* s)
...
}
*/
CSync::notify_one_relaxed(m_GCStopCond);

return 0;
}
Expand Down
14 changes: 10 additions & 4 deletions test/test_reuseaddr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,9 @@ void shutdownListener(SRTSOCKET bindsock)
EXPECT_NE(srt_setsockopt(bindsock, 0, SRTO_RCVSYN, &yes, sizeof yes), SRT_ERROR); // for async connect
EXPECT_NE(srt_close(bindsock), SRT_ERROR);

std::chrono::milliseconds check_period (250);
std::chrono::milliseconds check_period (100);
int credit = 400; // 10 seconds
auto then = std::chrono::steady_clock::now();

std::cout << "[T/S] waiting for cleanup of @" << bindsock << " up to 10s" << std::endl;
while (srt_getsockstate(bindsock) != SRTS_NONEXIST)
Expand All @@ -438,10 +439,15 @@ void shutdownListener(SRTSOCKET bindsock)
--credit;
if (!credit)
break;

//std::cerr << ".";
}
//std::cerr << std::endl;
auto now = std::chrono::steady_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(now - then);

// Keep as single string because this tends to be mixed from 2 threads.
std::ostringstream sout;
sout << "[T/S] @" << bindsock << " dissolved after "
<< (dur.count() / 1000.0) << "s" << std::endl;
std::cout << sout.str() << std::flush;

EXPECT_NE(credit, 0);
}
Expand Down

0 comments on commit 65bef37

Please sign in to comment.