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

Add switch to BlazorWebView to configure 'fire and forget' for Android Disposal #22496

Merged
merged 2 commits into from
May 20, 2024

Conversation

Eilon
Copy link
Member

@Eilon Eilon commented May 17, 2024

Description of Change

Add an opt-in switch to enable the BlazorWebView to 'fire and forget' the async disposal that occurs in BlazorWebView. By default, the BlazorWebView does async-over-sync for disposal, meaning that it blocks the thread until the async disposal is complete. Unfortunately, this can cause deadlocks if the disposal itself needs to run code on the same thread (because that thread is blocked while waiting). The control has had this logic for a long time, but it seems that on Android the luck has changed, and hangs are now quite common during disposal.

By default there is no behavior change.

If you are encountering hangs in Android with BlazorWebView, you can enable this AppContext Switch with one line of code in your app's MauiProgram.cs:

AppContext.SetSwitch("BlazorWebView.AndroidFireAndForgetAsync", isEnabled: true);

This issue has been reported in several different issues, but we think they all have this same root cause, and enabling this new switch should fix your app.

Confirmed fixed by this PR:

Hard to test, but seem to be exactly the same issue:

Caveats

Because enabling this new switch means that disposal can return before all objects are disposed, this can cause behavioral changes in an app. The items that are disposed are partially Blazor's own internal types, but also app-defined types such as scoped services used within the BlazorWebView portion of the app.

Issues Fixed

Fixes #13529

@Eilon Eilon added the area-blazor Blazor Hybrid / Desktop, BlazorWebView label May 17, 2024
@Eilon Eilon requested a review from a team as a code owner May 17, 2024 16:49
PureWeen
PureWeen previously approved these changes May 17, 2024
@@ -59,19 +59,37 @@ protected override AWebView CreatePlatformView()
return blazorAndroidWebView;
}

private const string AndroidFireAndForgetAsyncSwitch = "BlazorWebView.AndroidFireAndForgetAsync";
Copy link
Member

Choose a reason for hiding this comment

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

Not to bikeshed, but would "BlazorWebView.AndroidFireAndForgetDisposeAsync" be more targeted toward the specific change in behavior?

@Eilon Eilon merged commit 1c8e512 into main May 20, 2024
49 checks passed
@Eilon Eilon deleted the eilon/net8-blazor-androidhang2 branch May 20, 2024 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DisposeAsync() on IJSObjectReference causes hanging when called on Android 11 and lower on app suspension
4 participants