Skip to content

[Async v2] Implement async method variant handling in AddMethod and UpdateMethod#125397

Open
tommcdon wants to merge 22 commits intodotnet:mainfrom
tommcdon:dev/tommcdon/implement_addmethoddesc
Open

[Async v2] Implement async method variant handling in AddMethod and UpdateMethod#125397
tommcdon wants to merge 22 commits intodotnet:mainfrom
tommcdon:dev/tommcdon/implement_addmethoddesc

Conversation

@tommcdon
Copy link
Copy Markdown
Member

This change implements a TODO item in EEClass::AddMethodDesc to support Runtime Async

@tommcdon tommcdon added this to the 11.0.0 milestone Mar 10, 2026
@tommcdon tommcdon self-assigned this Mar 10, 2026
Copilot AI review requested due to automatic review settings March 10, 2026 18:34
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 implements runtime-async method variant support in the Edit-and-Continue (EnC) path by extending EEClass::AddMethodDesc/EEClass::AddMethod to carry async metadata (flags + optional alternate signature) and by updating EnC method update logic to consider async method variants.

Changes:

  • Extend EEClass::AddMethodDesc to accept async flags and an optional async-variant signature, and plumb that into MethodTableBuilder::InitMethodDesc.
  • Add async return-type classification and async-variant creation logic to EEClass::AddMethod.
  • Update EditAndContinueModule::UpdateMethod to also reset the entrypoint for the method’s async counterpart.

Reviewed changes

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

File Description
src/coreclr/vm/encee.cpp Resets entrypoints for async counterparts during EnC method updates.
src/coreclr/vm/class.h Extends AddMethodDesc signature to accept async flags and optional async signature.
src/coreclr/vm/class.cpp Implements async return classification and passes async flags/signature into EnC-added MethodDesc creation.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/coreclr/vm/class.cpp
Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/encee.cpp Outdated
Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/class.cpp Outdated
@tommcdon tommcdon force-pushed the dev/tommcdon/implement_addmethoddesc branch from 76d4eb1 to 437f9ed Compare March 20, 2026 20:08
@tommcdon tommcdon marked this pull request as ready for review March 20, 2026 20:09
Copilot AI review requested due to automatic review settings March 20, 2026 20:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 7 out of 7 changed files in this pull request and generated 3 comments.

Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/class.cpp
Comment thread src/coreclr/vm/encee.cpp Outdated
Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/encee.cpp
tommcdon added a commit to tommcdon/runtime that referenced this pull request Apr 3, 2026
…ejection, generic UpdateMethod comment

- class.cpp:579: Update GC violation comment to acknowledge the
  extern-alias edge case as Won't Fix per reviewer feedback
- class.cpp:597: Reject infrastructure async methods (IsMiAsync but
  not task-returning) on non-system modules with COR_E_BADIMAGEFORMAT,
  mirroring MethodTableBuilder validation
- encee.cpp:388: Add comment explaining that ResetCodeEntryPointForEnC
  cascades from thunk to async variant automatically, so generic
  UpdateMethod path does not need explicit async variant handling

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 3, 2026 00:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread src/coreclr/vm/method.cpp Outdated
Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/class.cpp Outdated
Copy link
Copy Markdown
Member

@noahfalk noahfalk left a comment

Choose a reason for hiding this comment

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

Looks good 👍 Couple suggestions inline

Copilot AI review requested due to automatic review settings April 4, 2026 23:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 7 out of 7 changed files in this pull request and generated 2 comments.

Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/class.cpp Outdated
Copilot AI review requested due to automatic review settings April 5, 2026 02:41
Copilot AI review requested due to automatic review settings April 16, 2026 16:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 9 out of 9 changed files in this pull request and generated 3 comments.

Comment thread src/coreclr/vm/class.cpp Outdated
Comment thread src/coreclr/vm/class.cpp
Comment thread src/coreclr/vm/class.cpp
tommcdon and others added 22 commits April 16, 2026 15:09
Use GetAsyncOtherVariantNoCreate  in UpdateMethod
Extract BuildAsyncVariantSignature to share async signature construction

The async variant signature construction logic (stripping Task/ValueTask
wrapper from the return type) was duplicated between MethodTableBuilder
(initial type load) and EEClass::AddMethod (EnC add method). Extract it
into a shared BuildAsyncVariantSignature helper in method.cpp.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When adding a new async method to a generic type via EnC, the async
variant MethodDesc was only created for the type definition but not
for existing canonical instantiations. This caused crashes when the
newly added async method was called on value type instantiations
(e.g. G<int>) because the thunk could not find its async variant.

Hoist the async variant signature and flags computation above the
generic instantiation loop, then create the async variant for each
canonical MethodTable alongside the primary thunk.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ejection, generic UpdateMethod comment

- class.cpp:579: Update GC violation comment to acknowledge the
  extern-alias edge case as Won't Fix per reviewer feedback
- class.cpp:597: Reject infrastructure async methods (IsMiAsync but
  not task-returning) on non-system modules with COR_E_BADIMAGEFORMAT,
  mirroring MethodTableBuilder validation
- encee.cpp:388: Add comment explaining that ResetCodeEntryPointForEnC
  cascades from thunk to async variant automatically, so generic
  UpdateMethod path does not need explicit async variant handling

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…VariantSignature

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ure async in EnC

- Clarify GC violation comment to explain the specific unlikely scenario
  (naming collision + extern alias + hot reload while debugging)
- Reject infrastructure async methods unconditionally in EnC rather than
  only on non-system modules, since no scenario benefits from allowing them

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…T_NOT_SUPPORTED

