Skip to content

Fix Native AOT completion for JS-invoked Tasks - #69437

Merged
wtgodbe merged 3 commits into
release/11.0-rc2from
javiercn-fix-native-aot-jsinvokable-promise
Sep 23, 2026
Merged

wtgodbe merged 3 commits into
release/11.0-rc2from
javiercn-fix-native-aot-jsinvokable-promise

Conversation

@javiercn

@javiercn javiercn commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Fix Native AOT completion for JS-invoked Tasks

Resolve JS-invoked task promises under Native AOT

Description

Native AOT applications could complete a [JSInvokable] method in managed code without resolving or rejecting the promise returned by invokeMethodAsync. Non-generic async methods could expose an internal Task<VoidTaskResult>, while generic Task<T> and ValueTask<T> paths depended on dynamically constructed generic helpers that might not have native code available.

This change:

  • uses the declared [JSInvokable] return type to identify non-generic Task and ValueTask results
  • avoids dynamically constructing TaskResultGetter<VoidTaskResult> under Native AOT
  • uses preserved Task<T>.Result and ValueTask<T>.AsTask members when dynamic code is unavailable
  • ensures faulted and canceled asynchronous invocations report one failure completion
  • adds unit coverage and a Native AOT browser matrix for completed and asynchronous Task, ValueTask, Task<T>, and ValueTask<T> methods

Fixes #69432

Customer Impact

In Native AOT-published Blazor Server applications, JavaScript awaiting a [JSInvokable] method that returns Task, ValueTask, Task<T>, or ValueTask<T> can wait indefinitely even after the managed method completes. Applications that use the returned promise for backpressure or state transitions can become permanently stuck and cannot recover without reloading. There is no application-level workaround that preserves asynchronous callbacks other than changing the callback to a synchronous method or avoiding Native AOT.

Regression?

  • Yes
  • No

This is a gap in the existing Native AOT JS interop implementation rather than a regression from a previously supported release.

Risk

  • High
  • Medium
  • Low

The change is localized to completion handling for JS-invoked asynchronous .NET methods. Existing dynamic-code paths remain unchanged; Native AOT uses preserved members on the already-closed Task<T> and ValueTask<T> types.

Verification

  • Manual (required)
  • Automated

Automated verification:

  • Native AOT JS interop E2E tests: 2 passed, including all eight completed/asynchronous return shapes and exact application-defined value-type result serialization
  • targeted JS interop dispatcher tests: 5 passed

Packaging changes reviewed?

  • Yes
  • No
  • N/A

No packaging changes.


When servicing release/2.3

  • Make necessary changes in eng/PatchConfig.props

Not applicable to release/11.0.

Use the declared JS-invokable return type to avoid dynamically constructing a result getter for non-generic Task and ValueTask completions. Keep unsuccessful asynchronous completions mutually exclusive and cover the Native AOT browser promise boundary.

Co-authored-by: Copilot App <[email protected]>
@javiercn
javiercn marked this pull request as ready for review September 22, 2026 11:14
@javiercn
javiercn requested a review from a team as a code owner September 22, 2026 11:14
Copilot AI lite review requested due to automatic review settings September 22, 2026 11:14

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 review overview

🔵 Needs a closer look

One or more issues must be addressed before approval.

Review effort: Lite
Findings: None

What changed in this PR

Fixes Native AOT JS-invoked non-generic Task completion by using declared return types and adding regression coverage.

Changes:

  • Avoids dynamically resolving VoidTaskResult for non-generic Task/ValueTask.
  • Reports faulted and canceled asynchronous invocations correctly.
  • Adds unit and Native AOT browser tests.
File Description
src/​JSInterop/​Microsoft.JSInterop/​test/​Infrastructure/​DotNetDispatcherTest.cs Updated as part of this pull request.
src/​JSInterop/​Microsoft.JSInterop/​src/​Infrastructure/​DotNetDispatcher.cs Updated as part of this pull request.
src/​Components/​Testing/​testassets/​NativeAotTestApp/​Components/​Pages/​Interop.razor Updated as part of this pull request.
src/​Components/​Testing/​testassets/​NativeAotTestApp.E2E.Tests/​Tests/​NativeAotFeatureTests.cs Updated as part of this pull request.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

javiercn and others added 2 commits September 22, 2026 13:38
Exercise Task, ValueTask, Task<T>, and ValueTask<T> through invokeMethodAsync in the Native AOT browser test. Preserve the existing fault-path structure while handling cancellation separately.

