-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Canceling JSHost.ImportAsync
fails because dynamic_import
in invoke-js.ts
is not cancelable
#80028
Comments
Tagging subscribers to 'arch-wasm': @lewing Issue DetailsDescriptionTLDR:The cancellation token given to // Repro here: https://github.com/nikonthethird/ImportAsyncError, or paste this code into an InitializeAsync of a page in Blazor WASM:
var cts = new CancellationTokenSource();
cts.Cancel();
await JSHost.ImportAsync("hello", "world.js", cts.Token); The code above fails with an exception because DescriptionAfter upgrading to .NET 7, an application of mine has been throwing the following error messages in the log:
This is what I discovered:
Since .NET 7 uses the Reproduction Steps@page "/"
@using System.Runtime.InteropServices.JavaScript
<h1>Hello, world!</h1>
@if (error is not null) {
<p>@error</p>
}
@code {
private String? error;
protected override async Task OnInitializedAsync() {
var cts = new CancellationTokenSource();
cts.Cancel();
try {
await JSHost.ImportAsync("hello", "world.js", cts.Token);
} catch (Exception ex) {
error = ex.ToString();
}
}
} A reproduction can also be found here. Expected behaviorThe import should be cancelled. Actual behaviorError: Assert failed: Promise is not controllable Regression?Yes, no problem with my code in .NET 6. Known WorkaroundsNone so far. Configuration.NET 7.0.1 Other informationNo response
|
/cc @pavelsavara @maraf |
thanks @nikonthethird , your explanation is perfect :) |
Description
TLDR:
The cancellation token given to
JSHost.ImportAsync
does not work because the underlying JS function called byImportAsync
does not return aPromise
that is "controllable" (outfitted with a special symbol).The code above fails with an exception because
dynamic_import
is not controllable (the promise generated by the function is not wrapped usingwrap_as_cancelable_promise
like for examplehttp_wasm_fetch
is).Description
After upgrading to .NET 7, an application of mine has been throwing the following error messages in the log:
This is what I discovered:
JSHost.ImportAsync
callsJavaScriptImports.DynamicImport
, then uses theCancelationHelper
function to callCancelablePromise.CancelPromise
when the cancellation token fires.CancelPromise
callsmono_wasm_cancel_promise
which throws the error above when the promise is not controllable, meaning when it does not have the promise control symbol.JavaScriptImports.DynamicImport
is directly from the async functiondynamic_import
, which is NOT wrapped withwrap_as_cancelable_promise
like all the others that useCancelationHelper
are.Since .NET 7 uses the
[JSImport]
source generator internally, I suspect the exceptions I am experiencing also come from some internalJSHost.ImportAsync
calls.Reproduction Steps
A reproduction can also be found here.
Expected behavior
The import should be cancelled.
Actual behavior
Error: Assert failed: Promise is not controllable
Regression?
Yes, no problem with my code in .NET 6.
Note that I do not even use
JSHost.ImportAsync
in my code directly, but .NET 7 itself uses the new[JSImport]
source generated, and this is where I expect the error to occur.Known Workarounds
None so far.
Configuration
.NET 7.0.1
Ubuntu 20.04 / Firefox
Windows 10 22H2 / Chromium
x64
It happens in all cases, I don't know why I even provided this info...
Other information
No response
The text was updated successfully, but these errors were encountered: