Roslyn-analyzers: CA2008 Do not create tasks without passing a TaskScheduler doesn't appear when pasing method group

Created on 29 Nov 2018  路  3Comments  路  Source: dotnet/roslyn-analyzers

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

Example: v2.6.2

Diagnostic ID

CA2008: Do not create tasks without passing a TaskScheduler

Do not create tasks unless you are using one of the overloads that takes a TaskScheduler. The default is to schedule on TaskScheduler.Current, which would lead to deadlocks. Either use TaskScheduler.Default to schedule on the thread pool, or explicitly pass TaskScheduler.Current to make your intentions clear.

I could not find a documentation page.

Repro steps

Use TaskFactory.StartNew as method group parameter.

public static class Program
{
    public static async Task Main(string[] args)
    {
        var taskFactory = new TaskFactory();
        var actions = new Action[] { Console.WriteLine, Console.WriteLine };
        await Task.WhenAll(actions.Select(taskFactory.StartNew)).ConfigureAwait(false);
    }
}

Expected behavior

Waring is displayed.

Actual behavior

No warning is displayed.


I just stumpled upon this "behaivor". It's not important for me I just wanted to report it for documentation reasons.

Bug help wanted

Most helpful comment

@AArnott

Oh wait, that's not the same issue. You're arguing that the diagnostic should appear with a method group.

Yep but I think this one could be the same.
CA2008 Do not create tasks without passing a TaskScheduler should not appear

All 3 comments

Amusingly, our version of this analyzer (VSTHRD105) got the same feedback.

Oh wait, that's not the same issue. You're arguing that the diagnostic should appear with a method group. But per the issue I linked to, in this particular example case (of using a custom TaskFactory) it may be good to suppress such a diagnostic.

@AArnott

Oh wait, that's not the same issue. You're arguing that the diagnostic should appear with a method group.

Yep but I think this one could be the same.
CA2008 Do not create tasks without passing a TaskScheduler should not appear

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OmarTawfik picture OmarTawfik  路  3Comments

sharwell picture sharwell  路  4Comments

stephentoub picture stephentoub  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments