From 25ca20710a73961594f8a8763656a09e94596c57 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Mon, 18 Nov 2024 16:22:38 -0800 Subject: [PATCH] PYTHON-4414 Fix order, add then discard is safer --- pymongo/asynchronous/pool.py | 2 +- pymongo/synchronous/pool.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pymongo/asynchronous/pool.py b/pymongo/asynchronous/pool.py index 1163018775..ca0cebd417 100644 --- a/pymongo/asynchronous/pool.py +++ b/pymongo/asynchronous/pool.py @@ -1296,8 +1296,8 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A conn = AsyncConnection(sock, self, self.address, conn_id) # type: ignore[arg-type] async with self.lock: - self.active_contexts.discard(tmp_context) self.active_contexts.add(conn.cancel_context) + self.active_contexts.discard(tmp_context) if tmp_context.cancelled: conn.cancel_context.cancel() try: diff --git a/pymongo/synchronous/pool.py b/pymongo/synchronous/pool.py index ea52848e61..86baf15b9a 100644 --- a/pymongo/synchronous/pool.py +++ b/pymongo/synchronous/pool.py @@ -1290,8 +1290,8 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect conn = Connection(sock, self, self.address, conn_id) # type: ignore[arg-type] with self.lock: - self.active_contexts.discard(tmp_context) self.active_contexts.add(conn.cancel_context) + self.active_contexts.discard(tmp_context) if tmp_context.cancelled: conn.cancel_context.cancel() try: