puma: Puma::Cluster#run': undefined method 'wait_readable' for nil

Describe the bug

Puma::Cluster#run': undefined method 'wait_readable' for nil
/usr/local/bundle/gems/puma-6.6.0/lib/puma/cluster.rb:459:in 'Puma::Cluster#run': undefined method 'wait_readable' for nil (NoMethodError)

            if read.wait_readable([0, @next_check - Time.now].max)
                   ^^^^^^^^^^^^^^
	from /usr/local/bundle/gems/puma-6.6.0/lib/puma/launcher.rb:203:in 'Puma::Launcher#run'
	from /usr/local/bundle/gems/puma-6.6.0/lib/puma/cli.rb:75:in 'Puma::CLI#run'
	from /usr/local/bundle/gems/puma-6.6.0/bin/puma:10:in '<top (required)>'
	from /usr/local/bundle/bin/puma:25:in 'Kernel#load'
	from /usr/local/bundle/bin/puma:25:in '<top (required)>'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/cli/exec.rb:58:in 'Kernel.load'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/cli/exec.rb:58:in 'Bundler::CLI::Exec#kernel_load'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/cli/exec.rb:23:in 'Bundler::CLI::Exec#run'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/cli.rb:456:in 'Bundler::CLI#exec'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/vendor/thor/lib/thor/command.rb:28:in 'Bundler::Thor::Command#run'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in 'Bundler::Thor::Invocation#invoke_command'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/vendor/thor/lib/thor.rb:527:in 'Bundler::Thor.dispatch'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/cli.rb:35:in 'Bundler::CLI.dispatch'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/vendor/thor/lib/thor/base.rb:584:in 'Bundler::Thor::Base::ClassMethods#start'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/cli.rb:29:in 'Bundler::CLI.start'
	from /usr/local/bundle/gems/bundler-2.5.23/exe/bundle:28:in 'block in <top (required)>'
	from /usr/local/bundle/gems/bundler-2.5.23/lib/bundler/friendly_errors.rb:117:in 'Bundler.with_friendly_errors'
	from /usr/local/bundle/gems/bundler-2.5.23/exe/bundle:20:in '<top (required)>'
	from /usr/local/bundle/bin/bundle:25:in 'Kernel#load'
	from /usr/local/bundle/bin/bundle:25:in '<main>'

Puma config:

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count

# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the `pidfile` that Puma will use.
# pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 1 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

Dockerfile:

FROM ruby:3.4.1-slim

ENV RAILS_ENV production

