Skip to content

Commit 228f716

Browse files
committed
PYTHON-2059 Do not send readPreference with OP_MSG getMore commands
1 parent 895b662 commit 228f716

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

pymongo/message.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def get_message(self, dummy0, sock_info, use_cmd=False):
422422
spec = self.as_command(sock_info)[0]
423423
if sock_info.op_msg_enabled:
424424
request_id, msg, size, _ = _op_msg(
425-
0, spec, self.db, ReadPreference.PRIMARY,
425+
0, spec, self.db, None,
426426
False, False, self.codec_options,
427427
ctx=sock_info.compression_context)
428428
return request_id, msg, size
@@ -685,7 +685,8 @@ def _op_msg(flags, command, dbname, read_preference, slave_ok, check_keys,
685685
opts, ctx=None):
686686
"""Get a OP_MSG message."""
687687
command['$db'] = dbname
688-
if "$readPreference" not in command:
688+
# getMore commands do not send $readPreference.
689+
if read_preference is not None and "$readPreference" not in command:
689690
if slave_ok and not read_preference.mode:
690691
command["$readPreference"] = (
691692
ReadPreference.PRIMARY_PREFERRED.document)

test/test_cursor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
InvalidOperation,
4141
OperationFailure)
4242
from pymongo.read_concern import ReadConcern
43+
from pymongo.read_preferences import ReadPreference
4344
from test import (client_context,
4445
unittest,
4546
IntegrationTest)
@@ -1407,6 +1408,26 @@ def test_delete_not_initialized(self):
14071408
cursor = Cursor.__new__(Cursor) # Skip calling __init__
14081409
cursor.__del__() # no error
14091410

1411+
@client_context.require_version_min(3, 6)
1412+
def test_getMore_does_not_send_readPreference(self):
1413+
listener = WhiteListEventListener('find', 'getMore')
1414+
client = rs_or_single_client(
1415+
event_listeners=[listener])
1416+
self.addCleanup(client.close)
1417+
coll = client[self.db.name].test
1418+
1419+
coll.delete_many({})
1420+
coll.insert_many([{} for _ in range(5)])
1421+
self.addCleanup(coll.drop)
1422+
1423+
list(coll.find(batch_size=3))
1424+
started = listener.results['started']
1425+
self.assertEqual(2, len(started))
1426+
self.assertEqual('find', started[0].command_name)
1427+
self.assertIn('$readPreference', started[0].command)
1428+
self.assertEqual('getMore', started[1].command_name)
1429+
self.assertNotIn('$readPreference', started[1].command)
1430+
14101431

14111432
class TestRawBatchCursor(IntegrationTest):
14121433
def test_find_raw(self):

0 commit comments

Comments
 (0)