Clamp set_threads to autoscale bounds

set_threads did not enforce ScalingConfig::min_threads / max_threads,
so a caller could push the pool outside the range that the monitor
thread and idle self-exit logic expect.  For example, set_threads(1)
with min_threads=2 would be rejected by _remove_thread_scaling_locked
partway through, leaving the pool in a partially-shrunk state.
Growing above max_threads had no guard at all.

Clamp the requested count to [min_threads, max_threads] when
autoscaling is enabled, mirroring the constructor's clamping logic.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Antonio S. Musumeci 2026-04-10 00:58:16 -04:00
parent 8f41b40692
commit 78d320a64c

View file

@ -777,6 +777,17 @@ ThreadPool::set_threads(const std::size_t count_)
if(count_ == 0)
return -EINVAL;
// Clamp to autoscale bounds so external callers can't push the
// pool outside the range the monitor and idle-exit logic expect.
std::size_t count = count_;
if(_autoscale_enabled)
{
if(count < _scaling_config.min_threads)
count = _scaling_config.min_threads;
if(count > _scaling_config.max_threads)
count = _scaling_config.max_threads;
}
std::size_t current;
{
@ -786,14 +797,14 @@ ThreadPool::set_threads(const std::size_t count_)
int rv = 0;
for(std::size_t i = current; i < count_; ++i)
for(std::size_t i = current; i < count; ++i)
{
rv = _add_thread_scaling_locked({});
if(rv != 0)
return rv;
}
for(std::size_t i = count_; i < current; ++i)
for(std::size_t i = count; i < current; ++i)
{
rv = _remove_thread_scaling_locked();
if(rv != 0)