Skip to content

CSHARP-5740: Remove duplicate read concern for operations in snapshot sessions#1977

Draft
kyra-rk wants to merge 4 commits intomongodb:mainfrom
kyra-rk:csharp-5740
Draft

CSHARP-5740: Remove duplicate read concern for operations in snapshot sessions#1977
kyra-rk wants to merge 4 commits intomongodb:mainfrom
kyra-rk:csharp-5740

Conversation

@kyra-rk
Copy link
Copy Markdown
Contributor

@kyra-rk kyra-rk commented Apr 28, 2026

No description provided.

@kyra-rk kyra-rk added the bug Fixes issues or unintended behavior. label Apr 28, 2026
public void GetReadConcernForSnapshotSession_should_return_expected_result(
bool isSnapshot,
int? snapshotTime,
string expectedResult)
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.

I noticed there wasn't any coverage for GetReadConcernForSnapshotSession, so I added unit tests for it.

@kyra-rk
Copy link
Copy Markdown
Contributor Author

kyra-rk commented Apr 28, 2026

Side question: The transaction path in CreateType0Section uses AddIfNotAlreadyAdded for readConcern, which silently drops the transaction's readConcern if the command already has one (e.g. via CreateCommand). Ideally shouldn't it use extraElements.Add() like the snapshot path, so the transaction's readConcern always wins? It doesn't hit any errors today because GetReadConcernForCommand returns null for IsInTransaction and there's a separate GetReadConcernForFirstCommandInTransaction, but a raw RunCommand with an explicit readConcern inside a transaction would get the wrong precedence. It's out of scope for this PR but I wanted to ask if this is something worth filing a ticket for?

Code links:

@kyra-rk kyra-rk requested a review from papafe April 29, 2026 14:48
@kyra-rk kyra-rk marked this pull request as ready for review April 29, 2026 14:48
@kyra-rk kyra-rk requested a review from a team as a code owner April 29, 2026 14:48
Copilot AI review requested due to automatic review settings April 29, 2026 14:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 addresses duplicated readConcern fields being sent when executing operations in snapshot sessions, by preventing normal operation-level read concern rendering when session.IsSnapshot is true and adding/adjusting tests to validate the behavior.

Changes:

  • Update ReadConcernHelper.GetReadConcernForCommand to return null for snapshot sessions (avoids emitting an additional readConcern from operation/collection settings).
  • Add/extend unit + wire-protocol tests to ensure only a single readConcern is present and that snapshot sessions behave as expected.
  • Add an integration test covering a snapshot session against a collection configured with a non-default read concern.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/MongoDB.Driver.Tests/ReadConcernIntegrationTests.cs Adds an integration test ensuring snapshot sessions don’t cause duplicated readConcern on find.
tests/MongoDB.Driver.Tests/Core/WireProtocol/CommandWriteProtocolTests.cs Adds protocol-level assertions that commands contain exactly one readConcern in snapshot/transaction scenarios.
tests/MongoDB.Driver.Tests/Core/Operations/ReadConcernHelperTests.cs Adds coverage for snapshot-session read concern helper behavior and expands connection/session test helpers.
src/MongoDB.Driver/Core/Operations/ReadConcernHelper.cs Changes GetReadConcernForCommand to suppress operation readConcern when session.IsSnapshot.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

result.Should().HaveCount(1);

var findEvent = eventCapturer.Events.OfType<CommandStartedEvent>().First();
findEvent.Command["readConcern"]["level"].AsString.Should().Be("snapshot");
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.

Hmm this is a good point, I will revise


var command = new BsonDocument
{
{ "find", "testCollection" }
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.

I will edit the name to be Execute_should_send_only_one_readConcern_for_snapshot_session_with_find_command

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.

I actually agree with this comment, and I think that this code could pass even without your fix.
You need to add a read concern in the operation, otherwise there won't be a duplicate in any case.

Copy link
Copy Markdown
Contributor

@papafe papafe Apr 29, 2026

Choose a reason for hiding this comment

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

Reading this more.... I'm not sure we're verifying anything here. The operation does not have a read concern, as we're not adding it (and that's what we want) and we're just verifying that a readConcern is added, but that's a different issue than the one in this ticket.
I think that the unit test you wrote, plus eventually the integration tests would be enough, and these two tests can be 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.

Agreed, will remove!

@kyra-rk kyra-rk marked this pull request as draft April 29, 2026 15:01
namespace MongoDB.Driver.Tests
{
[Trait("Category", "Integration")]
public class ReadConcernIntegrationTests : LoggableTestClass
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.

I like the idea of this integration test but but sure if we should keep this name for the file/class or we should change it. Or if it makes sense to move this test in some other file, but none of the ones we have seems to be relevant.
This is more of an open question/comment.

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.

Hmm yeah I honestly was struggling to find a good place for it. Maybe we can keep the name and move it under Core so it's next to tests/MongoDB.Driver.Tests/Core/ReadConcernTests.cs? I'm also not apposed to renaming it, but ReadConcern seemed more general than focusing on the Snapshot part

@kyra-rk kyra-rk requested a review from papafe April 29, 2026 18:19
@papafe
Copy link
Copy Markdown
Contributor

papafe commented Apr 30, 2026

Side question: The transaction path in CreateType0Section uses AddIfNotAlreadyAdded for readConcern, which silently drops the transaction's readConcern if the command already has one (e.g. via CreateCommand). Ideally shouldn't it use extraElements.Add() like the snapshot path, so the transaction's readConcern always wins? It doesn't hit any errors today because GetReadConcernForCommand returns null for IsInTransaction and there's a separate GetReadConcernForFirstCommandInTransaction, but a raw RunCommand with an explicit readConcern inside a transaction would get the wrong precedence. It's out of scope for this PR but I wanted to ask if this is something worth filing a ticket for?

Code links:

I think this is worth filing a ticket for. And if you got the time, creating a reproducible test that verifies this issue would be incredibly useful.

@papafe papafe requested a review from sanych-sun April 30, 2026 09:48
@papafe
Copy link
Copy Markdown
Contributor

papafe commented Apr 30, 2026

Overall this PR seems correct to me, added @sanych-sun as the reviewer to get another eye.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Fixes issues or unintended behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants