From 2d9b72ecc1e5ae214a964b7d6b692f1740c584a3 Mon Sep 17 00:00:00 2001 From: Will Jordan Date: Fri, 15 May 2020 10:27:37 -0700 Subject: [PATCH] Fix for spawning subprocesses with fork_worker option --- lib/puma/cluster.rb | 9 ++++++--- test/test_integration_cluster.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/puma/cluster.rb b/lib/puma/cluster.rb index 861b231a..4ffe0cc4 100644 --- a/lib/puma/cluster.rb +++ b/lib/puma/cluster.rb @@ -248,6 +248,7 @@ module Puma $0 = title Signal.trap "SIGINT", "IGNORE" + Signal.trap "SIGCHLD", "DEFAULT" fork_worker = @options[:fork_worker] && index == 0 @@ -284,9 +285,11 @@ module Puma if fork_worker restart_server.clear + worker_pids = [] Signal.trap "SIGCHLD" do - Process.wait(-1, Process::WNOHANG) rescue nil - wakeup! + wakeup! if worker_pids.reject! do |p| + Process.wait(p, Process::WNOHANG) rescue true + end end Thread.new do @@ -303,7 +306,7 @@ module Puma elsif idx == 0 # restart server restart_server << true << false else # fork worker - pid = spawn_worker(idx, master) + worker_pids << pid = spawn_worker(idx, master) @worker_write << "f#{pid}:#{idx}\n" rescue nil end end diff --git a/test/test_integration_cluster.rb b/test/test_integration_cluster.rb index 6c041505..524cd4ae 100644 --- a/test/test_integration_cluster.rb +++ b/test/test_integration_cluster.rb @@ -168,6 +168,20 @@ RUBY refute_includes pids, get_worker_pids(1, WORKERS - 1) end + def test_fork_worker_spawn + cli_server '', config: <'/dev/null') + sleep 0.01 + exitstatus = Process.detach(pid).value.exitstatus + [200, {}, [exitstatus.to_s]] +end +RUBY + assert_equal '0', read_body(connect) + end + def test_nakayoshi cli_server "-w #{WORKERS} test/rackup/hello.ru", config: <