Skip to content

Commit 60d1f69

Browse files
RUBY-3358 Handle nil session is write_worker (#2826)
1 parent 5f4a7e3 commit 60d1f69

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/mongo/retryable/write_worker.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ def write_with_retry(write_concern, ending_transaction: false, context:, &block)
103103
def nro_write_with_retry(write_concern, context:, &block)
104104
session = context.session
105105
server = select_server(cluster, ServerSelector.primary, session)
106+
options = session&.client&.options || {}
106107

107-
if session&.client.options[:retry_writes]
108+
if options[:retry_writes]
108109
begin
109110
server.with_connection(connection_global_id: context.connection_global_id) do |connection|
110111
yield connection, nil, context
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Mongo::Retryable::WriteWorker do
6+
describe '#nro_write_with_retry' do
7+
context 'when session is nil' do
8+
let(:retryable) do
9+
authorized_client['write_worker_test']
10+
end
11+
12+
let(:write_concern) do
13+
Mongo::WriteConcern.get(w: 0)
14+
end
15+
16+
let(:write_worker) do
17+
described_class.new(retryable)
18+
end
19+
20+
let(:context) do
21+
instance_double(Mongo::Operation::Context).tap do |context|
22+
allow(context).to receive(:session).and_return(nil)
23+
end
24+
end
25+
26+
before do
27+
# We avoid actual execution of the operation to speed up and simplify
28+
# the spec.
29+
allow(write_worker).to receive(:legacy_write_with_retry).and_return(nil)
30+
end
31+
32+
it 'does not raise' do
33+
expect do
34+
write_worker.nro_write_with_retry(write_concern, context: context)
35+
end.not_to raise_error
36+
end
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)