From a432e9d9f9a6890954c0ee79ed3795929c267c1f Mon Sep 17 00:00:00 2001 From: David Harsha Date: Tue, 30 Oct 2018 12:23:00 -0700 Subject: [PATCH] Run processes in new pgroup Some processes are getting orphaned when running Foreman with JRuby. Creating a new pgroup allows them all to be killed together. I believe the issue is related to how JRuby handles `Dir.chdir` by creating a shell process: `sh -c 'cd /chdir/target; ${command}'`. That causes a second process to be created that won't get cleaned up by killing the parent. --- lib/foreman/engine.rb | 4 ++-- lib/foreman/process.rb | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index a1316593..3b2c32b7 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -191,14 +191,14 @@ def kill_children(signal="SIGTERM") @running.each do |pid, (process, index)| system "sending #{signal} to #{name_for(pid)} at pid #{pid}" begin - Process.kill(signal, pid) + Process.kill("-#{signal}", pid) rescue Errno::ESRCH, Errno::EPERM end end else begin pids = @running.keys.compact - Process.kill signal, *pids unless pids.empty? + Process.kill("-#{signal}", *pids) unless pids.empty? rescue Errno::ESRCH, Errno::EPERM end end diff --git a/lib/foreman/process.rb b/lib/foreman/process.rb index ee3de948..2a681d5e 100644 --- a/lib/foreman/process.rb +++ b/lib/foreman/process.rb @@ -49,9 +49,10 @@ def run(options={}) env = @options[:env].merge(options[:env] || {}) output = options[:output] || $stdout runner = "#{Foreman.runner}".shellescape - + pgroup = Foreman.windows? ? :new_pgroup : :pgroup + Dir.chdir(cwd) do - Process.spawn env, expanded_command(env), :out => output, :err => output + Process.spawn env, expanded_command(env), :out => output, :err => output, pgroup => true end end