-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[blazor][wasm] Dispatch rendering to main thread (Net9) #52724
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9800678
rebase
pavelsavara 11ece92
check thread ID, not just installed context
pavelsavara 57b7b29
fix
pavelsavara 9cc8b8d
fix
pavelsavara d543432
Merge branch 'main' into wasm_mt_dispatcher2
pavelsavara b2f16f1
Merge branch 'main' into wasm_mt_dispatcher2
pavelsavara 650edd8
Update src/Components/WebAssembly/testassets/ThreadingApp/Pages/Fetch…
pavelsavara 8c0086e
feedback
pavelsavara 57551f7
Update src/Components/test/E2ETest/Tests/ThreadingAppTest.cs
pavelsavara ec33bae
Update src/Components/test/E2ETest/Tests/ThreadingAppTest.cs
pavelsavara 2563eda
feedback
pavelsavara f2549ca
- fix the initialization for unit tests on coreCLR
pavelsavara b22fa85
Merge branch 'main' into wasm_mt_dispatcher2
pavelsavara 3c9b737
better assert
pavelsavara 71d6523
Merge branch 'main' into wasm_mt_dispatcher2
pavelsavara 75044b1
fix
pavelsavara 5194b10
feedback/comments
pavelsavara 8c22942
feedback
pavelsavara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
src/Components/WebAssembly/WebAssembly/src/Rendering/WebAssemblyDispatcher.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Components.WebAssembly.Rendering; | ||
|
||
// When Blazor is deployed with multi-threaded runtime, WebAssemblyDispatcher will help to dispatch all Blazor JS interop calls to the main thread. | ||
// This is necessary because all JS objects have thread affinity. They are only available on the thread (WebWorker) which created them. | ||
// Also DOM is only available on the main (browser) thread. | ||
// Because all of the Dispatcher.InvokeAsync methods return Task, we don't need to propagate errors via OnUnhandledException handler | ||
pavelsavara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
internal sealed class WebAssemblyDispatcher : Dispatcher | ||
{ | ||
internal static SynchronizationContext? _mainSynchronizationContext; | ||
internal static int _mainManagedThreadId; | ||
|
||
// we really need the UI thread not just the right context, because JS objects have thread affinity | ||
public override bool CheckAccess() => _mainManagedThreadId == Environment.CurrentManagedThreadId; | ||
|
||
public override Task InvokeAsync(Action workItem) | ||
{ | ||
ArgumentNullException.ThrowIfNull(workItem); | ||
if (CheckAccess()) | ||
{ | ||
// this branch executes on correct thread and solved JavaScript objects thread affinity | ||
// but it executes out of order, if there are some pending jobs in the _mainSyncContext already, same as RendererSynchronizationContextDispatcher | ||
workItem(); | ||
// it can throw synchronously, same as RendererSynchronizationContextDispatcher | ||
return Task.CompletedTask; | ||
} | ||
|
||
var tcs = new TaskCompletionSource(); | ||
|
||
// RendererSynchronizationContext doesn't need to deal with thread affinity and so it could execute jobs on calling thread as optimization. | ||
// we could not do it for WASM/JavaScript, because we need to solve for thread affinity of JavaScript objects, so we always Post into the queue. | ||
_mainSynchronizationContext!.Post(static (object? o) => | ||
{ | ||
var state = ((TaskCompletionSource tcs, Action workItem))o!; | ||
try | ||
{ | ||
state.workItem(); | ||
state.tcs.SetResult(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
state.tcs.SetException(ex); | ||
} | ||
}, (tcs, workItem)); | ||
|
||
return tcs.Task; | ||
} | ||
|
||
public override Task<TResult> InvokeAsync<TResult>(Func<TResult> workItem) | ||
{ | ||
ArgumentNullException.ThrowIfNull(workItem); | ||
if (CheckAccess()) | ||
{ | ||
// it can throw synchronously, same as RendererSynchronizationContextDispatcher | ||
return Task.FromResult(workItem()); | ||
kg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var tcs = new TaskCompletionSource<TResult>(); | ||
|
||
_mainSynchronizationContext!.Post(static (object? o) => | ||
{ | ||
var state = ((TaskCompletionSource<TResult> tcs, Func<TResult> workItem))o!; | ||
try | ||
{ | ||
var res = state.workItem(); | ||
state.tcs.SetResult(res); | ||
} | ||
catch (Exception ex) | ||
{ | ||
state.tcs.SetException(ex); | ||
} | ||
}, (tcs, workItem)); | ||
|
||
return tcs.Task; | ||
} | ||
|
||
public override Task InvokeAsync(Func<Task> workItem) | ||
{ | ||
ArgumentNullException.ThrowIfNull(workItem); | ||
if (CheckAccess()) | ||
{ | ||
// this branch executes on correct thread and solved JavaScript objects thread affinity | ||
// but it executes out of order, if there are some pending jobs in the _mainSyncContext already, same as RendererSynchronizationContextDispatcher | ||
return workItem(); | ||
// it can throw synchronously, same as RendererSynchronizationContextDispatcher | ||
} | ||
|
||
var tcs = new TaskCompletionSource(); | ||
|
||
_mainSynchronizationContext!.Post(static (object? o) => | ||
{ | ||
var state = ((TaskCompletionSource tcs, Func<Task> workItem))o!; | ||
|
||
try | ||
{ | ||
state.workItem().ContinueWith(t => | ||
{ | ||
if (t.IsFaulted) | ||
{ | ||
state.tcs.SetException(t.Exception); | ||
} | ||
else if (t.IsCanceled) | ||
{ | ||
state.tcs.SetCanceled(); | ||
} | ||
else | ||
{ | ||
state.tcs.SetResult(); | ||
} | ||
}, TaskScheduler.FromCurrentSynchronizationContext()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
// it could happen that the workItem will throw synchronously | ||
state.tcs.SetException(ex); | ||
} | ||
}, (tcs, workItem)); | ||
|
||
return tcs.Task; | ||
} | ||
|
||
public override Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> workItem) | ||
{ | ||
ArgumentNullException.ThrowIfNull(workItem); | ||
if (CheckAccess()) | ||
{ | ||
// this branch executes on correct thread and solved JavaScript objects thread affinity | ||
// but it executes out of order, if there are some pending jobs in the _mainSyncContext already, same as RendererSynchronizationContextDispatcher | ||
return workItem(); | ||
// it can throw synchronously, same as RendererSynchronizationContextDispatcher | ||
} | ||
|
||
var tcs = new TaskCompletionSource<TResult>(); | ||
|
||
_mainSynchronizationContext!.Post(static (object? o) => | ||
{ | ||
var state = ((TaskCompletionSource<TResult> tcs, Func<Task<TResult>> workItem))o!; | ||
try | ||
{ | ||
state.workItem().ContinueWith(t => | ||
{ | ||
if (t.IsFaulted) | ||
{ | ||
state.tcs.SetException(t.Exception); | ||
} | ||
else if (t.IsCanceled) | ||
{ | ||
state.tcs.SetCanceled(); | ||
} | ||
else | ||
{ | ||
state.tcs.SetResult(t.Result); | ||
} | ||
}, TaskScheduler.FromCurrentSynchronizationContext()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
state.tcs.SetException(ex); | ||
} | ||
}, (tcs, workItem)); | ||
|
||
return tcs.Task; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.