Async / await · interview question
A desktop app freezes forever when a button handler calls GetDataAsync().Result. Explain why this deadlocks and how to fix it.
0 words · aim for 40+
Answer and explanation
Correct answer
The UI thread has a SynchronizationContext. GetDataAsync awaits something and, by default, captures that context so its continuation must run on the UI thread. Calling .Result blocks the UI thread waiting for the task. The task can't finish because its continuation is queued to the very thread that's blocked: a deadlock.
Fix: don't block. Make the handler async and await GetDataAsync() (async all the way). In library code, ConfigureAwait(false) avoids capturing the context, which also prevents this. ASP.NET Core has no SynchronizationContext, so it won't deadlock the same way, but blocking still wastes thread-pool threads and can cause starvation under load.
Key points:
- The UI thread's SynchronizationContext is captured by await
- .Result/.Wait() blocks that same thread
- The continuation can't run because the thread it needs is blocked
- Fix: async all the way (make the handler async and await)
- ConfigureAwait(false) in libraries avoids capturing the context
Why
This is the classic sync-over-async deadlock. Strong answers name the captured context and the blocked thread, and give both fixes.
Test yourself on Async / await
A free test with instant, explained results. No account needed.