- 
                Notifications
    
You must be signed in to change notification settings  - Fork 5.2k
 
Fix SemaphoreSlim validation in Wait/WaitAsync methods #121289
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
Fix SemaphoreSlim validation in Wait/WaitAsync methods #121289
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 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 
ArgumentOutOfRangeExceptionvalidation for negative timeout values (other than -1) inWait(int millisecondsTimeout)andWaitAsync(int millisecondsTimeout) - Added inline comments explaining the validation and subsequent method calls
 
        
          
                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
        
      …phoreSlim.cs Co-authored-by: Copilot <[email protected]>
…phoreSlim.cs Co-authored-by: Copilot <[email protected]>
| 
           What is the bug that this is fixing? Do we need to add tests for it?  | 
    
| 
           Those methods don't have validation now and are allowed to actually wait even if the  The bug should have been caught in: runtime/src/libraries/System.Threading/tests/SemaphoreSlimTests.cs Lines 66 to 71 in 3083294 
 runtime/src/libraries/System.Threading/tests/SemaphoreSlimTests.cs Lines 199 to 225 in 3083294 
 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  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?  | 
    
| 
           We do validate   | 
    
| 
           /ba-g unrelated errors, just adding some validation to some methods  | 
    
Fix range validation for some Wait/WaitAsync methods that take the timeout as an integer.