From 3a104e74832de4257c0bfd0e9efa692232f31c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20=C3=96hberg?= Date: Tue, 20 May 2025 10:12:47 +0200 Subject: [PATCH] STAR-1582 Fix deadlock on client stop Fix deadlock when shutting down a client, which would get stuck indefinitly 1 out of 5 times in the main thread when calling stop. The deadlock was casued by the main thread getting stuck while waiting for the client thread to join. At the same time, the client worker thread was stuck waiting for recv to return a value. In case no data was sent on the network and the remote side did not disconnect, the main thread will be stuck indefinitly. This commit solves the issue by first closing the SRT socket before trying to join the thread, instead of after, as earlier. This makes the recv call return an error and exit the thread loop. --- SRTNet.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/SRTNet.cpp b/SRTNet.cpp index 949041f..efeb5f8 100644 --- a/SRTNet.cpp +++ b/SRTNet.cpp @@ -899,10 +899,6 @@ bool SRTNet::stop() { } else if (mCurrentMode == Mode::client) { mClientActive = false; - if (mWorkerThread.joinable()) { - mWorkerThread.join(); - } - std::lock_guard lock(mNetMtx); if (mContext != SRT_INVALID_SOCK) { int result = srt_close(mContext); @@ -912,6 +908,11 @@ bool SRTNet::stop() { return false; } } + + if (mWorkerThread.joinable()) { + mWorkerThread.join(); + } + mClientConnected = false; SRT_LOGGER(true, LOGG_NOTIFY, "Client stopped");