Skip to content

Commit 68c4a6e

Browse files
committed
PYTHON-5053 - AsyncMongoClient.close() should await all background tasks
1 parent 702c86c commit 68c4a6e

File tree

9 files changed

+26
-0
lines changed

9 files changed

+26
-0
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,8 @@ async def close(self) -> None:
15641564
if self._encrypter:
15651565
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
15661566
await self._encrypter.close()
1567+
if not _IS_SYNC:
1568+
await self._kill_cursors_executor.join()
15671569
self._closed = True
15681570

15691571
if not _IS_SYNC:

pymongo/asynchronous/monitor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ async def close(self) -> None:
111111
open() restarts the monitor after closing.
112112
"""
113113
self.gc_safe_close()
114+
if not _IS_SYNC:
115+
await self._executor.join()
114116

115117
async def join(self, timeout: Optional[int] = None) -> None:
116118
"""Wait for the monitor to stop."""
@@ -191,6 +193,8 @@ def gc_safe_close(self) -> None:
191193

192194
async def close(self) -> None:
193195
self.gc_safe_close()
196+
if not _IS_SYNC:
197+
await self._executor.join()
194198
await self._rtt_monitor.close()
195199
# Increment the generation and maybe close the socket. If the executor
196200
# thread has the socket checked out, it will be closed when checked in.
@@ -460,6 +464,8 @@ async def close(self) -> None:
460464
self.gc_safe_close()
461465
# Increment the generation and maybe close the socket. If the executor
462466
# thread has the socket checked out, it will be closed when checked in.
467+
if not _IS_SYNC:
468+
await self._executor.join()
463469
await self._pool.reset()
464470

465471
async def add_sample(self, sample: float) -> None:

pymongo/asynchronous/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ async def close(self) -> None:
115115
)
116116

117117
await self._monitor.close()
118+
if not _IS_SYNC:
119+
await self._monitor.join()
118120
await self._pool.close()
119121

120122
def request_check(self) -> None:

pymongo/asynchronous/topology.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ async def close(self) -> None:
705705
# Stop SRV polling thread.
706706
if self._srv_monitor:
707707
await self._srv_monitor.close()
708+
if not _IS_SYNC:
709+
await self._srv_monitor.join()
708710

709711
self._opened = False
710712
self._closed = True

pymongo/periodic_executor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def close(self, dummy: Any = None) -> None:
7575
callback; see monitor.py.
7676
"""
7777
self._stopped = True
78+
if self._task is not None:
79+
self._task.cancel()
7880

7981
async def join(self, timeout: Optional[int] = None) -> None:
8082
if self._task is not None:

pymongo/synchronous/mongo_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,8 @@ def close(self) -> None:
15581558
if self._encrypter:
15591559
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
15601560
self._encrypter.close()
1561+
if not _IS_SYNC:
1562+
self._kill_cursors_executor.join()
15611563
self._closed = True
15621564

15631565
if not _IS_SYNC:

pymongo/synchronous/monitor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ def close(self) -> None:
111111
open() restarts the monitor after closing.
112112
"""
113113
self.gc_safe_close()
114+
if not _IS_SYNC:
115+
self._executor.join()
114116

115117
def join(self, timeout: Optional[int] = None) -> None:
116118
"""Wait for the monitor to stop."""
@@ -191,6 +193,8 @@ def gc_safe_close(self) -> None:
191193

192194
def close(self) -> None:
193195
self.gc_safe_close()
196+
if not _IS_SYNC:
197+
self._executor.join()
194198
self._rtt_monitor.close()
195199
# Increment the generation and maybe close the socket. If the executor
196200
# thread has the socket checked out, it will be closed when checked in.
@@ -460,6 +464,8 @@ def close(self) -> None:
460464
self.gc_safe_close()
461465
# Increment the generation and maybe close the socket. If the executor
462466
# thread has the socket checked out, it will be closed when checked in.
467+
if not _IS_SYNC:
468+
self._executor.join()
463469
self._pool.reset()
464470

465471
def add_sample(self, sample: float) -> None:

pymongo/synchronous/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def close(self) -> None:
115115
)
116116

117117
self._monitor.close()
118+
if not _IS_SYNC:
119+
self._monitor.join()
118120
self._pool.close()
119121

120122
def request_check(self) -> None:

pymongo/synchronous/topology.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,8 @@ def close(self) -> None:
703703
# Stop SRV polling thread.
704704
if self._srv_monitor:
705705
self._srv_monitor.close()
706+
if not _IS_SYNC:
707+
self._srv_monitor.join()
706708

707709
self._opened = False
708710
self._closed = True

0 commit comments

Comments
 (0)