ruby-concurrency/concurrent-ruby · error · ArgumentError

`max_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}

Error message

`max_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}

What it means

On MRI, ThreadPoolExecutor coerces max_threads with to_i and raises ArgumentError when it is below DEFAULT_MIN_POOL_SIZE (0), i.e. when negative. This guards the Ruby worker-spawn logic from impossible sizes before any thread is created.

Source

Thrown at lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:156

    # @!macro thread_pool_executor_method_prune_pool
    def prune_pool
      deprecated "#prune_pool has no effect and will be removed in next the release, see https://github.com/ruby-concurrency/concurrent-ruby/pull/1082."
    end

    private

    # @!visibility private
    def ns_initialize(opts)
      @min_length      = opts.fetch(:min_threads, DEFAULT_MIN_POOL_SIZE).to_i
      @max_length      = opts.fetch(:max_threads, DEFAULT_MAX_POOL_SIZE).to_i
      @idletime        = opts.fetch(:idletime, DEFAULT_THREAD_IDLETIMEOUT).to_i
      @max_queue       = opts.fetch(:max_queue, DEFAULT_MAX_QUEUE_SIZE).to_i
      @synchronous     = opts.fetch(:synchronous, DEFAULT_SYNCHRONOUS)
      @fallback_policy = opts.fetch(:fallback_policy, :abort)

      raise ArgumentError.new("`synchronous` cannot be set unless `max_queue` is 0") if @synchronous && @max_queue > 0
      raise ArgumentError.new("#{@fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICIES.include?(@fallback_policy)
      raise ArgumentError.new("`max_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if @max_length < DEFAULT_MIN_POOL_SIZE
      raise ArgumentError.new("`max_threads` cannot be greater than #{DEFAULT_MAX_POOL_SIZE}") if @max_length > DEFAULT_MAX_POOL_SIZE
      raise ArgumentError.new("`min_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}") if @min_length < DEFAULT_MIN_POOL_SIZE
      raise ArgumentError.new("`min_threads` cannot be more than `max_threads`") if min_length > max_length

      @pool                 = [] # all workers
      @ready                = [] # used as a stash (most idle worker is at the start)
      @queue                = [] # used as queue
      # @ready or @queue is empty at all times
      @scheduled_task_count = 0
      @completed_task_count = 0
      @largest_length       = 0
      @workers_counter      = 0
      @ruby_pid             = $$ # detects if Ruby has forked
    end

    # @!visibility private
    def ns_limited_queue?
      @max_queue != 0

View on GitHub (pinned to 0b88d5ff75)

Solutions

  1. Validate before constructing: raise if Integer(opts[:max_threads]) < 0
  2. Clamp computed sizes: [computed, 0].max
  3. Use a positive fallback default when core detection is unavailable

Example fix

# before
Concurrent::ThreadPoolExecutor.new(max_threads: detected_cores - 4)
# after
max_threads = [detected_cores - 4, 1].max
Concurrent::ThreadPoolExecutor.new(max_threads: max_threads)
Defensive patterns

Strategy: validation

Validate before calling

max_threads = Integer(cfg[:max_threads])
raise ArgumentError, "max_threads must be >= 0, got #{max_threads}" if max_threads.negative?
Concurrent::ThreadPoolExecutor.new(max_threads: max_threads)

Type guard

def valid_thread_count?(v, min = 0, max = 2_147_483_647)
  v.is_a?(Numeric) && v.to_i.between?(min, max)
end

Try / catch

begin
  Concurrent::ThreadPoolExecutor.new(max_threads: n)
rescue ArgumentError => e
  raise unless e.message.start_with?('`max_threads` cannot be less than')
  Concurrent::ThreadPoolExecutor.new(max_threads: [n, 0].max)
end

Prevention

When it happens

Trigger: max_threads: -5 on MRI; string '-2' coerced by to_i; sizing formulas (cores - overhead) that go negative when CPU detection fails or reports oddly in containers.

Common situations: Container/cgroup CPU quota weirdness; negative sentinels meant as 'unbounded'; config sign typos; CLI flag parsing producing negative numbers.

Related errors


AI-assisted analysis of ruby-concurrency/concurrent-ruby@0b88d5ff75 (2026-08-21). Data as JSON: /api/errors/247d04164e97ca6b. Report an issue: GitHub.