Skip to content

Commit 581e40e

Browse files
committed
Handle for SIGTERM
1 parent ae1c787 commit 581e40e

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

Diff for: lib/rake/application.rb

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def init(app_name="rake", argv = ARGV)
9494
# Backward compatibility for capistrano
9595
args = handle_options
9696
end
97+
98+
setup_signal_handling
9799
load_debug_at_stop_feature
98100
collect_command_line_tasks(args)
99101
end
@@ -857,5 +859,12 @@ def set_default_options # :nodoc:
857859
options.trace_rules = false
858860
end
859861

862+
def setup_signal_handling
863+
Signal.trap("TERM") do
864+
puts "SIGTERM received, exiting..."
865+
exit 143 # 128 + Signal.list["TERM"] (15)
866+
end
867+
end
868+
860869
end
861870
end

Diff for: test/support/rakefile_definitions.rb

+14
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,18 @@ def rakefile_stand_alone_filelist
514514
io << "puts FL\n"
515515
end
516516
end
517+
518+
def rakefile_with_long_running_task
519+
rakefile <<-TEST_TASK
520+
require 'rake/testtask'
521+
522+
task :default => :test
523+
Rake::TestTask.new(:test) do |t|
524+
t.test_files = ['a_test.rb']
525+
end
526+
TEST_TASK
527+
open "a_test.rb", "w" do |io|
528+
io << "sleep 20"
529+
end
530+
end
517531
end

Diff for: test/support/ruby_runner.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def run_ruby(option_list)
2121

2222
Open3.popen3(RUBY, *option_list) do |inn, out, err, wait|
2323
inn.close
24-
24+
@pid = wait.pid
2525
@exit = wait ? wait.value : $?
2626
@out = out.read
2727
@err = err.read

Diff for: test/test_rake_functional.rb

+14
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,20 @@ def test_stand_alone_filelist
516516
assert_equal 0, @exit.exitstatus unless uncertain_exit_status?
517517
end
518518

519+
# Test that SIGTERM is handled gracefully
520+
def test_sigterm_handling
521+
if !jruby? && can_detect_signals?
522+
rakefile_with_long_running_task
523+
Thread.new { rake }
524+
sleep 0.5
525+
Process.kill("TERM", @pid)
526+
_, status = Process.wait2(@pid)
527+
assert_equal(143, status.exitstatus, "Process should exit with status 143")
528+
else
529+
omit "Signal detection seems broken on this system"
530+
end
531+
end
532+
519533
private
520534

521535
# We are unable to accurately verify that Rake returns a proper

0 commit comments

Comments
 (0)