Fix bug in signal-handler teardown
2 files changed, 39 insertions(+), 11 deletions(-)

M lib/cztop/reactor/signal_handling.rb
M spec/cztop/reactor/signal_handling_spec.rb
M lib/cztop/reactor/signal_handling.rb +12 -5
@@ 64,7 64,9 @@ module CZTop::Reactor::SignalHandling
 		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 @@ module CZTop::Reactor::SignalHandling
 
 	### 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

          
M spec/cztop/reactor/signal_handling_spec.rb +27 -6
@@ 7,8 7,8 @@ require 'cztop/reactor/signal_handling'
 
 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 @@ describe CZTop::Reactor::SignalHandling 
 			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 @@ describe CZTop::Reactor::SignalHandling 
 
 
 	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 @@ describe CZTop::Reactor::SignalHandling 
 		}.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