Skip to content

Commit 7b77398

Browse files
committed
RUBY-1100 Check that connecting block succeeded or disconnect socket
1 parent 2d1d61c commit 7b77398

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lib/mongo/server/connectable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def ssl_options
8686

8787
def ensure_connected
8888
ensure_same_process!
89-
connect!
9089
begin
90+
connect!
9191
result = yield socket
9292
success = true
9393
result

lib/mongo/server/connection.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ def connect!
6363
authenticate!
6464
end
6565
true
66-
rescue Mongo::Auth::Unauthorized => e
67-
disconnect!
68-
raise e
6966
end
7067

7168
# Disconnect the connection.

spec/mongo/server/connection_pool_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,32 @@
192192
end
193193
end
194194
end
195+
196+
context 'when the connection does not finish authenticating before the thread is killed' do
197+
198+
let(:server) do
199+
Mongo::Server.new(address, cluster, monitoring, listeners, options)
200+
end
201+
202+
let!(:pool) do
203+
described_class.get(server)
204+
end
205+
206+
let(:options) do
207+
{ user: ROOT_USER.name, password: ROOT_USER.password }.merge(TEST_OPTIONS).merge(max_pool_size: 1)
208+
end
209+
210+
before do
211+
t = Thread.new {
212+
# Kill the thread when it's authenticating
213+
allow(Mongo::Auth).to receive(:get) { t.kill }
214+
pool.with_connection { |c| c.send(:ensure_connected) { |socket| socket } }
215+
}
216+
t.join
217+
end
218+
219+
it 'disconnects the socket' do
220+
expect(pool.checkout.send(:socket)).to be_nil
221+
end
222+
end
195223
end

0 commit comments

Comments
 (0)