Skip to content
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

CA1849 reports zero length timeouts as blocking (eg. SemaphoreSlim.Wait(0)) #7271

Open
fowl2 opened this issue Mar 28, 2024 · 0 comments · May be fixed by #7310
Open

CA1849 reports zero length timeouts as blocking (eg. SemaphoreSlim.Wait(0)) #7271

fowl2 opened this issue Mar 28, 2024 · 0 comments · May be fixed by #7310

Comments

@fowl2
Copy link
Contributor

fowl2 commented Mar 28, 2024

Analyzer

Diagnostic ID: CA1849: Call async methods when in an async method

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0, 9.0.0-preview.24122.1

Describe the bug

SemaphoreSlim.Wait will not block if 0 (or TimeSpan.Zero) is passed as the timeout:

millisecondsTimeout:
The number of milliseconds to wait, System.Threading.Timeout.Infinite(-1) to
wait indefinitely, or zero to test the state of the wait handle and return immediately.

Therefore this rule is a false positive in this case.

Steps To Reproduce

using System.Threading;
using System.Threading.Tasks;

static async Task Main()
{
    using SemaphoreSlim s = new(0);
    s.Wait(0);
}

Expected behavior

No warning.

Actual behavior

CA1849 'SemaphoreSlim.Wait(int)' synchronously blocks. Await 'SemaphoreSlim.WaitAsync()' instead.

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

Successfully merging a pull request may close this issue.

1 participant