Skip to content

Commit

Permalink
Merge pull request #272 from antifuchs/better-subprocess-handling
Browse files Browse the repository at this point in the history
Better subprocess handling
  • Loading branch information
turadg committed Mar 3, 2013
2 parents 7a0de42 + 29003f2 commit 383b443
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions rubygem/lib/zeus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,26 @@ def go(identifier=:boot)
Thread.new { notify_features(feature_pipe_w, features) }

# We are now 'connected'. From this point, we may receive requests to fork.
children = Set.new
loop do
messages = local.recv(2**16)

# Reap any child runners or slaves that might have exited in
# the meantime. Note that reaping them like this can leave <=1
# zombie process per slave around while the slave waits for a
# new command.
children.each do |pid|
children.delete(pid) if Process.waitpid(pid, Process::WNOHANG)
end

messages.split("\0").each do |new_identifier|
new_identifier =~ /^(.):(.*)/
code, ident = $1, $2
pid = nil
if code == "S"
fork { go(ident.to_sym) }
children << fork { go(ident.to_sym) }
else
fork { command(ident.to_sym, local) }
children << fork { command(ident.to_sym, local) }
end
end
end
Expand Down Expand Up @@ -107,14 +118,17 @@ def command(identifier, sock)

kill_command_if_client_quits!(pid, client_pid)

at_exit{ Process.kill(:TERM, client_pid) }

Process.wait(pid)
code = $?.exitstatus || 0

local.write "#{code}\0"

local.close
rescue Exception
# If anything at all went wrong, kill the client - if anything
# went wrong before the runner can clean up, it might hang
# around forever.
Process.kill(:TERM, client_pid)
end

def kill_command_if_client_quits!(command_pid, client_pid)
Expand Down

0 comments on commit 383b443

Please sign in to comment.