RUN mkdir -p /app \
  && apt-get update -qq \
  && apt-get install -yq apt-utils build-essential libpq-dev postgresql-client tzdata screen git curl shared-mime-info libjemalloc2\
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
  && truncate -s 0 /var/log/*log

ENV LD_PRELOAD="libjemalloc.so.2"
WORKDIR /app

COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN gem install bundler:2.6.2

COPY . .

EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

Gemfile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.4.1'


gem 'rails', '~> 8'

gem 'pg', '~> 1.5', '>= 1.5.9'

gem 'puma', '~> 6.6'
gem 'jbuilder', '~> 2.13'

gem 'oj', '~> 3.16'
gem 'active_interaction', '~> 5.5'

# disable warnings
gem 'mutex_m'
gem 'bigdecimal'
gem 'drb'
gem 'base64'
gem 'observer'
gem 'benchmark'
gem 'reline'

# Kafka
gem 'ruby-kafka', '~> 1.5'
gem 'avro', '~> 1.9'
gem 'avro_turf'
gem 'extlz4', '~> 0.3.2', require: false
gem 'karafka', '~> 2.2.7'

# Monitoring
gem 'elastic-apm', '~> 4.7'
gem 'lograge'

gem 'vernier', '~> 1.5'
gem 'stackprof', '0.2.27'
gem 'sentry-ruby', '~> 5.22'
gem 'sentry-rails', '~> 5.22'

gem 'concurrent-ruby'

gem 'faraday', '~> 2.12.2'

# Storages
gem 'redis', '~> 5.3'

group :development, :test do
  gem 'bundler-audit', '~> 0.9.2'
  gem 'brakeman', '~> 7', require: false
  gem 'pry-byebug'
end

group :test do
  gem 'rspec-rails', '~> 7.1'  
  gem 'database_cleaner-active_record', '~> 2.2'
  gem 'webmock', '~> 3.24'
  gem 'bullet', '~> 8'
end

Desktop (please complete the following information):

  • OS: [ Linux]
  • Puma Version 6.6

As I understand the reason in IO.pipe

IO.pipe
read, @wakeup = Puma::Util.pipe

I get correct result in console:

read, @wakeup = Puma::Util.pipe
=> [#<IO:fd 6>, #<IO:fd 7>]

I could not reproduce situation whem read is nil

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 48 (22 by maintainers)

Most upvoted comments

Ruby 3.4.4 has been released with the fix I linked. When you have the time please let me know if that solves the puma problem.

@byroot thanks, that was the missing step for a reproduction.

Here is the log from running the staging container with RUBYOPT="--yjit-disable --yjit-call-threshold=5". The container started, handled one meaningful request (health checks are not logged), then fell over 12 minutes later.

I think that this counts as a simple reproduction, what does the yjit team need to investigate further?

2025-05-01T10:31:43.877+09:30 | [1] Puma starting in cluster mode...
2025-05-01T10:31:43.877+09:30 | [1] * Puma version: 6.6.0 ("Return to Forever")
2025-05-01T10:31:43.877+09:30 | [1] * Ruby version: ruby 3.4.3 (2025-04-14 revision d0b7e5b6a0) +PRISM [aarch64-linux]
2025-05-01T10:31:43.877+09:30 | [1] * Min threads: 1
2025-05-01T10:31:43.877+09:30 | [1] * Max threads: 5
2025-05-01T10:31:43.877+09:30 | [1] * Environment: staging
2025-05-01T10:31:43.877+09:30 | [1] * Master PID: 1
2025-05-01T10:31:43.877+09:30 | [1] * Workers: 2
2025-05-01T10:31:43.877+09:30 | [1] * Restarts: (✔) hot (✖) phased
2025-05-01T10:31:43.877+09:30 | [1] * Preloading application
2025-05-01T10:31:50.424+09:30 | [1] * Listening on unix:/app/tmp/sockets/puma.sock
2025-05-01T10:31:50.428+09:30 | [1] ! WARNING: Detected 2 Thread(s) started in app boot:
2025-05-01T10:31:50.429+09:30 | [1] ! #<Thread:0x0000ffff89b7afa0 /app/vendor/bundle/ruby/3.4.0/gems/semantic_logger-4.16.1/lib/semantic_logger/appender/async.rb:77 sleep_forever> - <internal:thread_sync>:18:in 'Thread::Queue#pop'
2025-05-01T10:31:50.429+09:30 | [1] ! #<Thread:0x0000ffff8a2ec220 /app/vendor/bundle/ruby/3.4.0/gems/newrelic_rpm-9.18.0/lib/new_relic/agent/tracer.rb:424 sleep_forever> - <internal:thread_sync>:18:in 'Thread::Queue#pop'
2025-05-01T10:31:50.431+09:30 | [1] Use Ctrl-C to stop
2025-05-01T10:31:50.448+09:30 | [1] - Worker 0 (PID: 73) booted in 0.01s, phase: 0
2025-05-01T10:31:50.453+09:30 | [1] - Worker 1 (PID: 79) booted in 0.01s, phase: 0
2025-05-01T10:41:29.245+09:30 | {...,"name":"WellKnownsController","message":"Completed #show","payload":{"controller":"WellKnownsController","action":"show","params":{"name":"assetlinks.json"},"format":"*/*","method":"GET","path":"/.well-known/assetlinks.json","status":200,"view_runtime":34.29,"db_runtime":65.74,"queries_count":2,"cached_queries_count":0,"allocations":21465,"status_message":"OK"}}
2025-05-01T10:43:48.165+09:30 | bundler: failed to load command: puma (/app/vendor/bundle/ruby/3.4.0/bin/puma)
2025-05-01T10:43:48.239+09:30 | /app/vendor/bundle/ruby/3.4.0/gems/puma-6.6.0/lib/puma/cluster.rb:459:in 'Puma::Cluster#run': undefined method 'wait_readable' for nil (NoMethodError)
2025-05-01T10:43:48.239+09:30 | if read.wait_readable([0, @next_check - Time.now].max)
2025-05-01T10:43:48.239+09:30 | ^^^^^^^^^^^^^^
2025-05-01T10:43:48.239+09:30 | from /app/vendor/bundle/ruby/3.4.0/gems/puma-6.6.0/lib/puma/launcher.rb:203:in 'Puma::Launcher#run'
2025-05-01T10:43:48.239+09:30 | from /app/vendor/bundle/ruby/3.4.0/gems/puma-6.6.0/lib/puma/cli.rb:75:in 'Puma::CLI#run'
2025-05-01T10:43:48.239+09:30 | from /app/vendor/bundle/ruby/3.4.0/gems/puma-6.6.0/bin/puma:10:in '<top (required)>'
2025-05-01T10:43:48.239+09:30 | from /app/vendor/bundle/ruby/3.4.0/bin/puma:25:in 'Kernel#load'
2025-05-01T10:43:48.239+09:30 | from /app/vendor/bundle/ruby/3.4.0/bin/puma:25:in '<top (required)>'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/cli/exec.rb:59:in 'Kernel.load'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/cli/exec.rb:59:in 'Bundler::CLI::Exec#kernel_load'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/cli/exec.rb:23:in 'Bundler::CLI::Exec#run'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/cli.rb:452:in 'Bundler::CLI#exec'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/vendor/thor/lib/thor/command.rb:28:in 'Bundler::Thor::Command#run'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in 'Bundler::Thor::Invocation#invoke_command'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/vendor/thor/lib/thor.rb:538:in 'Bundler::Thor.dispatch'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/cli.rb:35:in 'Bundler::CLI.dispatch'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/vendor/thor/lib/thor/base.rb:584:in 'Bundler::Thor::Base::ClassMethods#start'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/cli.rb:29:in 'Bundler::CLI.start'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/gems/3.4.0/gems/bundler-2.6.7/exe/bundle:28:in 'block in <top (required)>'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/3.4.0/bundler/friendly_errors.rb:117:in 'Bundler.with_friendly_errors'
2025-05-01T10:43:48.239+09:30 | from /usr/local/lib/ruby/gems/3.4.0/gems/bundler-2.6.7/exe/bundle:20:in '<top (required)>'
2025-05-01T10:43:48.239+09:30 | from /usr/local/bin/bundle:25:in 'Kernel#load'
2025-05-01T10:43:48.239+09:30 | from /usr/local/bin/bundle:25:in '<main>'

