Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find usages of async where await is not used in Visual Studio or Unit Test

I'm currently working on a large application that makes extensive use of the async and await keywords in C#. Our IO calls are asynchronous pretty much throughout.

When using an async call that does not await (unintentionally, because a developer forgot to use it), it's possible to encounter task cancellations and other undesired behavior as a consequence of not awaiting the result.

E.g. doSomethingAsync().ConfigureAwait(false) vs

 await doSomethingAsync().ConfigureAwait(false)

Is there any way I can succinctly identify usages of async methods that are not awaiting properly?

Ideally it's something we could unit test (we use DI throughout as well). Simple method tests are unreliable because the task completing essentially becomes a race condition, where it will succeed often, and fail occasionally.

If unit testing for this is not possible, is there a way to search for and isolate these instances in-mass?

like image 642
Grant H. Avatar asked Oct 24 '25 06:10

Grant H.


1 Answers

There is a VS extension called AsyncFixer which tries to make recommendations.

https://marketplace.visualstudio.com/items?itemName=SemihOkur.AsyncFixer#qna

It may help, but unfortunately can't locate this pattern:

Task t = DelayAndThrowAsync(); // Never awaited!

You can explicitly do this to make the task 'go away', and it was an acceptable stop gap before my whole codebase was async - but now it pretty much is I need to find the offending unawaited tasks.

The problem is that TaskScheduler.UnobservedTaskException doesn't actually tell me where the offending task was :-/

like image 115
Simon_Weaver Avatar answered Oct 26 '25 19:10

Simon_Weaver