Co-authored-by: Copilot App <[email protected]>
Use preserved Task<T>.Result and ValueTask<T>.AsTask members when dynamic code is unavailable. Expand Native AOT browser coverage to completed and asynchronous task shapes with an application-defined value type.

Co-authored-by: Copilot App <[email protected]>
maraf
maraf previously approved these changes Sep 22, 2026

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

Looks good to me 👍

@javiercn javiercn added the Servicing-consider Shiproom approval is required for the issue label Sep 22, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hi @javiercn. Please make sure you've updated the PR description to use the Shiproom Template. Also, make sure this PR is not marked as a draft and is ready-to-merge.

To learn more about how to prepare a servicing PR click here.

@rbhanda rbhanda added this to the 11.0.0 milestone Sep 22, 2026
@wtgodbe
wtgodbe changed the base branch from release/11.0 to release/11.0-rc2 September 22, 2026 17:48
@wtgodbe
wtgodbe dismissed maraf’s stale review September 22, 2026 17:48

The base branch was changed.

@wtgodbe wtgodbe added Servicing-approved Shiproom has approved the issue and removed Servicing-consider Shiproom approval is required for the issue labels Sep 22, 2026
@@ -139,22 +141,31 @@ public static void BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo i
}

[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "We expect application code is configured to ensure return types of JSInvokable methods are retained.")]

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.

uhh... maybe this is safe? but I'm still scared 🙃

@wtgodbe
wtgodbe merged commit 5f5f31f into release/11.0-rc2 Sep 23, 2026
44 checks passed
@wtgodbe
wtgodbe deleted the javiercn-fix-native-aot-jsinvokable-promise branch September 23, 2026 00:17
wtgodbe added a commit that referenced this pull request Sep 23, 2026
* Fix Native AOT completion for JS-invoked Tasks

Use the declared JS-invokable return type to avoid dynamically constructing a result getter for non-generic Task and ValueTask completions. Keep unsuccessful asynchronous completions mutually exclusive and cover the Native AOT browser promise boundary.



* Expand Native AOT async JS interop coverage

Exercise Task, ValueTask, Task<T>, and ValueTask<T> through invokeMethodAsync in the Native AOT browser test. Preserve the existing fault-path structure while handling cancellation separately.



* Support generic JS interop tasks under Native AOT

Use preserved Task<T>.Result and ValueTask<T>.AsTask members when dynamic code is unavailable. Expand Native AOT browser coverage to completed and asynchronous task shapes with an application-defined value type.



---------

Co-authored-by: Javier Calvarro Nelson <[email protected]>
Co-authored-by: Copilot App <[email protected]>
wtgodbe added a commit that referenced this pull request Sep 23, 2026
* Merge pull request #69426 from dotnet/darc-release/11.0-1a353f84-3fb6-4f62-815a-b32e480af1f0

[release/11.0] Source code updates from dotnet/dotnet

