From c43a14f26908d8226b919facf95fddab11fb61c1 Mon Sep 17 00:00:00 2001 From: "Antonio S. Musumeci" Date: Fri, 10 Apr 2026 17:29:07 -0400 Subject: [PATCH] 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) --- vendored/libfuse/include/thread_pool.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/vendored/libfuse/include/thread_pool.hpp b/vendored/libfuse/include/thread_pool.hpp index 13f71b78..aa7982a7 100644 --- a/vendored/libfuse/include/thread_pool.hpp +++ b/vendored/libfuse/include/thread_pool.hpp @@ -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;