-
Notifications
You must be signed in to change notification settings - Fork 239
New Rule S6966: Awaitable method should be used #9101
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
521b41b
Scaffold rule
martin-strecker-sonarsource 3e44c6b
Split test cases and fix compiler errors
martin-strecker-sonarsource a6ddf4e
Remove CoreMetadataReference.SystemLinqQueryable
martin-strecker-sonarsource 3ca5936
Remove SystemComponentModelTypeConverter facade
martin-strecker-sonarsource 81f0c08
Add ISymbolExtensions for IsAwaitableNonDynamic
martin-strecker-sonarsource 0832792
WIP: First implementation
martin-strecker-sonarsource 7c06ec1
Make tests green
martin-strecker-sonarsource efbd46c
Fix test case for Net48
martin-strecker-sonarsource 3d77f07
Refactor
martin-strecker-sonarsource be4cba5
Fix test failures
martin-strecker-sonarsource 8fb42ab
ITs Akka
martin-strecker-sonarsource 6a27444
Rework type lookups for candidates
martin-strecker-sonarsource d05eb3c
Add FNs for local functions.
martin-strecker-sonarsource 6f26336
Refactorings
martin-strecker-sonarsource df0c267
Improve await placement
martin-strecker-sonarsource 3c47551
Remove uneccessary Parenthesis removal
martin-strecker-sonarsource 42d8966
Add more overload tests
martin-strecker-sonarsource ef44048
Address SQ issues
martin-strecker-sonarsource a5274db
Code smell
martin-strecker-sonarsource 38dc0e2
Update ITs
martin-strecker-sonarsource 8ab8ee9
Apply suggestions from code review
martin-strecker-sonarsource 9acfd85
Clean-up
martin-strecker-sonarsource 5b9eb09
Add support for SocketTaskExtensions
martin-strecker-sonarsource fad1172
FP: Fix support for lambdas
martin-strecker-sonarsource 2ad3efa
Fix EnclosingScope and tests
martin-strecker-sonarsource e7872d1
Simplify test case
martin-strecker-sonarsource d234de4
Use AnalyzerLanguage and expose OutputKind in SyntaxNodeExtensionsTest
martin-strecker-sonarsource 83fcd4f
Fix conditional compilation
martin-strecker-sonarsource 84c541e
Add EnumMember and Parameter to EnclosingScopeSyntaxKinds and tests
martin-strecker-sonarsource f88f805
Fix ISymbolExtensions annotations for Roslyn copied code.
martin-strecker-sonarsource 3e31d6b
Improve IsAsyncCodeBlock and add more tests
martin-strecker-sonarsource 6ef7f51
Remove whitespace
martin-strecker-sonarsource 3d79d9d
Unify IsAsyncCodeBlock cases
martin-strecker-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
analyzers/its/expected/BlazorSample/S6966-BlazorSample-net7.0.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "Issues": [ | ||
| { | ||
| "Id": "S6966", | ||
| "Message": "Await RunAsync instead.", | ||
| "Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/Projects/BlazorSample/BlazorSample/Program.cs#L29", | ||
| "Location": "Line 29 Position 1-10" | ||
| } | ||
| ] | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
.../its/expected/CSharpLatest/S6966-NetCore31.MVC.ConfigurableRules-netcoreapp3.1.Views.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "Issues": [ | ||
| { | ||
| "Id": "S6966", | ||
| "Message": "Await RenderSectionAsync instead.", | ||
| "Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/Projects/CSharpLatest/NetCore31WithConfigurableRules/Views/Shared/_Layout.cshtml#L46", | ||
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "Location": "Line 46 Position 7-48" | ||
| } | ||
| ] | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore6-net6.0.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "Issues": [ | ||
| { | ||
| "Id": "S6966", | ||
| "Message": "Await RunAsync instead.", | ||
| "Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/Projects/ManuallyAddedNoncompliantIssues.CS/AspNetCore6/Program.cs#L27", | ||
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "Location": "Line 27 Position 1-10" | ||
| } | ||
| ] | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore7-net7.0.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "Issues": [ | ||
| { | ||
| "Id": "S6966", | ||
| "Message": "Await RunAsync instead.", | ||
| "Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/Projects/ManuallyAddedNoncompliantIssues.CS/AspNetCore7/Program.cs#L27", | ||
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "Location": "Line 27 Position 1-10" | ||
| } | ||
| ] | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore8-net8.0.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "Issues": [ | ||
| { | ||
| "Id": "S6966", | ||
| "Message": "Await RunAsync instead.", | ||
| "Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/Projects/ManuallyAddedNoncompliantIssues.CS/AspNetCore8/Program.cs#L27", | ||
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "Location": "Line 27 Position 1-10" | ||
| } | ||
| ] | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
analyzers/its/expected/RazorSample/S6966-RazorSample-net7.0.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "Issues": [ | ||
| { | ||
| "Id": "S6966", | ||
| "Message": "Await RunAsync instead.", | ||
| "Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/Projects/RazorSample/RazorSample/Program.cs#L25", | ||
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "Location": "Line 25 Position 1-10" | ||
| } | ||
| ] | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
analyzers/its/expected/akka.net/S6966-Akka.Cluster.Sharding-netstandard2.0.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "Issues": [ | ||
| { | ||
| "Id": "S6966", | ||
| "Message": "Await StartProxyAsync instead.", | ||
| "Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/Projects/akka.net/src/contrib/cluster/Akka.Cluster.Sharding/ClusterSharding.cs#L742", | ||
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "Location": "Line 742 Position 24-92" | ||
| } | ||
| ] | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <p>In an <code>async</code> method, any blocking operations should be avoided.</p> | ||
| <h2>Why is this an issue?</h2> | ||
| <p>Using a synchronous method instead of its <a href="https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/">asynchronous</a> | ||
| counterpart in an <code>async</code> method blocks the execution and is considered bad practice for several reasons:</p> | ||
| <dl> | ||
| <dt> | ||
| Resource Utilization | ||
| </dt> | ||
| <dd> | ||
| <p>Each thread consumes system resources, such as memory. When a thread is blocked, it’s not doing any useful work, but it’s still consuming these | ||
| resources. This can lead to inefficient use of system resources.</p> | ||
| </dd> | ||
| <dt> | ||
| Scalability | ||
| </dt> | ||
| <dd> | ||
| <p>Blocking threads can limit the scalability of your application. In a high-load scenario where many operations are happening concurrently, each | ||
| blocked thread represents a missed opportunity to do useful work. This can prevent your application from effectively handling increased load.</p> | ||
| </dd> | ||
| <dt> | ||
| Performance | ||
| </dt> | ||
| <dd> | ||
| <p>Blocking threads can degrade the performance of your application. If all threads in the thread pool become blocked, new tasks can’t start | ||
| executing until an existing task completes and frees up a thread. This can lead to delays and poor responsiveness.</p> | ||
| </dd> | ||
| </dl> | ||
| <p>Instead of blocking, it’s recommended to use the <a | ||
| href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await"><code>async</code> operator</a> with async methods. This | ||
| allows the system to release the current thread back to the thread pool until the awaited task is complete, improving scalability and | ||
| responsiveness.</p> | ||
| <h2>How to fix it</h2> | ||
| <h3>Code examples</h3> | ||
| <h4>Noncompliant code example</h4> | ||
| <pre data-diff-id="1" data-diff-type="noncompliant"> | ||
| public async Task Examples(Stream stream, DbSet<Person> dbSet) | ||
| { | ||
| stream.Read(array, 0, 1024); // Noncompliant | ||
| File.ReadAllLines("path"); // Noncompliant | ||
| dbSet.ToList(); // Noncompliant in Entity Framework Core queries | ||
| dbSet.FirstOrDefault(x => x.Age >= 18); // Noncompliant in Entity Framework Core queries | ||
| } | ||
| </pre> | ||
| <h4>Compliant solution</h4> | ||
| <pre data-diff-id="1" data-diff-type="compliant"> | ||
| public async Task Examples(Stream stream, DbSet<Person> dbSet) | ||
| { | ||
| await stream.ReadAsync(array, 0, 1024); | ||
| await File.ReadAllLinesAsync("path"); | ||
| await dbSet.ToListAsync(); | ||
| await dbSet.FirstOrDefaultAsync(x => x.Age >= 18); | ||
| } | ||
| </pre> | ||
| <h2>Resources</h2> | ||
| <h3>Documentation</h3> | ||
| <ul> | ||
| <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/async">async (C# Reference)</a> </li> | ||
| <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await">await operator - asynchronously | ||
| await for a task to complete</a> </li> | ||
| </ul> | ||
| <h3>Articles & blog posts</h3> | ||
| <ul> | ||
| <li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/#dont-block-await-instead">Asynchronous | ||
| programming with async and await - Don’t block, await instead</a> </li> | ||
| <li> Microsoft Learn - <a | ||
| href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming">Async/Await - Best | ||
| Practices in Asynchronous Programming</a> </li> | ||
| <li> Microsoft Developer Blog - <a href="https://devblogs.microsoft.com/pfxteam/asyncawait-faq/">Async/Await FAQ</a> </li> | ||
| </ul> | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "title": "Awaitable method should be used", | ||
| "type": "CODE_SMELL", | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant\/Issue", | ||
| "constantCost": "5min" | ||
| }, | ||
| "tags": [ | ||
| "async-await" | ||
| ], | ||
| "defaultSeverity": "Major", | ||
| "ruleSpecification": "RSPEC-6966", | ||
| "sqKey": "S6966", | ||
| "scope": "All", | ||
| "quickfix": "targeted", | ||
| "code": { | ||
| "impacts": { | ||
| "RELIABILITY": "MEDIUM" | ||
| }, | ||
| "attribute": "COMPLETE" | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -317,6 +317,6 @@ | |
| "S6930", | ||
| "S6931", | ||
| "S6934", | ||
| "S6961" | ||
| "S6966" | ||
| ] | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.