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)
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
This fix will be part of SR9 https://github.com/dotnet/maui/pull/30115
I slightly modified the repro project proposed in https://github.com/dotnet/maui/issues/28051#issuecomment-2826319419 to automatically refresh the content of the
BindableLayoutevery 3 seconds.With
Microsoft.Maui.Controlsversion9.0.70, I can confirm that the crash occurred within a minute or so when the app is built inReleaseconfiguration.With the
Microsoft.Maui.Controlspackage9.0.90-ci.main.25312.24from 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:
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.