Skip to content
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

[browser] make dynamic import cancelable #80257

Merged
merged 3 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Threading;
using Xunit;
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters

Expand All @@ -31,6 +32,15 @@ public async Task MultipleImportAsync()
Assert.Same(instance1, instance2);
}

[Fact]
public async Task CancelableImportAsync()
{
var cts = new CancellationTokenSource();
cts.Cancel();
var actualEx = await Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs", cts.Token));
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Is there a way to reliably test that the operation gets cancelled in the middle? Passing an already cancelled token is a trivial case that is optimized in other asynchroonous functions. (and you can use new CancellationToken(true) instead of creating a CTS)
  • Shouldn't it throw OperationCancelledException?

Copy link
Member Author

Choose a reason for hiding this comment

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

The underlying JS dynamic import() is not cancelable.
The JS promises are rather "abandoned" than really canceled. We only reject the JS promise.
(In general, we don't know what else registered continuation on that promise on JS side)

That creates JavaScript error, not managed error and we marshal it as JSException consistently with all other JS Errors.

fetch is bit better because it has AbortController.

Copy link
Member

@maraf maraf Jan 6, 2023

Choose a reason for hiding this comment

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

@pavelsavara Did you tried something like this?

var cts = new CancellationTokenSource();
var exTask = Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs", cts.Token));

cts.Cancel();

var actualEx = await exTask;

Copy link
Member Author

Choose a reason for hiding this comment

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

added to the unit test, it works fine

Assert.Equal("OperationCanceledException", actualEx.Message);
}

[Fact]
public unsafe void GlobalThis()
{
Expand Down
20 changes: 12 additions & 8 deletions src/mono/wasm/runtime/invoke-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { mono_wasm_new_external_root } from "./roots";
import { mono_wasm_symbolicate_string } from "./logging";
import { mono_wasm_get_jsobj_from_js_handle } from "./gc-handles";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { wrap_as_cancelable_promise } from "./cancelable-promise";

const fn_wrapper_by_fn_handle: Function[] = <any>[null];// 0th slot is dummy, we never free bound functions

Expand Down Expand Up @@ -317,7 +318,7 @@ export function get_global_this(): any {
export const importedModulesPromises: Map<string, Promise<any>> = new Map();
export const importedModules: Map<string, Promise<any>> = new Map();

export async function dynamic_import(module_name: string, module_url: string): Promise<any> {
export function dynamic_import(module_name: string, module_url: string): Promise<any> {
mono_assert(module_name, "Invalid module_name");
mono_assert(module_url, "Invalid module_name");
let promise = importedModulesPromises.get(module_name);
Expand All @@ -328,13 +329,16 @@ export async function dynamic_import(module_name: string, module_url: string): P
promise = import(/* webpackIgnore: true */module_url);
importedModulesPromises.set(module_name, promise);
}
const module = await promise;
if (newPromise) {
importedModules.set(module_name, module);
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: imported ES6 module '${module_name}' from '${module_url}'`);
}
return module;

return wrap_as_cancelable_promise(async () => {
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
const module = await promise;
if (newPromise) {
importedModules.set(module_name, module);
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: imported ES6 module '${module_name}' from '${module_url}'`);
}
return module;
});
}


Expand Down