From 1e3560014fb1cd31c776fc07cf3aa9696b376c7f Mon Sep 17 00:00:00 2001 From: itytophile <45497473+itytophile@users.noreply.github.com> Date: Sat, 25 Feb 2023 23:41:05 +0100 Subject: [PATCH] Prevent deadlock when server is stopping (#426) --- ixwebsocket/IXSocketServer.cpp | 13 ++++++++++++- ixwebsocket/IXSocketServer.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index 5d7512ba..c4ea1afb 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -219,6 +219,10 @@ namespace ix if (_gcThread.joinable()) { _stopGc = true; + { + std::lock_guard lock{ _conditionVariableMutexGC }; + _canContinueGC = true; + } _conditionVariableGC.notify_one(); _gcThread.join(); _stopGc = false; @@ -451,7 +455,10 @@ namespace ix if (!_stopGc) { std::unique_lock lock(_conditionVariableMutexGC); - _conditionVariableGC.wait(lock); + if(!_canContinueGC) { + _conditionVariableGC.wait(lock, [this]{ return _canContinueGC; }); + } + _canContinueGC = false; } } } @@ -465,6 +472,10 @@ namespace ix { // a connection got terminated, we can run the connection thread GC, // so wake up the thread responsible for that + { + std::lock_guard lock{ _conditionVariableMutexGC }; + _canContinueGC = true; + } _conditionVariableGC.notify_one(); } diff --git a/ixwebsocket/IXSocketServer.h b/ixwebsocket/IXSocketServer.h index fe0f7e28..c56b650e 100644 --- a/ixwebsocket/IXSocketServer.h +++ b/ixwebsocket/IXSocketServer.h @@ -126,5 +126,6 @@ namespace ix // as a connection std::condition_variable _conditionVariableGC; std::mutex _conditionVariableMutexGC; + bool _canContinueGC{ false }; }; } // namespace ix