PYTHON-5809 - Send afterClusterTime on writes in causally-consistent sessions#2851
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates PyMongo’s session command decoration so that write commands executed in causally consistent sessions (and not in a transaction) include readConcern.afterClusterTime once the session has an operationTime, preventing potentially stale primary writes.
Changes:
- Add a shared allowlist of write command names and use it in
ClientSession._apply_to/AsyncClientSession._apply_toto attachafterClusterTimeon eligible writes in causally consistent sessions. - Extend the unified test runner to support the
dropDatabaseclient operation and sync new causal-consistency unified spec tests. - Update existing unified transaction/retryable-write expectations and remove the obsolete Evergreen spec patch.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
pymongo/operations.py |
Adds _WRITES_WITH_CLUSTER_TIME allowlist for command names that should carry afterClusterTime on causal writes. |
pymongo/synchronous/client_session.py |
Applies afterClusterTime to eligible write commands in causally consistent sessions (non-transaction). |
pymongo/asynchronous/client_session.py |
Async equivalent of the session write afterClusterTime behavior. |
test/unified_format.py |
Adds unified runner support for client.dropDatabase. |
test/asynchronous/unified_format.py |
Async unified runner support for client.dropDatabase. |
test/test_session.py |
Removes the write “no readConcern” assertion; keeps coverage for explain/getMore behavior. |
test/asynchronous/test_session.py |
Async equivalent adjustment to the session tests. |
test/causal_consistency/causal-consistency-write-commands.json |
New unified spec coverage asserting afterClusterTime on causal write commands. |
test/causal_consistency/causal-consistency-clientBulkWrite.json |
New unified spec coverage for bulkWrite (clientBulkWrite) including afterClusterTime. |
test/test_causal_consistency.py |
Sync unified spec test generator entry point for causal consistency scenarios. |
test/asynchronous/test_causal_consistency.py |
Async unified spec test generator entry point for causal consistency scenarios. |
test/transactions/unified/retryable-writes.json |
Updates expected command monitoring events to include afterClusterTime in readConcern on relevant writes. |
test/transactions/unified/commit.json |
Updates expected events to include afterClusterTime where applicable. |
test/transactions-convenient-api/unified/callback-commits.json |
Updates expected transaction start events to include afterClusterTime. |
test/transactions-convenient-api/unified/callback-aborts.json |
Updates expected transaction start events to include afterClusterTime. |
tools/synchro.py |
Includes the new causal consistency test module in the converted test list. |
.evergreen/resync-specs.sh |
Adds a causal_consistency resync target for pulling spec tests. |
.evergreen/spec-patch/PYTHON-5809.patch |
Removes the now-unneeded patch that previously adjusted spec expectations. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
blink1073
reviewed
Jun 4, 2026
| self.assertIsNone(rc) | ||
|
|
||
| @async_client_context.require_no_standalone | ||
| async def test_writes_do_not_include_read_concern(self): |
Contributor
Author
There was a problem hiding this comment.
As a result of these changes, causally-consistent writes (the default for an explicit session) now include an empty read concern to communicate afterClusterTime.
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-5809
Changes in this PR
Send
afterClusterTimeon writes in causally-consistent sessions to prevent possible stale primary writes.Test Plan
Synced the new unified tests.
Checklist
Checklist for Author
[ ] Did you update the changelog (if necessary)?[ ] Is any followup work tracked in a JIRA ticket? If so, add link(s).Checklist for Reviewer