From bf2c607aaa22d253b9367c11b0a198bd4244ad2f Mon Sep 17 00:00:00 2001 From: furszy Date: Tue, 10 Feb 2026 16:32:47 -0500 Subject: [PATCH] threadpool: active-wait during shutdown Instead of waiting for the workers to finish processing tasks, help them actively inside Stop(). This speeds up the JSON-RPC and REST server shutdown procedure, and results in a faster node shutdown when many requests remain unhandled --- src/util/threadpool.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/util/threadpool.h b/src/util/threadpool.h index 6fc294989d1..ca39afd4ab2 100644 --- a/src/util/threadpool.h +++ b/src/util/threadpool.h @@ -139,6 +139,9 @@ public: threads_to_join.swap(m_workers); } m_cv.notify_all(); + // Help draining queue + while (ProcessTask()) {} + // Free resources for (auto& worker : threads_to_join) worker.join(); // Since we currently wait for tasks completion, sanity-check empty queue @@ -187,18 +190,19 @@ public: * @brief Execute a single queued task synchronously. * Removes one task from the queue and executes it on the calling thread. */ - void ProcessTask() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) + bool ProcessTask() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) { std::packaged_task task; { LOCK(m_mutex); - if (m_work_queue.empty()) return; + if (m_work_queue.empty()) return false; // Pop the task task = std::move(m_work_queue.front()); m_work_queue.pop(); } task(); + return true; } /**