From 67759cdd6d2d2623a5c239234b4e5a1eb4db9c92 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Thu, 27 Feb 2025 16:25:01 -0800 Subject: [PATCH 1/2] PYTHON-5167 Async Condition.wait has to reacquire the lock even after a timeout --- pymongo/lock.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pymongo/lock.py b/pymongo/lock.py index 6bf7138017..c8fb3801a2 100644 --- a/pymongo/lock.py +++ b/pymongo/lock.py @@ -82,9 +82,11 @@ def _release_locks() -> None: async def _async_cond_wait(condition: Condition, timeout: Optional[float]) -> bool: + fut = asyncio.ensure_future(condition.wait()) try: - return await wait_for(condition.wait(), timeout) + return await wait_for(fut, timeout) except asyncio.TimeoutError: + await fut return False From ce4eeb2c9d178df6f0e8dfedd132d7f3363b8fe3 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Thu, 27 Feb 2025 16:42:34 -0800 Subject: [PATCH 2/2] PYTHON-5167 Shield wait() from wait_for cancel --- pymongo/lock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymongo/lock.py b/pymongo/lock.py index c8fb3801a2..f05ad52b0f 100644 --- a/pymongo/lock.py +++ b/pymongo/lock.py @@ -84,7 +84,7 @@ def _release_locks() -> None: async def _async_cond_wait(condition: Condition, timeout: Optional[float]) -> bool: fut = asyncio.ensure_future(condition.wait()) try: - return await wait_for(fut, timeout) + return await wait_for(asyncio.shield(fut), timeout) except asyncio.TimeoutError: await fut return False