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

Report blocking Thread.Sleep calls in async methods #6359

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public override void Initialize(AnalysisContext context)
GetSymbolAndAddToList("Result", WellKnownTypeNames.SystemThreadingTasksValueTask, SymbolKind.Property, syncBlockingSymbols, context.Compilation);
GetSymbolAndAddToList("GetResult", WellKnownTypeNames.SystemRuntimeCompilerServicesTaskAwaiter, SymbolKind.Method, syncBlockingSymbols, context.Compilation);
GetSymbolAndAddToList("GetResult", WellKnownTypeNames.SystemRuntimeCompilerServicesValueTaskAwaiter, SymbolKind.Method, syncBlockingSymbols, context.Compilation);
GetSymbolAndAddToList("Sleep", WellKnownTypeNames.SystemThreadingThread, SymbolKind.Method, syncBlockingSymbols, context.Compilation);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

if (!syncBlockingTypes.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Microsoft.NetCore.Analyzers.Runtime.UnitTests
{
public class UseAsyncMethodInAsyncContextTests
{

[Fact]
public async Task TaskWaitInTaskReturningMethodGeneratesWarning()
{
Expand Down Expand Up @@ -49,6 +48,39 @@ End Module
await CreateVBTestAndRunAsync(testVB, VerifyVB.Diagnostic(UseAsyncMethodInAsyncContext.DescriptorNoAlternativeMethod).WithLocation(0).WithArguments("Public Overloads Sub Wait()"));
}

[Fact]
public async Task ThreadSleepInTaskReturningMethodGeneratesWarning()
{
var testCS = @"
using System.Threading;
using System.Threading.Tasks;

class Test {
Task T() {
{|#0:Thread.Sleep(500)|};
return Task.FromResult(1);
}
}
";
await CreateCSTestAndRunAsync(testCS, VerifyCS.Diagnostic(UseAsyncMethodInAsyncContext.DescriptorNoAlternativeMethod).WithLocation(0).WithArguments("Thread.Sleep(int)"));

var testVB = @"
Imports System.Threading
Imports System.Threading.Tasks

Module Program
Sub Main()
Test()
End Sub
Function Test() As Task
{|#0:Thread.Sleep(500)|}
Return Task.FromResult(1)
End Function
End Module
";
await CreateVBTestAndRunAsync(testVB, VerifyVB.Diagnostic(UseAsyncMethodInAsyncContext.DescriptorNoAlternativeMethod).WithLocation(0).WithArguments("Public Shared Overloads Sub Sleep(millisecondsTimeout As Integer)"));
}

[Fact]
public async Task TaskWaitInValueTaskReturningMethodGeneratesWarning()
{
Expand Down