0

Recently I have been working with the C# asynchronous computing feature exposed by the async method modifier, and the await keyword for tasks that I need the result of. I have used this feature in the past a lot, but the concept that I am still having a very hard time getting a grasp on is deadlocking. I have read numerous explanations; however, I still cannot tell, just by looking, whether or not a method will deadlock, or if I can validly make another call to an asynchronous method without accidentally deadlocking if the Task returned by the method is not used correctly.

I do not really understand what using the await keyword does that's so special, so as to cause a result to never be returned if a user tries to get the Result property, for example, of a Task returned from an asynchronous method.

Also what are the cases where it would be allowed? I know there is something about blocking and non-blocking operations, but I mean generally what code will cause this issue and what code won't?

Before anyone says that it's due to the fact that getting Result blocks the thread until the Task finishes executing causing it to be impossible for the result to be populated because it was running on the same thread, I know! I just don't understand; when you don't use any sort of asynchronous pattern, doesn't the result from a method need to be waited for anyways?

Alex Fanat
  • 748
  • 10
  • 22
  • 1
    Have you encountered the words `Synchronization Context` yet? There's quite a bit already written about that concept and realising that it's the core of the deadlocking could be quite important. – Damien_The_Unbeliever Jan 02 '18 at 10:32
  • 3
    Asynchronous deadlocks are common when you block on async code, so [Don't Block on Async Code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). – YuvShap Jan 02 '18 at 10:33
  • Your question is really too broad. It shows no evidence of research, and there are _many_ different reasons something might deadlock. It all boils down to one thing: deadlock occurs when two different code paths attempt to acquire two different resources, but in different order, such that each code path has acquired the resource the other is trying to get next. In async/await, typically this happens when some asynchronously executing code explicitly invokes back to the UI thread and holds that thread, which prevents any awaits that would resume in the UI thread from completing. – Peter Duniho Jan 02 '18 at 20:56
  • @PeterDuniho After some further research that branched off of YuvalShap's provided link, I now understand why and will post an answer. This question is actually not a duplicate because even though I asked for an example, that was not the main question; I was trying to ask why getting the result of an in-progress task will only deadlock as soon as an await is encountered, because I did not completely understand how asynchronous methods resume computation once they have completed awaiting another task. – Alex Fanat Jan 03 '18 at 03:38

0 Answers0