aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Real multithreading in Blazor WebAssembly

Open BlenderMender opened this issue 5 years ago • 194 comments

Is your feature request related to a problem? Please describe.

I have load of CPU intensive requests connected to vial for the application data from sensors, and I would like to have a real multithreading for multiple sources which streams large amount of data nonstop.

Describe the solution you'd like

Multithreading which is available already in WEBASM to be exposed to Blazor Client side.

BlenderMender avatar Dec 10 '19 09:12 BlenderMender

Thanks for contacting us, @BlenderMender. WASM 1.0 spec doesn't include real multithreading, and this is the spec we currently target. We expect improvements in this area in the future.

mkArtakMSFT avatar Dec 10 '19 17:12 mkArtakMSFT

Wonder if the actual issue is letting JS be multithreaded and some peoples fear that this will cause some disturbance because threadings. If this is the case I have the follow suggestion:

  • Let the JS use 1 thread locked only for it - emulating the current state of the things, and the rest threads be free for background work of the C# application.

YordanYanakiev avatar Dec 10 '19 23:12 YordanYanakiev

I would dive deep into blazor (without any return) if there would be support for multithreading for client side. Make it happen!

Marcin-Perz avatar Jan 25 '20 11:01 Marcin-Perz

I've created a small library called BlazorWorker which creates a new dotnet process using web workers. It's very similar to multithreading, main difference being no shared memory, only message passing @BlenderMender @Marcin-Perz I would appreciate your feedback on the API, to see if it's a path worth pursuing in the meantime.

Tewr avatar Mar 23 '20 07:03 Tewr

This is blocked on https://github.com/mono/mono/issues/12453

mkArtakMSFT avatar Apr 28 '20 21:04 mkArtakMSFT

Thumbs up for for proper .NET Tasks and Threads

maxim-saplin avatar May 13 '20 20:05 maxim-saplin

Hi, yes please make it happen. Thanks in avance.

acceliance avatar May 24 '20 09:05 acceliance

Given the current limitations around browser support we’ve decided to push this work out of .NET 5. We will reevaluate the state of threading support in browsers during .NET 6 planning phase.

mkArtakMSFT avatar May 28 '20 22:05 mkArtakMSFT

Good lords. But this is the most expected thing. The blazor boilerplate and the way the .NET works, and the whole ideology of client side .NET project requires multithreading and real async to work correctly. I vote to back off a bunch of other features and push for multithreading than vice-versa..

NikolaStamenov avatar May 29 '20 01:05 NikolaStamenov

Does this mean we will have to wait 2021 november for threading support?

mertkokusen avatar Jun 01 '20 16:06 mertkokusen

Alright: With the current Firefox 79 the SharedArrayBuffers were reactivated. They were temporarily disabled because of Spectre. Also in Firefox 79, threads for WASM were released: https://hacks.mozilla.org/2020/07/firefox-79/ (note: SharedArrayBuffers was one prerequisite for threads in WASM)

Thus, it should be possible for .NET (Mono) to implement threadding, right?

SommerEngineering avatar Jul 29 '20 12:07 SommerEngineering

Seems like again our trust into MS Frameworks is going to be betrayed. Blazor WebASM currently performing the worst from all available frameworks, because it's awful JS interloop and lack of multi-threading which is essential for .NET.

Please MS team - do something. Do not kill it before it's born, and do it now, December is going to be awfully late, since the disappointment will start taking space.

NikolaStamenov avatar Aug 03 '20 06:08 NikolaStamenov

Ok for the previous comment but, does the underlying framework actually support for threading ? Because you know if not, this very discussion thread would be meaningless (i hope not of course)

Thank you for answering

acceliance avatar Aug 03 '20 07:08 acceliance

Blazor is on top of Mono, Mono is on top of WebAsm. WebASM does support multithreading, Mono does support multithreading - but the implementation lack atm I believe for Mono because.. idk..

YordanYanakiev avatar Aug 03 '20 08:08 YordanYanakiev

Sounds great so the path is open for .net blazor :-)

acceliance avatar Aug 03 '20 08:08 acceliance

It would be good if there was a documentation article regarding the current state of multi threading in blazor wasm. Reading issues on github doesn't always give a good sense if things are still current. Also, a "good to know" article about common pitfalls regarding the current single thread environment of blazor wasm. What's the Behavior of a "Thread.Sleep" or a "ManualResetEvent.WaitOne" ect. I just dropped an old library of mine into blazor wasm and I had to hunt down those things.

Henk7 avatar Sep 23 '20 20:09 Henk7

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost avatar Oct 09 '20 18:10 ghost

