Skip to content

Conversation

@jkotas
Copy link
Member

@jkotas jkotas commented Nov 14, 2025

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
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

@jkotas jkotas force-pushed the fix-async-reflection branch from 0e5d4c3 to 23c468a Compare November 14, 2025 02:46
Copilot finished reviewing on behalf of jkotas November 14, 2025 02:47
@jkotas jkotas requested a review from VSadov November 14, 2025 02:50
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 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
Copy link
Member

@VSadov VSadov left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@jkotas
Copy link
Member Author

jkotas commented Nov 14, 2025

/ba-g timeouts

@jkotas jkotas merged commit 217fd74 into dotnet:main Nov 14, 2025
133 of 147 checks passed
@jkotas jkotas deleted the fix-async-reflection branch November 14, 2025 13:15
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.

2 participants