33

Why .NET chose stackless async

The framing chapter: green threads prototyped and rejected, and why that choice shapes every pattern that follows.

Chapter 33

Learning objectives

By the end of this chapter you can:

  • State what a suspended await costs at runtime: a small heap-allocated state machine, not a parked thread.
  • Trace SailingBookingDesk.AcceptAsync from straight-line source to a state machine that suspends at its one await and resumes when IBookingLedger.ConfirmAsync completes.
  • Explain why AcceptAllAsync can start a thousand bookings from one calling thread without a thousand threads.
  • Say why HasCapacityAsync returns ValueTask<bool> and completes with no Task allocation.
  • Name the .NET team’s 2022 green-threads prototype, what it tried to do differently, and why the team kept the stackless model.

Why this matters

An async method in C# doesn’t run on its own thread. It compiles into a state machine, a small heap object that records where execution paused and the locals it needs to resume. That was a design decision with a real alternative, and .NET prototyped the alternative. The stackless model explains why a booking desk holds a thousand in-flight confirmations on a handful of thread-pool threads, why a CancellationToken check matters before the first await, and why ValueTask<TResult> exists alongside Task<TResult>.

SailingBookingDesk accepts consignments onto one sailing by confirming each against an IBookingLedger. A real confirmation is I/O-bound: a network round trip or a database write, during which the initiating thread has nothing to do. AcceptAsync awaits one confirmation; AcceptAllAsync runs a batch concurrently.

Diagram 33-why-dotnet-chose-stackless-async hasn't been drawn yet. See diagrams/README.md for the conventions.

Figure 33.1: A thousand SailingBookingDesk bookings suspend at IBookingLedger.ConfirmAsync and resume later, each one costing a small heap state machine rather than a dedicated thread. Download .excalidraw

A method that compiles into a state machine

AcceptAsync reads as sequential code:

public async Task<BookingReceipt> AcceptAsync(BookingRequest request, CancellationToken ct = default)
{
    var seq = await ledger.ConfirmAsync(request.ConsignmentId, ct);
    Interlocked.Increment(ref _accepted);
    return new BookingReceipt(request.ConsignmentId, seq);
}

The compiler rewrites it into a type implementing a state-machine interface, with:

  • a numbered state field recording which await, if any, it’s suspended at;
  • fields for every parameter and local needed across a suspension (request, ct, later seq);
  • a MoveNext method holding the original logic, split at each await.

Calling AcceptAsync runs MoveNext until the method finishes or reaches an await on incomplete work. Then MoveNext returns, the calling thread is free, and the state machine schedules MoveNext again for when ConfirmAsync’s task completes.

The locals live in fields because no stack frame survives to hold them: the calling thread has moved on. A suspended await is an object sized to one method’s locals, tens or hundreds of bytes, against the megabyte-scale stack a dedicated thread reserves. That’s the stackless model’s defining property.

The alternative the .NET team tried and set aside

In 2022 the .NET team prototyped stackful coroutines under the name green threads: lightweight, runtime-scheduled threads with their own growable stacks. A green thread can suspend several frames below the call that blocks, because its whole stack travels with it. The appeal was that existing synchronous code, including library code a team doesn’t control, could scale without async and await threaded through every method on the path.

The team wound the prototype down the following year. Its published results, in the same dotnet/runtimelab repository, favored the model already in production:

  Stackful green thread Stackless state machine
Held across a suspension The whole growable call stack One method’s locals
Can suspend deep in a call chain Yes Only at an await
Code changes needed None in principle async/await on the call path
Footprint at 100,000 outstanding operations 100,000 stacks 100,000 small objects
Status Prototype, wound down Shipping since C# 5, tuned, used across the base class library

Diagram 33-stackless-vs-stackful hasn't been drawn yet. See diagrams/README.md for the conventions.

Figure 33.2: A stackful green thread carries a full growable call stack across every suspension; a stackless async method carries only the small state machine its own locals need. Download .excalidraw

A thousand bookings, one calling thread, no thread each

AcceptAllAsync makes the payoff observable:

