Skip to content

Commit 96d5cf5

Browse files
authored
Fix ArgumentError when a server is marked unknown (#2949)
1 parent d0cd1ac commit 96d5cf5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/mongo/server.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,9 @@ def unknown!(options = {})
616616
return
617617
end
618618

619-
if options[:generation] && options[:generation] < pool&.generation
619+
# NOTE: You cannot use safe navigation here because if pool is nil you end
620+
# up trying to evaluate Integer < nil which is invalid.
621+
if options[:generation] && pool && options[:generation] < pool.generation
620622
return
621623
end
622624

spec/mongo/server/connection_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ class ConnectionSpecTestException < Exception; end
260260
it_behaves_like 'marks server unknown'
261261
it_behaves_like 'logs a warning'
262262
it_behaves_like 'adds server diagnostics'
263+
264+
context 'when the error includes a generation' do
265+
let(:exception) do
266+
Mongo::Error::SocketError.new.tap do |exc|
267+
allow(exc).to receive(:generation).and_return(1234)
268+
allow(exc).to receive(:service_id).and_return('fake')
269+
end
270+
end
271+
272+
it 'does not fail marking the server unknown' do
273+
expect(error).to eq(exception)
274+
end
275+
end
263276
end
264277

265278
context 'when #authenticate! raises an exception' do

0 commit comments

Comments
 (0)