loading experience

.NET

.NET 11: Runtime Async, async built into the runtime

Runtime Async in .NET 11: cleaner stack traces, JIT optimizations, tiered compilation, tailcalls and continuations without ExecutionContext.

Runtime Async (runtime-native async, "Runtime Async V2") is one of the most significant changes in the .NET 11 runtime. Instead of the compiler emitting classes that implement a state machine, the runtime itself tracks suspension and resumption of asynchronous execution. According to the official documentation, the result is cleaner stack traces, better debuggability and lower overhead.

How to enable it

In a project that targets net11.0, add this property to the project file:

<PropertyGroup>
  <Features>runtime-async=on</Features>
</PropertyGroup>
  • A net11.0 project no longer requires EnablePreviewFeatures.
  • The runtime libraries are compiled with runtime-async=on and no longer contain compiler-generated state machines. That makes it possible to migrate an entire app (with only library dependencies) to the new model, and it gives broad functional and performance validation of the feature. The product team welcomes reports, positive or negative, on throughput and library size.
  • The DOTNET_RuntimeAsync and UNSUPPORTED_RuntimeAsync environment variables have been removed. To opt out per project, set <UseRuntimeAsync>false</UseRuntimeAsync> in the project file.

Cleaner live stack traces

The most visible improvement is in live stack traces: what profilers, debuggers and new StackTrace() see during execution. With compiler-generated async, each async method produces several frames of infrastructure; with Runtime Async, the actual methods appear directly on the stack. In the official sample:

await OuterAsync();
static async Task OuterAsync()
{
    await Task.CompletedTask;
    await MiddleAsync();
}
static async Task MiddleAsync()
{
    await Task.CompletedTask;
    await InnerAsync();
}
static async Task InnerAsync()
{
    await Task.CompletedTask;
    Console.WriteLine(new StackTrace(fNeedFileInfo: true));
}
  • Without runtime-async: 13 frames, with the state-machine infrastructure (AsyncMethodBuilderCore.Start) clearly visible.
  • With runtime-async: 5 frames, the real call chain (InnerAsync, MiddleAsync, OuterAsync, Main$, Main).

Exception stack traces (inside a catch) already look the same with or without Runtime Async, because the existing ExceptionDispatchInfo cleanup in compiler-generated code handles that case. The gain is for profilers, diagnostic logging and the debugger's call stack window.

NativeAOT, ReadyToRun and debugging

  • NativeAOT and ReadyToRun: Runtime Async also works with ahead-of-time compilation. The runtime reuses continuation objects more aggressively and avoids saving unchanged locals, which reduces allocation pressure.
  • Debugging: breakpoints now bind correctly inside runtime-async methods, and the debugger can step through await boundaries without jumping into compiler-generated infrastructure.
  • Covariant Task to Task<T> overrides: when a derived class returns Task<T> for a base method that returns Task, the runtime generates a void-returning thunk that bridges the calling-convention difference, so virtual dispatch works for both flavors. The same fix applies to NativeAOT.
  • Inlining in crossgen2: the restrictions that prevented inlining of runtime-async methods during R2R compilation were removed. All async tests pass with both crossgen2 and composite R2R, and inlining of await-less async calls (the synchronous fast path) is confirmed end to end.

Performance improvements

  • JIT async support: the JIT compiles a dedicated runtime-async version of a synchronous, task-returning method rather than going through a thunk. It turns the method's tail calls into runtime-async calls and awaits the task that would otherwise be returned, removing a layer of indirection.
  • Tail-merged suspension points: the generated code is smaller.
  • Cached continuations: continuations used for runtime-async callable task thunks are cached and reused.
  • Pooled methods opt out: methods that are already pooled opt out of runtime-async, avoiding redundant work.
  • Tiered async compilation: async versions flow through tiered compilation, so hot async methods get tier 1 optimizations after warm-up.
  • Task and ValueTask factory intrinsics: the JIT recognizes Task.FromResult, Task.CompletedTask and ValueTask.FromResult and folds them into faster async paths.
  • Tail-await optimizations: implicit tailcalls from async methods are enabled again when the method directly returns another async result, and await Task.Yield() avoids extra allocations in runtime-async paths.

Continuations without ExecutionContext

ExecutionContext carries ambient state, such as AsyncLocal<T> values, across await points. Every Task continuation used to capture a snapshot of the context and restore it before running, even when no AsyncLocal<T> state was in use and the restore was a no-op.

The runtime now detects when a continuation has nothing to restore and skips the capture/restore cycle entirely. Task, Task<T>, ValueTask and ValueTask<T> all benefit, as does the runtime-async implementation path. Applications that use ConfigureAwait(false) and AsyncLocal<T> sparingly see reduced overhead in high-throughput async code paths.

Diagnostics

The async profiler now instruments both runtime-async methods and compiler-generated async state-machine methods, so tools receive one consistent event model regardless of the async implementation style.

Sources

Comments (0)

No comments yet.

Leave a comment