From f4272ed58932a350ae1eeb10a0e38570c9b42d75 Mon Sep 17 00:00:00 2001 From: Shayon Mukherjee Date: Mon, 29 Apr 2024 16:24:33 -0400 Subject: [PATCH] Handle for SIGTERM --- lib/rake/application.rb | 9 +++++++++ test/support/rakefile_definitions.rb | 14 ++++++++++++++ test/support/ruby_runner.rb | 2 +- test/test_rake_functional.rb | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/rake/application.rb b/lib/rake/application.rb index 2ea8c780c..713e272f5 100644 --- a/lib/rake/application.rb +++ b/lib/rake/application.rb @@ -94,6 +94,8 @@ def init(app_name="rake", argv = ARGV) # Backward compatibility for capistrano args = handle_options end + + setup_signal_handling load_debug_at_stop_feature collect_command_line_tasks(args) end @@ -857,5 +859,12 @@ def set_default_options # :nodoc: options.trace_rules = false end + def setup_signal_handling + Signal.trap("TERM") do + puts "SIGTERM received, exiting..." + 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/support/ruby_runner.rb b/test/support/ruby_runner.rb index 160a57090..b5eaadf17 100644 --- a/test/support/ruby_runner.rb +++ b/test/support/ruby_runner.rb @@ -21,7 +21,7 @@ def run_ruby(option_list) Open3.popen3(RUBY, *option_list) do |inn, out, err, wait| inn.close - + @pid = wait.pid @exit = wait ? wait.value : $? @out = out.read @err = err.read diff --git a/test/test_rake_functional.rb b/test/test_rake_functional.rb index 6ab005f53..c4c20a999 100644 --- a/test/test_rake_functional.rb +++ b/test/test_rake_functional.rb @@ -516,6 +516,20 @@ 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 + if !jruby? && can_detect_signals? + rakefile_with_long_running_task + Thread.new { rake } + sleep 1 + Process.kill("TERM", @pid) + _, status = Process.wait2(@pid) + assert_equal(143, status.exitstatus, "Process should exit with status 143") + else + omit "Signal detection seems broken on this system" + end + end + private # We are unable to accurately verify that Rake returns a proper