thread_pool: skip _remove_thread_scaling_locked during shutdown

_add_thread_scaling_locked already bails out with -ECANCELED when
_stop is set.  Mirror that check in _remove_thread_scaling_locked so
the monitor cannot enqueue a wakeup poison pill after shutdown begins.
The pill would be benign (workers break on _stop before claiming), but
it would sit in the queue until BoundedQueue is destroyed, and skipping
it also avoids an unnecessary semaphore signal on the teardown path.
This commit is contained in:
Antonio S. Musumeci 2026-04-18 17:50:30 -04:00
parent 1b158549b8
commit e55bf1829b

View file

@ -1105,6 +1105,14 @@ inline
int
ThreadPool::_remove_thread_scaling_locked()
{
// Skip once shutdown has begun. Mirrors _add_thread_scaling_locked's
// early return for symmetry and prevents the monitor from enqueuing
// wakeup pills that no worker will claim (workers see _stop and
// BREAK before reaching _try_claim_and_remove_self), which would
// leave stale pills in the queue until BoundedQueue is destroyed.
if(_stop.load(std::memory_order_acquire))
return -ECANCELED;
// Hold _threads_mutex across both the size check and the
// _remove_count increment so that a concurrent removal (idle
// self-exit or another _remove_thread call) cannot shrink the