From beb6961769798121d7a7f0c98ce00992020f2f24 Mon Sep 17 00:00:00 2001 From: "Antonio S. Musumeci" Date: Thu, 9 Apr 2026 23:28:28 -0400 Subject: [PATCH] Fix _maybe_grow_on_pressure burning cooldown at max threads The CAS on _last_scale_time_usecs was performed before checking whether the pool was already at max_threads. When the pool was at capacity, the cooldown window was consumed but no thread was added. This suppressed legitimate scaling attempts from the monitor thread for the entire cooldown period. Move the CAS after the max-threads check and the _scaling_mutex acquisition so the cooldown is only claimed when a thread will actually be added. The mutex serializes concurrent callers, and the CAS still deduplicates concurrent pressure-grow attempts that pass the check simultaneously. Co-Authored-By: Claude Opus 4.6 (1M context) --- vendored/libfuse/include/thread_pool.hpp | 28 +++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/vendored/libfuse/include/thread_pool.hpp b/vendored/libfuse/include/thread_pool.hpp index 339db117..efb35794 100644 --- a/vendored/libfuse/include/thread_pool.hpp +++ b/vendored/libfuse/include/thread_pool.hpp @@ -650,11 +650,6 @@ ThreadPool::_maybe_grow_on_pressure() if((now - last) < (std::uint64_t)_scaling_config.cooldown_usecs) return; - // CAS to claim the cooldown window — only one caller wins. - if(!_last_scale_time_usecs.compare_exchange_strong(last,now, - std::memory_order_relaxed)) - return; - // Hold _scaling_mutex so the thread-count check and add are atomic // with respect to other scaling operations. mutex_lockguard(_scaling_mutex); @@ -665,14 +660,21 @@ ThreadPool::_maybe_grow_on_pressure() count = _threads.size(); } - if(count < _scaling_config.max_threads) - { - _add_thread_scaling_locked({}); - syslog(LOG_DEBUG, - "threadpool (%s): fast-path grow to %zu threads (queue full)", - _name.c_str(), - count + 1); - } + if(count >= _scaling_config.max_threads) + return; + + // CAS to claim the cooldown window — only one caller wins. + // Done after the max-threads check so we don't burn the cooldown + // when the pool is already at capacity. + if(!_last_scale_time_usecs.compare_exchange_strong(last,now, + std::memory_order_relaxed)) + return; + + _add_thread_scaling_locked({}); + syslog(LOG_DEBUG, + "threadpool (%s): fast-path grow to %zu threads (queue full)", + _name.c_str(), + count + 1); }