fix: validate and cap thread and queue counts

- std::stoi threw std::out_of_range for a func.readdir concurrency or
  queue depth above INT_MAX, e.g. cor:99999999999. Given at mount,
  mergerfs aborted. Set at runtime through .mergerfs, the daemon shut
  down and the mount went away, or with process-thread-count>0 the
  request never got a reply. Parse with str::from, which reports the
  overflow, and reject the value like any other malformed one.
- Reject a readdir queue depth whose product with the concurrency
  overflows an int, e.g. cor:2147483647:2147483647, instead of
  wrapping it.
- Limit read-thread-count, process-thread-count and the cor/cosr pool
  size to 64 threads per pool, after resolving automatic and
  CPU-divided counts, and log a warning when clamping. Resolve
  negative FUSE counts without overflowing on INT_MIN, and drop the
  unreachable negative readdir concurrency branch.
- process-thread-queue-depth is multiplied by the process thread count.
  Saturate the product at INT_MAX instead of overflowing.
- Correct the docs: cor/cosr counts accept digits only and 0 means the
  core count, at most 8; a queue depth of 0 means 2 per thread;
  read-thread-count=0 with process-thread-count=0 is 2 read threads
  and max(2, cores - 2) process threads, at most 8; the queue limit is
  depth times process threads, each read thread holding one in-flight
  buffer of its own. Point at threads.md for how the settings combine.
- Document the per-read-thread /dev/fuse clones and their fallback.
- tests.cpp covers the overflow cases.

Performance: default thread counts are unchanged (8 on hosts with 8 or
more cores). Only explicit or CPU-divided counts above 64, which
2.42.0 honored, are reduced.
This commit is contained in:
Antonio SJ Musumeci 2026-11-21 14:33:27 -05:00
parent 61597c8dfd
commit d679fdd265
7 changed files with 143 additions and 44 deletions

View file

@ -17,6 +17,7 @@
#include "fuse_msgbuf.hpp"
#include <cassert>
#include <limits>
#include <memory>
#include <vector>
@ -198,7 +199,8 @@ struct SyncWorker
static
int
_calculate_thread_count(const int raw_thread_count_)
_calculate_thread_count(const int raw_thread_count_,
const char *option_)
{
int thread_count = 1;
@ -209,7 +211,7 @@ _calculate_thread_count(const int raw_thread_count_)
}
else if(raw_thread_count_ < 0)
{
thread_count = (::_nproc() / -raw_thread_count_);
thread_count = (::_nproc() / -(long long)raw_thread_count_);
thread_count = std::max(1,thread_count);
}
else if(raw_thread_count_ > 0)
@ -217,6 +219,14 @@ _calculate_thread_count(const int raw_thread_count_)
thread_count = raw_thread_count_;
}
if(thread_count > ThreadPool::max_thread_count())
{
SysLog::warning("{}={} requests {} threads; limiting pool to {}",
option_,raw_thread_count_,thread_count,
ThreadPool::max_thread_count());
thread_count = ThreadPool::max_thread_count();
}
return thread_count;
}
@ -245,19 +255,28 @@ _calculate_thread_counts(int *read_thread_count_,
else if((*read_thread_count_ == 0) && (*process_thread_count_ != -1))
{
*read_thread_count_ = 2;
*process_thread_count_ = ::_calculate_thread_count(*process_thread_count_);
*process_thread_count_ = ::_calculate_thread_count(*process_thread_count_,
"process-thread-count");
}
else
{
*read_thread_count_ = ::_calculate_thread_count(*read_thread_count_);
*read_thread_count_ = ::_calculate_thread_count(*read_thread_count_,
"read-thread-count");
if(*process_thread_count_ != -1)
*process_thread_count_ = ::_calculate_thread_count(*process_thread_count_);
*process_thread_count_ = ::_calculate_thread_count(*process_thread_count_,
"process-thread-count");
}
if(*process_thread_queue_depth_ <= 0)
*process_thread_queue_depth_ = 2;
*process_thread_queue_depth_ *= std::abs(*process_thread_count_);
// Saturate rather than overflow. A total this large is unbounded in
// practice anyway since the queue only counts free slots.
if(*process_thread_queue_depth_ >
(std::numeric_limits<int>::max() / std::abs(*process_thread_count_)))
*process_thread_queue_depth_ = std::numeric_limits<int>::max();
else
*process_thread_queue_depth_ *= std::abs(*process_thread_count_);
}
int