Skip to content

Conversation

@eduardo-vp
Copy link
Member

@eduardo-vp eduardo-vp commented Dec 23, 2025

Fixes #122546.

The following code can now be Native AOT compiled and run with runtime-async enabled.

class Program
{
    static async Task Main()
    {
        Console.WriteLine("Hello world");
        await MyAsyncMethod();
    }

    static async Task MyAsyncMethod()
    {
        MethodBase currentMethod = MethodBase.GetCurrentMethod();
        Console.WriteLine($"Current method: {currentMethod?.Name}");
        await Task.Delay(100);
    }
}

@dotnet-policy-service
Copy link
Contributor

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

Copy link
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 attempts to fix an issue where MethodBase.GetCurrentMethod() fails when called from within an async method in Native AOT compilation. The fix adds logic to handle async methods by unwrapping async method variants to get the original method definition before creating the reflection thunk.

Key Changes

  • Adds async method handling in the ExpandIntrinsicForCallsite method for MethodBase.GetCurrentMethod() intrinsic expansion
  • When the callsite is an async method, the code attempts to get the target of the async variant method before generating the thunk

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

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

We also need a test. Conveniently, a test already exists, we just need to remove ActiveIssue (the issue number is wrong):

[ActiveIssue("https://github.com/dotnet/runtime/issues/122517", typeof(TestLibrary.Utilities), nameof(TestLibrary.Utilities.IsNativeAot))]

@eduardo-vp eduardo-vp merged commit 44e1a20 into dotnet:main Dec 23, 2025
104 checks passed
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.

MethodBase.GetCurrentMethod with runtime async

2 participants