pymongo 4.15+fastapi fails to connect to replicaset

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • 🔵 Done
    • Python Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • 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
       
       
       

       

            Assignee:
            Unassigned
            Reporter:
            Yifei N/A
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: