Skip to content

Fix RUBY-2805 Push monitor thread can exit when address resolution fails #2356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mongo/server/push_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def do_work
if new_description.topology_version
@topology_version = new_description.topology_version
end
rescue Mongo::Error => exc
rescue IOError, SocketError, SystemCallError, Mongo::Error => exc
stop_requested = @lock.synchronize { @stop_requested }
if stop_requested
# Ignore the exception, see RUBY-2771.
Expand Down
22 changes: 22 additions & 0 deletions spec/mongo/server/monitor/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@
end
end

describe '#connect!' do

let(:options) do
SpecConfig.instance.test_options.merge(
app_metadata: monitor_app_metadata,
)
end

context 'when address resolution fails' do
let(:connection) { described_class.new(server.address, options) }

it 'propagates the exception' do
connection

expect(Socket).to receive(:getaddrinfo).and_raise(SocketError.new('Test exception'))
lambda do
connection.connect!
end.should raise_error(SocketError, 'Test exception')
end
end
end

describe '#check_document' do
context 'with API version' do
let(:meta) do
Expand Down
82 changes: 82 additions & 0 deletions spec/mongo/server/push_monitor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true
# encoding: utf-8

require 'spec_helper'

describe Mongo::Server::PushMonitor do
before(:all) do
ClientRegistry.instance.close_all_clients
end

let(:address) do
default_address
end

let(:listeners) do
Mongo::Event::Listeners.new
end

let(:monitor_options) do
{}
end

let(:monitor_app_metadata) do
Mongo::Server::Monitor::AppMetadata.new(
server_api: SpecConfig.instance.ruby_options[:server_api],
)
end

let(:cluster) do
double('cluster').tap do |cluster|
allow(cluster).to receive(:run_sdam_flow)
allow(cluster).to receive(:heartbeat_interval).and_return(1000)
end
end

let(:server) do
Mongo::Server.new(address, cluster, Mongo::Monitoring.new, listeners,
monitoring_io: false)
end

let(:monitor) do
register_background_thread_object(
Mongo::Server::Monitor.new(server, listeners, Mongo::Monitoring.new,
SpecConfig.instance.test_options.merge(cluster: cluster).merge(monitor_options).update(
app_metadata: monitor_app_metadata,
push_monitor_app_metadata: monitor_app_metadata))
)
end

let(:topology_version) do
Mongo::TopologyVersion.new('processId' => BSON::ObjectId.new, 'counter' => 1)
end

let(:check_document) do
{hello: 1}
end

let(:push_monitor) do
described_class.new(monitor, topology_version, monitor.monitoring,
**monitor.options.merge(check_document: check_document))
end

describe '#do_work' do
it 'works' do
lambda do
push_monitor.do_work
end.should_not raise_error
end

context 'network error during check' do
it 'does not propagate the exception' do
push_monitor

expect(Socket).to receive(:getaddrinfo).and_raise(SocketError.new('Test exception'))
lambda do
push_monitor.do_work
end.should_not raise_error
end
end
end

end