-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Extend allowed SemaphoreSlim wait TimeSpan values to match Timer and other APIs #117398
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
Conversation
Tagging subscribers to this area: @mangod9 |
There was a problem hiding this 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 extends SemaphoreSlim
to support TimeSpan
timeouts up to Timer.MaxSupportedTimeout
(instead of int.MaxValue
), refactors internal timeout logic to use unsigned timeouts, updates error messages, and adjusts tests accordingly.
- Updated
SemaphoreSlim
wait methods (sync and async) to useuint
timeouts and validate againstTimer.MaxSupportedTimeout
. - Added a
uint
overload inTimeoutHelper.UpdateTimeOut
for unsigned infinite and large-timeout support. - Adjusted tests and resource strings to reflect the new maximum timeout and renamed the error message resource.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/libraries/System.Threading/tests/SemaphoreSlimTests.cs | Extended tests to cover the new max TimeSpan values and negative cases |
src/libraries/System.Private.CoreLib/src/System/Threading/TimeoutHelper.cs | Introduced uint overload for UpdateTimeOut to support unsigned infinite timeouts |
src/libraries/System.Private.CoreLib/src/System/Threading/SemaphoreSlim.cs | Refactored Wait /WaitAsync to use uint timeouts, use Timer.MaxSupportedTimeout , and updated exception messages |
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx | Added SemaphoreSlim_Wait_TimeSpanTimeoutWrong resource for the updated validation |
Comments suppressed due to low confidence (1)
src/libraries/System.Private.CoreLib/src/System/Threading/SemaphoreSlim.cs:265
- The code refers to
timeout.Infinite
, which does not exist. It should comparetotalMilliseconds
againstTimeout.Infinite
to detect the infinite-timeout case.
return Wait(totalMilliseconds == timeout.Infinite ? Timeout.UnsignedInfinite : (uint)totalMilliseconds, cancellationToken);
ba94339
to
53ec2ab
Compare
63df413
to
d2a4063
Compare
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/TimeoutHelper.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/TimeoutHelper.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/SemaphoreSlim.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/SemaphoreSlim.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/SemaphoreSlim.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/SemaphoreSlim.cs
Outdated
Show resolved
Hide resolved
f2d735e
to
20157b5
Compare
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Threading/ManualResetEventSlim.cs
Outdated
Show resolved
Hide resolved
40048e1
to
321730c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@@ -211,8 +211,7 @@ public void Wait(CancellationToken cancellationToken) | |||
/// <returns>true if the current thread successfully entered the <see cref="SemaphoreSlim"/>; | |||
/// otherwise, false.</returns> | |||
/// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout"/> is a negative | |||
/// number other than -1 milliseconds, which represents an infinite time-out -or- timeout is greater |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comments are not the official sources for the docs. Could you please submit PR to https://github.com/dotnet/dotnet-api-docs/ to add a note that the ArgumentOutOfRangeException is no longer thrown for timeout greater than int.MaxValue in .NET 10?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR here dotnet/dotnet-api-docs#11677
Fixes #116877.
Allow SemaphoreSlim to accept a TimeSpan that goes all the way until UInt32.MaxValue - 1 milliseconds, matching the range that other APIs support.