Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-5044

Successive AsyncMongoClients on a single loop always timeout on server selection

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 4.11
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Python Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Context

      Any AsyncMongoClient created after the first on a given asyncio event loop will experience a server selection timeout on its first operation. With the default timeout configuration, this will cause a 20-second delay for this first operation, for each client after the first.

      A brief example:

      for i in range(2): 
          start = time.monotonic() client = AsyncMongoClient()     
          await client.admin.command("ping") 
          await client.close() 
          print(f"Client {i} took {time.monotonic() - start} seconds")  
      

      This will output the following:

      Client 0 took 0.004705582978203893 seconds
      Client 1 took 20.0034456250141374767 seconds

      This timeout is caused by the `_async_receive` coroutine waited on by `async_receive_data` not properly cancelling when its parent task is cancelled. Explicitly cancelling this task when an `asyncio.CancelledError` is raised solves the problem.

      Definition of done

      AsyncMongoClient instances after the first on a given asyncio event loop should not experience a server selection timeout before their first operation succeeds.

            Assignee:
            noah.stapp@mongodb.com Noah Stapp
            Reporter:
            noah.stapp@mongodb.com Noah Stapp
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              None
              None
              None
              None