mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-11-25 09:26:25 -05:00
thread_pool: close TOCTOU in set_threads by reading _remove_count under lock
set_threads read _threads.size() under _threads_mutex then read _remove_count outside the lock. Between the two reads a worker could claim a _remove_count decrement and erase itself from _threads via _remove_self, causing set_threads to double-count that shrink: once from the reduced _threads.size() (had the lock been re-acquired) and once from the stale _remove_count that was already claimed. This could cause a spurious over-shrink and a -EINVAL return even though the pool was at the target size. Move the _remove_count load inside the _threads_mutex scope so both values are read atomically with respect to worker self-removal. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
06a2ba2f35
commit
c43a14f269
1 changed files with 6 additions and 4 deletions
|
|
@ -933,12 +933,14 @@ ThreadPool::set_threads(const std::size_t count_)
|
|||
{
|
||||
mutex_lockguard(_threads_mutex);
|
||||
current = _threads.size();
|
||||
// Read _remove_count under the same lock to get a consistent
|
||||
// snapshot. Without the lock a worker could claim a
|
||||
// _remove_count decrement and erase itself from _threads
|
||||
// between the two reads, causing set_threads to double-count
|
||||
// the shrink and over-remove.
|
||||
pending = _remove_count.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
// Account for removal signals already in flight so we don't
|
||||
// over-shrink when set_threads is called before prior removals
|
||||
// have drained.
|
||||
pending = _remove_count.load(std::memory_order_relaxed);
|
||||
std::size_t effective = (current > pending) ? (current - pending) : 0;
|
||||
|
||||
int rv = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue