From bc60d9b2abfbf64c848ff2e7bdb5cb69d3d84577 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Fri, 1 Nov 2013 15:16:19 -0700 Subject: Force subprocess to exit after timeout Fixes MIXLIB-16. This issue is particularly prominent when yum/rpm commands go off the deep end repeatedly, but affects any case where a process takes longer than the timeout to complete. --- spec/mixlib/shellout_spec.rb | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/mixlib/shellout_spec.rb b/spec/mixlib/shellout_spec.rb index 9814473..d068dfc 100644 --- a/spec/mixlib/shellout_spec.rb +++ b/spec/mixlib/shellout_spec.rb @@ -825,12 +825,40 @@ describe Mixlib::ShellOut do end context 'with subprocess that takes longer than timeout' do - let(:cmd) { ruby_eval.call('sleep 2') } - let(:options) { { :timeout => 0.1 } } + let(:cmd) do + ruby_eval.call(<<-CODE) + STDOUT.sync = true + trap(:TERM) { puts "got term"; exit!(123) } + sleep 10 + CODE + end + let(:options) { { :timeout => 1 } } it "should raise CommandTimeout" do lambda { executed_cmd }.should raise_error(Mixlib::ShellOut::CommandTimeout) end + + it "should ask the process nicely to exit" do + lambda { executed_cmd }.should raise_error(Mixlib::ShellOut::CommandTimeout) + executed_cmd.stdout.should include("got term") + executed_cmd.exitstatus.should == 123 + end + + context "and the child is unresponsive" do + let(:cmd) do + ruby_eval.call(<<-CODE) + STDOUT.sync = true + trap(:TERM) { puts "nanana cant hear you" } + sleep 10 + CODE + end + + it "should KILL the wayward child" do + lambda { executed_cmd }.should raise_error(Mixlib::ShellOut::CommandTimeout) + executed_cmd.stdout.should include("nanana cant hear you") + executed_cmd.status.termsig.should == 9 + end + end end context 'with subprocess that exceeds buffersize' do -- cgit v1.2.1