Potential deadlocks due to .Result calls?
See original GitHub issueScanning through the code ready for the codeathon tomorrow, I’ve spotted a few .Result
calls. One example is in SendRequestStatusToGetASmokeAlarm.Send
, where it does this: _httpClient.SendAsync(request).Result
. There are a few more examples though. This can cause deadlocks and should be avoided. I’ve lost countless hours in the past due to someone using .Result
, and causing random deadlocks.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
'await' works, but calling task.Result hangs/deadlocks
Acquiring a value via an async method: var result = Task.Run(() => asyncGetValue()).Result;. Syncronously calling an async method. Task.
Read more >Understanding Async, Avoiding Deadlocks in C# | by Eke ...
If called from threadpool thread then a theadpool thread is blocked, which will lead to a deadlock if the work load is high...
Read more >Potential deadlock due to calling callbacks while holding a ...
As locks are acquired in reverse orders from two threads, a deadlock is possible. The fundamental issue is that we should not call...
Read more >C# Deadlocks in Depth – Part 2
One potential danger manifests when you are using an async method from a non-async method. In those cases, you can use the .Result...
Read more >Don't Block on Async Code
This kind of deadlock is always the result of mixing synchronous with asynchronous code. Usually this is because people are just trying out ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree that this should be fixed. If there are rare places we can’t do async all the way, we should record those and decide what the options are in each case.
Therefore I’d suggest the scope of this is to properly await any async calls wherever .Result or .GetAwaiter().GetResult() calls are made.
Good spot @dracan
@dracan , @bernard-chen I’ve used AsyncHelper in cases when async is not possible due to framework, platform, library, whatever - and it does its job. Agree that those special cases have to isolated and treated very special - more like an exceptional situation and not normal one.