Skip to content
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

RUBY-1872 Start background threads last and stop them first #1419

Merged
merged 3 commits into from Jul 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/mongo/cluster.rb
Expand Up @@ -146,27 +146,30 @@ def initialize(seeds, monitoring, options = Options::Redacted.new)
)
end

servers.each do |server|
server.start_monitoring
end

if options[:monitoring_io] == false
# Omit periodic executor construction, because without servers
# no commands can be sent to the cluster and there shouldn't ever
# be anything that needs to be cleaned up.
#
# Also omit legacy single round of SDAM on the main thread,
# as it would race with tests that mock SDAM responses.
# Omit monitoring individual servers and the legacy single round of
# of SDAM on the main thread, as it would race with tests that mock
# SDAM responses.
@connecting = @connected = false
return
end

servers.each do |server|
server.start_monitoring
end

if options[:cleanup] != false
@cursor_reaper = CursorReaper.new
@socket_reaper = SocketReaper.new(self)
@periodic_executor = PeriodicExecutor.new(@cursor_reaper, @socket_reaper)
@periodic_executor.run!

ObjectSpace.define_finalizer(self, self.class.finalize({}, @periodic_executor, @session_pool))

@periodic_executor.run!
end

@connecting = false
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/server.rb
Expand Up @@ -181,14 +181,14 @@ def connectable?; end
#
# @since 2.0.0
def disconnect!(wait=false)
monitor.stop!(wait)
begin
# For backwards compatibility we disconnect/clear the pool rather
# than close it here.
pool.disconnect!
rescue Error::PoolClosedError
# If the pool was already closed, we don't need to do anything here.
end
monitor.stop!(wait)
@connected = false
true
end
Expand Down Expand Up @@ -228,8 +228,8 @@ def start_monitoring
Monitoring::Event::ServerOpening.new(address, cluster.topology)
)
if options[:monitoring_io] != false
monitor.run!
ObjectSpace.define_finalizer(self, self.class.finalize(monitor))
monitor.run!
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/spec_tests/sdam_monitoring_spec.rb
Expand Up @@ -29,6 +29,12 @@
@servers_cache = {}
@client.cluster.servers_list.each do |server|
@servers_cache[server.address.to_s] = server

# Since we set monitoring_io: false, servers are not monitored
# by the cluster. Start monitoring on them manually (this publishes
# the server opening event but, again due to monitoring_io being
# false, does not do network I/O or change server status)>
server.start_monitoring
end
end

Expand Down