- Rewrite GC violation comment per jkotas: the issue is unresolved
  TypeRef/AssemblyRef for Task/ValueTask, not type misidentification.
  System.Runtime is typically already resolved so this is unlikely.
- Use CORDBG_E_ENC_EDIT_NOT_SUPPORTED instead of COR_E_BADIMAGEFORMAT
  for infrastructure async rejection, since this is an unsupported edit
  rather than malformed metadata.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Allocate the async variant signature from each instantiation's own
LoaderAllocator instead of reusing the type definition's allocation.
AddMethodDesc stores the signature as a raw pointer in AsyncMethodData,
so it must be owned by the same allocator as the MethodDesc to avoid
dangling pointers on collectible ALC unload.

Also fix GC violation comment per jkotas feedback and use
CORDBG_E_ENC_EDIT_NOT_SUPPORTED for infrastructure async rejection.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Generic instantiations are guaranteed to have same or shorter lifetime
than the method definition, so reusing the type definition's signature
allocation is safe. Remove the unnecessary per-allocator copy.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Previously, AddMethod eagerly created async variant MethodDescs for all
runtime-async methods added via EnC (on the type-def and every existing
generic instantiation). This commit defers variant creation to the point
of first use:

- FindOrCreateAssociatedMethodDesc (genmeth.cpp): lazily creates the
  variant using double-check locking with m_InstMethodHashTableCrst,
  matching the existing NewInstantiatedMethodDesc pattern.

- LoadTypicalMethodDefinition (method.cpp): triggers lazy creation when
  navigating from an instantiation variant back to the type-def variant.

- AddAsyncVariant (class.cpp): new helper that computes the variant
  signature on-demand from metadata (ClassifyMethodReturnKind +
  BuildAsyncVariantSignature) rather than storing it on primary thunks.

- ResetCodeEntryPointForEnC (method.cpp): gracefully handles the case
  where the variant has not been created yet (no-op).

- InitMethodDesc (methodtablebuilder.cpp): restored original contract
  where only IsAsyncVariant methods store a signature.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
AddChunk publishes a new MethodDescChunk to a lock-free linked list
that concurrent readers iterate without holding a lock. On weakly
ordered architectures (ARM64), plain stores can be reordered, allowing
a reader to see the new chunk pointer before the chunk's internal data
(MethodDescs, flags, etc.) is fully visible.

Use VolatileStore at both publish points (head and tail) to act as a
release barrier, matching the pattern used elsewhere in the VM (e.g.
dacenumerablehash.inl, codeversion.cpp, eehash.inl). This is a no-op
on x86/x64 (strong memory model).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Gate ClassifyMethodReturnKind on IsMiAsync in AddMethod so non-async
  methods never hit the GC-triggering type resolution path. Updated
  comment to note EnC runs with threads suspended.

- Fix redundant double-reset in UpdateMethod: ResetCodeEntryPointForEnC
  already cascades from thunk to IL-owning variant, so skip the explicit
  variant reset when pMethod is the thunk.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…avoidance

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The explicit reset of the paired async variant in UpdateMethod is dead
code: UpdateMethod always receives the thunk (from the delta metadata
token), so !IsAsyncThunkMethod() is always false and the block never
fires. Even if it did, ResetCodeEntryPointForEnC already cascades from
the thunk to the IL-owning variant automatically.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Consolidate the IsMiAsync handling into a single block with early
return for non-task-returning methods, removing the intermediate
isAsyncTaskReturning variable and separate if/else-if chain.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- AddAsyncVariant: replace standalone assert with runtime guard that
  returns COR_E_BADIMAGEFORMAT for non-task-returning methods in
  Release builds (assert inside for Debug).
- FCAMD: gate lazy async variant creation on IsAsyncThunkMethod() to
  prevent calling AddAsyncVariant on non-async EnC methods.
- GetChunks/GetNextChunk: pair VolatileStore in AddChunk with
  VolatileLoad on readers for proper acquire/release semantics on
  weak memory models. DAC paths use plain loads.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restore the comment explaining that ClassifyMethodReturnKind matching
by type name without verifying the assembly reference is a legitimate
but corner case bug (extern alias + naming collision + hot reload),
accepted as Won't Fix rather than claiming GC is safe in practice.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Remove pMDescInCanonMT->GetMethodTable() != pExactMT->GetCanonicalMethodTable()
  condition (always false since GetParallelMethodDesc searches the
  canonical MT). Replace with asserts to verify the invariant.
- Update GC safety comment to acknowledge the ClassifyMethodReturnKind
  extern alias corner case as a legitimate Won't Fix bug.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Update AsyncVariantLookup enum values: AsyncOtherVariant -> Async,
  MatchingAsyncVariant -> Ordinary
- Update ClassifyMethodReturnKind calls to pass new elementTypeLength param
- Update FindOrCreateAssociatedMethodDesc call to match new parameter order
  (AsyncVariantLookup moved before forceRemotableMethod)
- Update debugger.cpp: HasAsyncOtherVariant -> IsAsyncThunkMethod,
  GetAsyncOtherVariantNoCreate -> GetAsyncVariantNoCreate
- Add missing variable declarations in methodtablebuilder.cpp
- Remove tautological assert in FCAMD lazy creation block

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…cation

Reverts the unnecessary variable declaration reordering in
EnumerateClassMethods that fell out of the rebase conflict resolution.
Keeps the improved comment in InitMethodDesc that clarifies primary
thunks use the original metadata signature and do not store one.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 17, 2026 01:11
@tommcdon tommcdon force-pushed the dev/tommcdon/implement_addmethoddesc branch from ec5469d to fb41135 Compare April 17, 2026 01:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 9 out of 9 changed files in this pull request and generated no new comments.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants