diff --git a/lib/rake/application.rb b/lib/rake/application.rb index 2ea8c780c..8b95242a6 100644 --- a/lib/rake/application.rb +++ b/lib/rake/application.rb @@ -94,6 +94,12 @@ def init(app_name="rake", argv = ARGV) # Backward compatibility for capistrano args = handle_options end + + setup_signal_handling do + load_debug_at_stop_feature + collect_command_line_tasks(args) + end + load_debug_at_stop_feature collect_command_line_tasks(args) end @@ -857,5 +863,13 @@ def set_default_options # :nodoc: options.trace_rules = false end + def setup_signal_handling(&block) + Signal.trap("TERM") do + puts "SIGTERM received, starting cleanup..." + yield + exit 143 # 128 + Signal.list["TERM"] (15) + end + end + end end diff --git a/test/support/rakefile_definitions.rb b/test/support/rakefile_definitions.rb index 5dacd3783..c38fd673e 100644 --- a/test/support/rakefile_definitions.rb +++ b/test/support/rakefile_definitions.rb @@ -514,4 +514,18 @@ def rakefile_stand_alone_filelist io << "puts FL\n" end end + + def rakefile_with_long_running_task + rakefile <<-TEST_TASK + require 'rake/testtask' + + task :default => :test + Rake::TestTask.new(:test) do |t| + t.test_files = ['a_test.rb'] + end + TEST_TASK + open "a_test.rb", "w" do |io| + io << "sleep 20" + end + end end diff --git a/test/test_rake_functional.rb b/test/test_rake_functional.rb index 6ab005f53..265c1dd6a 100644 --- a/test/test_rake_functional.rb +++ b/test/test_rake_functional.rb @@ -2,6 +2,7 @@ require File.expand_path("../helper", __FILE__) require "fileutils" require "open3" +require "stringio" class TestRakeFunctional < Rake::TestCase # :nodoc: include RubyRunner @@ -516,6 +517,18 @@ def test_stand_alone_filelist assert_equal 0, @exit.exitstatus unless uncertain_exit_status? end + # Test that SIGTERM is handled gracefully + def test_sigterm_handling + rakefile_with_long_running_task + pid = Process.spawn('rake') + Process.detach(pid) + sleep 1 + Process.kill("TERM", pid) + _, status = Process.wait2(pid) + + assert_equal(143, status.exitstatus, "Process should exit with status 130") + end + private # We are unable to accurately verify that Rake returns a proper