Merge pull request #10686 from Bo98/safe_fork-status

utils/fork: handle termsig in safe_fork
This commit is contained in:
Bo Anderson 2021-02-26 04:25:54 +00:00 committed by GitHub
commit 06381deef4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 7 deletions

View File

@ -240,7 +240,14 @@ rescue Exception => e # rubocop:disable Lint/RescueException
error_hash["env"] = e.env
when "ErrorDuringExecution"
error_hash["cmd"] = e.cmd
error_hash["status"] = e.status.exitstatus
error_hash["status"] = if e.status.is_a?(Process::Status)
{
exitstatus: e.status.exitstatus,
termsig: e.status.termsig,
}
else
e.status
end
error_hash["output"] = e.output
end

View File

@ -579,19 +579,32 @@ class ErrorDuringExecution < RuntimeError
@status = status
@output = output
raise ArgumentError, "Status cannot be nil." if status.nil?
exitstatus = case status
when Integer
status
when Hash
status["exitstatus"]
else
status&.exitstatus
status.exitstatus
end
termsig = case status
when Integer
nil
when Hash
status["termsig"]
else
status.termsig
end
redacted_cmd = redact_secrets(cmd.shelljoin.gsub('\=', "="), secrets)
reason = if exitstatus
"exited with #{exitstatus}"
elsif (uncaught_signal = status&.termsig)
"was terminated by uncaught signal #{Signal.signame(uncaught_signal)}"
elsif termsig
"was terminated by uncaught signal #{Signal.signame(termsig)}"
else
raise ArgumentError, "Status neither has `exitstatus` nor `termsig`."
end

View File

@ -5,7 +5,7 @@ describe ErrorDuringExecution do
subject(:error) { described_class.new(command, status: status, output: output) }
let(:command) { ["false"] }
let(:status) { instance_double(Process::Status, exitstatus: exitstatus) }
let(:status) { instance_double(Process::Status, exitstatus: exitstatus, termsig: nil) }
let(:exitstatus) { 1 }
let(:output) { nil }

View File

@ -186,7 +186,7 @@ describe "Exception" do
describe ErrorDuringExecution do
subject { described_class.new(["badprg", "arg1", "arg2"], status: status) }
let(:status) { instance_double(Process::Status, exitstatus: 17) }
let(:status) { instance_double(Process::Status, exitstatus: 17, termsig: nil) }
its(:to_s) { is_expected.to eq("Failure while executing; `badprg arg1 arg2` exited with 17.") }
end

View File

@ -51,7 +51,14 @@ module Utils
# to rescue them further down.
if e.is_a?(ErrorDuringExecution)
error_hash["cmd"] = e.cmd
error_hash["status"] = e.status.exitstatus
error_hash["status"] = if e.status.is_a?(Process::Status)
{
exitstatus: e.status.exitstatus,
termsig: e.status.termsig,
}
else
e.status
end
error_hash["output"] = e.output
end