From bdc36b360ea093e51cae4f983752bf077982fde0 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 11 Jan 2019 01:18:59 +0000 Subject: Ensure to terminate the child git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66789 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/lib/envutil.rb | 66 +++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 30 deletions(-) (limited to 'test/lib') diff --git a/test/lib/envutil.rb b/test/lib/envutil.rb index 5d3bce99ec..f4d63d5a0a 100644 --- a/test/lib/envutil.rb +++ b/test/lib/envutil.rb @@ -65,6 +65,41 @@ module EnvUtil end module_function :apply_timeout_scale + def terminate(pid, signal = :TERM, pgroup = nil, reprieve = 1) + reprieve = apply_timeout_scale(reprieve) if reprieve + + signals = Array(signal).select do |sig| + DEFAULT_SIGNALS[sig.to_s] or + DEFAULT_SIGNALS[Signal.signame(sig)] rescue false + end + signals |= [:ABRT, :KILL] + case pgroup + when 0, true + pgroup = -pid + when nil, false + pgroup = pid + end + while signal = signals.shift + begin + Process.kill signal, pgroup + rescue Errno::EINVAL + next + rescue Errno::ESRCH + break + end + if signals.empty? or !reprieve + Process.wait(pid) + else + begin + Timeout.timeout(reprieve) {Process.wait(pid)} + rescue Timeout::Error + end + end + end + $? + end + module_function :terminate + def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr = false, encoding: nil, timeout: 10, reprieve: 1, timeout_error: Timeout::Error, stdout_filter: nil, stderr_filter: nil, @@ -72,7 +107,6 @@ module EnvUtil rubybin: EnvUtil.rubybin, precommand: nil, **opt) timeout = apply_timeout_scale(timeout) - reprieve = apply_timeout_scale(reprieve) if reprieve in_c, in_p = IO.pipe out_p, out_c = IO.pipe if capture_stdout @@ -108,35 +142,7 @@ module EnvUtil if (!th_stdout || th_stdout.join(timeout)) && (!th_stderr || th_stderr.join(timeout)) timeout_error = nil else - signals = Array(signal).select do |sig| - DEFAULT_SIGNALS[sig.to_s] or - DEFAULT_SIGNALS[Signal.signame(sig)] rescue false - end - signals |= [:ABRT, :KILL] - case pgroup = opt[:pgroup] - when 0, true - pgroup = -pid - when nil, false - pgroup = pid - end - while signal = signals.shift - begin - Process.kill signal, pgroup - rescue Errno::EINVAL - next - rescue Errno::ESRCH - break - end - if signals.empty? or !reprieve - Process.wait(pid) - else - begin - Timeout.timeout(reprieve) {Process.wait(pid)} - rescue Timeout::Error - end - end - end - status = $? + status = terminate(pid, signal, opt[:pgroup], reprieve) end stdout = th_stdout.value if capture_stdout stderr = th_stderr.value if capture_stderr && capture_stderr != :merge_to_stdout -- cgit v1.2.3