Skip to content

Commit

Permalink
Handle for SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
shayonj committed Apr 29, 2024
1 parent ae1c787 commit 4bdd3ae
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 128 + Signal.list["TERM"] # 143
end
end

end
end
14 changes: 14 additions & 0 deletions test/support/rakefile_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/support/ruby_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/test_rake_functional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 0.5
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
Expand Down

0 comments on commit 4bdd3ae

Please sign in to comment.