* Replace explicit command after navigation with automatic dismissing by ChromeDriver. (#69416) (#69440)

* Replace explicit command after navigation with automatic dismissing by ChromeDriver.

* Feedback: dismissing should be opt-in.

Co-authored-by: Ilona Tomkowicz <[email protected]>

* Fix Native AOT completion for JS-invoked Tasks (#69437)

* Fix Native AOT completion for JS-invoked Tasks

Use the declared JS-invokable return type to avoid dynamically constructing a result getter for non-generic Task and ValueTask completions. Keep unsuccessful asynchronous completions mutually exclusive and cover the Native AOT browser promise boundary.

Co-authored-by: Copilot App <[email protected]>

* Expand Native AOT async JS interop coverage

Exercise Task, ValueTask, Task<T>, and ValueTask<T> through invokeMethodAsync in the Native AOT browser test. Preserve the existing fault-path structure while handling cancellation separately.

Co-authored-by: Copilot App <[email protected]>

* Support generic JS interop tasks under Native AOT

Use preserved Task<T>.Result and ValueTask<T>.AsTask members when dynamic code is unavailable. Expand Native AOT browser coverage to completed and asynchronous task shapes with an application-defined value type.

Co-authored-by: Copilot App <[email protected]>

---------

Co-authored-by: Copilot App <[email protected]>

* Remove DeviceBoundSessions implementation (#69462)

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Youssef1313 <[email protected]>

* Update dependencies from build 333446 (#69444)

Updated Dependencies:
Microsoft.NET.Runtime.WebAssembly.Sdk, Microsoft.NETCore.BrowserDebugHost.Transport, Microsoft.NET.Runtime.MonoAOTCompiler.Task, dotnet-ef, Microsoft.Bcl.AsyncInterfaces, Microsoft.Bcl.Cryptography, Microsoft.Bcl.TimeProvider, Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Design, Microsoft.EntityFrameworkCore.InMemory, Microsoft.EntityFrameworkCore.Relational, Microsoft.EntityFrameworkCore.Sqlite, Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.Tools, Microsoft.Extensions.Caching.Abstractions, Microsoft.Extensions.Caching.Memory, Microsoft.Extensions.Configuration, Microsoft.Extensions.Configuration.Abstractions, Microsoft.Extensions.Configuration.Binder, Microsoft.Extensions.Configuration.CommandLine, Microsoft.Extensions.Configuration.EnvironmentVariables, Microsoft.Extensions.Configuration.FileExtensions, Microsoft.Extensions.Configuration.Ini, Microsoft.Extensions.Configuration.Json, Microsoft.Extensions.Configuration.UserSecrets, Microsoft.Extensions.Configuration.Xml, Microsoft.Extensions.DependencyInjection, Microsoft.Extensions.DependencyInjection.Abstractions, Microsoft.Extensions.DependencyModel, Microsoft.Extensions.Diagnostics, Microsoft.Extensions.Diagnostics.Abstractions, Microsoft.Extensions.FileProviders.Abstractions, Microsoft.Extensions.FileProviders.Composite, Microsoft.Extensions.FileProviders.Physical, Microsoft.Extensions.FileSystemGlobbing, Microsoft.Extensions.HostFactoryResolver.Sources, Microsoft.Extensions.Hosting, Microsoft.Extensions.Hosting.Abstractions, Microsoft.Extensions.Http, Microsoft.Extensions.Logging, Microsoft.Extensions.Logging.Abstractions, Microsoft.Extensions.Logging.Configuration, Microsoft.Extensions.Logging.Console, Microsoft.Extensions.Logging.Debug, Microsoft.Extensions.Logging.EventLog, Microsoft.Extensions.Logging.EventSource, Microsoft.Extensions.Logging.TraceSource, Microsoft.Extensions.Options, Microsoft.Extensions.Options.ConfigurationExtensions, Microsoft.Extensions.Options.DataAnnotations, Microsoft.Extensions.Primitives, Microsoft.Internal.Runtime.AspNetCore.Transport, Microsoft.NETCore.App.Ref, Microsoft.NETCore.Platforms, System.Collections.Immutable, System.Composition, System.Configuration.ConfigurationManager, System.Diagnostics.DiagnosticSource, System.Diagnostics.EventLog, System.Diagnostics.PerformanceCounter, System.DirectoryServices.Protocols, System.Formats.Asn1, System.Formats.Cbor, System.IO.Hashing, System.IO.Pipelines, System.Memory.Data, System.Net.Http.Json, System.Net.Http.WinHttpHandler, System.Net.ServerSentEvents, System.Numerics.Tensors, System.Reflection.Metadata, System.Resources.Extensions, System.Runtime.Caching, System.Security.Cryptography.Pkcs, System.Security.Cryptography.Xml, System.Security.Permissions, System.ServiceProcess.ServiceController, System.Text.Encodings.Web, System.Text.Json, System.Threading.AccessControl, System.Threading.Channels, System.Threading.RateLimiting (Version 11.0.0-rc.2.26471.109 -> 11.0.0-rc.2.26472.108)
Microsoft.DotNet.Arcade.Sdk, Microsoft.DotNet.Build.Tasks.Archives, Microsoft.DotNet.Build.Tasks.Installers, Microsoft.DotNet.Build.Tasks.Templating, Microsoft.DotNet.Helix.JobMonitor, Microsoft.DotNet.Helix.Sdk, Microsoft.DotNet.RemoteExecutor, Microsoft.DotNet.SharedFramework.Sdk (Version 11.0.0-beta.26471.109 -> 11.0.0-beta.26472.108)
Microsoft.Web.Xdt (Version 3.3.0-rc.2.26471.109 -> 3.3.0-rc.2.26472.108)
NuGet.Frameworks, NuGet.Packaging, NuGet.Versioning (Version 7.12.0-rc.47209 -> 8.0.0-rc.47408)
[[ commit created by automation ]]

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>

---------

Co-authored-by: dotnet-maestro[bot] <42748379+dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Ilona Tomkowicz <[email protected]>
Co-authored-by: Javier Calvarro Nelson <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Youssef1313 <[email protected]>
Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: William Godbe <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Servicing-approved Shiproom has approved the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants