Skip to content
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

VSTHRD103 fired on Task.WaitAll #951

Open
AlexanderHawliczek opened this issue Nov 16, 2021 · 6 comments
Open

VSTHRD103 fired on Task.WaitAll #951

AlexanderHawliczek opened this issue Nov 16, 2021 · 6 comments

Comments

@AlexanderHawliczek
Copy link

Bug description

VSTHRD103 complains on:
Task.WaitAll(taskLArray, CancellationToken.None);
suggesting
Task.await(taskList.ToArray(), CancellationToken.None);

Repro steps

Task.WaitAll(someTaskArray, CancellationToken.None);

Expected behavior

do nothing

Actual behavior

Corrects to
Task.await(someTaskArray, CancellationToken.None);

  • Version used: 17.0.64
  • Application (if applicable): VS Studio 2022 prof
@AArnott
Copy link
Member

AArnott commented Nov 16, 2021

I can't repro this.

image

No warnings appear.

What you say the analyzer is suggesting doesn't make any sense. I don't think the code fix would know how to add .ToArray() to the first argument or change taskLArray to taskList.

Having one of our analyzers recommend Task.WhenAll over Task.WaitAll, particularly while inside an async method, would be by design.

@AlexanderHawliczek
Copy link
Author

My apologies. I mixed it up.
I was able to recreate it right away with VS 2019 .Net 5
Version 17.0.64

Original

    public async Task DummyAsync()
    {
      var taskArray = new Task[1] { new Task(() => Console.WriteLine("")) };
      Task.WaitAll(taskArray, CancellationToken.None);
    }

image

Result

    public async Task DummyAsync()
    {
      var taskArray = new Task[1] { new Task(() => Console.WriteLine("")) };
      await Task;
    }

Btw if the method is a simple void, it almost gets it right:
Original non async method

    public void DummyAsync()
    {
      var taskArray = new Task[1] { new Task(() => Console.WriteLine("")) };
      Task.WaitAll(taskArray, CancellationToken.None);
    }

Changed

    public async Task DummyAsync()
    {
      var taskArray = new Task[1] { new Task(() => Console.WriteLine("")) };
      await Task.WhenAll(taskArray, CancellationToken.None);
    }

Just the CancellationToken.None breaks the code. Works fine if token is not in original

@AArnott
Copy link
Member

AArnott commented Nov 17, 2021

Thanks for the better code snippets. I still cannot repro though. What version of the microsoft.visualstudio.threading.analyzers package are you using? Consider sending a zip of your minimal repro project if that's easier.

@AlexanderHawliczek
Copy link
Author

Here you go.
New simple console project only the VS threading package added
Microsoft-VS-Threading-Issue.zip

On an additional example I found this
image
Stack trace is in the project at the example causing it.

@AArnott
Copy link
Member

AArnott commented Nov 23, 2021

Perfect. These all repro for me. I'll keep this around and turn each of these into unit tests in the repo to fix them up.

@hcoona
Copy link

hcoona commented Jul 4, 2023

ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants