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 9198f7c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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
13 changes: 13 additions & 0 deletions test/test_rake_functional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require File.expand_path("../helper", __FILE__)
require "fileutils"
require "open3"
require "stringio"

class TestRakeFunctional < Rake::TestCase # :nodoc:
include RubyRunner
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9198f7c

Please sign in to comment.