lib/bunny/session.rb in bunny-0.9.0.pre9 vs lib/bunny/session.rb in bunny-0.9.0.pre10

- old
+ new

@@ -169,19 +169,27 @@ return self if connected? @status = :connecting self.reset_continuations - self.initialize_transport + begin + # close existing transport if we have one, + # to not leak sockets + self.maybe_close_transport + self.initialize_transport - self.init_connection - self.open_connection + self.init_connection + self.open_connection - @event_loop = nil - self.start_main_loop if @threaded + @event_loop = nil + self.start_main_loop if @threaded - @default_channel = self.create_channel + @default_channel = self.create_channel + rescue Exception => e + @status = :not_connected + raise e + end self end def read_write_timeout @@ -729,9 +737,14 @@ end # @api private def initialize_transport @transport = Transport.new(self, @host, @port, @opts.merge(:session_thread => Thread.current)) + end + + # @api private + def maybe_close_transport + @transport.close if @transport end # Sends AMQ protocol header (also known as preamble). # @api private def send_preamble