Pymongo throws error saying No replica set members match selector "Primary()"

I’m encountering the following error when trying to fetch banned users and chats using the Motor library with MongoDB Atlas:

Relevant code snippet

b_users, b_chats = await db.get_banned()
b_chats = [chat['id'] async for chat in chats]

Error Message:

pymongo.errors.ServerSelectionTimeoutError: No replica set members match selector "Primary()", Timeout: 30s, Topology Description: <TopologyDescription id: 65217586b0ced5475d5e4b50, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.zaiqq.mongodb.net', 27017) server_type: RSSecondary, rtt: 0.0024830238521099095>, <ServerDescription ('cluster0-shard-00-01.zaiqq.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('cluster0-shard-00-01.zaiqq.mongodb.net:27017: The read operation timed out')>, <ServerDescription ('cluster0-shard-00-02.zaiqq.mongodb.net', 27017) server_type: RSSecondary, rtt: 0.0026440465152263643>]>

Traceback snippet

Traceback (most recent call last):
  File "/root/tessa/bot.py", line 112, in <module>
    app.run()
  File "/root/tessa/venv/lib/python3.10/site-packages/pyrogram/methods/utilities/run.py", line 80, in run
    run(self.start())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/root/tessa/bot.py", line 41, in start
    b_users, b_chats = await db.get_banned()
  File "/root/tessa/database/users_chats_db.py", line 82, in get_banned
    b_chats = [chat['id'] async for chat in chats]
  File "/root/tessa/database/users_chats_db.py", line 82, in <listcomp>
    b_chats = [chat['id'] async for chat in chats]
  File "/root/tessa/venv/lib/python3.10/site-packages/motor/core.py", line 1158, in next
    if self.alive and (self._buffer_size() or await self._get_more()):
  File "/usr/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/root/tessa/venv/lib/python3.10/site-packages/pymongo/cursor.py", line 1155, in _refresh
    self.__send_message(q)
  File "/root/tessa/venv/lib/python3.10/site-packages/pymongo/cursor.py", line 1044, in __send_message
    response = client._run_operation(
  File "/root/tessa/venv/lib/python3.10/site-packages/pymongo/mongo_client.py", line 1424, in _run_operation
    return self._retryable_read(
  File "/root/tessa/venv/lib/python3.10/site-packages/pymongo/mongo_client.py", line 1514, in _retryable_read
    server = self._select_server(
  File "/root/tessa/venv/lib/python3.10/site-packages/pymongo/mongo_client.py", line 1346, in _select_server
    server = topology.select_server(server_selector)
  File "/root/tessa/venv/lib/python3.10/site-packages/pymongo/topology.py", line 244, in select_server
    return random.choice(self.select_servers(selector,
  File "/root/tessa/venv/lib/python3.10/site-packages/pymongo/topology.py", line 202, in select_servers
    server_descriptions = self._select_servers_loop(
  File "/root/tessa/venv/lib/python3.10/site-packages/pymongo/topology.py", line 218, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: No replica set members match selector "Primary()", Timeout: 30s, Topology Description: <TopologyDescription id: 65217586b0ced5475d5e4b50, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.zaiqq.mongodb.net', 27017) server_type: RSSecondary, rtt: 0.0024830238521099095>, <ServerDescription ('cluster0-shard-00-01.zaiqq.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('cluster0-shard-00-01.zaiqq.mongodb.net:27017: The read operation timed out')>, <ServerDescription ('cluster0-shard-00-02.zaiqq.mongodb.net', 27017) server_type: RSSecondary, rtt: 0.0026440465152263643>]>
  • I’m using the Motor library to connect to MongoDB Atlas.
  • The error occurs when attempting to fetch each document from cursor returned by find()
  • Using Atlas free tier
  • Already added 0.0.0.0/0 in Network access

Any assistance or insights into resolving this issue would be greatly appreciated.

Hi @MoviezHood and welcome to MongoDB community forums!!

The error message that you are observing here mostly occurs when the application is unable to resolve the Primary host on the network.
In other words, the application tries to connect with the primary but was unable to connect may be because of a network partition. You can refer to the Automatic Failovers for further details

In saying, it would be helpful for us to triage the issue if possible in depth if you could share the following informations:

  1. Can you share the output for rs.status() from the deployment ?

Are you seeing the issue only while connecting the application with the Atlas cluster or this occurs while using the cluster outside using shell or Compass as well?

Finally, can you also try connecting the application using the connection string from the connection modal that specifies all 3 hostnames instead of the SRV record.
You can get it by choosing the Python for the Driver option and then Choose the Version 3.4 or later for the version option.

Warn regards
Aasawari

1 Like

Hi @Aasawari and thank you for your responce.

Unfortunately, this issue occurs at random so I cannot reproduce this issue. And during this time when it happens I won’t be able to connect to Shell or Compass. So that makes this issue in answering your 2 questions. But I will definitely try the new string with 3 hostnames when this issue arises and let you know if that fixes it that time.

Regards,
Ben

1 Like