Skip to content

Commit d1462b2

Browse files
p-mongop
andauthored
RUBY-2132 stop connecting connections in ensure_connected (#1937)
* RUBY-2132 stop connecting connections in ensure_connected * fix perished connection tests * remove this test since it does not terminate thread prematurely as it claims Co-authored-by: Oleg Pudeyev <oleg@bsdpower.com>
1 parent 57f80e4 commit d1462b2

File tree

6 files changed

+52
-76
lines changed

6 files changed

+52
-76
lines changed

lib/mongo/error.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def add_label(label)
169169
require 'mongo/error/bulk_write_error'
170170
require 'mongo/error/closed_stream'
171171
require 'mongo/error/connection_check_out_timeout'
172+
require 'mongo/error/connection_perished'
172173
require 'mongo/error/credential_check_error'
173174
require 'mongo/error/crypt_error'
174175
require 'mongo/error/extra_file_chunk'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (C) 2020 MongoDB Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
module Mongo
16+
class Error
17+
18+
# Exception raised when trying to perform operations on a connection that
19+
# experienced a network error.
20+
class ConnectionPerished < Error
21+
end
22+
end
23+
end

lib/mongo/server/connection.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ def error?
152152
#
153153
# @since 2.0.0
154154
def connect!
155+
if error?
156+
raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is perished. Reconnecting closed or errored connections is no longer supported"
157+
end
158+
155159
if closed?
156-
if Lint.enabled?
157-
raise Error::LintError, "Reconnecting closed connections is no longer supported (for #{address})"
158-
else
159-
log_warn("Reconnecting closed connections is deprecated (for #{address})")
160-
end
160+
raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is closed. Reconnecting closed or errored connections is no longer supported"
161161
end
162162

163163
unless @socket

lib/mongo/server/connection_common.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,18 @@ def ssl_options
110110

111111
def ensure_connected
112112
begin
113-
connect!
113+
unless socket
114+
raise ArgumentError, "Connection #{generation}:#{id} for #{address.seed} is not connected"
115+
end
116+
if @error
117+
raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is perished"
118+
end
114119
result = yield socket
115120
success = true
116121
result
117122
ensure
118123
unless success
119-
disconnect!(reason: :error)
124+
@error = true
120125
end
121126
end
122127
end

spec/mongo/server/connection_pool_spec.rb

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -735,37 +735,6 @@ def create_pool(min_pool_size)
735735
end
736736
end
737737

738-
context 'when the connection does not finish authenticating before the thread is killed' do
739-
let!(:pool) do
740-
server.pool
741-
end
742-
743-
let(:options) do
744-
{ min_pool_size:0, max_pool_size: 1 }
745-
end
746-
747-
before do
748-
pool
749-
ClientRegistry.instance.close_all_clients
750-
end
751-
752-
it 'creates a new connection' do
753-
t = Thread.new do
754-
expect {
755-
pool.with_connection do |c|
756-
expect(c).to receive(:connect!).and_raise(Mongo::Error)
757-
c.send(:ensure_connected) { |socket| socket}
758-
end
759-
}.to raise_error(Mongo::Error)
760-
expect(pool.size).to be(0)
761-
end
762-
t.join
763-
764-
expect(pool.check_out).to be_a(Mongo::Server::Connection)
765-
expect(pool.size).to be(1)
766-
end
767-
end
768-
769738
describe '#close_idle_sockets' do
770739
let!(:pool) do
771740
server.pool

spec/mongo/server/connection_spec.rb

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -687,44 +687,22 @@ class ConnectionSpecTestException < Exception; end
687687
/Got response for request ID \d+ but expected response for request ID \d+/)
688688
end
689689

690-
context 'linting' do
691-
require_linting
692-
693-
it 'does not permit sending over the connection' do
694-
expect {
695-
connection.dispatch([ query_alice ])
696-
}.to raise_error(Mongo::Error::UnexpectedResponse)
697-
698-
expect do
699-
connection.dispatch([ query_alice ]).documents
700-
end.to raise_error(Mongo::Error::LintError, /Trying to deliver a message over a disconnected connection/)
701-
end
702-
703-
it 'marks the connection no longer usable' do
704-
expect {
705-
connection.dispatch([ query_alice ])
706-
}.to raise_error(Mongo::Error::UnexpectedResponse)
690+
it 'marks connection perished' do
691+
expect {
692+
connection.dispatch([ query_alice ])
693+
}.to raise_error(Mongo::Error::UnexpectedResponse)
707694

708-
expect do
709-
connection.connect!
710-
end.to raise_error(Mongo::Error::LintError, /Reconnecting closed connections is no longer supported/)
711-
end
695+
connection.should be_error
712696
end
713697

714-
context 'not linting' do
715-
skip_if_linting
716-
717-
it 'does not affect subsequent requests but warns' do
718-
expect(Mongo::Logger.logger).to receive(:warn).once.and_call_original
719-
720-
expect {
721-
connection.dispatch([ query_alice ])
722-
}.to raise_error(Mongo::Error::UnexpectedResponse)
698+
it 'makes the connection no longer usable' do
699+
expect {
700+
connection.dispatch([ query_alice ])
701+
}.to raise_error(Mongo::Error::UnexpectedResponse)
723702

724-
docs = connection.dispatch([ query_alice ]).documents
725-
expect(docs).to_not be_empty
726-
expect(docs.first['name']).to eq('alice')
727-
end
703+
expect {
704+
connection.dispatch([ query_alice ])
705+
}.to raise_error(Mongo::Error::ConnectionPerished)
728706
end
729707
end
730708

@@ -847,9 +825,9 @@ class ConnectionSpecTestException < Exception; end
847825
end.to raise_error(Mongo::Error::SocketError)
848826
end
849827

850-
it 'disconnects and raises the exception' do
828+
it 'marks connection perished' do
851829
result
852-
expect(connection).to_not be_connected
830+
expect(connection).to be_error
853831
end
854832

855833
it 'disconnects connection pool' do
@@ -881,9 +859,9 @@ class ConnectionSpecTestException < Exception; end
881859
end.to raise_error(Mongo::Error::SocketTimeoutError)
882860
end
883861

884-
it 'disconnects the used connection' do
862+
it 'marks connection perished' do
885863
result
886-
expect(connection).to_not be_connected
864+
expect(connection).to be_error
887865
end
888866

889867
=begin These assertions require a working cluster with working SDAM flow, which the tests do not configure

0 commit comments

Comments
 (0)