Search code examples
pythonmongodbpymongoreplicaset

MongoReplicaSetClient fails unexpectedly during primary re-election


I'm working on migrating our codebase which refers to mongo resources to using replication and I am seeing some unexpected behavior from the MongoReplicaSetClient. The client works fine unless I attempt a write during the time that a new primary is being is being elected.

The simple test I'm running is:
* Start up a 3 node replica set
* Connect with MongoReplicaSetClient('localhost:27017,localhost:27018,localhost:27017', replicaSet='rs0'
* Perform a write
* Kill the primary and immediately perform a write
* Perform another write after primary election has completed

All the above steps work fine except for the second to last where the primary is down. According to the MongoReplicaSetClient documentation I would expect an AutoReconnect error to be thrown for all write attempts executed while no primary exists. However, what I'm seeing instead is a generic AssertionError.

db = MongoReplicaSetClient('localhost:27017,localhost:27018,localhost:27017', replicaSet='rs0', w=2).testdb
db.testcol.save({'test': 1})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-x86_64/egg/pymongo/collection.py", line 266, in save
  File "build/bdist.linux-x86_64/egg/pymongo/collection.py", line 362, in insert
  File "build/bdist.linux-x86_64/egg/pymongo/message.py", line 248, in _do_batched_insert
  File "build/bdist.linux-x86_64/egg/pymongo/mongo_replica_set_client.py", line 1456, in _send_message
  File "build/bdist.linux-x86_64/egg/pymongo/pool.py", line 397, in maybe_return_socket
AssertionError

Does anyone know if this is expected behavior? I'd like to handle this type of event properly or even block all writes until election has completed.


Solution

  • The reported error is a bug, which we'll fix in the next release, PyMongo 2.7. The bug is described here: https://jira.mongodb.org/browse/PYTHON-601