An async method blocks on a task or sleeps instead of awaiting
What it finds
task.Result, task.Wait(), GetAwaiter().GetResult() and Thread.Sleep block the thread an async method runs on, which wastes it and can deadlock when the awaited work needs the same context. await and Task.Delay wait without blocking.
From the rule's source, blocking_in_async.rs.
using System.Threading;using System.Threading.Tasks; class Sync{ async Task<int> Load(Task<int> pending) { Thread.Sleep(100);tx0 reports: Thread.Sleep blocks inside an async method var a = pending.Result;tx0 reports: .Result blocks inside an async method var b = FetchAsync().GetAwaiter().GetResult();tx0 reports: GetAwaiter().GetResult() blocks inside an async method pending.Wait();tx0 reports: Wait() blocks inside an async method return a + b + await pending; } int Blocking(Task<int> pending) => pending.Result; Task<int> FetchAsync() => Task.FromResult(1);} 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:
[rules]
"tx0/csharp:blocking-in-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:blocking-in-async] reason="..."tx0 explain tx0/csharp:blocking-in-async shows the same in the terminal. Found a false positive? Report it.