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) <noreply@anthropic.com>
This commit is contained in:
Antonio S. Musumeci 2026-04-10 12:56:51 -04:00
parent 4d35726fc6
commit 62d905bb29

View file

@ -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;