Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle for SIGTERM #561

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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