Repository navigation
Throw clear error when invoking async JSInvokable method synchronously - #68789
SergioAlmeida29 wants to merge 5 commits into
Conversation
Calling an async Task- or ValueTask-returning [JSInvokable] method via the synchronous invokeMethod path serialized the live task instance, producing confusing JSON serialization errors, or silently returning meaningless state for a non-generic ValueTask. DotNetDispatcher.Invoke now detects these results and throws an InvalidOperationException naming the method and directing to invokeMethodAsync. The asynchronous dispatch path is unaffected. Fixes dotnet#46811 Signed-off-by: Sergio Almeida <sergioalmeida29.05@gmail.com>
|
Thanks for your PR, @SergioAlmeida29. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
@dotnet-policy-service agree |
PureWeen
left a comment
There was a problem hiding this comment.
Written by a human with the help of GitHub Copilot.
| // A Task or ValueTask result means the targeted method is asynchronous and was invoked | ||
| // synchronously. Serializing the task instance produces confusing serialization-related | ||
| // errors, so fail with a message that explains the actual problem. | ||
| if (syncResult is Task |
There was a problem hiding this comment.
This guard still misses non-generic ValueTask: a boxed ValueTask is neither a Task nor a generic ValueTask<>, so synchronous invokeMethod still falls through to serialization instead of producing the new actionable error. Could we include syncResult is ValueTask and add a synchronous invocation test for it? Sync coverage for ValueTask<T> would also exercise the other new branch.
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
Thanks for the contribution. This checks the return value after the method has run, so side effects still occur and the returned task is not observed. #69659 rejects async methods before invocation, so I suggest we continue with that one. |
Summary of the changes (Less than 80 chars)
Throw clear error when invoking async JSInvokable methods synchronously
Description
Calling a
Task- orValueTask<T>-returning[JSInvokable]method via synchronousinvokeMethodserialized the live task instance, producing confusing JSON serialization errors (or meaningless JSON for non-genericValueTask).DotNetDispatcher.Invokenow detects these results and throws anInvalidOperationExceptionnaming the method and directing toinvokeMethodAsync. The async dispatch path (BeginInvokeDotNet) is unaffected. The synchronous guard coversTask,Task<T>,ValueTask, andValueTask<T>. Regression tests exercise both ValueTask forms through synchronous dispatch, alongside existing synchronous and asynchronous coverage.Fixes #46811