Skip to content

PYTHON-5809 - Send afterClusterTime on writes in causally-consistent sessions#2851

Merged
NoahStapp merged 2 commits into
mongodb:masterfrom
NoahStapp:PYTHON-5809
Jun 4, 2026
Merged

PYTHON-5809 - Send afterClusterTime on writes in causally-consistent sessions#2851
NoahStapp merged 2 commits into
mongodb:masterfrom
NoahStapp:PYTHON-5809

Conversation

@NoahStapp

Copy link
Copy Markdown
Contributor

PYTHON-5809

Changes in this PR

Send afterClusterTime on 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 there test coverage?
  • [ ] Is any followup work tracked in a JIRA ticket? If so, add link(s).

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?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_to to attach afterClusterTime on eligible writes in causally consistent sessions.
  • Extend the unified test runner to support the dropDatabase client 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.

@NoahStapp NoahStapp requested a review from blink1073 June 4, 2026 19:04
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

self.assertIsNone(rc)

@async_client_context.require_no_standalone
async def test_writes_do_not_include_read_concern(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these tests removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a result of these changes, causally-consistent writes (the default for an explicit session) now include an empty read concern to communicate afterClusterTime.

@blink1073 blink1073 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@NoahStapp NoahStapp merged commit a39b361 into mongodb:master Jun 4, 2026
86 of 88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants