🔧 The TaskCompletionSource That Deadlocked Its Own Caller
A plain TaskCompletionSource<T> runs every continuation chained onto its Task synchronously, on whatever thread happens to call SetResult – by default, that’s not a background detail, it’s the thread that just finished the work you’re wrapping. If that continuation then tries to acquire a lock the calling code is still holding, or blocks waiting on something that thread also owns, you get a deadlock that only appears under contention.
🔎 The Problem
private readonly object _lock = new object();
private TaskCompletionSource<int> _pendingResult;
public void OnHardwareCallback(int value)
{
lock (_lock)
{
// SetResult runs every awaiter's continuation
// synchronously, right here, before this method
// returns - while _lock is still held.
_pendingResult?.SetResult(value);
}
}
public async Task<int> WaitForNextValueAsync()
{
var tcs = new TaskCompletionSource<int>();
_pendingResult = tcs;
int result = await tcs.Task;
lock (_lock) // Deadlock: the continuation above already
{ // tried to re-enter this same lock.
// ...
}
return result;
}
✅ Fix: Force Continuations Off the Calling Thread
- Construct it with
new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously)– this is the single flag that moves every continuation onto a thread-pool thread instead of running it inline inside SetResult, and it should be the default choice unless you’ve specifically measured that synchronous completion is safe. - Never call SetResult (or SetException/SetCanceled) while holding a lock the continuation might also need – even with RunContinuationsAsynchronously set, it’s a fragile pattern that becomes a real deadlock the moment someone changes the continuation’s code later.
- If you’re on an older target that predates the option, wrap the call in
Task.Run(() => tcs.SetResult(value))to get the same effect – it costs a thread-pool hop, but it’s cheap insurance against a deadlock that only shows up under production load.
🚨 Why This Is Dangerous in Production
- The deadlock depends on timing and lock contention, so it can pass every test and every staging run and only reproduce under real concurrent load – exactly the conditions you can’t easily attach a debugger to.
- It’s not unique to a hand-rolled TaskCompletionSource either – any library that exposes one under the hood (a custom async wrapper around a callback-based API, a manual connection pool) inherits the same synchronous-continuation risk unless it was built with the flag set.
A TaskCompletionSource doesn’t ask whose thread it’s running on – it just runs there, and if that thread is still holding a lock your continuation needs, you’ve built a deadlock with no waiting involved.
