From b6b27bc383ecfc96180e7245f9928754de9eba27 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 14 May 2021 18:17:31 +0100 Subject: [PATCH] DEV: Improve auto-restart parent process detection logic (#13068) The auto restart logic was sending a USR2 to the parent process without checking what the parent process actually was. In some situations, it might not be the `bin/unicorn` supervisor. This commit switches to use a global variable for the supervisor PID. This will be much less prone to unexpected behavior. --- bin/unicorn | 2 ++ config/initializers/000-development_reload_warnings.rb | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bin/unicorn b/bin/unicorn index 106649c75f4..cc241696db7 100755 --- a/bin/unicorn +++ b/bin/unicorn @@ -87,6 +87,8 @@ end # This is handy if you want to bind a key to restarting unicorn in dev if dev_mode + $unicorn_dev_supervisor_pid = Process.pid # rubocop:disable Style/GlobalVars + restart = true while restart restart = false diff --git a/config/initializers/000-development_reload_warnings.rb b/config/initializers/000-development_reload_warnings.rb index bedc2c1c587..374bb7415f6 100644 --- a/config/initializers/000-development_reload_warnings.rb +++ b/config/initializers/000-development_reload_warnings.rb @@ -12,7 +12,8 @@ if Rails.env.development? && !Rails.configuration.cache_classes && Discourse.run ] Listen.to(*paths, only: /\.rb$/) do |modified, added, removed| - auto_restart = ENV["AUTO_RESTART"] != "0" + supervisor_pid = $unicorn_dev_supervisor_pid # rubocop:disable Style/GlobalVars + auto_restart = supervisor_pid && ENV["AUTO_RESTART"] != "0" files = modified + added + removed @@ -25,7 +26,7 @@ if Rails.env.development? && !Rails.configuration.cache_classes && Discourse.run message = auto_restart ? "Restarting server..." : "Server restart required. Automate this by setting AUTO_RESTART=1." STDERR.puts "[DEV]: Edited files which are not autoloaded. #{message}" STDERR.puts not_autoloaded.map { |path| "- #{path}".indent(7) }.join("\n") - Process.kill("USR2", Process.ppid) if auto_restart + Process.kill("USR2", supervisor_pid) if auto_restart end end.start end