🔧 The Parallel Loop That Was Never Actually Parallel
An await foreach loop over an IAsyncEnumerable<T> pulls items one at a time by design – each MoveNextAsync call on the underlying async iterator has to complete before the next one starts, since that’s fundamentally how pull-based async streaming works. Wrapping the per-item work inside that loop in Task.WhenAll looks like it should parallelize the processing, but if the tasks are created and awaited sequentially inside the loop body rather than collected into a list first, each iteration’s single-item WhenAll still waits for that one task to finish before the loop can even ask the stream for its next item – the WhenAll call is technically running tasks concurrently, there’s just only ever one task in it at any given moment.
🔎 The Problem
async Task ProcessAllAsync(IAsyncEnumerable<Order> orders)
{
await foreach (var order in orders)
{
// Looks parallel - it's calling Task.WhenAll! - but there's
// only ever ONE task in the array on any given iteration.
await Task.WhenAll(ProcessOrderAsync(order));
// The loop still can't ask for the NEXT order from the
// stream until this WhenAll (and therefore this one task)
// completes - so items are still processed strictly one at
// a time, exactly as if Task.WhenAll was never called at all.
}
}
// Intended behavior was likely: pull several orders, kick off their
// processing concurrently, and only then wait for that batch as a
// group - which this code never actually does.
✅ Fix: Batch the Items Before You WhenAll Them
- Explicitly buffer a batch of items from the async stream into a List<T> first (using a fixed batch size, or Chunk-style helper for async streams), THEN call Task.WhenAll once against the full list of per-item tasks for that batch – this is what actually lets several items process concurrently, since every task in the batch is created and running before any of them are awaited.
- If true full-stream concurrency (not just batches) is the actual goal, use a bounded concurrency pattern like a SemaphoreSlim alongside a collection of in-flight tasks, starting new tasks from the stream as the semaphore permits rather than waiting for one single item’s task to fully complete before pulling the next.
- Where IAsyncEnumerable’s pull-one-at-a-time nature isn’t actually required (the whole source fits comfortably in memory), consider materializing it to a List<T> with ToListAsync() first, and parallelize over that concrete list instead – this avoids fighting the streaming abstraction’s inherent sequential-pull behavior when the use case never actually needed streaming in the first place.
🚨 Why This Is Dangerous
- The code compiles cleanly, runs without any exception, and produces entirely correct output – it’s purely a performance bug, with results that look identical whether the items were processed one at a time or genuinely in parallel, which makes it very easy to ship and never notice until someone specifically investigates why a ‘parallelized’ operation takes exactly as long as a fully sequential one would.
- Because Task.WhenAll genuinely is the correct building block for real concurrency, seeing it present in the code during a review reads as evidence the concurrency was handled correctly – the actual mistake (a single-element array instead of a real batch) is a small, easy detail to miss precisely because the reviewer’s attention is drawn to confirming WhenAll is there at all, not to counting how many tasks it’s actually given.
Task.WhenAll doesn’t make anything run in parallel by itself – it waits for however many tasks you hand it, and handing it exactly one, over and over inside a loop, is just awaiting that one task with extra steps.