@tekknolagi yes I’m happy to do that. Reach out to me directly.

I spent a lot of time but wasn’t able to get a reproduction outside of ECS Fargate. It seems to be specific to the boot process + lazy initialisation of YJIT by Rails, so if you’re experiencing it see if switching to bin/rails as your entry point or explicitly enabling YJIT (RUBYOPT: "--yjit") resolves the issue.

I don’t think this is widespread because I think that it only affects sockets opened prior to YJIT initialisation.

We’ve changed all our containers to use bin/rails as the entry point and we haven’t seen the issue since. I don’t think it’s worth digging in further personally.

@soulcutter do you have a local reproduction?

I’ve found that I can reproduce in 5 minutes on Amazon ECS ARM, but I can’t produce locally using the same container on Mac M4.

I’m not clear on how a user can overwrite that behaviour.

https://guides.rubyonrails.org/configuring.html#config-yjit

If you can, it would be very valuable to turn off YJIT and see if the problem persist or go away.

@byroot @jjb sorry, we are in fact using yjit.

I was confused by the output of ruby -v. I assumed that it would show +YJIT if it was running:

$ bundle exec ruby -v
ruby 3.4.3 (2025-04-14 revision d0b7e5b6a0) +PRISM [aarch64-linux]
$ bundle exec ruby -v --yjit
ruby 3.4.3 (2025-04-14 revision d0b7e5b6a0) +YJIT +PRISM [aarch64-linux]
$ bin/rails c
Loading production environment (Rails 8.0.2)
> RubyVM::YJIT.enabled?
=> true

I see from the Rails commit history that it’s actually enabled from an initializer. I’m not clear on how a user can overwrite that behaviour.

We upgraded to Rails 8.0.1 and Ruby 3.4.1 in December 2024 so it was running for a long time with this setup without experiencing this puma issue.

I think that @jjb is correct that we were in fact using preload_app! (by default) because we run puma with two workers. If so, the only changes on the day this issue showed up were enabling plugin :solid_queue plugin and disabling plugin :tmp_restart (not sure why this change was made).

We experience this issue multiple times a day, ~10 hours between failures. It’s so predictable that we’ve had two containers started at the same time fall over at the same time 10 hours later.

This issue occurs in a client app but they’re off-season now so we can run a patched puma, but I’ll need to find some time to set it up. Maybe next week.