Skip to content

Add analyzer: Call async methods when in an async method #45661

Closed

Description

@jeffhandley

AB#1245428

Background and Motivation

The VSTHRD103 analyzer exists and it is broadly valuable. We should promote this analyzer to be part of the standard set of analyzers that ship with the .NET SDK.

Examples of patterns that are flagged by this analyzer

Task DoAsync()
{
    file.Read(buffer, 0, 10);
}

All methods where an Async-suffixed equivalent exists will produce this warning
when called from a Task-returning method.
In addition, calling Task.Wait(), Task<T>.Result or Task.GetAwaiter().GetResult()
will produce this warning.

Solution

Await the async version of the method:

async Task DoAsync()
{
    await file.ReadAsync(buffer, 0, 10);
}

Activity

  1. added
    api-suggestion
    code-analyzer
    code-fixer
    on Dec 6, 2020
  2. added this to the 6.0.0 milestone on Dec 6, 2020
  3. added
    untriaged
    on Dec 6, 2020
  4. stephentoub commented on Dec 6, 2020

    @stephentoub
    Member

    This is worth pursuing in some fashion, but it also has the potential to be very noisy, e.g. it'll wreak havoc on the pattern of sharing one code path between async and sync code, e.g.

    if (async)
    {
    await socket.ConnectAsync(endPoint, cancellationToken).ConfigureAwait(false);
    }
    else
    {
    using (cancellationToken.UnsafeRegister(static s => ((Socket)s!).Dispose(), socket))
    {
    socket.Connect(endPoint);
    }
    }

  5. jeffhandley commented on Dec 6, 2020

    @jeffhandley
    MemberAuthor

    I expect one of the first steps we'll take on this proposal is to run the existing analyzer against the runtime repo. From that, we can assess what the false positive rate would be and guide the path forward.

  6. self-assigned this
    on Jan 6, 2021
  7. added
    Priority:1
    on Jan 14, 2021
  8. carlossanlop commented on Jan 14, 2021

    @carlossanlop
    Contributor

    This is worth pursuing in some fashion, but it also has the potential to be very noisy, e.g. it'll wreak havoc on the pattern of sharing one code path between async and sync code

    I think this analyzer offers great value. We can always set the category to Info or even Hidden to reduce the noise.

    cc @buyaa-n

  9. added
    Cost:S
    on Jan 14, 2021
  10. carlossanlop commented on Jan 15, 2021

    @carlossanlop
    Contributor

    Considering the VSTHRD103 analyzer already exists, we want to:

    • Bring it to the API Proposal meeting to decide if we want to turn it on.
    • In Parallel, test it in the dotnet/runtime repo to see how well it performs there. If we find any bugs or unhandled cases, address them.

    Now, since this analyzer does not live in the dotnet/roslyn-analyzers repo, I'm unsure what the process would be to enable it, if we decide to do so.

    @mavasani @AArnott can you help me answer the following questions?:

  11. 6 remaining items

  12. added
    api-needs-work
    on Jan 26, 2021
  13. terrajobst commented on Jan 26, 2021

    @terrajobst
    Contributor

    Video

    • This might be noisy, at least in the runtime repo
    • We should run this first so we can see what the noise is to make a determination of on/off by default and severity
  14. carlossanlop commented on Feb 20, 2021

    @carlossanlop
    Contributor

    I turned it on locally for the runtime repo and I got 259 Errors 10 errors in CLR. I created a draft PR so we can have an official CI output linked to this issue: #48540

    @terrajobst @stephentoub @buyaa-n I think this analyzer should be enabled as Info for dotnet/runtime repo. It would also make sense to mark it as Info globally.

    Should I bring this to API review again, or would it be ok to have it approved offline?

  15. carlossanlop commented on Apr 8, 2021

    @carlossanlop
    Contributor

    @terrajobst based on this comment by Stephen, can you please mark this API as approved? The analyzer will be disabled by default in the runtime repo, and the default severity will be info.

  16. added
    api-approved
    and removed
    api-needs-work
    on Apr 13, 2021
  17. jeffhandley commented on Jun 24, 2021

    @jeffhandley
    MemberAuthor

    We are targeting 6.0 RC1 for this analyzer & fixer.

  18. jeffhandley commented on Sep 15, 2021

    @jeffhandley
    MemberAuthor
  19. ghost locked as resolved and limited conversation to collaborators on Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Cost:SPriority:1api-approvedarea-System.Threading.Taskscode-analyzercode-fixer

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions