RUBY-231 RUBY-250 Validate socket against pid
This commit is contained in:
parent
a8ce896c9f
commit
997d9b8ae2
|
@ -41,6 +41,7 @@ module Mongo
|
||||||
@socket_ops = Hash.new { |h, k| h[k] = [] }
|
@socket_ops = Hash.new { |h, k| h[k] = [] }
|
||||||
|
|
||||||
@sockets = []
|
@sockets = []
|
||||||
|
@pids = {}
|
||||||
@checked_out = []
|
@checked_out = []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -54,6 +55,7 @@ module Mongo
|
||||||
end
|
end
|
||||||
@host = @port = nil
|
@host = @port = nil
|
||||||
@sockets.clear
|
@sockets.clear
|
||||||
|
@pids.clear
|
||||||
@checked_out.clear
|
@checked_out.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -83,6 +85,7 @@ module Mongo
|
||||||
@connection.apply_saved_authentication(:socket => socket)
|
@connection.apply_saved_authentication(:socket => socket)
|
||||||
|
|
||||||
@sockets << socket
|
@sockets << socket
|
||||||
|
@pids[socket] = Process.pid
|
||||||
@checked_out << socket
|
@checked_out << socket
|
||||||
socket
|
socket
|
||||||
end
|
end
|
||||||
|
@ -115,12 +118,22 @@ module Mongo
|
||||||
|
|
||||||
# Checks out the first available socket from the pool.
|
# Checks out the first available socket from the pool.
|
||||||
#
|
#
|
||||||
|
# If the pid has changed, remove the socket and check out
|
||||||
|
# new one.
|
||||||
|
#
|
||||||
# This method is called exclusively from #checkout;
|
# This method is called exclusively from #checkout;
|
||||||
# therefore, it runs within a mutex.
|
# therefore, it runs within a mutex.
|
||||||
def checkout_existing_socket
|
def checkout_existing_socket
|
||||||
socket = (@sockets - @checked_out).first
|
socket = (@sockets - @checked_out).first
|
||||||
@checked_out << socket
|
if @pids[socket] != Process.pid
|
||||||
socket
|
@pids[socket] = nil
|
||||||
|
@sockets.delete(socket)
|
||||||
|
socket.close
|
||||||
|
checkout_new_socket
|
||||||
|
else
|
||||||
|
@checked_out << socket
|
||||||
|
socket
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check out an existing socket or create a new socket if the maximum
|
# Check out an existing socket or create a new socket if the maximum
|
||||||
|
|
|
@ -220,6 +220,7 @@ class TestConnection < Test::Unit::TestCase
|
||||||
|
|
||||||
conn.primary_pool.host = 'localhost'
|
conn.primary_pool.host = 'localhost'
|
||||||
conn.primary_pool.port = Mongo::Connection::DEFAULT_PORT
|
conn.primary_pool.port = Mongo::Connection::DEFAULT_PORT
|
||||||
|
conn.primary_pool.instance_variable_set("@pids", {dropped_socket => Process.pid})
|
||||||
conn.primary_pool.instance_variable_set("@sockets", [dropped_socket])
|
conn.primary_pool.instance_variable_set("@sockets", [dropped_socket])
|
||||||
|
|
||||||
assert !conn.active?
|
assert !conn.active?
|
||||||
|
|
Loading…
Reference in New Issue