uno icon indicating copy to clipboard operation
uno copied to clipboard

[WASM] Creating more than two concurrent Long Running tasks causes the UI to Freeze in WASM.

Open EvarDion opened this issue 3 years ago • 4 comments
trafficstars

Current behavior

Creating more than 2 Tasks with the TaskCreationOptions.LongRunning flag and then running them concurrently in WASM causes the entire browser to freeze with no errors given in the developer console as to what may be the cause.

TaskCreationOptions.LongRunning works as expected in .WINDOWS (UWP)

Attached solution works as expected in WASM when other flags are used such as: TaskCreationOptions.PreferFairness
TaskCreationOptions.None

Relevant code is as follows:

Threading example

WASMThreading.zip

Expected behavior

Tasks created with TaskCreationOptions.LongRunning flag are generally given their own thread ID outside of the threadpool as not to negatively impact the threadpool.

How to reproduce it (as minimally and precisely as possible)

No response

Workaround

For now, do not use TaskCreationOptions.LongRunning

Works on UWP/WinUI

Yes

Environment

Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia

NuGet package version(s)

Microsoft.Extensions.Logging" Version="5.0.0" Uno.Extensions.Logging.WebAssembly.Console" Version="1.3.0" Uno.WinUI.WebAssembly" Version="4.4.13" Uno.WinUI.RemoteControl" Version="4.4.13" Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="4.4.13" Uno.Wasm.Bootstrap" Version="4.0.0-dev.174" Uno.Wasm.Bootstrap.DevServer" Version="4.0.0-dev.174"

Affected platforms

WebAssembly

IDE

Visual Studio 2022

IDE version

17.3.0 Preview 2.0

Relevant plugins

No response

Anything else we need to know?

Same behavior when targeting .NET 6 or .NET 7 preview.

EvarDion avatar Jul 05 '22 09:07 EvarDion

Have you followed the steps here to enable Threading support preview in WASM? https://platform.uno/blog/uno-platform-4-4-wasm-threadingexception-handling-rich-animations-gamepad-apis-and-more/

Are threads working in their simplest form?

Are you also saying you can run 2 long running tasks without isssue and it's once you create a 3rd one that it fails?

Uno Platform
Pixel-Perfect Multi-Platform Applications with C# and WinUI for Windows, WebAssembly, iOS, macOS, Android and Linux

francoistanguay avatar Jul 05 '22 14:07 francoistanguay

Have you followed the steps here to enable Threading support preview in WASM? Yes Are threads working in their simplest form? When using Threads explicitly the problem is the same, creating 2 additional concurrent threads works but as soon as you try 3 it freezes the browser for an unknown period of time. Are you also saying you can run 2 long running tasks without issue and it's once you create a 3rd one that it fails? Yes

This is the output I get when running 100 Tasks concurrently in browser using TaskCreationOptions.PreferFairness but when using TaskCreationOptions.LongRunning the browser (Brave, Chrome, Edge) chokes on 3 Concurrent Tasks. In fact in some cases it seems to lock up the entire browser to the point where I can't even close it in Windows 10 and I need to use Task manager to close the browser.

Test

EvarDion avatar Jul 05 '22 21:07 EvarDion

UPDATE:

It turns out that spawning threads from the Main/UI thread in WASM can cause deadlocks which is what happens when you create many Long Running Tasks from the UI thread (e.g. button_click, page constructor, etc).

Workaround: If you need to create LongRunning tasks or explicit Threads then it's best to wrap that code inside it's own Task which will then run on a Threadpool/Background thread and spawn any new threads from there (and not from the Main/UI thread).

Example Code:

Threading Solution

Result: (Multiple threads working in WASM as expected)

Multithreading Working

It would be good if the above workaround could be built into Uno by default as the overhead would be minimal since web workers for the Task Scheduler's ThreadPool are already created on app startup.

This will save a lot of headaches for developers in the future because deadlocks are a nightmare to debug in C# and even harder to debug in the browser.

EvarDion avatar Jul 06 '22 07:07 EvarDion

Thanks for the feedback! This is definitely something that the .NET team will be interested in. We'll follow up with them.

jeromelaban avatar Aug 01 '22 15:08 jeromelaban