Repository navigation
PYTHON-6187 Publish CommandFailedEvent for cancelled commands - #3226
Open
aclark4life wants to merge 2 commits into
Open
aclark4life wants to merge 2 commits into
aclark4life wants to merge 2 commits into
Conversation
aclark4life
marked this pull request as ready for review
November 13, 2026 23:27
aclark4life
requested review from
sleepyStick
and
a balanced review from Copilot
November 13, 2026 23:27
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The cancellation test relies on a 100 ms sleep rather than confirming that find has started, so it can fail spuriously and needs bounded event synchronization with guaranteed task cleanup.
1 open issue
What changed in this PR
This PR fixes command monitoring for interrupted operations so cancellation publishes a corresponding failure event and log message before the exception propagates.
Changes:
- Catch
BaseExceptionin command runners and provide fallback messages for empty exception text. - Add an async cancellation regression test checking correlated command events.
- Document the fix in the 4.21.0 changelog.
| File | Description |
|---|---|
test/asynchronous/test_async_cancellation.py |
Adds cancellation-event regression coverage. |
pymongo/synchronous/command_runner.py |
Mirrors interruption handling in the generated sync runner. |
pymongo/message.py |
Broadens the exception-conversion annotation to BaseException. |
pymongo/asynchronous/command_runner.py |
Publishes failure telemetry for cancelled commands. |
doc/changelog.rst |
Records the cancellation-monitoring fix. |
Review effort: Balanced
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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-6187
Changes in this PR
_run_commandin both the async and sync command runners now catchesBaseExceptioninstead ofException, so a cancellation (asyncio.CancelledError) and, in the sync driver,KeyboardInterrupt/SystemExitpublish theCommandFailedEventand "Command failed" log message before the exception propagates, as the Command Logging and Monitoring spec guarantees.""(CancelledError,KeyboardInterrupt,SystemExit) get anerrmsgofoperation interrupted by <ClassName>instead of an empty message._convert_exceptionnow annotates its parameter asBaseException.Test Plan
test/asynchronous/test_async_cancellation.py: cancel a blockedfind(failCommandblockConnection) and assert a correlatingCommandFailedEventwitherrtype == "CancelledError". Verified it fails without the fix and passes with it.test_monitoring.py,test_command_monitoring.py,test_command_logging.py, andtest_async_cancellation.pypass for both async and sync suites (167 passed).just typingpasses.Checklist
Checklist for Author
Checklist for Reviewer