Skip to content

CSHARP-5618: CSOT: Race condition in ExclusiveConnectionPool could lead to ArgumentOutOfRangeException #1712

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 3 commits into from
Jun 18, 2025

Conversation

sanych-sun
Copy link
Member

No description provided.

@sanych-sun sanych-sun requested a review from a team as a code owner June 17, 2025 19:05
@sanych-sun sanych-sun requested review from papafe and adelinowona and removed request for a team and papafe June 17, 2025 19:05
@@ -53,7 +53,13 @@ public TimeSpan RemainingTimeout
return System.Threading.Timeout.InfiniteTimeSpan;
}

return Timeout - Stopwatch.Elapsed;
var result = Timeout - Stopwatch.Elapsed;
Copy link
Member Author

@sanych-sun sanych-sun Jun 17, 2025

Choose a reason for hiding this comment

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

Many .net APIs that accepts timeouts validates the timeout and throws ArgumentException on negative values (for example SemaphoreSlim). On other hand if it's TimeSpan.Zero it just considered as timeout out. So it's safer to use TimeSpan.Zero for timed out contexts.

@@ -29,7 +29,7 @@ public static void ExecuteOnNewThreads(int threadsCount, Action<int> action, int

if (exceptions.Any())
{
throw exceptions.First();
throw new AggregateException(exceptions);
Copy link
Member Author

@sanych-sun sanych-sun Jun 17, 2025

Choose a reason for hiding this comment

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

throwing like it was before messes up the call stack, when AggregateException can preserve the original stack trace of InnerException.

@sanych-sun sanych-sun added the bug label Jun 17, 2025
@sanych-sun sanych-sun changed the title CSHARP-5618: Race condition in ExclusiveConnectionPool could lead to ArgumentOutOfRangeException CSHARP-5618: CSOT: Race condition in ExclusiveConnectionPool could lead to ArgumentOutOfRangeException Jun 17, 2025
@@ -151,6 +162,11 @@ public OperationContext WithTimeout(TimeSpan timeout)
timeout = remainingTimeout;
}

if (timeout == TimeSpan.Zero)
{
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.

Are you sure about throwing here, can't we create timed out contexts?? Feels like throwing should be part of some explicit validation like ThrowIfTimedOutOrCanceled.

Copy link
Member Author

Choose a reason for hiding this comment

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

Honestly not sure. Probably you are right, as throwing here might be not-expected, but throwing once we decide to Wait on something - will be more expected behavior.
Will revert.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted.

Oleksandr Poliakov added 2 commits June 17, 2025 14:05
@sanych-sun sanych-sun requested a review from BorisDog June 17, 2025 21:16
Copy link
Contributor

@BorisDog BorisDog 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 73c8d10 into mongodb:main Jun 18, 2025
30 of 35 checks passed
@sanych-sun sanych-sun deleted the csharp5618 branch June 18, 2025 18:31
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.

2 participants