@@ -119,8 +119,10 @@ def connect
119119 BSON ::BSON_CODER . update_max_bson_size ( self )
120120 else
121121 if @secondary_pools . empty?
122+ close # close any existing pools and sockets
122123 raise ConnectionFailure , "Failed to connect any given host:port"
123124 else
125+ close # close any existing pools and sockets
124126 raise ConnectionFailure , "Failed to connect to primary node."
125127 end
126128 end
@@ -136,7 +138,7 @@ def connecting?
136138 #
137139 # @return [Boolean]
138140 def read_primary?
139- !@read_pool || @read_pool . length . zero?
141+ !@read_pool
140142 end
141143 alias :primary? :read_primary?
142144
@@ -194,9 +196,13 @@ def check_is_master(node)
194196
195197 check_set_name ( config , socket )
196198 rescue OperationFailure , SocketError , SystemCallError , IOError => ex
197- close unless connected?
199+ # It's necessary to rescue here. The #connect method will keep trying
200+ # until it has no more nodes to try and raise a ConnectionFailure if
201+ # it can't connect to a primary.
198202 ensure
203+ socket . close if socket
199204 @nodes_tried << node
205+
200206 if config
201207 nodes = [ ]
202208 nodes += config [ 'hosts' ] if config [ 'hosts' ]
@@ -208,8 +214,6 @@ def check_is_master(node)
208214 @logger . warn ( "MONGODB #{ config [ 'msg' ] } " )
209215 end
210216 end
211-
212- socket . close if socket
213217 end
214218
215219 config
0 commit comments