Interview Question

How to avoid deadlocks with async?

Use async all the way and avoid blocking waits or locks around await.

💡 Concept ✅ Quick Revision 🔷 C#

Answer

Avoid async deadlocks by keeping the call chain asynchronous and awaiting tasks instead of synchronously blocking on them. • Do not call Result, Wait, or GetAwaiter().GetResult on incomplete tasks in a context-sensitive call chain. • Do not hold a synchronous lock while awaiting. • Library code can use ConfigureAwait(false) when it does not require the captured context.

💡 C# Example

static async Task<string> LoadAsync(HttpClient client) { return await client.GetStringAsync("https://example.com").ConfigureAwait(false); }

⚡ Quick Revision

Use async all the way and avoid blocking waits or locks around await.