Skip to content

Commit f1f79ea

Browse files
committed
Handle a subprocess being already terminated
1 parent 6982357 commit f1f79ea

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

spec/mspec/lib/mspec/runner/actions/timeout.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def start
5252
else
5353
# Do not exit but signal the subprocess so we can get their output
5454
MSpec.subprocesses.each do |pid|
55-
Process.kill :SIGTERM, pid
55+
kill_wait_one_second :SIGTERM, pid
56+
hard_kill :SIGKILL, pid
5657
end
5758
@fail = true
5859
@current_state = nil
@@ -89,12 +90,28 @@ def finish
8990
@thread.join
9091
end
9192

93+
private def hard_kill(signal, pid)
94+
begin
95+
Process.kill signal, pid
96+
rescue Errno::ESRCH
97+
# Process already terminated
98+
end
99+
end
100+
101+
private def kill_wait_one_second(signal, pid)
102+
begin
103+
Process.kill signal, pid
104+
sleep 1
105+
rescue Errno::ESRCH
106+
# Process already terminated
107+
end
108+
end
109+
92110
private def show_backtraces
93111
java_stacktraces = -> pid {
94112
if RUBY_ENGINE == 'truffleruby' || RUBY_ENGINE == 'jruby'
95113
STDERR.puts 'Java stacktraces:'
96-
Process.kill :SIGQUIT, pid
97-
sleep 1
114+
kill_wait_one_second :SIGQUIT, pid
98115
end
99116
}
100117

@@ -118,8 +135,7 @@ def finish
118135

119136
if RUBY_ENGINE == 'truffleruby'
120137
STDERR.puts "\nRuby backtraces:"
121-
Process.kill :SIGALRM, pid
122-
sleep 1
138+
kill_wait_one_second :SIGALRM, pid
123139
else
124140
STDERR.puts "Don't know how to print backtraces of a subprocess on #{RUBY_ENGINE}"
125141
end

0 commit comments

Comments
 (0)