ContinueWith or TaskFactory.StartNew runs on whatever scheduler is current, as no scheduler is passed

tx0/csharp:task-without-schedulerrecommendedwarningreliability/logicdepth fastbug

What it finds

Without an explicit TaskScheduler, ContinueWith and Task.Factory.StartNew use TaskScheduler.Current, the scheduler of the task that happens to be running. Called from a UI continuation, work meant for the thread pool then runs on the UI thread. Passing TaskScheduler.Default, or using Task.Run and await, makes the choice explicit.

From the rule's source, task_without_scheduler.rs.

Example from the rule's tests
using System.Threading.Tasks; class Jobs{    void Run(Task task)    {        task.ContinueWith(t => Log());tx0 reports: ContinueWith runs on the current scheduler, which depends on the caller        task.ContinueWith(t => Log(), TaskScheduler.Default);        Task.Factory.StartNew(Log);tx0 reports: StartNew runs on the current scheduler, which depends on the caller    }     void Log() { }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:task-without-scheduler" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:task-without-scheduler] reason="..."

tx0 explain tx0/csharp:task-without-scheduler shows the same in the terminal. Found a false positive? Report it.