Skip to content

Handle Synchronized async versions#129901

Merged
jakobbotsch merged 8 commits into
dotnet:mainfrom
jakobbotsch:fix-synchronized-async-versions
Jun 30, 2026
Merged

Handle Synchronized async versions#129901
jakobbotsch merged 8 commits into
dotnet:mainfrom
jakobbotsch:fix-synchronized-async-versions

Conversation

@jakobbotsch

@jakobbotsch jakobbotsch commented Jun 26, 2026

Copy link
Copy Markdown
Member

Non-async Task-returning methods can be Synchronized. We were missing handling for that async version scenario in the JIT and interpreter. Handle the situation by disallowing the tailcall optimization in synchronized methods and making sure we exit the lock before we await the resulting task.

Non-async Task-returning methods can be Synchronized. We were missing
handling for that scenario in the JIT and interpreter.
Copilot AI review requested due to automatic review settings June 26, 2026 17:56
@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

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 updates CoreCLR’s runtime-async (“async version”) handling for MethodImplOptions.Synchronized methods that return Task / ValueTask without being async, ensuring the monitor is released at the correct point in both the JIT and interpreter paths, and adds a regression test for the scenario.

Changes:

  • JIT: avoid async-version “tail await” recognition for synchronized methods and emit an explicit MON_EXIT before wrapping the return value in an await.
  • JIT flowgraph: avoid injecting the usual synchronized-return MON_EXIT blocks for async versions (since they’re now emitted during import).
  • Interpreter: centralize creation of the synchronized/async return-value temp and adjust async-version return wrapping / tailcall peep behavior.
  • Tests: add a new async regression test project covering both Task and ValueTask.

Reviewed changes

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

Show a summary per file
File Description
src/tests/async/regression/synchronized-async-version.csproj Adds a new IL test project for the regression scenario.
src/tests/async/regression/synchronized-async-version.cs Adds xUnit coverage for synchronized non-async Task/ValueTask returns under runtime async.
src/coreclr/jit/importer.cpp Ensures synchronized async versions release the monitor before awaiting/wrapping return values; blocks tail-await recognition for synchronized methods.
src/coreclr/jit/flowgraph.cpp Avoids duplicating synchronized-return monitor-exit insertion for async versions; makes lvaMonAcquired allocation idempotent.
src/coreclr/interpreter/compiler.h Adds declarations/state related to synchronized async-version return handling.
src/coreclr/interpreter/compiler.cpp Adds CreateSynchronizedRetValVar helper and adjusts return/tailcall logic for synchronized async versions.

Comment thread src/tests/async/regression/synchronized-async-version.cs Outdated
Comment thread src/tests/async/regression/synchronized-async-version.cs Outdated
Comment thread src/tests/async/regression/synchronized-async-version.cs Outdated
Comment thread src/coreclr/interpreter/compiler.cpp Outdated
Comment thread src/coreclr/interpreter/compiler.h Outdated
Copilot AI review requested due to automatic review settings June 26, 2026 18:04

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

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

Comment thread src/tests/async/regression/synchronized-async-version.cs
Comment thread src/tests/async/regression/synchronized-async-version.cs
Comment thread src/coreclr/interpreter/compiler.cpp Outdated
Comment thread src/coreclr/interpreter/compiler.h Outdated
Copilot AI review requested due to automatic review settings June 26, 2026 18:10

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

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

Comment thread src/coreclr/interpreter/compiler.cpp Outdated
Comment thread src/coreclr/interpreter/compiler.h
Comment thread src/tests/async/regression/synchronized-async-version.cs
Comment thread src/tests/async/regression/synchronized-async-version.cs
Copilot AI review requested due to automatic review settings June 26, 2026 18:22

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

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

Comment thread src/coreclr/interpreter/compiler.cpp
Comment thread src/coreclr/interpreter/compiler.cpp
Comment thread src/coreclr/interpreter/compiler.cpp
Copilot AI review requested due to automatic review settings June 26, 2026 18:35

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread src/tests/async/regression/synchronized-async-version.csproj
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 26, 2026 18:45

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread src/tests/async/regression/synchronized-async-version.csproj
@jakobbotsch jakobbotsch marked this pull request as ready for review June 29, 2026 10:33
@jakobbotsch

Copy link
Copy Markdown
Member Author

cc @dotnet/jit-contrib PTAL @AndyAyersMS for the JIT part, @BrzVlad for the interpreter part

@jakobbotsch

Copy link
Copy Markdown
Member Author

/ba-g Timeout

@jakobbotsch jakobbotsch merged commit e3f7eec into dotnet:main Jun 30, 2026
150 of 153 checks passed
@jakobbotsch jakobbotsch deleted the fix-synchronized-async-versions branch June 30, 2026 07:29
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 30, 2026
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.

4 participants