Skip to content

Conversation

eduardo-vp
Copy link
Member

@eduardo-vp eduardo-vp commented Jul 8, 2025

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.

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 8, 2025
@eduardo-vp eduardo-vp added area-System.Threading and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jul 8, 2025
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

@eduardo-vp eduardo-vp changed the title Extend allowed SemaphoreSlim wait TimeSpam values to match Timer and other APIs Extend allowed SemaphoreSlim wait TimeSpan values to match Timer and other APIs Jul 8, 2025
@eduardo-vp eduardo-vp marked this pull request as ready for review July 8, 2025 00:43
@Copilot Copilot AI review requested due to automatic review settings July 8, 2025 00:43
Copy link
Contributor

@Copilot 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 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 use uint timeouts and validate against Timer.MaxSupportedTimeout.
  • Added a uint overload in TimeoutHelper.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 compare totalMilliseconds against Timeout.Infinite to detect the infinite-timeout case.
            return Wait(totalMilliseconds == timeout.Infinite ? Timeout.UnsignedInfinite : (uint)totalMilliseconds, cancellationToken);

@eduardo-vp eduardo-vp force-pushed the extend-semaphore-wait branch from f2d735e to 20157b5 Compare July 31, 2025 03:56
@eduardo-vp eduardo-vp force-pushed the extend-semaphore-wait branch from 40048e1 to 321730c Compare July 31, 2025 20:30
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM!

@eduardo-vp eduardo-vp merged commit b267b85 into dotnet:main Aug 12, 2025
141 of 143 checks passed
@@ -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
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, will do

Copy link
Member Author

Choose a reason for hiding this comment

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

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

Successfully merging this pull request may close these issues.

Extend SemaphoreSlim WaitAsync millisecond bound to Timer.MaxSupportedTimeout to match Timer and other APIs
2 participants