diff --git a/doc/changelog.rst b/doc/changelog.rst index dc0e697fdf..e0ea49b019 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -34,6 +34,10 @@ Bug fixes due to a CPython memory leak bug (`python/cpython#116810 `_). The bug was fixed in Python 3.12.7 and Python 3.13.1. Users of Python 3.12 and 3.13 should upgrade to at least those versions (`PYTHON-6182`_). +- Fixed a bug where ``AsyncMongoClient`` could not recover after a replica set + member removed itself from the topology with its own ``hello`` response (for + example, during a node restart): the monitor's executor cancelled its own + task, so the monitor never ran again (`PYTHON-6226`_). .. _PYTHON-5929: https://jira.mongodb.org/browse/PYTHON-5929 .. _PYTHON-6172: https://jira.mongodb.org/browse/PYTHON-6172 @@ -41,6 +45,7 @@ Bug fixes .. _PYTHON-5873: https://jira.mongodb.org/browse/PYTHON-5873 .. _PYTHON-6145: https://jira.mongodb.org/browse/PYTHON-6145 .. _PYTHON-6182: https://jira.mongodb.org/browse/PYTHON-6182 +.. _PYTHON-6226: https://jira.mongodb.org/browse/PYTHON-6226 Changes in Version 4.19.0 (2026/10/14) -------------------------------------- diff --git a/pymongo/asynchronous/server.py b/pymongo/asynchronous/server.py index 8764bfcb28..0e2656c4a6 100644 --- a/pymongo/asynchronous/server.py +++ b/pymongo/asynchronous/server.py @@ -73,8 +73,12 @@ async def close(self) -> None: """ self._sdam.server_closed(self._description.address) - await self._monitor.close() - await self._pool.close() + try: + await self._monitor.close() + finally: + # The pool must close even if cancellation or an error + # interrupts the monitor close (PYTHON-6226). + await self._pool.close() def request_check(self) -> None: """Check the server's state soon.""" diff --git a/pymongo/asynchronous/topology.py b/pymongo/asynchronous/topology.py index 8f1b4b1a21..560466387a 100644 --- a/pymongo/asynchronous/topology.py +++ b/pymongo/asynchronous/topology.py @@ -704,10 +704,13 @@ async def _update_servers(self) -> None: for address, server in list(self._servers.items()): if not self._description.has_server(address): - await server.close() + # Update our state before closing: close() can cancel the + # calling task, so the stale server must be recorded as + # removed before close() runs, not after (PYTHON-6226). if not _IS_SYNC: self._monitor_tasks.append(server._monitor) self._servers.pop(address) + await server.close() async def cleanup_monitors(self) -> None: tasks = [] diff --git a/pymongo/periodic_executor.py b/pymongo/periodic_executor.py index 88149115ff..ec0a344877 100644 --- a/pymongo/periodic_executor.py +++ b/pymongo/periodic_executor.py @@ -75,7 +75,16 @@ def close(self, dummy: Any = None) -> None: callback; see monitor.py. """ self._stopped = True - if self._task is not None: + if self._task is None: + return + try: + current = asyncio.current_task() + except RuntimeError: + # close() also runs as a weakref callback, off the event loop. + current = None + # Never cancel the calling task: self-cancellation aborts close() + # mid-way and leaves the monitor dead (PYTHON-6226). + if self._task is not current: self._task.cancel() async def join(self, timeout: Optional[int] = None) -> None: diff --git a/pymongo/synchronous/server.py b/pymongo/synchronous/server.py index 0faed74f76..cb16752173 100644 --- a/pymongo/synchronous/server.py +++ b/pymongo/synchronous/server.py @@ -73,8 +73,12 @@ def close(self) -> None: """ self._sdam.server_closed(self._description.address) - self._monitor.close() - self._pool.close() + try: + self._monitor.close() + finally: + # The pool must close even if cancellation or an error + # interrupts the monitor close (PYTHON-6226). + self._pool.close() def request_check(self) -> None: """Check the server's state soon.""" diff --git a/pymongo/synchronous/topology.py b/pymongo/synchronous/topology.py index 9accc15dc1..8ffe447cc5 100644 --- a/pymongo/synchronous/topology.py +++ b/pymongo/synchronous/topology.py @@ -702,10 +702,13 @@ def _update_servers(self) -> None: for address, server in list(self._servers.items()): if not self._description.has_server(address): - server.close() + # Update our state before closing: close() can cancel the + # calling task, so the stale server must be recorded as + # removed before close() runs, not after (PYTHON-6226). if not _IS_SYNC: self._monitor_tasks.append(server._monitor) self._servers.pop(address) + server.close() def cleanup_monitors(self) -> None: tasks = [] diff --git a/test/asynchronous/test_discovery_and_monitoring.py b/test/asynchronous/test_discovery_and_monitoring.py index a3129812b1..ca44bfbac2 100644 --- a/test/asynchronous/test_discovery_and_monitoring.py +++ b/test/asynchronous/test_discovery_and_monitoring.py @@ -20,11 +20,14 @@ import os import platform import socketserver +import struct import sys import threading import time +import unittest from asyncio import StreamReader, StreamWriter from pathlib import Path +from typing import Any from pymongo.asynchronous.pool import AsyncConnection from pymongo.errors import ConnectionFailure @@ -38,6 +41,8 @@ from unittest.mock import patch from bson import Timestamp, json_util +from bson import decode as bson_decode +from bson import encode as bson_encode from pymongo import common, monitoring from pymongo.asynchronous.settings import TopologySettings from pymongo.asynchronous.topology import Topology @@ -58,7 +63,7 @@ ServerHeartbeatStartedEvent, ) from pymongo.server_description import SERVER_TYPE, ServerDescription -from pymongo.topology_description import TOPOLOGY_TYPE +from pymongo.topology_description import TOPOLOGY_TYPE, TopologyDescription from pymongo.topology_shared import _ErrorContext from test.asynchronous import ( AsyncIntegrationTest, @@ -679,6 +684,156 @@ async def handle_client(reader: StreamReader, writer: StreamWriter): ) +class TestMonitorRecoversAfterSelfRemoval(AsyncUnitTest): + @unittest.skipUnless(not _IS_SYNC, "PYTHON-6226: monitor task self-cancellation is async-only") + async def test_monitor_recovers_after_self_removal(self): + # Regression test for PYTHON-6226: a monitor that closed its own + # server used to cancel itself, leaving the client stuck in + # ReplicaSetNoPrimary. The client must recover. + + # Fake two-node replica set on ephemeral ports. + role: dict[int, str] = {} + hosts: list[str] = [] + + def make_handler(port_box): + async def handle(reader: StreamReader, writer: StreamWriter): + port = port_box["port"] + try: + while True: + length, req_id, _, _ = struct.unpack( + # The ignore is for the sync mirror, where + # unasync turns this read into a bare call. + "