Skip to content

Commit

Permalink
Run processes in new pgroup (#1)
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.

Co-authored-by: David Harsha <david.harsha@elastic.co>
  • Loading branch information
dentarg and David Harsha committed Oct 28, 2022
1 parent a5f9b78 commit 65af5e7
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 65af5e7

Please sign in to comment.