You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ReentrantSemaphore - Implement EnterAsync with timeout #1362
Is your feature request related to a problem? Please describe.
SemaphoreSlim and AsyncSemaphore have been banned via analyzers with the replacement preference being ReentrantSemaphore due to it being JTF aware.
AsyncSemaphore's EnterAsync implementation allows for a timeout parameter
ReentrantSemaphore's ExecuteAsync implementation always enters the underlying AsyncSemphare with an infinite timeout
Describe the solution you'd like
AsyncSemaphore supports an EnterAsync timeout. I would like to see ReentrantSemaphore allow for this parameter to be passed as an argument during ExecuteAsync.
Describe alternatives you've considered
Unless I am mistaken,
You can partially work around this by passing a cancellation token that times out, but this timeout is not specific to acquiring the lock. If the critical section is a long running operation, it is possible that a passed in cancellation token would cancel even though entrance into the critical section was achieved.
it is possible that a passed in cancellation token would cancel even though entrance into the critical section was achieved
but then the call would have succeed and not thrown, so the caller would have first seen that it entered and acquired the lock, construct whatever scope is necessary to safely release it when leaving the scope, and only then (by its own choice) may or may not check the cancellation token again
any other implementation would be bugged regardless of your particular usecase via a timeout and it would never be safe to pass a cancellation token
so while having a particular API with timeout support probably might be more efficient it shouldn't be necessary for correctness
Thank you for the response. Perhaps I did misunderstand the 'ExecuteAsync' implementation.
Would it be correct to say that once the the operation callback is invoked OperationCancelled exception will not be thrown and it is the caller's responsibility to check the CancellationToken inside of the Task that was passed as a parameter?
changed the title [-]ReentrantSemaphore - Implement EntryAsync with timeout[/-][+]ReentrantSemaphore - Implement EnterAsync with timeout[/+]on Dec 20, 2024
Activity
weltkante commentedon Dec 19, 2024
but then the call would have succeed and not thrown, so the caller would have first seen that it entered and acquired the lock, construct whatever scope is necessary to safely release it when leaving the scope, and only then (by its own choice) may or may not check the cancellation token again
any other implementation would be bugged regardless of your particular usecase via a timeout and it would never be safe to pass a cancellation token
so while having a particular API with timeout support probably might be more efficient it shouldn't be necessary for correctness
robert-at-pieces commentedon Dec 19, 2024
Tobias Käs (@weltkante)
Thank you for the response. Perhaps I did misunderstand the 'ExecuteAsync' implementation.
Would it be correct to say that once the the operation callback is invoked OperationCancelled exception will not be thrown and it is the caller's responsibility to check the CancellationToken inside of the Task that was passed as a parameter?
weltkante commentedon Dec 19, 2024
yes, all checks in the code happen before, and if the operation itself wants to check it then it'll do it on its own terms
(also I'm not part of the repo team, just subscribed to the issue list, so feel free to wait for an 'official' confirmation)
[-]ReentrantSemaphore - Implement EntryAsync with timeout[/-][+]ReentrantSemaphore - Implement EnterAsync with timeout[/+]