Skip to content

Commit 2765fc2

Browse files
authored
Merge pull request mongodb#777 from estolfo/connect-errors
Don't call ensure_connected from within #authenticate!, loop possible
2 parents 21ba665 + 4978ebf commit 2765fc2

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

lib/mongo/server/connection.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,13 @@ def authenticate!
170170
end
171171

172172
def default_mechanism
173-
ensure_connected do |socket|
174-
read_with_one_retry do
175-
socket.write(Monitor::Connection::ISMASTER_BYTES)
176-
ismaster = Protocol::Reply.deserialize(socket, max_message_size).documents[0]
177-
min_wire_version = ismaster[Description::MIN_WIRE_VERSION] || Description::LEGACY_WIRE_VERSION
178-
max_wire_version = ismaster[Description::MAX_WIRE_VERSION] || Description::LEGACY_WIRE_VERSION
179-
features = Description::Features.new(min_wire_version..max_wire_version)
180-
(features.scram_sha_1_enabled? || @server.features.scram_sha_1_enabled?) ? :scram : :mongodb_cr
181-
end
182-
end
173+
return (@server.features.scram_sha_1_enabled? ? :scram : :mongodb_cr) unless socket
174+
socket.write(Monitor::Connection::ISMASTER_BYTES)
175+
ismaster = Protocol::Reply.deserialize(socket, max_message_size).documents[0]
176+
min_wire_version = ismaster[Description::MIN_WIRE_VERSION] || Description::LEGACY_WIRE_VERSION
177+
max_wire_version = ismaster[Description::MAX_WIRE_VERSION] || Description::LEGACY_WIRE_VERSION
178+
features = Description::Features.new(min_wire_version..max_wire_version)
179+
(features.scram_sha_1_enabled? || @server.features.scram_sha_1_enabled?) ? :scram : :mongodb_cr
183180
end
184181

185182
def write(messages, buffer = BSON::ByteBuffer.new)

spec/mongo/server/connection_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
end
119119

120120
let!(:error) do
121-
e = begin; connection.connect!; rescue => ex; ex; end
121+
e = begin; connection.send(:ensure_connected); rescue => ex; ex; end
122122
end
123123

124124
it 'raises an error' do

0 commit comments

Comments
 (0)