microsoft / microsoft/vs-threading

ReentrantSemaphore - Implement EnterAsync with timeout

Open
#1,362 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
1k
Forks
160
Avg merge
1d 12h
Merged PRs (30d)
28

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in ReentrantSemaphore.cs at ExecuteAsync and compare its infinite-timeout acquisition with AsyncSemaphore.cs and the linked EnterAsync implementation. Add support for passing an acquisition timeout to ExecuteAsync, preserving the distinction between waiting for the semaphore and cancellation during the critical section.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.