From 9dd99a71b8aee21fdb244bdba8bb6246f60427dc Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Wed, 28 Oct 2026 15:01:33 -0400 Subject: [PATCH 1/2] PYTHON-6200 Wait for monitor threads/tasks to finish in test_close_closes_sockets --- test/asynchronous/test_client.py | 4 ++++ test/test_client.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/test/asynchronous/test_client.py b/test/asynchronous/test_client.py index a39b5b1384..2b61dda182 100644 --- a/test/asynchronous/test_client.py +++ b/test/asynchronous/test_client.py @@ -1456,6 +1456,10 @@ async def test_close_closes_sockets(self): topology = client._topology await client.close() for server in topology._servers.values(): + # Wait for monitor tasks to finish. + # Can be removed once PYTHON-1634 is implemented. + await server._monitor._executor.join(10) + await server._monitor._rtt_monitor._executor.join(10) self.assertFalse(server._pool.conns) self.assertTrue(server._monitor._executor._stopped) self.assertTrue(server._monitor._rtt_monitor._executor._stopped) diff --git a/test/test_client.py b/test/test_client.py index 378f6c9f8f..dff1bea01f 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -1425,6 +1425,10 @@ def test_close_closes_sockets(self): topology = client._topology client.close() for server in topology._servers.values(): + # Wait for monitor tasks to finish. + # Can be removed once PYTHON-1634 is implemented. + server._monitor._executor.join(10) + server._monitor._rtt_monitor._executor.join(10) self.assertFalse(server._pool.conns) self.assertTrue(server._monitor._executor._stopped) self.assertTrue(server._monitor._rtt_monitor._executor._stopped) From 217dbcfd951c081bc155342b13c17da5470fadc8 Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Thu, 29 Oct 2026 10:45:44 -0400 Subject: [PATCH 2/2] PYTHON-6200 Reset monitor pools to prevent races with close() --- pymongo/asynchronous/monitor.py | 6 ++++++ pymongo/synchronous/monitor.py | 6 ++++++ 2 files changed, 12 insertions(+) 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."""