An async lambda is passed to ForEach or Parallel.For, which take an action and do not wait for its task

tx0/csharp:async-lambda-as-actiondefaultwarningreliability/concurrencydepth fastbug

What it finds

List<T>.ForEach, Parallel.For and Parallel.ForEach take an Action, so an async lambda given to them becomes an async void delegate: each call returns at its first await, the loop finishes before the work does, and exceptions are raised where nobody catches them. A foreach with await, Task.WhenAll or Parallel.ForEachAsync waits for the work.

From the rule's source, async_lambda_as_action.rs.

Example from the rule's tests
using System.Collections.Generic;using System.Threading.Tasks; class Uploads{    void Run(List<string> files)    {        files.ForEach(async f => await Send(f));tx0 reports: this async lambda runs as async void; ForEach does not wait for it        Parallel.ForEach(files, async f => await Send(f));tx0 reports: this async lambda runs as async void; ForEach does not wait for it        files.ForEach(f => Send(f).Wait());    }     Task Send(string f) => Task.CompletedTask;}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:async-lambda-as-action" = "off"

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:async-lambda-as-action] reason="..."

tx0 explain tx0/csharp:async-lambda-as-action shows the same in the terminal. Found a false positive? Report it.