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?
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 :-/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With