# HG changeset patch # User Michael Granger # Date 1558547988 25200 # Wed May 22 10:59:48 2019 -0700 # Node ID 200c8be89458703633ac2c06189fc15670e1f286 # Parent 3689a0184718c56b8b57d657254b830d403c2e1a Fix bug in signal-handler teardown diff --git a/lib/cztop/reactor/signal_handling.rb b/lib/cztop/reactor/signal_handling.rb --- a/lib/cztop/reactor/signal_handling.rb +++ b/lib/cztop/reactor/signal_handling.rb @@ -64,7 +64,9 @@ return yield ensure + self.log.debug "Going to reset signal traps..." self.reset_signal_traps( *signals ) + self.log.debug "Going to clean up signal handler..." self.clean_up_signal_handling( reactor ) end @@ -99,12 +101,17 @@ ### Tear down the data structures for signal handling def clean_up_signal_handling( reactor ) - reactor.unregister( @self_pipe[:reader] ) + if @self_pipe + self.log.info "Cleaning up signal handler self-pipe." + reactor.unregister( @self_pipe[:reader] ) - @self_pipe[:writer].options.linger = 0 - @self_pipe[:writer].close - @self_pipe[:reader].options.linger = 0 - @self_pipe[:reader].close + @self_pipe[:writer].options.linger = 0 + @self_pipe[:writer].close + @self_pipe[:reader].options.linger = 0 + @self_pipe[:reader].close + else + self.log.info "No self-pipe; skipping signal-handler cleanup." + end Thread.main[ SIGNAL_QUEUE_KEY ].clear end diff --git a/spec/cztop/reactor/signal_handling_spec.rb b/spec/cztop/reactor/signal_handling_spec.rb --- a/spec/cztop/reactor/signal_handling_spec.rb +++ b/spec/cztop/reactor/signal_handling_spec.rb @@ -7,8 +7,8 @@ describe CZTop::Reactor::SignalHandling do - it "can add a signal handler setup to a reactor for a block" do - including_class = Class.new do + let( :including_class ) do + class_instance = Class.new do def initialize @signals = [] @@ -21,9 +21,12 @@ end end + class_instance.include( described_class ) + return class_instance + end - including_class.include( described_class ) + it "can add a signal handler setup to a reactor for a block" do obj = including_class.new reactor = CZTop::Reactor.new @@ -38,10 +41,10 @@ it "raises if the including class doesn't provide a #handle_signal method" do - including_class = Class.new - including_class.include( described_class ) + test_class = Class.new + test_class.include( described_class ) - obj = including_class.new + obj = test_class.new reactor = CZTop::Reactor.new expect { @@ -53,5 +56,23 @@ }.to raise_error( NotImplementedError, /unhandled signal USR1/i ) end + + it "doesn't try to tear down signal handling if it didn't get set up" do + error_class = Class.new( StandardError ) + allow( CZTop::Socket::PAIR ).to receive( :new ). + and_raise( error_class.new("something bad happened") ) + + obj = including_class.new + reactor = CZTop::Reactor.new + + begin + obj.with_signal_handler( reactor, :USR1 ) do + fail "we shouldn't get here" + end + rescue error_class => err + expect( err.message ).to eq( "something bad happened" ) + end + end + end