public async Task<IReadOnlyList<BookingReceipt>> AcceptAllAsync(
    IReadOnlyCollection<BookingRequest> requests, CancellationToken ct = default)
{
    var tasks = requests.Select(r => AcceptAsync(r, ct)).ToList();
    return await Task.WhenAll(tasks);
}

ToList() enumerates on the calling thread. Each AcceptAsync runs to its await, suspends and hands back an incomplete Task, and the loop moves on. When ToList() returns, every booking has started and every one is suspended at the same point, with the calling thread never blocked. Task.WhenAll then suspends AcceptAllAsync until all of them finish.

A gated ledger demonstrates it. Every confirmation parks behind one shared TaskCompletionSource, and the ledger records which managed thread reached each confirmation before its await. With a thousand bookings started:

  • all thousand reach the gate;
  • GatedBookingLedger.DistinctPreAwaitThreadCount is 1, so they all started from one thread;
  • none has completed until the gate opens.

A stackful model would need a thousand growable stacks for that; the stackless model uses a thousand small objects.

Diagram 33-suspended-without-a-thread hasn't been drawn yet. See diagrams/README.md for the conventions.

Figure 33.3: A thousand booking requests start from one thread, suspend at the ledger gate with zero threads held, and only resume once the test releases them together. Download .excalidraw

A single booking shows the resume half. It stays incomplete until the gate releases, then MoveNext runs again after the await, with request and the new seq available from the state machine’s fields as if they were locals. None of this shows in the source: write the method as if it runs straight through, and the compiler carries the state across suspensions.

Skipping the state machine entirely with ValueTask

HasCapacityAsync has no async keyword and no await:

public ValueTask<bool> HasCapacityAsync(CancellationToken ct = default) =>
    new(Volatile.Read(ref _accepted) < sailingCapacity);

It reads a counter and compares it with a limit, finishing before it returns. A Task<bool> would work at every call site but allocate a heap object for a result that was never asynchronous. ValueTask<TResult> is a struct that holds a completed result with no allocation and wraps a real Task<TResult> only when the work goes asynchronous. The returned value reports IsCompletedSuccessfully before anyone awaits it: no suspension, and no state machine built for the call.

Build, run, observe

cd examples/33-why-dotnet-chose-stackless-async
dotnet test

All 6 tests pass, and scripts/verify.sh at the repo root runs them with every other example. They cover a ledger-sequenced receipt for one booking, distinct sequence numbers across a thousand concurrent bookings, the synchronous capacity check, a cancelled token throwing before any confirmation starts, and the thousand-bookings-from-one-thread suspension.

When not to use it

  • CPU-bound work. A routing calculation over a large sailing schedule, or a hash over a large manifest, has no idle wait to release the thread during. Declaring it async adds a state machine around work that never suspends. Use parallelism across threads instead.
  • Async-over-sync. Task.Run around a blocking call moves the block onto a pool thread, which then waits for the whole call. That burns a pool thread and adds a scheduling hop. The in-memory ConfirmAsync earns its signature because Task.Yield suspends it and frees the thread.
  • Fire-and-forget. An unawaited task drops the one guarantee Task provides: knowing when the work finished and whether it threw. A desk that discarded ConfirmAsync’s task would lose ledger failures, or see them later as unobserved task exceptions with no link to the booking. AcceptAsync always awaits and propagates success, cancellation or failure to its caller.

🤖 Explore with your agent

“In SailingBookingDesk, make AcceptAsync async and await ledger.ConfirmAsync(request.ConsignmentId, ct) directly, passing the token. AcceptAllAsync starts one AcceptAsync per request and awaits Task.WhenAll. Leave HasCapacityAsync without async: return new ValueTask<bool>(...) built from Volatile.Read(ref _accepted) < sailingCapacity. Don’t use Task.Run, Task.FromResult, .Result, .Wait() or Thread.Sleep, and don’t discard a task.”

Run dotnet test --project SailingBookings.Tests --filter "BookingDeskTests|StacklessModelTests"; all 6 must pass.

For deeper coverage