You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PYTHON-6226 AsyncMongoClient stays stuck in ReplicaSetNoPrimary after a monitor task cancels itself - #3180
A replica set member can remove itself from the topology with its own hello response, for example during a node restart. The monitor's executor then cancelled its own task, so the monitor never ran again and the client stayed in ReplicaSetNoPrimary until restart.
A monitor closing its own executor no longer cancels itself.
Closing the executor from another task, or from a thread without a running event loop, keeps its previous behavior.
Test Plan
The self-close unit test and the integration test fail before the fix and pass after it.
Executor unit tests cover closing from the monitor's own task and from a thread without an event loop.
An integration test fakes a two-node replica set, removes each node from the topology for a few seconds, and requires the client to recover (skips on the sync driver, which is not affected).
Also run: the full periodic executor suite, the sync mirrors of the new tests, just typing, and just lint-manual.
Checklist
Checklist for Author
Did you update the changelog (if necessary)?
Is there test coverage?
Is any followup work tracked in a JIRA ticket? If so, add link(s). Not yet: the ticket's suggested extra hardening has no ticket.
Checklist for Reviewer
Does the title of the PR reference a JIRA Ticket?
Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
Is all relevant documentation (README or docstring) updated?
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The test can hang on Python versions where Server.wait_closed() waits for accepted connections to disconnect, so client.close() must be awaited before waiting for the fake servers to close.
Fixes async monitor self-cancellation that could leave AsyncMongoClient stuck in ReplicaSetNoPrimary after a replica set member removes itself from the topology.
Changes:
Prevents an executor from cancelling its own task while preserving cancellation from other callers.
Adds executor lifecycle tests and a two-node replica set recovery regression test.
Documents the fix in the changelog.
File
Description
test/test_periodic_executor.py
Sync mirror of executor lifecycle tests.
test/test_discovery_and_monitoring.py
Sync mirror with the async-only recovery test skipped.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Cancellation during monitor cleanup can leave the application pool unclosed after its server is removed, preventing later cleanup from retrying the close.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The new test cancels a real task from another thread, causing a RuntimeError and assertion failure when asyncio debug mode is enabled.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Avoid cancelling a real task from another thread in the test
test/asynchronous/test_periodic_executor.py:220
With asyncio debug mode enabled (for example, PYTHONASYNCIODEBUG=1), this worker cancels a real task on another thread's running event loop, raising RuntimeError and failing this assertion. Since the PR intentionally preserves existing off-loop cancellation behavior, use a recording task mock to test the current_task() fallback, then cancel and join the real executor on the event-loop thread. Regenerate the synchronous test mirror after updating the async source.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PYTHON-6226
Changes in this PR
A replica set member can remove itself from the topology with its own
helloresponse, for example during a node restart. The monitor's executor then cancelled its own task, so the monitor never ran again and the client stayed inReplicaSetNoPrimaryuntil restart.Test Plan
The self-close unit test and the integration test fail before the fix and pass after it.
just typing, andjust lint-manual.Checklist
Checklist for Author
Checklist for Reviewer