Skip to content

Throw clear error when invoking async JSInvokable method synchronously - #68789

Closed
SergioAlmeida29 wants to merge 5 commits into
dotnet:mainfrom
SergioAlmeida29:fix/46811-jsinterop-sync-invoke-async
Closed

SergioAlmeida29 wants to merge 5 commits into
dotnet:mainfrom
SergioAlmeida29:fix/46811-jsinterop-sync-invoke-async

Conversation

@SergioAlmeida29

@SergioAlmeida29 SergioAlmeida29 commented Aug 25, 2026 •

Copy link
Copy Markdown
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Summary of the changes (Less than 80 chars)

Throw clear error when invoking async JSInvokable methods synchronously

Description

Calling a Task- or ValueTask<T>-returning [JSInvokable] method via synchronous invokeMethod serialized the live task instance, producing confusing JSON serialization errors (or meaningless JSON for non-generic ValueTask). DotNetDispatcher.Invoke now detects these results and throws an InvalidOperationException naming the method and directing to invokeMethodAsync. The async dispatch path (BeginInvokeDotNet) is unaffected. The synchronous guard covers Task, Task<T>, ValueTask, and ValueTask<T>. Regression tests exercise both ValueTask forms through synchronous dispatch, alongside existing synchronous and asynchronous coverage.

Fixes #46811

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>
Copilot AI lite review requested due to automatic review settings August 25, 2026 23:38
@SergioAlmeida29
SergioAlmeida29 requested a review from a team as a code owner August 25, 2026 23:38

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 25, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @SergioAlmeida29. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@SergioAlmeida29

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@Youssef1313 Youssef1313 added the area-blazor Includes: Blazor, Razor Components label Sep 2, 2026

@PureWeen PureWeen 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.

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

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.

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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Sep 26, 2026
@kotlarmilos

Copy link
Copy Markdown
Member

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.

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

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling Asynchronous .NET JS Interop Method Synchronously from JS Produces Misleading Error Message

5 participants