-
Type:
Task
-
Resolution: Fixed
-
Priority:
Unknown
-
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.