A task is waited for synchronously with Wait(), .Result or GetAwaiter().GetResult()
What it finds
Blocking on a task holds a thread for the whole wait and, where a synchronization context is present, as in UI code and classic ASP.NET, can deadlock when the task needs that context to finish. Inside async methods another rule reports this; here the call is in synchronous code, where it is sometimes the only option, so the rule is strict.
From the rule's source, sync_over_async.rs.
using System.Threading.Tasks; class Startup{ void Run(Task task) { task.Wait();tx0 reports: Wait() blocks this thread until the task ends Load().GetAwaiter().GetResult();tx0 reports: GetAwaiter().GetResult() blocks this thread until the task ends } Task Load() => Task.CompletedTask;} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:sync-over-async" = "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:sync-over-async] reason="..."tx0 explain tx0/csharp:sync-over-async shows the same in the terminal. Found a false positive? Report it.