Restore current task tracking for runtime async dispatch - #132969
Merged
max-charlamb merged 1 commit intoSep 1, 2026
Merged
max-charlamb merged 1 commit into
max-charlamb merged 1 commit into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 3514239f-5fe4-4f96-a3c8-0ef46470104e
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @agocke |
Member
Author
|
@EgorBot -windows_amd -windows_intel --envvars DOTNET_TieredCompilation:0 using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(DispatchContinuationsBenchmarks).Assembly).Run(args);
public class DispatchContinuationsBenchmarks
{
private const int IterationCount = 10_000_000;
private static int s_value;
private readonly NullAwaiter _awaiter = new();
[IterationSetup]
public void Warmup()
{
for (int i = 0; i < IterationCount; i++)
{
s_value += i;
}
}
[Benchmark(OperationsPerInvoke = IterationCount)]
public void DispatchContinuations()
{
Task task = RunContinuations(_awaiter);
while (!task.IsCompleted)
{
_awaiter.Continue();
}
task.GetAwaiter().GetResult();
}
[RuntimeAsyncMethodGeneration(true)]
private static async Task RunContinuations(NullAwaiter awaiter)
{
for (int i = 0; i < IterationCount; i++)
{
await awaiter;
}
}
private sealed class NullAwaiter : ICriticalNotifyCompletion
{
private Action? _continuation;
public NullAwaiter GetAwaiter() => this;
public bool IsCompleted => false;
public void Continue()
{
Action continuation = _continuation!;
_continuation = null;
continuation();
}
public void GetResult()
{
}
public void OnCompleted(Action continuation) => throw new NotSupportedException();
public void UnsafeOnCompleted(Action continuation) => _continuation = continuation;
}
}
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
internal sealed class RuntimeAsyncMethodGenerationAttribute(bool runtimeAsync) : Attribute
{
public bool RuntimeAsync => runtimeAsync;
}
} |
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
max-charlamb
requested review from
jakobbotsch,
jkotas,
lateralusX,
rcj1 and
tommcdon
August 31, 2026 15:04
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
Pull request overview
Restores population of AsyncDispatcherInfo.CurrentTask during the standard (non-instrumented) runtime-async continuation dispatch path in CoreCLR, so diagnostic tooling can reliably correlate nested runtime-async tasks with their continuation chains.
Changes:
- Set
asyncDispatcherInfo.CurrentTask = this;in the non-instrumentedDispatchContinuationspath to match the intent of the instrumented path (which sets it viaRuntimeAsyncInstrumentationHelpers.ResumeRuntimeAsyncContext).
File summaries
| File | Description |
|---|---|
| src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs | Restores AsyncDispatcherInfo.CurrentTask assignment for standard continuation dispatch to enable correct current-task tracking during dispatch. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Lite
jakobbotsch
approved these changes
Aug 31, 2026
rcj1
approved these changes
Aug 31, 2026
lateralusX
approved these changes
Sep 1, 2026
tommcdon
approved these changes
Sep 1, 2026
Member
Author
|
/backport to release/11.0 |
Contributor
|
Started backporting to |
4 tasks
steveisok
pushed a commit
that referenced
this pull request
Sep 15, 2026
…ch (#133015) Backport of #132969 to release/11.0 /cc @max-charlamb ## Customer Impact - [ ] Customer reported - [x] Found internally On the standard, non-instrumented runtime-async dispatch path, `AsyncDispatcherInfo.CurrentTask` is not populated. As a result, diagnostics tools analyzing a live process or crash dump cannot deterministically associate a runtime-async continuation chain with its parent `Task`, which can produce incomplete or incorrect async task relationships. The issue affects .NET 11 runtime-async diagnostics when instrumentation is not enabled. ## Regression - [x] Yes - [ ] No This regression was introduced by #126091, when the instrumented and non-instrumented continuation-dispatch paths were separated. Before that change, the shared dispatch path populated `CurrentTask`. The instrumented path still sets it through `RuntimeAsyncInstrumentationHelpers.ResumeRuntimeAsyncContext`, but the standard path lost the assignment. ## Testing The corresponding fix on `main` built Release CoreCLR and libraries successfully and ran `System.Runtime.Tests` with 77,810 tests passing and 88 skipped. BenchmarkDotNet testing covered 10 million runtime-async suspension/resumption operations per invocation and found no performance regression across 10 paired runs. No new test was added because this change restores diagnostic metadata held in stack-local dispatcher state rather than changing observable task execution behavior. The issue was missed when #126091 split the dispatch paths because existing tests validate runtime-async behavior and instrumentation, but do not inspect the non-instrumented stack state consumed by live-process and crash-dump diagnostics. ## Risk Low. The change adds one pointer assignment to a stack-local `AsyncDispatcherInfo` immediately before continuation dispatch. It restores the behavior that existed before #126091 and matches the current-task tracking already performed by the instrumented path. It does not change control flow, public APIs, task completion semantics, or persisted data. Performance measurements detected no regression; ReadyToRun code size for `RuntimeAsyncTask<int>.DispatchContinuations` increased by 7 bytes, from 1,488 to 1,495 bytes. **IMPORTANT**: If this backport is for a servicing release, please verify that: - For .NET 8 and .NET 9: The PR target branch is `release/X.0-staging`, not `release/X.0`. - For .NET 10+: The PR target branch is `release/X.0` (no `-staging` suffix). ## Package authoring no longer needed in .NET 9 **IMPORTANT**: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version. Keep in mind that we still need package authoring in .NET 8 and older versions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AsyncDispatcherInfo.CurrentTaskon the standard, non-instrumentedDispatchContinuationspath.This assignment was present before the instrumented and non-instrumented dispatch paths were separated in #126091.
Reasoning
Without this link, diagnostics tools cannot deterministically find the parent Task for a runtime async continuation chain in non-instrumented scenarios like crash dumps. While it is possible to use heuristics to link the tasks (trying to find the 'this' parameter of the
DispatchContinuationscaller) these do not work in all scenarios.Given the very low overhead of writing this field, I believe it is valuable enough to re-add to the normal path.
Performance
Measured on Windows x64 under Hyper-V using BenchmarkDotNet with tiered compilation disabled. The benchmark performs 10,000 runtime-async suspensions and resumptions per invocation and compares separate baseline and changed Release testhosts.
Across 10 paired runs:
The changed runtime measured 1.83% faster overall. The eight additional runs measured 1.24% faster, with the changed runtime faster in all eight. Because the change adds a store and shifts generated code layout, the apparent improvement should be treated as layout or environmental noise rather than a causal speedup. No performance regression was detected.
ReadyToRun code size for
RuntimeAsyncTask<int>.DispatchContinuationsincreased by 7 bytes, from 1,488 to 1,495 bytes.Testing
System.Runtime.Tests: 77,810 total, 0 failed, 88 skippedNote
This pull request description was generated with GitHub Copilot.