Add analyzer to warn on blocking in async methods#308
Merged
SergeyTeplyakov merged 3 commits intomasterfrom Jul 1, 2025
Merged
Add analyzer to warn on blocking in async methods#308SergeyTeplyakov merged 3 commits intomasterfrom
SergeyTeplyakov merged 3 commits intomasterfrom
Conversation
Introduces DoNotBlockUnnecessarilyInAsyncMethodsAnalyzer (EPC35) to detect and warn when Task.Result, Task.Wait(), or GetAwaiter().GetResult() are used inside async methods, which can cause deadlocks. Adds corresponding diagnostic descriptor, updates analyzer release notes, and provides comprehensive unit tests. Refactors TaskTypeExtensions for improved clarity and consistency.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new analyzer (EPC35) that warns when code blocks on tasks inside async methods, refactors a helper for clarity, and includes tests and release notes.
- Introduces
DoNotBlockUnnecessarilyInAsyncMethodsAnalyzerto detectResult,Wait(), andGetAwaiter().GetResult()inside async contexts - Updates
TaskTypeExtensionsparameter names for consistency - Adds diagnostic descriptor EPC35, release notes, and comprehensive unit tests
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/ErrorProne.NET.CoreAnalyzers/TaskTypeExtensions.cs | Renamed method parameter from returnType to type for consistency |
| src/ErrorProne.NET.CoreAnalyzers/DiagnosticDescriptors.cs | Added EPC35 descriptor for blocking in async methods |
| src/ErrorProne.NET.CoreAnalyzers/AsyncAnalyzers/DoNotBlockUnnecessarilyInAsyncMethodsAnalyzer.cs | Implemented new analyzer logic for blocking APIs in async |
| src/ErrorProne.NET.CoreAnalyzers/AnalyzerReleases.Unshipped.md | Documented EPC35 in release notes |
| src/ErrorProne.NET.CoreAnalyzers.Tests/AsyncAnalyzers/DoNotBlockUnnecessarilyInAsyncMethodsAnalyzerTests.cs | Added tests for Task.Result, Task.Wait(), GetAwaiter().GetResult() in async |
Comments suppressed due to low confidence (2)
src/ErrorProne.NET.CoreAnalyzers/TaskTypeExtensions.cs:36
- [nitpick] Consider renaming the parameter
typeto something liketypeSymbolto clarify that this represents a Roslyn symbol and avoid confusion with the C#typekeyword.
public static bool IsTaskLike(this ITypeSymbol? type, Compilation compilation, TaskLikeTypes typesToCheck)
src/ErrorProne.NET.CoreAnalyzers/AsyncAnalyzers/DoNotBlockUnnecessarilyInAsyncMethodsAnalyzer.cs:41
- Add unit tests covering
ValueTask<T>.Resultusage inside async methods to verify that the analyzer correctly flags blocking on ValueTask results as well.
propertyReference.Instance?.Type?.IsTaskLike(context.Compilation) == true)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces DoNotBlockUnnecessarilyInAsyncMethodsAnalyzer (EPC35) to detect and warn when Task.Result, Task.Wait(), or GetAwaiter().GetResult() are used inside async methods, which can cause deadlocks. Adds corresponding diagnostic descriptor, updates analyzer release notes, and provides comprehensive unit tests. Refactors TaskTypeExtensions for improved clarity and consistency.