I've read here https://blog.stephencleary.com/2009/10/synchronizationcontext-properties.html that ASP.NET applications' execution context does not have specific associated thread. Does it mean code after await will(can) be executed in different thread with the same context ? In this case how it is possible that deadlock can be caused by synchronous execution? Or ASP.NET application is not the case for such deadlock ?
Thanks in advance.
For ASP.NET Classic (.NET Framework), there is a special AspNetSynchronizationContext
, the continuation will post back to the original context thread.
ASP.NET Core there isn’t one. If you inspect SynchronizationContext.Current
you’ll find that it’s set to null
. As such, a continuation is free to use what ever thread it chooses, and will suffer no classic deadlocks in that respect
Update
Some great corrections from @StephenCleary in the comments
Minor correction : on classic ASP.NET, the
SynchronizationContext
represents the request context, not a specific thread.The method may resume on any thread pool thread after the
await
. The deadlock occurs because there is a lock as part of that request context to ensure that only one thread at a time may be in the request context.So, when the async method is ready to resume, a thread pool thread is taken which enters the request context and tries to take that lock. If there's another thread blocked on that task in the context, the lock is already taken and a deadlock will occur