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)
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.
Potential YJIT fix: https://github.com/ruby/ruby/pull/13245
@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?
@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/railsas 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/railsas 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.
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+YJITif it was running: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 enablingplugin :solid_queueplugin and disablingplugin :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.