diff --git a/pymongo/asynchronous/monitor.py b/pymongo/asynchronous/monitor.py index 8ed4078fd7..6885fa090a 100644 --- a/pymongo/asynchronous/monitor.py +++ b/pymongo/asynchronous/monitor.py @@ -242,6 +242,8 @@ async def _run(self) -> None: finally: if self._executor._stopped: await self._rtt_monitor.close() + # Make sure no connections outlive close(). + await self._reset_connection() async def _check_server(self) -> ServerDescription: """Call hello or read the next streaming response. @@ -440,6 +442,10 @@ async def _run(self) -> None: await self.close() except Exception: await self._pool.reset() + finally: + if self._executor._stopped: + # Make sure no connections outlive close(). + await self._pool.reset() async def _ping(self) -> float: """Run a "hello" command and return the RTT.""" diff --git a/pymongo/synchronous/monitor.py b/pymongo/synchronous/monitor.py index cbc5796a8a..9708c6e1a2 100644 --- a/pymongo/synchronous/monitor.py +++ b/pymongo/synchronous/monitor.py @@ -240,6 +240,8 @@ def _run(self) -> None: finally: if self._executor._stopped: self._rtt_monitor.close() + # Make sure no connections outlive close(). + self._reset_connection() def _check_server(self) -> ServerDescription: """Call hello or read the next streaming response. @@ -438,6 +440,10 @@ def _run(self) -> None: self.close() except Exception: self._pool.reset() + finally: + if self._executor._stopped: + # Make sure no connections outlive close(). + self._pool.reset() def _ping(self) -> float: """Run a "hello" command and return the RTT."""