Additional customer requests here: https://github.com/dotnet/runtime/issues/40619

danroth27 avatar Oct 27 '20 22:10 danroth27

Current state of browser support for shared array buffers, which is required to support multithreading: https://caniuse.com/?search=sharedarraybuffer. Still not supported in Safari on desktop or on mobile. Mobile support in general is largely missing. Reasonable desktop support with Edge, Chrome, and Firefox.

danroth27 avatar Oct 27 '20 22:10 danroth27

Emh.. actually they all does support it, but it is disabled due Meltdown ( 2018 ). The state of the vulnerability should be checked and patched by the OS providers maybe..

YordanYanakiev avatar Oct 27 '20 22:10 YordanYanakiev

Given the current limitations around browser support we’ve decided to push this work out of .NET 5. We will reevaluate the state of threading support in browsers during .NET 6 planning phase.

Hopefully this re-evaluation can happen again as support of threads would be a powerful feature for Blazor in the client. Opens up a whole new category of scale for apps in Blazor.

RChrisCoble avatar Oct 28 '20 15:10 RChrisCoble

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Dec 08 '20 21:12 ghost

Agree with @RChrisCoble - hope this is considered for inclusion soon. Looks like Safari has enabled it via a preview flag.

ghost avatar Mar 02 '21 06:03 ghost

Agree with @RChrisCoble - hope this is considered for inclusion soon. Looks like Safari has enabled it via a preview flag.

It's not in the plan for .Net 6, so looks like we're waiting for .Net 7.

RChrisCoble avatar Mar 02 '21 14:03 RChrisCoble

Agree with @RChrisCoble - hope this is considered for inclusion soon. Looks like Safari has enabled it via a preview flag.

It's not in the plan for .Net 6, so looks like we're waiting for .Net 7.

Do we have anything that says it is officially in the .NET 7 roadmap?

andersson09 avatar Apr 17 '21 18:04 andersson09

:(

Marcin-Perz avatar Apr 19 '21 13:04 Marcin-Perz

Agree with @RChrisCoble - hope this is considered for inclusion soon. Looks like Safari has enabled it via a preview flag.

It's not in the plan for .Net 6, so looks like we're waiting for .Net 7.

Do we have anything that says it is officially in the .NET 7 roadmap?

Well, publicly nothing is 'officially' in scope for .Net 7. I'm sure MS has their own internal multi-year .Net roadmaps, but we would need to wait for the conclusion of their .Net 7 release planning in 2022 Q1 before knowing for sure.

In .Net 6 they are investing in "Blazor Desktop" tooling, which is fantastic, but if you're using any task based parallelism wherein you are doing a .Wait() or .Result waiting for a task(s), that code will work in the Blazor Desktop model but not the web. This is a little weird from my standpoint, so I hope they enable the task based parallelism in Blazor Client topologies so you don't have to deal with this nuance. Sure there are workarounds like building a custom task scheduler, but from a QA standpoint it really would be handy to have a single code base, which is the ultimate vision of Blazor/MBB/MAUI.

RChrisCoble avatar Apr 19 '21 16:04 RChrisCoble

In my arrogant opinion - this should be investigated as a side project which actually can wait, since desktop aps for sure will not go web anytime soon. Actually the opposite - most of developers I know stick with even tools like C++ Builder because performance, stability, and look, and they are absolutely sure that none desktop app based on web core will go any further than "hello world" on their company project list anytime soon. Blazor is loosing momentum going sideways instead of fixing absolutely important issues and adding very important features which is from long time in the Web dev's lists. And hell - since when the opinion of Apple mater for Microsoft ?! But as I have said - this is just me and my arrogant opinion.

YordanYanakiev avatar Apr 19 '21 20:04 YordanYanakiev

@RChrisCoble I don't want to derail the conversation, but could you provide some more information for working around it with a custom task scheduler? Code references, or an article or something? Thanks ^^

Cryru avatar Apr 20 '21 07:04 Cryru

@RChrisCoble I don't want to derail the conversation, but could you provide some more information for working around it with a custom task scheduler? Code references, or an article or something? Thanks ^^

My suggestion is somewhat theoretical, but plug those words into Google and you can find samples. The idea is you derive from TaskScheduler and instead of leveraging the thread pool you execute the tasks on the current thread, as there is only 1 thread in Blazor Client.

In a perfect world, you can just modify your code to use async/await everywhere, which doesn't hang in Blazor client with the understanding that it's also not running in parallel.

RChrisCoble avatar Apr 20 '21 15:04 RChrisCoble