ruby-concurrency/concurrent-ruby · error · ArgumentError

`min_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}

Error message

`min_threads` cannot be less than #{DEFAULT_MIN_POOL_SIZE}

What it means

On JRuby, ThreadPoolExecutor coerces min_threads with to_i and raises ArgumentError when it is below DEFAULT_MIN_POOL_SIZE (0), i.e. negative. min_threads is the floor of warm workers the pool keeps alive, so a negative count is meaningless.

Source

Thrown at lib/concurrent-ruby/concurrent/executor/java_thread_pool_executor.rb:120

      # @!macro thread_pool_executor_method_prune_pool
      def prune_pool
        deprecated "#prune_pool has no effect and will be removed in the next release."
      end

      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("`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
        raise ArgumentError.new("#{fallback_policy} is not a valid fallback policy") unless FALLBACK_POLICY_CLASSES.include?(@fallback_policy)

        if @max_queue == 0
          if @synchronous
            queue = java.util.concurrent.SynchronousQueue.new
          else
            queue = java.util.concurrent.LinkedBlockingQueue.new
          end
        else
          queue = java.util.concurrent.LinkedBlockingQueue.new(@max_queue)
        end

        @executor = java.util.concurrent.ThreadPoolExecutor.new(
            min_length,
            max_length,
            idletime,
            java.util.concurrent.TimeUnit::SECONDS,

View on GitHub (pinned to 0b88d5ff75)

Solutions

  1. Validate before constructing: raise if Integer(opts[:min_threads]) < 0
  2. Clamp computed values: [computed, 0].max
  3. Use 0 to mean 'no warm threads' instead of a negative sentinel

Example fix

# before
Concurrent::ThreadPoolExecutor.new(min_threads: ENV.fetch('MIN', 1).to_i - 2)
# after
min_threads = [ENV.fetch('MIN', 1).to_i - 2, 0].max
Concurrent::ThreadPoolExecutor.new(min_threads: min_threads)
Defensive patterns

Strategy: validation

Validate before calling

min_threads = Integer(cfg.fetch(:min_threads, 0))
raise ArgumentError, "min_threads must be >= 0, got #{min_threads}" if min_threads.negative?
Concurrent::ThreadPoolExecutor.new(min_threads: min_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(min_threads: n)
rescue ArgumentError => e
  raise unless e.message.start_with?('`min_threads` cannot be less than')
  Concurrent::ThreadPoolExecutor.new(min_threads: [n, 0].max)
end

Prevention

When it happens

Trigger: min_threads: -1 on JRuby; string '-4' coerced by to_i; formulas like keep_alive - reserve that go negative in constrained environments.

Common situations: Environment-derived sizing that underflows in containers with tiny cgroup CPU quotas; config sign typos; negative sentinels intended to disable warm threads.

Related errors


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