Skip to content

Commit 8f8acbf

Browse files
committed
Stop monitor on server garbage collection
1 parent 62c46ce commit 8f8acbf

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/mongo/server.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Mongo
2727
# @since 2.0.0
2828
class Server
2929
extend Forwardable
30+
include Loggable
3031

3132
# @return [ String ] The configured address for the server.
3233
attr_reader :address
@@ -120,6 +121,19 @@ def disconnect!
120121
monitor.stop! and true
121122
end
122123

124+
# When the server is flagged for garbage collection, stop the monitor
125+
# thread.
126+
#
127+
# @example Finalize the object.
128+
# Server.finalize(monitor)
129+
#
130+
# @param [ Server::Monitor ] monitor The server monitor.
131+
#
132+
# @since 2.2.0
133+
def self.finalize(monitor)
134+
proc { monitor.stop! }
135+
end
136+
123137
# Instantiate a new server object. Will start the background refresh and
124138
# subscribe to the appropriate events.
125139
#
@@ -145,6 +159,7 @@ def initialize(address, cluster, monitoring, event_listeners, options = {})
145159
@monitor = Monitor.new(address, event_listeners, options)
146160
monitor.scan!
147161
monitor.run!
162+
ObjectSpace.define_finalizer(self, self.class.finalize(monitor))
148163
end
149164

150165
# Get a pretty printed server inspection.

0 commit comments

Comments
 (0)