.NET: Simplify ClientHeadersScope to rely on AsyncLocal natural restoration - #5676
Merged
Roger Barreto (rogerbarreto) merged 4 commits intoMay 11, 2026
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.
Roger Barreto (rogerbarreto)
temporarily deployed
to
integration
May 6, 2026 15:24 — with
GitHub Actions
Inactive
Roger Barreto (rogerbarreto)
temporarily deployed
to
integration
May 6, 2026 15:24 — with
GitHub Actions
Inactive
Peter Ibekwe (peibekwe)
approved these changes
May 6, 2026
westey (westey-m)
approved these changes
May 6, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
May 6, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
May 7, 2026
Roger Barreto (rogerbarreto)
enabled auto-merge
May 7, 2026 18:18
Roger Barreto (rogerbarreto)
temporarily deployed
to
integration
May 7, 2026 18:18 — with
GitHub Actions
Inactive
Roger Barreto (rogerbarreto)
temporarily deployed
to
integration
May 7, 2026 18:18 — with
GitHub Actions
Inactive
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
May 7, 2026
Roger Barreto (rogerbarreto)
enabled auto-merge
May 7, 2026 21:01
Roger Barreto (rogerbarreto)
temporarily deployed
to
integration
May 7, 2026 21:01 — with
GitHub Actions
Inactive
Roger Barreto (rogerbarreto)
temporarily deployed
to
integration
May 7, 2026 21:01 — with
GitHub Actions
Inactive
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
May 7, 2026
This was referenced Jun 5, 2026
Closed
This was referenced Jun 12, 2026
This was referenced Jun 24, 2026
This was referenced Jul 1, 2026
This was referenced Jul 8, 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.
Closes #6003
.NET: Simplify ClientHeadersScope to rely on AsyncLocal natural restoration