maui: ViewExtensions.OnUnloaded causes app crash if the view is already disposed, this happens for web view in Blazor Hybrid apps

Description

We frequently see production app crashes associated the following stack trace in our Blazor Hybrid app for Android:

Exception android.runtime.JavaProxyThrowable: [System.ObjectDisposedException]: ObjectDisposed_Generic
ObjectDisposed_ObjectName_Name, Microsoft.AspNetCore.Components.WebView.Maui.BlazorAndroidWebView
  at Java.Interop.JniPeerMembers.AssertSelf (Unknown Source)
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (Unknown Source)
  at Android.Views.View.RemoveOnAttachStateChangeListener (Unknown Source)
  at Android.Views.View+<>c__DisplayClass3033_0.<remove_ViewDetachedFromWindow>b__0 (Unknown Source)
  at Java.Interop.EventHelper.RemoveEventHandler (Unknown Source)
  at Android.Views.View.remove_ViewDetachedFromWindow (Unknown Source)
  at Microsoft.Maui.Platform.ViewExtensions+<>c__DisplayClass43_0.<OnUnloaded>b__1 (Unknown Source)
  at Microsoft.Maui.Platform.ActionDisposable.Dispose (Unknown Source)
  at Microsoft.Maui.Platform.ViewExtensions+<>c__DisplayClass43_0.<OnUnloaded>b__3 (Unknown Source)
  at Java.Lang.Thread+RunnableImplementor.Run (Unknown Source)
  at Java.Lang.IRunnableInvoker.n_Run (Unknown Source)
  at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V + 0x5 (Unknown Source)
  at mono.java.lang.RunnableImplementor.n_run (RunnableImplementor.java)
  at mono.java.lang.RunnableImplementor.run (RunnableImplementor.java:29)
  at android.os.Handler.handleCallback (Handler.java:1000)
  at android.os.Handler.dispatchMessage (Handler.java:104)
  at android.os.Looper.loopOnce (Looper.java:242)
  at android.os.Looper.loop (Looper.java:362)
  at android.app.ActivityThread.main (ActivityThread.java:8393)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:552)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:992)

As you may notice, there are no own methods in this stack trace.

As far as I can judge, it’s an attempt to remove native event handler on already disposed native web view triggered by the following code in ViewExtensions.OnUnloaded(...):

EventHandler<AView.ViewDetachedFromWindowEventArgs>? routedEventHandler = null;
ActionDisposable? disposable = new ActionDisposable(() =>
{
	if (routedEventHandler != null)
		view.ViewDetachedFromWindow -= routedEventHandler; // HERE
});
routedEventHandler = (_, __) => ...;

There is a number of ways to address it, but even suppression of an exception for view.ViewDetachedFromWindow -= ... would be fine for the logic in OnUnloaded.

We couldn’t find a workaround for this specific issue: no own methods on call stack in this case + it seems pretty hard to replace internal BlazorAndroidWebView with our own implementation. So I’d appreciate any suggestions for Blazor Hybrid apps here in the meantime, i.e. until the moment this issue is fixed.

Steps to Reproduce

Sorry, no exact steps - the issue is happening fairly rarely in production, but frequently enough to be our #1 app crash cause.

Link to public reproduction project repository

No response

Version with bug

9.0.40 SR4

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No.

Relevant log output


About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 23 (8 by maintainers)

Commits related to this issue

Most upvoted comments

This issue has been verified in Visual Studio (17.14.0 Preview 4.0). After several refresh attempts (around 10 to 15 clicks), the issue was reproduced on 9.0.60 of the Android platform. I have attached the video for reference.

https://github.com/user-attachments/assets/63af512c-c221-46ca-beff-713d6cf47b78

If you’re able to validate the nugets on this PR #29934

Please let us know if that resolves

I slightly modified the repro project proposed in https://github.com/dotnet/maui/issues/28051#issuecomment-2826319419 to automatically refresh the content of the BindableLayout every 3 seconds.

With Microsoft.Maui.Controls version 9.0.70, I can confirm that the crash occurred within a minute or so when the app is built in Release configuration.

With the Microsoft.Maui.Controls package 9.0.90-ci.main.25312.24 from https://github.com/dotnet/maui/pull/29934, with the same build conf and everything, I let it run for an hour and I didn’t observe any crash. The PR seems to resolve the problem 👍

Thanks a lot!

@jfversluis: I have a repro here: https://github.com/mfeingol/repros/tree/master/MauiRepro_28051

It would be great to have this fixed, as this crash is plaguing my application in some fairly basic scenarios. The problem appears to happen when a Behavior is attached to Views inside a collection and the collection’s contents are refreshed.

Repro steps: compile the project in release mode and deploy to an emulator. On the main page, click on the “Repro” toolbar item repeatedly. It shouldn’t take too long to see the app crash in the logs:

ObjectDisposed_ObjectName_Name, Microsoft.Maui.Platform.MauiMaterialButton
	at Java.Interop.JniPeerMembers.AssertSelf + 0x29(Unknown Source)
	at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod + 0x0(Unknown Source)
	at Android.Views.View.RemoveOnAttachStateChangeListener + 0x36(Unknown Source)
	at Android.Views.View+<>c__DisplayClass3033_0.<remove_ViewDetachedFromWindow>b__0 + 0x0(Unknown Source)
	at Java.Interop.EventHelper.RemoveEventHandler + 0x32(Unknown Source)
	at Android.Views.View.remove_ViewDetachedFromWindow + 0x14(Unknown Source)
	at Microsoft.Maui.Platform.ViewExtensions+<>c__DisplayClass43_0.<OnUnloaded>b__1 + 0x8(Unknown Source)
	at Microsoft.Maui.Platform.ActionDisposable.Dispose + 0x10(Unknown Source)
	at Microsoft.Maui.Platform.ViewExtensions+<>c__DisplayClass43_0.<OnUnloaded>b__3 + 0x1d(Unknown Source)
	at Java.Lang.Thread+RunnableImplementor.Run + 0x8(Unknown Source)
	at Java.Lang.IRunnableInvoker.n_Run + 0x8(Unknown Source)
	at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V + 0x5(Unknown Source)
	at mono.java.lang.RunnableImplementor.n_run(Native Method)
	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
	at android.os.Handler.handleCallback(Handler.java:958)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:205)
	at android.os.Looper.loop(Looper.java:294)
	at android.app.ActivityThread.main(ActivityThread.java:8177)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

If you’re able to validate the nugets on this PR https://github.com/dotnet/maui/pull/29934

Please let us know if that resolves

As for the repro, unfortunately I don’t have time for this. As I said, this issue is fairly frequent in Google Play Store crash logs, but reproducing it reliably may require unpredictable amount of time.

@jfversluis I’ve untagged Blazor as this seems to be related to the webview itself and not something Blazor specific.