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
This commit is contained in:
furszy
2026-02-10 16:32:47 -05:00
parent e88d274430
commit bf2c607aaa

View File

@@ -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<void()> 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;
}
/**