-
Type:
Bug
-
Resolution: Duplicate
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
🔵 Done
-
Python Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Detailed steps to reproduce the problem?
I encounter this problem when using pymongo within a fastapi application:
```
mongo_client = AsyncMongoClient(
  "mongo:27017",
  directConnection=True,
 )
command = await mongo_client["test_db_name"].command({"buildInfo": 1})
```
Some additional information:
1. When downgrade to pymongo==4.14, the problem is gone
2. I tried different urls, such as "mongo:27017/?replicaSet=rs0", it does not work
3. remove directConnection does not work
4. When creating a standalone script, the code could successfully execute. I suspect it is related to both v4.15 and uvicorn
5. I starts mongodb replicaset in docker-compose, the execution environment is a dev container. I can connect to the database via mongosh or a standalone script (as I mentioned before). The command I start mongodb within the mongo container is `–replSet rs0 --bind_ip_all --port 27017`. It is latest mongo image (I think it is unrelated)
```
  _  _File "/workspaces/project/.venv/lib/python3.12/site-packages/beanie/odm/utils/init.py", line 574, in  Â
       init_document                                                  Â
         build_info = await self.database.command({"buildInfo": 1})                         Â
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                         Â
        File "/workspaces/project/.venv/lib/python3.12/site-packages/pymongo/_csot.py", line 115, in      Â
       csot_wrapper                                                  Â
         return await func(self, *args, **kwargs)                                  Â
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                  Â
        File "/workspaces/project/.venv/lib/python3.12/site-packages/pymongo/asynchronous/database.py", line  Â
       931, in command                                                 Â
         async with await self._client._conn_for_reads(                               Â
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                               Â
        File "/workspaces/project/.venv/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line
       1889, in _conn_for_reads                                            Â
         server = await self._select_server(read_preference, session, operation)                   Â
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                   Â
        File "/workspaces/project/.venv/lib/python3.12/site-packages/pymongo/asynchronous/mongo_client.py", line
       1837, in _select_server                                             Â
         server = await topology.select_server(                                   Â
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                   Â
        File "/workspaces/project/.venv/lib/python3.12/site-packages/pymongo/asynchronous/topology.py", line  Â
       409, in select_server                                              Â
         server = await self._select_server(                                     Â
             ^^^^^^^^^^^^^^^^^^^^^^^^^^                                     Â
        File "/workspaces/project/.venv/lib/python3.12/site-packages/pymongo/asynchronous/topology.py", line  Â
       387, in _select_server                                             Â
         servers = await self.select_servers(                                    Â
              ^^^^^^^^^^^^^^^^^^^^^^^^^^                                    Â
        File "/workspaces/project/.venv/lib/python3.12/site-packages/pymongo/asynchronous/topology.py", line  Â
       294, in select_servers                                             Â
         server_descriptions = await self._select_servers_loop(                           Â
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                           Â
        File "/workspaces/project/.venv/lib/python3.12/site-packages/pymongo/asynchronous/topology.py", line  Â
       344, in _select_servers_loop                                          Â
         raise ServerSelectionTimeoutError(                                     Â
       pymongo.errors.ServerSelectionTimeoutError: mongo:27017: timed out (configured timeouts: socketTimeoutMS:    Â
       20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id:      Â
       68c2dedf46aaf379e2794aa4, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mongo', 27017)   Â
       server_type: Unknown, rtt: None, error=NetworkTimeout('mongo:27017: timed out (configured timeouts:       Â
       socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>
```
Definition of done: what must be done to consider the task complete?
The exact Python version used, with patch level:
3.12.3
The exact version of PyMongo used, with patch level:
4.15.0
Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.
docker-compose:
```
mongo:
  image: mongo:latest
  restart: unless-stopped
  volumes:
   - mongodb-data:/data/db
  # https://medium.com/workleap/the-only-local-mongodb-replica-set-with-docker-compose-guide-youll-ever-need-2f0b74dd8384
  command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
  healthcheck:
   test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'mongo:27017'}]}) }" | mongosh --port 27017 --quiet
   interval: 5s
   timeout: 30s
   start_period: 0s
   start_interval: 1s
   retries: 30
  networks:
   - dev-network
```
The operating system and version (e.g. Windows 7, OSX 10.8, ...)
Web framework or asynchronous network library used, if any, with version (e.g. Django 1.7, mod_wsgi 4.3.0, gevent 1.0.1, Tornado 4.0.2, ...)
Â
fastapi 0.116.1
uvicorn 0.35.0
Security Vulnerabilities
If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here
Â
Â
Â
Â
- duplicates
-
PYTHON-5544 AsyncMongoClient: timed out any request when using uvloop
-
- Investigating
-
- is duplicated by
-
PYTHON-5544 AsyncMongoClient: timed out any request when using uvloop
-
- Investigating
-