Skip to content

CSHARP-3549: CSOT: Add timeoutMS to settings #1721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2025

Conversation

sanych-sun
Copy link
Member

No description provided.

@sanych-sun sanych-sun requested a review from a team as a code owner July 10, 2025 00:35
@sanych-sun sanych-sun marked this pull request as draft July 10, 2025 22:02
@sanych-sun sanych-sun force-pushed the csharp3549 branch 2 times, most recently from 0942f85 to af44109 Compare July 19, 2025 01:35
@sanych-sun sanych-sun marked this pull request as ready for review July 28, 2025 17:14
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

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

Overall looks good. See minor comments.

@@ -459,12 +460,12 @@ public static string IsNullOrNotEmpty(string value, string paramName)
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsValidTimeout(TimeSpan value, string paramName)
{
if (value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan)
if (value > TimeSpan.Zero || value == Timeout.InfiniteTimeSpan)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you flip the logic in this method?

All the other methods end with return value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Somehow this way it's easier to understand for me. But not critical, will revert to follow other methods pattern.

{
throw new TimeoutException();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it OK that we no longer throw an exception?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, you are right. We should throw for negative timeouts. Will return the exception.


namespace MongoDB.Driver
internal static class ClientSessionExtensions
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this file/class be called IClientSessionExtensions?

@@ -87,7 +87,7 @@ public CancellationToken CombinedCancellationToken
}
private Stopwatch Stopwatch { get; }

public TimeSpan Timeout { get; }
public TimeSpan? Timeout { get; }
Copy link
Contributor

Choose a reason for hiding this comment

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

I find it really unhelpful when a class has BOTH fields and auto-properties.

I'm not a fan of auto-properties in general, but I'm specially not a fan of mixing fields and auto-properties in the same class. Mixing them makes it much harder to understand what state a class maintains.

@@ -114,13 +109,13 @@ public override IAsyncCursor<TResult> Aggregate<TResult>(IClientSessionHandle se
if (isAggregateToCollection)
{
var aggregateOperation = CreateAggregateToCollectionOperation(renderedPipeline, options);
ExecuteWriteOperation(session, aggregateOperation, cancellationToken);
ExecuteWriteOperation(session, aggregateOperation, options.Timeout, cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sometime I see options.Timeout and other times I see options?.Timeout.

Should they ALL have ??

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me double-check. I think in some cases there is no-way to have null options.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. There are number of cases when options is guaranteed to be not-null. In all other cases there is Null-conditional operators.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a pain. I leave it to you then to check that there are no missing ?.

@@ -296,7 +297,7 @@ public TimeSpan HeartbeatTimeout
get { return _heartbeatTimeout; }
set
{
if (value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan)
if (value < TimeSpan.Zero && value != System.Threading.Timeout.InfiniteTimeSpan)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

Now as MongoUrlBuilder has property named Timeout, we should use full name here.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's unfortunate.

@sanych-sun sanych-sun requested a review from rstam July 29, 2025 22:21
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

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

LGTM with minor comments

@@ -444,11 +444,12 @@ public static string IsNullOrNotEmpty(string value, string paramName)
/// <returns>The value of the parameter.</returns>
public static TimeSpan? IsNullOrValidTimeout(TimeSpan? value, string paramName)
{
if (value != null)
if (value == null)
Copy link
Contributor

Choose a reason for hiding this comment

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

There was probably no need to touch this file at all.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind leaving these changes in. I think they improve this code a bit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe not...

In general I don't like making random changes to files that have nothing to do with the PR.

If we think these methods could be written in a better way we should make a ticket for it.

The reason that all methods end with return value is that in general an Ensure method might throw more than one exception, so the general pattern is:

  • check for invalid condition 1 and throw exception with suitable message
  • check for invalid condition 2 and throw exception with suitable message
  • ...
  • return value

Copy link
Member Author

@sanych-sun sanych-sun Jul 30, 2025

Choose a reason for hiding this comment

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

Same story here as with previous change in the file: some how "quick returns" works better for me to understand the code. But I do not mind to revert the code.
And technically this method is kind of related to the PR: we used to have unused method Ensure.IsValidTimeout. Instead of implementing new method I've decided to adopt it and was making sure the logic is appropriate for the usage. That's the reason why I touched the file.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK

Copy link
Member Author

Choose a reason for hiding this comment

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

Anyhow: reverted 🙂

@@ -114,13 +109,13 @@ public override IAsyncCursor<TResult> Aggregate<TResult>(IClientSessionHandle se
if (isAggregateToCollection)
{
var aggregateOperation = CreateAggregateToCollectionOperation(renderedPipeline, options);
ExecuteWriteOperation(session, aggregateOperation, cancellationToken);
ExecuteWriteOperation(session, aggregateOperation, options.Timeout, cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a pain. I leave it to you then to check that there are no missing ?.

@@ -296,7 +297,7 @@ public TimeSpan HeartbeatTimeout
get { return _heartbeatTimeout; }
set
{
if (value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan)
if (value < TimeSpan.Zero && value != System.Threading.Timeout.InfiniteTimeSpan)
Copy link
Contributor

Choose a reason for hiding this comment

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

That's unfortunate.

throw new TimeoutException();
}
serverTimeout -= _roundTripTime;
if (serverTimeout < TimeSpan.Zero)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be changed to <=?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, thank you!

adelinowona
adelinowona previously approved these changes Jul 30, 2025
Copy link
Contributor

@adelinowona adelinowona left a comment

Choose a reason for hiding this comment

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

LGTM! left a comment you can tackle if you think it's ok to do it now.

Comment on lines 254 to 259
var effectiveComment = _comment;
var effectiveHint = _hint;
var effectiveMax = _max;
var effectiveMaxTime = _maxTime;
var effectiveMin = _min;
var effectiveReturnKey = _returnKey;
var effectiveShowRecordId = _showRecordId;
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the point of all this effective variables? can't we just remove them while we are here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

throw new TimeoutException();
}
serverTimeout -= _roundTripTime;
if (serverTimeout < TimeSpan.Zero)
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

@@ -444,11 +444,12 @@ public static string IsNullOrNotEmpty(string value, string paramName)
/// <returns>The value of the parameter.</returns>
public static TimeSpan? IsNullOrValidTimeout(TimeSpan? value, string paramName)
{
if (value != null)
if (value == null)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind leaving these changes in. I think they improve this code a bit.

@@ -444,11 +444,12 @@ public static string IsNullOrNotEmpty(string value, string paramName)
/// <returns>The value of the parameter.</returns>
public static TimeSpan? IsNullOrValidTimeout(TimeSpan? value, string paramName)
{
if (value != null)
if (value == null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe not...

In general I don't like making random changes to files that have nothing to do with the PR.

If we think these methods could be written in a better way we should make a ticket for it.

The reason that all methods end with return value is that in general an Ensure method might throw more than one exception, so the general pattern is:

  • check for invalid condition 1 and throw exception with suitable message
  • check for invalid condition 2 and throw exception with suitable message
  • ...
  • return value

@@ -444,11 +444,12 @@ public static string IsNullOrNotEmpty(string value, string paramName)
/// <returns>The value of the parameter.</returns>
public static TimeSpan? IsNullOrValidTimeout(TimeSpan? value, string paramName)
{
if (value != null)
if (value == null)
Copy link
Contributor

Choose a reason for hiding this comment

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

OK

Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

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

LGTM

@sanych-sun sanych-sun merged commit a93f29e into mongodb:main Jul 30, 2025
33 of 35 checks passed
@sanych-sun sanych-sun deleted the csharp3549 branch July 30, 2025 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants