Skip to content

Commit 2b058a2

Browse files
committed
PYTHON-5044 - Successive AsyncMongoClients on a single loop always timeout on server selection
1 parent cfe7784 commit 2b058a2

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,8 @@ async def close(self) -> None:
15651565
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
15661566
await self._encrypter.close()
15671567
self._closed = True
1568+
# Yield to the asyncio event loop so all executor tasks properly exit after cancellation
1569+
await asyncio.sleep(0)
15681570

15691571
if not _IS_SYNC:
15701572
# Add support for contextlib.aclosing.

pymongo/periodic_executor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def close(self, dummy: Any = None) -> None:
7575
callback; see monitor.py.
7676
"""
7777
self._stopped = True
78+
if self._task:
79+
self._task.cancel()
7880

7981
async def join(self, timeout: Optional[int] = None) -> None:
8082
if self._task is not None:

pymongo/synchronous/mongo_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,7 @@ def close(self) -> None:
15591559
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
15601560
self._encrypter.close()
15611561
self._closed = True
1562+
# Yield to the asyncio event loop so all executor tasks properly exit after cancellation
15621563

15631564
if not _IS_SYNC:
15641565
# Add support for contextlib.closing.

0 commit comments

Comments
 (0)