From a2010f17b88ee72940f4c0955a67c3cee8a2e4f9 Mon Sep 17 00:00:00 2001 From: John Sully Date: Mon, 31 May 2021 01:14:11 +0000 Subject: [PATCH 1/2] Fix deadlock in async work queue Former-commit-id: 3265f2908e8d3aa567b3b9e6b141a7881c795053 --- src/AsyncWorkQueue.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AsyncWorkQueue.cpp b/src/AsyncWorkQueue.cpp index dae36cc06..9544d1a00 100644 --- a/src/AsyncWorkQueue.cpp +++ b/src/AsyncWorkQueue.cpp @@ -25,7 +25,8 @@ void AsyncWorkQueue::WorkerThreadMain() while (!m_fQuitting) { std::unique_lock lock(m_mutex); - m_cvWakeup.wait(lock); + if (m_workqueue.empty()) + m_cvWakeup.wait(lock); while (!m_workqueue.empty()) { From ac49c7158c6399d73b67a364b99514b6f9a56cbd Mon Sep 17 00:00:00 2001 From: John Sully Date: Mon, 31 May 2021 01:14:28 +0000 Subject: [PATCH 2/2] Eliminate unnecessary global locks Former-commit-id: 8f924ed0979f3cf7cd290395d1b1eec358979325 --- src/AsyncWorkQueue.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/AsyncWorkQueue.cpp b/src/AsyncWorkQueue.cpp index 9544d1a00..fe02e5212 100644 --- a/src/AsyncWorkQueue.cpp +++ b/src/AsyncWorkQueue.cpp @@ -18,9 +18,9 @@ void AsyncWorkQueue::WorkerThreadMain() vars.clients_pending_asyncwrite = listCreate(); - aeAcquireLock(); + m_mutex.lock(); m_vecpthreadVars.push_back(&vars); - aeReleaseLock(); + m_mutex.unlock(); while (!m_fQuitting) { @@ -42,9 +42,11 @@ void AsyncWorkQueue::WorkerThreadMain() lock.unlock(); serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch(); - aeAcquireLock(); - ProcessPendingAsyncWrites(); - aeReleaseLock(); + if (listLength(serverTL->clients_pending_asyncwrite)) { + aeAcquireLock(); + ProcessPendingAsyncWrites(); + aeReleaseLock(); + } g_pserver->garbageCollector.endEpoch(serverTL->gcEpoch); serverTL->gcEpoch.reset(); } @@ -61,6 +63,7 @@ bool AsyncWorkQueue::removeClientAsyncWrites(client *c) { bool fFound = false; aeAcquireLock(); + m_mutex.lock(); for (auto pvars : m_vecpthreadVars) { listIter li; @@ -75,6 +78,7 @@ bool AsyncWorkQueue::removeClientAsyncWrites(client *c) } } } + m_mutex.unlock(); aeReleaseLock(); return fFound; }