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

Issues using Blazor Multithreading within iframes #86696

Closed
1 task done
IsaMorphic opened this issue May 23, 2023 · 3 comments · Fixed by #86664
Closed
1 task done

Issues using Blazor Multithreading within iframes #86696

IsaMorphic opened this issue May 23, 2023 · 3 comments · Fixed by #86664

Comments

@IsaMorphic
Copy link
Contributor

IsaMorphic commented May 23, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Using the wasm-buildtools for .NET 8.0 Preview 4, I have come across an unpredictable and frequent runtime bug when embedding a multithreaded Blazor application within an iframe on another page. I've double checked that all CSP and CORS policies are in order and not the root cause.

In fact, I found someone else reporting this exact same issue, only in a completely different use case over by the emscripten repository: emscripten-core/emscripten#18034

The workaround being tossed out there by several developers is to make sure that all calls to emcc use the flag -s TEXTDECODER=0

I've been able to greatly reduce the frequency of this error by adding this flag using these properties:
<EmccCompileOptimizationFlag>-O2 -s TEXTDECODER=0</EmccCompileOptimizationFlag>
<EmccLinkOptimizationFlag>-O2 -s TEXTDECODER=0</EmccLinkOptimizationFlag>
to my csproj file.

I have also confirmed that these flags are being used to compile my assemblies and dependencies. However, I can't figure out a way to add this flag to the main dotnet.wasm emcc command to ensure that my dotnet-runtime.js file does not use TextDecoder to marshal WASM memory across threads.

Interesting to note is that the frequency of this error greatly decreases when embedding the blazor application page with object tags instead of iframe.

Regardless, the error I am receiving exhibits the same, inconsistent behavior. I'm guessing this is a Blazor toolchain issue that can be fixed by making sure that the -s TEXTDECODER=0 flag is used when compiling dotnet-runtime.js at publish-time; to safeguard against any regressions of users this bug doesn't affect, this flag would only be added if WasmEnableThreads is also explicitly enabled. I'm willing to write a PR that implements this.

Expected Behavior

The expected behavior is that I can use my multithreaded Blazor WebAssembly application within an iframe without the previously described inconsistent crashing behavior.

Steps To Reproduce

No response

Exceptions (if any)

Uncaught TypeError: Failed to execute 'decode' on 'TextDecoder': The provided ArrayBufferView value must not be shared.

.NET Version

8.0.100-preview.4.23260.5

Anything else?

No response

@IsaMorphic
Copy link
Contributor Author

Hey there again everyone,

I managed to isolate the issue after figuring out that I can use an OS-wide environment variable to append whatever arguments I want to all calls to emcc. Its called EMCC_CFLAGS. Turns out that my issue is rooted inside of the mono wasm runtime. I've found the exact fallacious line of code and will open a PR there instead to resolve this.

Here's a link to the problematic method for anyone curious:

decode(start: CharPtr, end: CharPtr): string {

Thanks,
~Isabelle

IsaMorphic referenced this issue in IsaMorphic/runtime-wasmfix May 23, 2023
…ffer from different JavaScript realm

When performing tasks such as allocating or marshalling large amounts of memory in multithreaded .NET land, inconsistent and frequent crashing behavior is exhibited. 

Related issues: 
emscripten-core/emscripten#15217
https://github.com/dotnet/aspnetcore/issues/48390
@IsaMorphic IsaMorphic reopened this May 23, 2023
@javiercn javiercn transferred this issue from dotnet/aspnetcore May 24, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 24, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 24, 2023
@vcsjones vcsjones added area-System.Runtime.InteropServices.JavaScript and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 24, 2023
@radical radical added arch-wasm WebAssembly architecture area-VM-meta-mono labels May 24, 2023
@ghost
Copy link

ghost commented May 24, 2023

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Issue Details

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Using the wasm-buildtools for .NET 8.0 Preview 4, I have come across an unpredictable and frequent runtime bug when embedding a multithreaded Blazor application within an iframe on another page. I've double checked that all CSP and CORS policies are in order and not the root cause.

In fact, I found someone else reporting this exact same issue, only in a completely different use case over by the emscripten repository: emscripten-core/emscripten#18034

The workaround being tossed out there by several developers is to make sure that all calls to emcc use the flag -s TEXTDECODER=0

I've been able to greatly reduce the frequency of this error by adding this flag using these properties:
<EmccCompileOptimizationFlag>-O2 -s TEXTDECODER=0</EmccCompileOptimizationFlag>
<EmccLinkOptimizationFlag>-O2 -s TEXTDECODER=0</EmccLinkOptimizationFlag>
to my csproj file.

I have also confirmed that these flags are being used to compile my assemblies and dependencies. However, I can't figure out a way to add this flag to the main dotnet.wasm emcc command to ensure that my dotnet-runtime.js file does not use TextDecoder to marshal WASM memory across threads.

Interesting to note is that the frequency of this error greatly decreases when embedding the blazor application page with object tags instead of iframe.

Regardless, the error I am receiving exhibits the same, inconsistent behavior. I'm guessing this is a Blazor toolchain issue that can be fixed by making sure that the -s TEXTDECODER=0 flag is used when compiling dotnet-runtime.js at publish-time; to safeguard against any regressions of users this bug doesn't affect, this flag would only be added if WasmEnableThreads is also explicitly enabled. I'm willing to write a PR that implements this.

Expected Behavior

The expected behavior is that I can use my multithreaded Blazor WebAssembly application within an iframe without the previously described inconsistent crashing behavior.

Steps To Reproduce

No response

Exceptions (if any)

Uncaught TypeError: Failed to execute 'decode' on 'TextDecoder': The provided ArrayBufferView value must not be shared.

.NET Version

8.0.100-preview.4.23260.5

Anything else?

No response

Author: IsaMorphic
Assignees: -
Labels:

arch-wasm, untriaged, area-VM-meta-mono, area-System.Runtime.InteropServices.JavaScript

Milestone: -

@radical
Copy link
Member

radical commented May 24, 2023

cc @pavelsavara

@pavelsavara pavelsavara self-assigned this May 25, 2023
@pavelsavara pavelsavara added this to the Future milestone May 25, 2023
@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed untriaged New issue has not been triaged by the area owner labels May 25, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label May 28, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jun 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.