Skip to content

Commit

Permalink
Fire and forget enabled by default (#2628)
Browse files Browse the repository at this point in the history
* Fire and forget enabled by default.

* Edits.
  • Loading branch information
davidbritch authored Nov 13, 2024
1 parent d9cf7a9 commit dc648aa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
18 changes: 9 additions & 9 deletions docs/user-interface/controls/blazorwebview.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Host a Blazor web app in a .NET MAUI app using BlazorWebView"
description: "The .NET MAUI BlazorWebView control enables you to host a Blazor web app in your .NET MAUI app, and integrate the app with device features."
ms.date: 10/01/2024
ms.date: 11/13/2024
---

# Host a Blazor web app in a .NET MAUI app using BlazorWebView
Expand Down Expand Up @@ -218,20 +218,20 @@ To play inline video in a Blazor hybrid app on iOS, in a <xref:Microsoft.AspNetC

::: moniker range=">=net-maui-9.0"

## Fix disposal deadlocks on Android
## Disposal deadlocks on Android

By default, <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> performs async-over-sync disposal, which means that it blocks the thread until the async disposal is complete. However, this can cause deadlocks if the disposal needs to run code on the same thread (because the thread is blocked while waiting).
By default, <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> fires and forgets the async disposal of the underlying `WebViewManager`. This reduces the potential for disposal deadlocks to occur on Android.

If you encounter hangs on Android with <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> you should enable an <xref:System.AppContext> switch in the `CreateMauiApp` method in *MauiProgram.cs*:
> [!WARNING]
> This fire-and-forget default behavior means that disposal can return before all objects are disposed, which can cause behavioral changes in your 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 <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> portion of your app.

To opt out of this behavior, you should configure your app to block on dispose via an <xref:System.AppContext> switch in the `CreateMauiApp` method in your `MauiProgram` class:

```csharp
AppContext.SetSwitch("BlazorWebView.AndroidFireAndForgetAsync", true);
AppContext.SetSwitch("BlazorWebView.AndroidFireAndForgetAsync", false);
```

This switch enables <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> to fire and forget the async disposal that occurs, and as a result fixes the majority of the disposal deadlocks that occur on Android.

> [!WARNING]
> Enabling this switch means that disposal can return before all objects are disposed, which can cause behavioral changes in your 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 <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> portion of your app.
If your app is configured to block on dispose via this switch, <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> performs async-over-sync disposal, which means that it blocks the thread until the async disposal is complete. However, this can cause deadlocks if the disposal needs to run code on the same thread (because the thread is blocked while waiting).

## Host content using the legacy behavior on iOS and Mac Catalyst

Expand Down
11 changes: 8 additions & 3 deletions docs/whats-new/dotnet-9.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,18 @@ To opt into using the `0.0.0.1` address, add the following code to the `CreateMa
AppContext.SetSwitch("BlazorWebView.AppHostAddressAlways0000", true);
```

If you encounter hangs on Android with <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> you should enable an <xref:System.AppContext> switch in the `CreateMauiApp` method in your `MauiProgram` class:
By default, <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> now fires and forgets the async disposal of the underlying `WebViewManager`. This reduces the potential for disposal deadlocks to occur on Android.

> [!WARNING]
> This fire-and-forget default behavior means that disposal can return before all objects are disposed, which can cause behavioral changes in your 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 <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> portion of your app.
To opt out of this behavior, you should configure your app to block on dispose via an <xref:System.AppContext> switch in the `CreateMauiApp` method in your `MauiProgram` class:

```csharp
AppContext.SetSwitch("BlazorWebView.AndroidFireAndForgetAsync", true);
AppContext.SetSwitch("BlazorWebView.AndroidFireAndForgetAsync", false);
```

This switch enables <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> to fire and forget the async disposal that occurs, and as a result fixes the majority of the disposal deadlocks that occur on Android. For more information, see [Fix disposal deadlocks on Android](~/user-interface/controls/blazorwebview.md#fix-disposal-deadlocks-on-android).
If your app is configured to block on dispose via this switch, <xref:Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebView> performs async-over-sync disposal, which means that it blocks the thread until the async disposal is complete. However, this can cause deadlocks if the disposal needs to run code on the same thread (because the thread is blocked while waiting).

### Buttons on iOS

Expand Down

0 comments on commit dc648aa

Please sign in to comment.