Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JSON to marshal errors from children #4717

Merged
merged 1 commit into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion Library/Homebrew/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
require "debrew"
require "fcntl"
require "socket"
require "json"
require "json/add/core"

class Build
attr_reader :formula, :deps, :reqs
Expand Down Expand Up @@ -190,7 +192,17 @@ def fixopt(f)
build = Build.new(formula, options)
build.install
rescue Exception => e # rubocop:disable Lint/RescueException
Marshal.dump(e, error_pipe)
error_hash = JSON.parse e.to_json

# Special case: We need to toss our build state into the error hash
# for proper analytics reporting and sensible error messages.
if e.is_a?(BuildError)
error_hash["cmd"] = e.cmd
error_hash["args"] = e.args
error_hash["env"] = e.env
end

error_pipe.write error_hash.to_json
error_pipe.close
exit! 1
end
12 changes: 7 additions & 5 deletions Library/Homebrew/dev-cmd/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ def test
exec(*args)
end
end
rescue ::Test::Unit::AssertionFailedError => e
rescue ChildProcessError => e
ofail "#{f.full_name}: failed"
puts e.message
rescue Exception => e # rubocop:disable Lint/RescueException
ofail "#{f.full_name}: failed"
puts e, e.backtrace
case e.inner["json_class"]
when "Test::Unit::AssertionFailedError"
puts e.inner["m"]
else
puts e.inner["json_class"], e.backtrace
end
ensure
ENV.replace(env)
end
Expand Down
25 changes: 22 additions & 3 deletions Library/Homebrew/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,16 @@ def initialize(formula)
end

class BuildError < RuntimeError
attr_reader :formula, :env
attr_reader :formula, :cmd, :args, :env
attr_accessor :options

def initialize(formula, cmd, args, env)
@formula = formula
@cmd = cmd
@args = args
@env = env
args = args.map { |arg| arg.to_s.gsub " ", "\\ " }.join(" ")
super "Failed executing: #{cmd} #{args}"
pretty_args = args.map { |arg| arg.to_s.gsub " ", "\\ " }.join(" ")
super "Failed executing: #{cmd} #{pretty_args}"
end

def issues
Expand Down Expand Up @@ -596,3 +598,20 @@ def initialize(bottle_path, formula_path)
EOS
end
end

# Raised when a child process sends us an exception over its error pipe.
class ChildProcessError < RuntimeError
attr_reader :inner

def initialize(inner)
@inner = inner

super <<~EOS
An exception occured within a build process:
#{inner["json_class"]}: #{inner["m"]}
EOS

# Clobber our real (but irrelevant) backtrace with that of the inner exception.
set_backtrace inner["b"]
end
end
15 changes: 13 additions & 2 deletions Library/Homebrew/formula_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,25 @@ def build
raise "Empty installation"
end
rescue Exception => e # rubocop:disable Lint/RescueException
e.options = display_options(formula) if e.is_a?(BuildError)
# If we've rescued a ChildProcessError and that ChildProcessError
# contains a BuildError, then we reconstruct the inner build error
# to make analytics happy.
if e.is_a?(ChildProcessError) && e.inner["json_class"] == "BuildError"
build_error = BuildError.new(formula, e["cmd"], e["args"], e["env"])
build_error.set_backtrace e.backtrace
build_error.options = display_options(formula)

e = build_error
end

ignore_interrupts do
# any exceptions must leave us with nothing installed
formula.update_head_version
formula.prefix.rmtree if formula.prefix.directory?
formula.rack.rmdir_if_possible
end
raise

raise e
end

def link(keg)
Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/postinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "debrew"
require "fcntl"
require "socket"
require "json/add/core"

begin
error_pipe = UNIXSocket.open(ENV["HOMEBREW_ERROR_PIPE"], &:recv_io)
Expand All @@ -15,7 +16,7 @@
formula.extend(Debrew::Formula) if ARGV.debug?
formula.run_post_install
rescue Exception => e # rubocop:disable Lint/RescueException
Marshal.dump(e, error_pipe)
error_pipe.write e.to_json
error_pipe.close
exit! 1
end
3 changes: 2 additions & 1 deletion Library/Homebrew/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "formula_assertions"
require "fcntl"
require "socket"
require "json/add/core"

TEST_TIMEOUT_SECONDS = 5 * 60

Expand All @@ -28,7 +29,7 @@
raise "test returned false" if formula.run_test == false
end
rescue Exception => e # rubocop:disable Lint/RescueException
Marshal.dump(e, error_pipe)
error_pipe.write e.to_json
error_pipe.close
exit! 1
end
6 changes: 4 additions & 2 deletions Library/Homebrew/utils/fork.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "fcntl"
require "socket"
require "json"
require "json/add/core"

module Utils
def self.safe_fork(&_block)
Expand All @@ -15,7 +17,7 @@ def self.safe_fork(&_block)
write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
yield
rescue Exception => e # rubocop:disable Lint/RescueException
Marshal.dump(e, write)
write.write e.to_json
write.close
exit!
else
Expand All @@ -36,7 +38,7 @@ def self.safe_fork(&_block)
data = read.read
read.close
Process.wait(pid) unless socket.nil?
raise Marshal.load(data) unless data.nil? || data.empty? # rubocop:disable Security/MarshalLoad
raise ChildProcessError, JSON.parse(data) unless data.nil? || data.empty?
raise Interrupt if $CHILD_STATUS.exitstatus == 130
raise "Forked child process failed: #{$CHILD_STATUS}" unless $CHILD_STATUS.success?
end
Expand Down