Clean up socket and filesystem resources separately

This commit is contained in:
Jack Nagel 2015-04-24 22:13:45 -04:00
parent 16dfe3dd40
commit f7ded45bc7

View File

@ -3,42 +3,42 @@ require "socket"
module Utils module Utils
def self.safe_fork(&block) def self.safe_fork(&block)
socket_path = "#{Dir.mktmpdir("homebrew", HOMEBREW_TEMP)}/socket" Dir.mktmpdir("homebrew", HOMEBREW_TEMP) do |tmpdir|
server = UNIXServer.new(socket_path) UNIXServer.open("#{tmpdir}/socket") do |server|
ENV["HOMEBREW_ERROR_PIPE"] = socket_path read, write = IO.pipe
read, write = IO.pipe
pid = fork do pid = fork do
begin ENV["HOMEBREW_ERROR_PIPE"] = server.path
server.close
read.close begin
write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) server.close
yield read.close
rescue Exception => e write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
Marshal.dump(e, write) yield
write.close rescue Exception => e
exit! 1 Marshal.dump(e, write)
write.close
exit! 1
end
end
ignore_interrupts(:quietly) do # the child will receive the interrupt and marshal it back
begin
socket = server.accept_nonblock
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
retry unless Process.waitpid(pid, Process::WNOHANG)
else
socket.send_io(write)
end
write.close
data = read.read
read.close
Process.wait(pid) unless socket.nil?
raise Marshal.load(data) unless data.nil? or data.empty?
raise Interrupt if $?.exitstatus == 130
raise "Suspicious failure" unless $?.success?
end
end end
end end
ignore_interrupts(:quietly) do # the child will receive the interrupt and marshal it back
begin
socket = server.accept_nonblock
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
retry unless Process.waitpid(pid, Process::WNOHANG)
else
socket.send_io(write)
end
write.close
data = read.read
read.close
Process.wait(pid) unless socket.nil?
raise Marshal.load(data) unless data.nil? or data.empty?
raise Interrupt if $?.exitstatus == 130
raise "Suspicious failure" unless $?.success?
end
ensure
server.close
FileUtils.rm_r File.dirname(socket_path)
end end
end end