Fix TaskQueue worker thread 100% CPU spin when idle (#1899)
Some checks failed
Book-CI / test (push) Has been cancelled
Book-CI / test-1 (push) Has been cancelled
Book-CI / test-2 (push) Has been cancelled
Deploy / deploy (macos-latest) (push) Has been cancelled
Deploy / deploy (ubuntu-latest) (push) Has been cancelled
Deploy / deploy (windows-latest) (push) Has been cancelled

* initial fix for issue 1858

* [fix]: add done flag check to sync() wait predicate to prevent deadlock during destruction

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Ben Appleby <Ben.Appleby@microsoft.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
benapple [msft] 2026-03-27 08:20:42 -07:00 committed by GitHub
parent 7a9daf0cd4
commit 7a02daa694
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 4 deletions

View file

@ -24,7 +24,11 @@ TaskQueue::TaskQueue() : done(false), pending(0) {
}
TaskQueue::~TaskQueue() {
done.store(true, std::memory_order_release);
{
std::lock_guard<std::mutex> lock(mtx);
done.store(true, std::memory_order_release);
}
cv.notify_all();
if (workerThread.joinable()) workerThread.join();
Node* node = head.load(std::memory_order_relaxed);
@ -40,11 +44,18 @@ void TaskQueue::enqueue(std::function<void()> task) {
Node* node = new Node(task);
Node* prev = tail.exchange(node, std::memory_order_acq_rel);
prev->next.store(node, std::memory_order_release);
{
std::lock_guard<std::mutex> lock(mtx);
}
cv.notify_one();
}
void TaskQueue::sync(size_t allow_n_pending) {
// Spin until the pending task count drops to the allowed threshold.
while (pending.load(std::memory_order_acquire) > allow_n_pending);
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&] {
return pending.load(std::memory_order_acquire) <= allow_n_pending
|| done.load(std::memory_order_acquire);
});
}
void TaskQueue::worker() {
@ -58,7 +69,17 @@ void TaskQueue::worker() {
delete curr;
curr = next;
head.store(curr, std::memory_order_release);
pending.fetch_sub(1, std::memory_order_acq_rel);
{
std::lock_guard<std::mutex> lock(mtx);
pending.fetch_sub(1, std::memory_order_acq_rel);
}
cv.notify_all();
} else {
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&] {
return curr->next.load(std::memory_order_acquire) != nullptr
|| done.load(std::memory_order_acquire);
});
}
}
}