puma / puma

A Ruby/Rack web server built for parallelism

Home Page:https://puma.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`preload_app` doesn't default to `true` if more than 1 worker

x-yuri opened this issue · comments

Describe the bug
The comment and the code suggest that if there are more than 1 worker, preload_app should be true, but it isn't.

Puma config:
The command line option is -w 2.

To Reproduce
a.sh:

docker run --rm --init -it alpine:3.17 sh -euc '
  with_config=$1
  apk add build-base ruby ruby-dev ruby-bundler
  echo "
    source \"https://rubygems.org\"
    gem \"puma\", \"6.0.1\"
  " > Gemfile
  echo "
    run lambda { |env| [200, {\"content-type\" => \"text/plain\"}, [\"OK\"]] }
  " > config.ru
  echo "
    preload_app!
  " > config.rb
  bundle
  puma -w 2 ${with_config:+-C config.rb}
' - "${1-}"
$ sh a.sh
...
[136] Puma starting in cluster mode...
[136] * Puma version: 6.0.1 (ruby 3.1.3-p185) ("Sunflower")
[136] *  Min threads: 0
[136] *  Max threads: 5
[136] *  Environment: development
[136] *   Master PID: 136
[136] *      Workers: 2
[136] *     Restarts: (✔) hot (✔) phased
[136] * Listening on http://0.0.0.0:9292
[136] Use Ctrl-C to stop
[137] + Gemfile in context: /Gemfile
[138] + Gemfile in context: /Gemfile
[136] - Worker 1 (PID: 138) booted in 0.02s, phase: 0
[136] - Worker 0 (PID: 137) booted in 0.02s, phase: 0
$ sh a.sh with-config
[136] Puma starting in cluster mode...
[136] * Puma version: 6.0.1 (ruby 3.1.3-p185) ("Sunflower")
[136] *  Min threads: 0
[136] *  Max threads: 5
[136] *  Environment: development
[136] *   Master PID: 136
[136] *      Workers: 2
[136] *     Restarts: (✔) hot (✖) phased
[136] * Preloading application
[136] * Listening on http://0.0.0.0:9292
[136] Use Ctrl-C to stop
[137] + Gemfile in context: /Gemfile
[136] - Worker 0 (PID: 137) booted in 0.0s, phase: 0
[138] + Gemfile in context: /Gemfile
[136] - Worker 1 (PID: 138) booted in 0.0s, phase: 0

This happens because at this point @options[:workers] is 0, since file options become available here, and user options here.

Just in case, this has nothing to do with docker.

Expected behavior
Either it should default to true, or that piece should be removed.

Desktop (please complete the following information):

  • OS: Linux
  • Puma Version 6.0.1

Thanks for this great summary.

I've actually come to the same conclusion before, at #2481 (comment) and in #2483. I think what was discussed in #2483 for Puma 6 must have been forgotten. Something for Puma 7? 😂

Maybe this issue can be the issue tracking it this time...

So, Puma will preload your app if you start it using WEB_CONCURRENCY=N puma ... (N >= 2)

App preloading (in cluster mode) would solve the issue #2818 too as this would make the localhost gem available.