.NET: Simplify ClientHeadersScope to rely on AsyncLocal natural restoration#5676
Open
rogerbarreto wants to merge 3 commits intomicrosoft:mainfrom
Open
Conversation
Wesley pointed out (with a clean demo) that AsyncLocal<T> mutations made
inside an awaited async method do not leak back to the caller after the
method returns - the runtime restores the caller's view automatically.
ClientHeadersAgent.RunCoreAsync and RunCoreStreamingAsync are the only
callers of the scope, both are async methods awaited by their callers,
so the explicit using/Dispose pattern was doing work the runtime already
does for us.
* ClientHeadersScope collapsed to a single Current { get; set; } property
over an AsyncLocal<IReadOnlyDictionary<string,string>?>. Drops Push,
the Scope struct, and Dispose. XML doc explains the AsyncLocal natural-
restoration semantics so the design intent is self-documenting.
* ClientHeadersAgent uses a direct ClientHeadersScope.Current = snapshot
before delegating. Drops the local RunAsyncCoreAsync helper and the
snapshot-passed-as-parameter dance.
* Test 10 renamed to ClientHeadersScope_IsAsyncLocalIsolatedAndAutoRestoresAsync;
drops the LIFO claim, keeps the parallel-isolation assertion, and adds
a Wesley-style 'set inside async, caller sees null on return' assertion.
* Test 12 switches from using ClientHeadersScope.Push to direct
Current = ... with try/finally for test isolation.
Snapshot deep-copy in TrySnapshot stays - it defends against caller
mutating the source Dictionary mid-run, which is independent of the
AsyncLocal restoration mechanism.
peibekwe
approved these changes
May 6, 2026
westey-m
approved these changes
May 6, 2026
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
Simplifies
ClientHeadersScopeby dropping the explicitusing/Disposepattern and relying on the naturalAsyncLocal<T>restoration that happens when an awaitedasyncmethod returns.What changes
ClientHeadersScopecollapses to a singleCurrent { get; set; }property over anAsyncLocal<IReadOnlyDictionary<string, string>?>. DropsPush, theScopestruct, andDispose. XML doc explains the AsyncLocal natural-restoration semantics so the design intent is self-documenting.ClientHeadersAgentuses a directClientHeadersScope.Current = snapshotbefore delegating in bothRunCoreAsyncandRunCoreStreamingAsync. Drops the localRunAsyncCoreAsynchelper and the snapshot-passed-as-parameter dance.ClientHeadersScope_IsAsyncLocalIsolatedAndAutoRestoresAsync; keeps the parallel-isolation assertion and adds a "caller sees null after the awaited probe returns" assertion.using ClientHeadersScope.Push(...)to directCurrent = ...withtry/finallyfor test isolation.Why
AsyncLocal<T>mutations made inside an awaitedasyncmethod are visible within that method (and all of its awaits) but do not propagate back to the caller after the method returns. SinceClientHeadersAgentis the only caller of the scope and both call sites areasyncmethods awaited by their callers, the explicit using/Dispose pattern was doing work the runtime already does for us.The snapshot deep-copy in
TrySnapshotstays. It defends against caller mutating the sourceDictionarymid-run, which is independent of the AsyncLocal restoration mechanism.Verification
dotnet format --verify-no-changesclean on the changed project.