From 06a12246f5d8966531bd8afd2e2f5254295970a1 Mon Sep 17 00:00:00 2001 From: smallmodel <15067410+smallmodel@users.noreply.github.com> Date: Thu, 13 Mar 2025 22:37:21 +0100 Subject: [PATCH] Notify the request thread of shutdown only if it's waiting This avoids a deadlock that would occur if notify_all() is called before the thread is waiting --- code/sys/sys_update_checker.cpp | 22 ++++++++++++---------- code/sys/sys_update_checker.h | 1 - 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/code/sys/sys_update_checker.cpp b/code/sys/sys_update_checker.cpp index d40d8060..c7915503 100644 --- a/code/sys/sys_update_checker.cpp +++ b/code/sys/sys_update_checker.cpp @@ -210,7 +210,13 @@ void UpdateCheckerThread::Shutdown() if (osThread) { // Notify and shutdown the thread - clientWake.notify_all(); + { + // Wait until the request thread is ready + std::lock_guard l(clientWakeMutex); + clientWake.notify_all(); + } + + // Wait for the thread to exit osThread->join(); delete osThread; @@ -358,19 +364,15 @@ void UpdateCheckerThread::RequestThread() InitClient(); while (handle && shouldBeActive) { + std::unique_lock l(clientWakeMutex); DoRequest(); - RequestThreadSleep(); + + const std::chrono::seconds interval = std::chrono::seconds(Q_max(1, com_updatecheck_interval->integer) * 60); + + clientWake.wait_for(l, interval); } ShutdownClient(); requestThreadIsActive = qfalse; } - -void UpdateCheckerThread::RequestThreadSleep() -{ - const std::chrono::seconds interval = std::chrono::seconds(Q_max(1, com_updatecheck_interval->integer) * 60); - - std::unique_lock l(clientWakeMutex); - clientWake.wait_for(l, interval); -} diff --git a/code/sys/sys_update_checker.h b/code/sys/sys_update_checker.h index e3905012..ac5e8748 100644 --- a/code/sys/sys_update_checker.h +++ b/code/sys/sys_update_checker.h @@ -61,7 +61,6 @@ private: bool ParseVersionNumber(const char *value, int& major, int& minor, int& patch) const; void RequestThread(); - void RequestThreadSleep(); void DoRequest(); private: