diff --git a/pymongo/asynchronous/mongo_client.py b/pymongo/asynchronous/mongo_client.py index 8319755d0f..2f13b92c6b 100644 --- a/pymongo/asynchronous/mongo_client.py +++ b/pymongo/asynchronous/mongo_client.py @@ -854,6 +854,7 @@ def __init__( server_monitoring_mode=options.server_monitoring_mode, ) + self._opened = False self._init_background() if _IS_SYNC and connect: @@ -1528,9 +1529,11 @@ async def _get_topology(self) -> Topology: If this client was created with "connect=False", calling _get_topology launches the connection process in the background. """ - await self._topology.open() - async with self._lock: - self._kill_cursors_executor.open() + if not self._opened: + await self._topology.open() + async with self._lock: + self._kill_cursors_executor.open() + self._opened = True return self._topology @contextlib.asynccontextmanager diff --git a/pymongo/synchronous/mongo_client.py b/pymongo/synchronous/mongo_client.py index 69bfa7a9d5..20d1b3f82f 100644 --- a/pymongo/synchronous/mongo_client.py +++ b/pymongo/synchronous/mongo_client.py @@ -853,6 +853,7 @@ def __init__( server_monitoring_mode=options.server_monitoring_mode, ) + self._opened = False self._init_background() if _IS_SYNC and connect: @@ -1527,9 +1528,11 @@ def _get_topology(self) -> Topology: If this client was created with "connect=False", calling _get_topology launches the connection process in the background. """ - self._topology.open() - with self._lock: - self._kill_cursors_executor.open() + if not self._opened: + self._topology.open() + with self._lock: + self._kill_cursors_executor.open() + self._opened = True return self._topology @contextlib.contextmanager