Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not close systemd activated socket on pumactl restart #2504

Closed
fsateler opened this issue Dec 4, 2020 · 3 comments · Fixed by #2563 or #2573
Closed

Do not close systemd activated socket on pumactl restart #2504

fsateler opened this issue Dec 4, 2020 · 3 comments · Fixed by #2563 or #2573

Comments

@fsateler
Copy link

fsateler commented Dec 4, 2020

Describe the bug

When using systemd socket activation, issuing pumactl restart causes puma to close the systemd-activated socket, leaving the server inaccessible (but still running).

Puma config:

systemd-socket-activate --listen=0.0.0.0:3001 bundle exec puma -C config/puma.rb hello.ru

# 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
set_default_host '0.0.0.0'
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port        ENV.fetch("PORT") { 3001 }

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

dir = File.absolute_path(__dir__ + '/../tmp/sockets')
FileUtils.mkdir_p dir
activate_control_app "unix://#{dir}/pumactl.sock", no_token: true
# 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") { 2 }

# 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

To Reproduce

Use the template hello.ru and the above config (generated by a rails new, plus the control socket):

run lambda { |env| [200, {"Content-Type" => "text/plain"}, ["Hello World"]] }

Run it with:

systemd-socket-activate --listen=0.0.0.0:3001 bundle exec puma -C config/puma.rb hello.ru

Then issue a restart:

bin/pumactl restart

And observe that the inherited socket is closed:

* Restarting...
Puma starting in single mode...
* Puma version: 5.1.0 (ruby 2.6.6-p146) ("At Your Service")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 68425
* Inherited tcp://0.0.0.0:3001
* Closing unused activated socket: tcp:0.0.0.0:3001
* Starting control server on unix:///home/felipe/src/satelabs/buk/test-app2/tmp/sockets/pumactl.sock
Use Ctrl-C to stop

And now the 3001 port is not accessible:

% curl localhost:3001
curl: (7) Failed to connect to localhost port 3001: Connection refused

Expected behavior

I expected the inherited socket to continue being used after a restart, just as it works without socket activation.

Desktop (please complete the following information):

  • OS: Linux
  • Puma Version: 4.3.6, 5.0.4, 5.1.0
@dentarg dentarg changed the title Closing unused activated socket Do not close systemd activated socket on pumactl restart Feb 6, 2021
@dentarg dentarg added the restart label Feb 6, 2021
@MSP-Greg
Copy link
Member

@fsateler

Thank you for the report, and sorry for the delay. I have a fix, but I've got one question.

I can repo this, sort of. I need to start with:

systemd-socket-activate ** bundle exec --keep-file-descriptors **

rather than:

systemd-socket-activate ** bundle exec **

So, were you using --keep-file-descriptors?

@fsateler
Copy link
Author

No, I was not using that flag. AFAICT, adding that does not change anything

@MSP-Greg
Copy link
Member

@fsateler

Sorry I wasn't clear last night. --keep-file-descriptors doesn't have anything to do with the bug/issue, but Puma won't start unless I use it. I'm using Windows WSL2/Ubuntu 20.04 locally.

I believe I've got a fix, working on a test...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants