Skip to content

Fix check for invocation of async infrastructure methods via reflection - #121609

Merged
jkotas merged 4 commits into
dotnet:mainfrom
jkotas:fix-async-reflection
Nov 14, 2025
Merged

jkotas merged 4 commits into
dotnet:mainfrom
jkotas:fix-async-reflection

Conversation

@jkotas

@jkotas jkotas commented Nov 14, 2025 •

Copy link
Copy Markdown
Member

MethodInfo.Invoke has multiple implementation strategies. The check for async infrastructure methods needs to be placed in the code that is shared by all implementation strategies.

Before this change, this check would not kick in under managed debugger or when the test is run with Switch.System.Reflection.ForceEmitInvoke=true appcontext switch.

Also, this check should fail with NotSupportedException, without wrapping it in TargetInvocationException() to match existing behaviors.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
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 the exception handling when async infrastructure methods are invoked via reflection. Previously, calling AsyncHelpers methods via reflection would throw a TargetInvocationException, but now correctly throws NotSupportedException to match the pattern used for other async methods.

Key changes:

  • Added explicit checks in managed code (RuntimeMethodInfo.cs) to detect AsyncHelpers async methods and mark them as non-invokable
  • Removed a redundant generic instantiation check in native code that was preventing proper async method detection
  • Updated test expectations to verify NotSupportedException is thrown

Reviewed Changes

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

File Description
src/tests/async/reflection/reflection-simple.cs Updated test assertion to expect NotSupportedException instead of TargetInvocationException
src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs Added checks to detect AsyncHelpers async methods and throw appropriate exception
src/coreclr/vm/reflectioninvocation.cpp Removed redundant check for shared generic instantiations

MethodInfo.Invoke has multiple implementation strategies. The check for async infrastructure methods needs to be placed in the code that is shared by all implementation strategies.

Before this change, this check would not kick under managed debugger or when the test is run with Switch.System.Reflection.ForceEmitInvoke=true appcontext switch.

Also, this check should fail with NotSupportedException, without wrapping it in TargetInvocationException() to match existing behaviors.
@jkotas
jkotas force-pushed the fix-async-reflection branch from 01d4d79 to f944483 Compare November 14, 2025 04:10
@jkotas
jkotas requested a review from VSadov November 14, 2025 04:12

@VSadov VSadov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks!

@jkotas

jkotas commented Nov 14, 2025

Copy link
Copy Markdown
Member Author

/ba-g timeouts

@jkotas
jkotas merged commit 217fd74 into dotnet:main Nov 14, 2025
@jkotas
jkotas deleted the fix-async-reflection branch November 14, 2025 13:15
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants