Skip to content

Conversation

@eduardo-vp
Copy link
Member

@eduardo-vp eduardo-vp commented Nov 3, 2025

Fix range validation for some Wait/WaitAsync methods that take the timeout as an integer.

Copilot AI review requested due to automatic review settings November 3, 2025 08:03
@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 Nov 3, 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 Nov 3, 2025
@dotnet-policy-service
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 Fix SemaphoreSlim validation in constructors Fix SemaphoreSlim validation in Wait methods Nov 3, 2025
Copy link
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 adds input validation for the millisecondsTimeout parameter to two overloads of Wait and WaitAsync methods in SemaphoreSlim, bringing them into alignment with other overloads that already perform this validation.

  • Added ArgumentOutOfRangeException validation for negative timeout values (other than -1) in Wait(int millisecondsTimeout) and WaitAsync(int millisecondsTimeout)
  • Added inline comments explaining the validation and subsequent method calls

@eduardo-vp eduardo-vp changed the title Fix SemaphoreSlim validation in Wait methods Fix SemaphoreSlim validation in Wait/WaitAsync methods Nov 3, 2025
@jkotas
Copy link
Member

jkotas commented Nov 3, 2025

What is the bug that this is fixing? Do we need to add tests for it?

@eduardo-vp
Copy link
Member Author

eduardo-vp commented Nov 3, 2025

Those methods don't have validation now and are allowed to actually wait even if the millisecondsTimeout argument is less than -1.

The bug should have been caught in:

[Fact]
public static void RunSemaphoreSlimTest1_Wait_NegativeCases()
{
// Invalid timeout
RunSemaphoreSlimTest1_Wait_Helper(10, 10, -10, true, typeof(ArgumentOutOfRangeException));
}

private static void RunSemaphoreSlimTest1_Wait_Helper
(int initial, int maximum, object timeout, bool returnValue, Type exceptionType)
{
SemaphoreSlim semaphore = new SemaphoreSlim(initial, maximum);
try
{
bool result = false;
if (timeout is TimeSpan)
{
result = semaphore.Wait((TimeSpan)timeout);
}
else
{
result = semaphore.Wait((int)timeout);
}
Assert.Equal(returnValue, result);
if (result)
{
Assert.Equal(initial - 1, semaphore.CurrentCount);
}
}
catch (Exception ex)
{
Assert.NotNull(exceptionType);
Assert.IsType(exceptionType, ex);
}
}

Wait didn't throw an exception, it continued the test execution and asserted that the wait was successful and the count decremented.

In this case the test can be fixed by updating the expected result of Wait to false such that if the semaphore successfully waits, an exception will be thrown.

However the problem is that the code was expected to throw but it didn't and it never checked that the exception was not thrown. I believe this could potentially happen for other tests too. Shall we update the tests pattern to verify this?

@eduardo-vp
Copy link
Member Author

eduardo-vp commented Nov 3, 2025

We do validate millisecondsTimeout in all the Wait/WaitAsync methods, only these two were missing the validation.

@eduardo-vp eduardo-vp requested a review from jkotas November 3, 2025 18:10
@eduardo-vp
Copy link
Member Author

/ba-g unrelated errors, just adding some validation to some methods

@eduardo-vp eduardo-vp merged commit 3023763 into dotnet:main Nov 3, 2025
133 of 142 checks passed
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.

2 participants