You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Returning Task without awaiting it is contradictory to MSFT employee's advice #16187
The last code block in this docs article either throws an exception or directly returns a Task without awaiting it. This is directly contradictory to what David Fowler and Stephen Cleary suggest. See also this issue regarding David Fowler's advice: davidfowl/AspNetCoreDiagnosticScenarios#53
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: c0a70d19-0d23-f877-8860-df514cec3c40
Version Independent ID: 942dbec3-a753-733f-c551-03d203e48969
Whether to await or directly return a Task is a recommendation with a lot of subtleties. We should explain it better. The reason behind the subtleties is how synchronous and asynchronous methods differ: For any method with the async modifier, the compiler generates all the code for successfully managing awaits in the method. This includes rearranging any finally clauses so that those are executed only after all asynchronous work has completed. Another consideration is that any async method will never throw exceptions synchronously. Instead, it returns a Task that will be in the Faulted state if an exception is thrown.
The creation of the state machine ensures that resources aren't disposed of while still needed for asynchronous tasks. That fact leads to recommending that methods that return a Task should be async. On the other hand, there is some overhead from the extra code generated for the state machine. For that reason, some of the code in ASP.NET Core does not follow that recommendation for performance reasons.
The code in this article points to another reason to (sometimes) return a Task from a synchronous method: to ensure that exceptions stemming from programming errors are thrown synchronously. Jon Skeet and I have both advocated this because it makes debugging easier for developers using your APIs.
We should update this article (and a few others in the async project) to include these subtleties.
This issue has been closed as part of the issue backlog grooming process outlined in #22351.
That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the @dotnet/docs team for visibility.
Activity
BillWagner commentedon Dec 11, 2019
Thanks for writing this @georg-jung
Whether to await or directly return a Task is a recommendation with a lot of subtleties. We should explain it better. The reason behind the subtleties is how synchronous and asynchronous methods differ: For any method with the
asyncmodifier, the compiler generates all the code for successfully managingawaits in the method. This includes rearranging anyfinallyclauses so that those are executed only after all asynchronous work has completed. Another consideration is that anyasyncmethod will never throw exceptions synchronously. Instead, it returns aTaskthat will be in theFaultedstate if an exception is thrown.The creation of the state machine ensures that resources aren't disposed of while still needed for asynchronous tasks. That fact leads to recommending that methods that return a
Taskshould beasync. On the other hand, there is some overhead from the extra code generated for the state machine. For that reason, some of the code in ASP.NET Core does not follow that recommendation for performance reasons.The code in this article points to another reason to (sometimes) return a Task from a synchronous method: to ensure that exceptions stemming from programming errors are thrown synchronously. Jon Skeet and I have both advocated this because it makes debugging easier for developers using your APIs.
We should update this article (and a few others in the async project) to include these subtleties.
/cc @stephentoub
ericsampson commentedon Jan 7, 2020
This is a subtle topic. It would be great to have it covered in a single authoritative place. Cheers!
dotnet-bot commentedon Jan 25, 2021
This issue has been closed as part of the issue backlog grooming process outlined in #22351.
That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the
@dotnet/docsteam for visibility.ericsampson commentedon Jan 25, 2021
@BillWagner as you mentioned above, it would be good to have documentation for this topic somewhere. Or is it just too low priority relative to bugs?
remove discussion of Task-returning non-async methods
remove discussion of Task-returning non-async methods (#23001)
Devops ebook: Actions index (#23462)
Releases - DevOps for ASP.NET Core Developers eBook V1.1 (#23784)