diff --git a/lib/mongo/server.rb b/lib/mongo/server.rb index c00285034e..1b07ecc490 100644 --- a/lib/mongo/server.rb +++ b/lib/mongo/server.rb @@ -616,7 +616,9 @@ def unknown!(options = {}) return end - if options[:generation] && options[:generation] < pool&.generation + # NOTE: You cannot use safe navigation here because if pool is nil you end + # up trying to evaluate Integer < nil which is invalid. + if options[:generation] && pool && options[:generation] < pool.generation return end diff --git a/spec/mongo/server/connection_spec.rb b/spec/mongo/server/connection_spec.rb index 3d584ae090..93c759a562 100644 --- a/spec/mongo/server/connection_spec.rb +++ b/spec/mongo/server/connection_spec.rb @@ -260,6 +260,19 @@ class ConnectionSpecTestException < Exception; end it_behaves_like 'marks server unknown' it_behaves_like 'logs a warning' it_behaves_like 'adds server diagnostics' + + context 'when the error includes a generation' do + let(:exception) do + Mongo::Error::SocketError.new.tap do |exc| + allow(exc).to receive(:generation).and_return(1234) + allow(exc).to receive(:service_id).and_return('fake') + end + end + + it 'does not fail marking the server unknown' do + expect(error).to eq(exception) + end + end end context 'when #authenticate! raises an exception' do