Skip to content

Save async contexts for ValueTask methods#129890

Open
jakobbotsch wants to merge 1 commit into
dotnet:mainfrom
jakobbotsch:fix-129771
Open

Save async contexts for ValueTask methods#129890
jakobbotsch wants to merge 1 commit into
dotnet:mainfrom
jakobbotsch:fix-129771

Conversation

@jakobbotsch

@jakobbotsch jakobbotsch commented Jun 26, 2026

Copy link
Copy Markdown
Member

We were not properly saving/restoring async contexts for ValueTask methods because of an incorrect flag check.

Fix #129771

Also add a mechanism to allow a bisection-like search of optimized awaits in the JIT.

This is a surprising bug/test hole. It is also surprising that it was exposed by #128384, but the test seems to be timing sensitive -- it also stops reproducing if I introduce an await Task.Yield() in WaitForConnectionWithTelemetryAsync.

We were not properly saving/restoring async contexts for ValueTask
methods because of an incorrect flag check.
Copilot AI review requested due to automatic review settings June 26, 2026 13:03
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jun 26, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Runtime Async’s ExecutionContext save/restore behavior for ValueTask-returning async variants by correcting an overly strict async-flag check in the VM. It also adds/updates tests to cover the regression and re-enables a previously skipped System.Net.Http diagnostics test.

Changes:

  • Fix VM async-flag logic to treat ValueTask async variants as requiring async-context save/restore.
  • Add an ExecutionContext regression test covering async ValueTask flows and re-enable the affected diagnostics functional test.
  • Add DEBUG-only JIT knobs to selectively enable/break on await-optimization by hash range.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tests/async/execution-context/execution-context.cs Adds a new ValueTask-focused ExecutionContext regression test.
src/libraries/System.Net.Http/tests/FunctionalTests/DiagnosticsTests.cs Removes ActiveIssue to re-enable the diagnostics test previously associated with the bug.
src/coreclr/vm/method.hpp Fixes async-flag check for ValueTask variants; adds operator~ helper for flag masking.
src/coreclr/jit/jitconfigvalues.h Introduces DEBUG-only config entries for await optimization range/break.
src/coreclr/jit/importer.cpp Adds an await-level “optimize or not” check (DEBUG-configurable) when selecting async variants.
src/coreclr/jit/compiler.h Declares the new importer helper (impCheckOptimizeAwait).

Comment thread src/tests/async/execution-context/execution-context.cs
Comment thread src/coreclr/jit/importer.cpp
Comment thread src/coreclr/vm/method.hpp
AsyncMethodFlags asyncFlags = GetAddrOfAsyncMethodData()->flags;
// asynccall that is also async variant, but not a thunk
return asyncFlags == (AsyncMethodFlags::AsyncCall | AsyncMethodFlags::IsAsyncVariant);
return (asyncFlags & ~AsyncMethodFlags::IsAsyncVariantForValueTask) == (AsyncMethodFlags::AsyncCall | AsyncMethodFlags::IsAsyncVariant);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual bug fix.

@jakobbotsch jakobbotsch marked this pull request as ready for review June 26, 2026 16:16
@jakobbotsch

Copy link
Copy Markdown
Member Author

PTAL @VSadov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI runtime-async

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DiagnosticsTest Http2 SendAsync_Success_ConnectionSetupActivityGraphRecorded: Unexpected EOF trying to read request header

2 participants