Skip to content

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

Merged
Eilon merged 2 commits into
mainfrom
eilon/net8-blazor-androidhang2
May 20, 2024
Merged

Add switch to BlazorWebView to configure 'fire and forget' for Android Disposal#22496
Eilon merged 2 commits into
mainfrom
eilon/net8-blazor-androidhang2

Conversation

@Eilon
Copy link
Copy Markdown
Contributor

@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
@Eilon Eilon enabled auto-merge (squash) May 18, 2024 00:38
return blazorAndroidWebView;
}

private const string AndroidFireAndForgetAsyncSwitch = "BlazorWebView.AndroidFireAndForgetAsync";
Copy link
Copy Markdown
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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

5 participants