Skip to content

Commit

Permalink
Run processes in new pgroup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
David Harsha committed Oct 31, 2018
1 parent c0b178c commit c27bead
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/foreman/engine.rb
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/foreman/process.rb
Expand Up @@ -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

Expand Down

0 comments on commit c27bead

Please sign in to comment.