Skip to content

Returning Task without awaiting it is contradictory to MSFT employee's advice #16187

Closed

Description

@georg-jung

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.

Activity

  1. BillWagner commented on Dec 11, 2019

    @BillWagner
    Member

    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 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.

    /cc @stephentoub

  2. ericsampson commented on Jan 7, 2020

    @ericsampson

    This is a subtle topic. It would be great to have it covered in a single authoritative place. Cheers!

  3. added
    won't fix
    on Jan 25, 2021
  4. dotnet-bot commented on Jan 25, 2021

    @dotnet-bot
    Contributor

    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.

  5. ericsampson commented on Jan 25, 2021

    @ericsampson

    @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?

  6. removed
    won't fix
    on Jan 25, 2021
  7. self-assigned this
    on Feb 24, 2021
  8. added a commit that references this issue on Feb 24, 2021
    22d4dc8
  9. added a commit that references this issue on Mar 3, 2021
    f209668
  10. added a commit that references this issue on Mar 24, 2021
    2a6dc59
  11. added a commit that references this issue on Apr 20, 2021
    a1c84bd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions