From f3b4703ee259534f5df4924ba410d957ab00664d Mon Sep 17 00:00:00 2001 From: "Antonio S. Musumeci" Date: Fri, 10 Apr 2026 22:15:35 -0400 Subject: [PATCH] fix constructor resource leak and monitor condvar lost-wakeup race Two fixes: 1. Constructor: when no threads can be spawned the constructor throws, but the destructor never runs for a partially-constructed object. The pthread_cond_t and pthread_mutex_t initialized earlier in the constructor body were leaked. Destroy them before throwing. 2. Monitor: the _stop check was outside _monitor_mutex, so the destructor could set _stop and signal between the check and pthread_cond_timedwait, losing the wakeup and delaying shutdown by up to one sample interval (~100ms). Move the _stop check under _monitor_mutex so it is serialized with the destructor's signal, closing the race. Co-Authored-By: Claude Opus 4.6 (1M context) --- vendored/libfuse/include/thread_pool.hpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/vendored/libfuse/include/thread_pool.hpp b/vendored/libfuse/include/thread_pool.hpp index 71f15b90..3d9e753a 100644 --- a/vendored/libfuse/include/thread_pool.hpp +++ b/vendored/libfuse/include/thread_pool.hpp @@ -296,7 +296,11 @@ ThreadPool::ThreadPool(const unsigned thread_count_, pthread_sigmask(SIG_SETMASK,&oldset,NULL); if(_threads.empty()) - throw std::runtime_error("threadpool: failed to spawn any threads"); + { + pthread_cond_destroy(&_monitor_cond); + pthread_mutex_destroy(&_monitor_mutex); + throw std::runtime_error("threadpool: failed to spawn any threads"); + } syslog(LOG_DEBUG, "threadpool (%s): spawned %zu threads w/ max queue depth %u", @@ -557,7 +561,7 @@ ThreadPool::monitor_routine(void *arg_) std::size_t expected_count = btp->thread_count(); bool acted_last_cycle = false; - while(!btp->_stop.load(std::memory_order_acquire)) + while(true) { // Sleep for one sample interval, but wake immediately if the // destructor signals _monitor_cond. @@ -573,7 +577,16 @@ ThreadPool::monitor_routine(void *arg_) abs_ts.tv_nsec -= 1000000000L; } + // Check _stop under _monitor_mutex to close the lost-wakeup + // race: without this, the destructor could set _stop and + // signal between our check and the timedwait, losing the + // signal and delaying shutdown by up to one sample interval. pthread_mutex_lock(&btp->_monitor_mutex); + if(btp->_stop.load(std::memory_order_acquire)) + { + pthread_mutex_unlock(&btp->_monitor_mutex); + break; + } pthread_cond_timedwait(&btp->_monitor_cond, &btp->_monitor_mutex, &abs_ts);