From 62d905bb29218fdf65f257648f211022356b8174 Mon Sep 17 00:00:00 2001 From: "Antonio S. Musumeci" Date: Fri, 10 Apr 2026 12:56:51 -0400 Subject: [PATCH] Fix monitor/fast-path cooldown race in hill-climb scaling The monitor thread checked _last_scale_time_usecs without holding _scaling_mutex, then called add_thread()/remove_thread() which each acquired the mutex independently. The fast-path _maybe_grow_on_pressure could scale between the monitor's cooldown check and its scaling action, allowing two scaling events closer together than cooldown_usecs. Fix: hold _scaling_mutex across both the cooldown check and the scaling action in the monitor loop, using the _locked variants directly. This ensures the monitor and fast-path pressure grow are mutually exclusive for the entire check-and-act sequence. Co-Authored-By: Claude Opus 4.6 (1M context) --- vendored/libfuse/include/thread_pool.hpp | 69 ++++++++++++------------ 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/vendored/libfuse/include/thread_pool.hpp b/vendored/libfuse/include/thread_pool.hpp index 4b8aba02..a80f67cc 100644 --- a/vendored/libfuse/include/thread_pool.hpp +++ b/vendored/libfuse/include/thread_pool.hpp @@ -598,45 +598,48 @@ ThreadPool::monitor_routine(void *arg_) } } - // Respect the global cooldown so the monitor and fast-path - // pressure grow don't step on each other. + // Hold _scaling_mutex across the cooldown check and the scaling + // action so the monitor and fast-path pressure grow cannot both + // pass the cooldown gate simultaneously. { + mutex_lockguard(btp->_scaling_mutex); + std::uint64_t now = _now_usecs(); std::uint64_t last = btp->_last_scale_time_usecs.load(std::memory_order_relaxed); if((now - last) < (std::uint64_t)btp->_scaling_config.cooldown_usecs) continue; - } - if(direction > 0 && count < btp->_scaling_config.max_threads) - { - btp->add_thread(); - btp->_last_scale_time_usecs.store(_now_usecs(), - std::memory_order_relaxed); - acted_last_cycle = true; - expected_count = btp->thread_count(); - syslog(LOG_DEBUG, - "threadpool (%s): hill-climb grow to %zu threads " - "(ema %.1f -> %.1f)", - btp->_name.c_str(), - expected_count, - prev_ema, - ema_throughput); - } - else if(direction < 0 && count > btp->_scaling_config.min_threads) - { - btp->remove_thread(); - btp->_last_scale_time_usecs.store(_now_usecs(), - std::memory_order_relaxed); - acted_last_cycle = true; - expected_count = btp->thread_count(); - syslog(LOG_DEBUG, - "threadpool (%s): hill-climb shrink to %zu threads " - "(ema %.1f -> %.1f)", - btp->_name.c_str(), - expected_count, - prev_ema, - ema_throughput); - } + if(direction > 0 && count < btp->_scaling_config.max_threads) + { + btp->_add_thread_scaling_locked({}); + btp->_last_scale_time_usecs.store(_now_usecs(), + std::memory_order_relaxed); + acted_last_cycle = true; + expected_count = btp->thread_count(); + syslog(LOG_DEBUG, + "threadpool (%s): hill-climb grow to %zu threads " + "(ema %.1f -> %.1f)", + btp->_name.c_str(), + expected_count, + prev_ema, + ema_throughput); + } + else if(direction < 0 && count > btp->_scaling_config.min_threads) + { + btp->_remove_thread_scaling_locked(); + btp->_last_scale_time_usecs.store(_now_usecs(), + std::memory_order_relaxed); + acted_last_cycle = true; + expected_count = btp->thread_count(); + syslog(LOG_DEBUG, + "threadpool (%s): hill-climb shrink to %zu threads " + "(ema %.1f -> %.1f)", + btp->_name.c_str(), + expected_count, + prev_ema, + ema_throughput); + } + } } return nullptr;