Skip to content

Commit 06f654c

Browse files
RUBY-3105 Do not raise errors when closing cursor (#2615)
Co-authored-by: Neil Shweky <[email protected]>
1 parent de292d7 commit 06f654c

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lib/mongo/cursor.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,6 @@ def closed?
276276
# the server.
277277
#
278278
# @return [ nil ] Always nil.
279-
#
280-
# @raise [ Error::OperationFailure ] If the server cursor close fails.
281-
# @raise [ Error::SocketError | Error::SocketTimeoutError ] When there is a network error.
282279
def close
283280
return if closed?
284281

@@ -294,6 +291,8 @@ def close
294291
end
295292

296293
nil
294+
rescue Error::OperationFailure, Error::SocketError, Error::SocketTimeoutError
295+
# Errors are swallowed since there is noting can be done by handling them.
297296
ensure
298297
end_session
299298
@cursor_id = 0

spec/mongo/cursor_spec.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,11 @@
697697

698698
describe '#close' do
699699
let(:view) do
700-
Mongo::Collection::View.new(authorized_collection)
700+
Mongo::Collection::View.new(
701+
authorized_collection,
702+
{},
703+
batch_size: 2,
704+
)
701705
end
702706

703707
let(:server) do
@@ -712,25 +716,34 @@
712716
described_class.new(view, reply, server)
713717
end
714718

719+
let(:documents) do
720+
(1..10).map{ |i| { field: "test#{i}" }}
721+
end
722+
723+
before do
724+
authorized_collection.drop
725+
authorized_collection.insert_many(documents)
726+
end
727+
715728
it 'closes' do
729+
expect(cursor).not_to be_closed
716730
cursor.close
717731
expect(cursor).to be_closed
718732
end
719733

720734
context 'when there is a socket error during close' do
721735
clean_slate
722736

723-
before do
737+
it 'does not raise an error' do
738+
cursor
724739
server.with_connection do |conn|
725740
expect(conn).to receive(:deliver)
741+
.at_least(:once)
726742
.and_raise(Mongo::Error::SocketError, "test error")
727743
end
728-
end
729-
730-
it 'raises an error' do
731744
expect do
732745
cursor.close
733-
end.to raise_error(Mongo::Error::SocketError, "test error")
746+
end.not_to raise_error
734747
end
735748
end
736749
end

0 commit comments

Comments
 (0)