dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

WebView error if we leave page while it's navigating #20627

Closed ESO-ST closed 5 months ago

ESO-ST commented 7 months ago

Description

If we have a webview and it's navigating somewhere, say for example we pressed on a link and it's loading a page, if while that navigation is taking place we leave the page where the webview is, we get an error.

ObjCRuntime.ObjCException
  Message=Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: Completion handler passed to -[Microsoft_Maui_Platform_MauiWebViewUIDelegate webView:contextMenuConfigurationForElement:completionHandler:] was not called
Native stack trace:
    0   CoreFoundation                      0x00007ff80048d28d __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007ff800057894 objc_exception_throw + 48
    2   CoreFoundation                      0x00007ff80048d16b -[NSException initWithCoder:] + 0
    3   WebKit                              0x00000001119ba879 _ZN6WebKit28CompletionHandlerCallCheckerD2Ev + 127
    4   WebKit                              0x0000000111a223d5 _ZNK3WTF20ThreadSafeRefCountedIN6WebKit28CompletionHandlerCallCheckerELNS_17DestructionThreadE0EE5derefEv + 33
    5   WebKit                              0x00000001121a0a69 _ZZN3WTF8BlockPtrIFvP26UIContextMenuConfigurationEE12fromCallableIZ70-[WKContentView(WKInteractionPreview) continueContextMenuInteraction:]E5$_102EES4_T_ENUlPKvE_8__invokeES9_ + 31
    6   libsystem_blocks.dylib              0x00007ff8000c1214 _call_dispose_helpers_excp + 45
    7   libsystem_blocks.dylib              0x00007ff8000c1ea5 _Block_release + 232
    8   libdispatch.dylib                   0x00007ff80014bd3a _dispatch_client_callout + 8
    9   libdispatch.dylib                   0x00007ff80015aac0 _dispatch_main_queue_drain + 1420
    10  libdispatch.dylib                   0x00007ff80015a526 _dispatch_main_queue_callback_4CF + 31
    11  CoreFoundation                      0x00007ff8003e9850 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    12  CoreFoundation                      0x00007ff8003e418b __CFRunLoopRun + 2463
    13  CoreFoundation                      0x00007ff8003e3409 CFRunLoopRunSpecific + 557
    14  GraphicsServices                    0x00007ff80a650187 GSEventRunModal + 137
    15  UIKitCore                           0x000000011b1df3a2 -[UIApplication _run] + 972
    16  UIKitCore                           0x000000011b1e3e10 UIApplicationMain + 123
    17  libxamarin-dotnet-debug.dylib       0x00000001056f850a xamarin_UIApplicationMain + 58
    18  libmonosgen-2.0.dylib               0x0000000105e960f5 do_icall + 341
    19  libmonosgen-2.0.dylib               0x0000000105e94887 do_icall_wrapper + 295
    20  libmonosgen-2.0.dylib               0x0000000105e855a6 mono_interp_exec_method + 3990
    21  libmonosgen-2.0.dylib               0x0000000105e82d13 interp_runtime_invoke + 259
    22  libmonosgen-2.0.dylib               0x0000000105f69748 mono_runtime_invoke_checked + 136
    23  libmonosgen-2.0.dylib               0x0000000105f7109b mono_runtime_exec_main_checked + 107
    24  libmonosgen-2.0.dylib               0x0000000105dd5482 mono_jit_exec + 354
    25  libxamarin-dotnet-debug.dylib       0x000000010573cbea xamarin_main + 1898
    26  WebViewCrashMidLoad                 0x0000000103dd4a04 main + 68
    27  dyld                                0x00000001047023ee start_sim + 10
    28  ???                                 0x00000001089f13a6 0x0 + 4439610278
  Source=Microsoft.iOS
  StackTrace:
   at ObjCRuntime.Runtime.ThrowNSException(IntPtr ns_exception) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 507
   at ObjCRuntime.Runtime.throw_ns_exception(IntPtr exc) in /Users/builder/azdo/_work/1/s/xamarin-macios/runtime/Delegates.generated.cs:line 279
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at WebViewCrashMidLoad.Program.Main(String[] args) in C:\Users\ortiz\source\repos\WebViewCrashMidLoad\WebViewCrashMidLoad\Platforms\iOS\Program.cs:line 13

Steps to Reproduce

press click me to open a modal page with a web view. press on a link in the webview to make it load that url, while that's taking place leave the page by pressing click me.

Link to public reproduction project repository

https://github.com/ESO-ST/WebViewCrashMidLoad

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 15

Did you find any workaround?

No response

Relevant log output

No response

RoiChen001 commented 5 months ago

Can repro it at iOS platform on 17.10.0 Preview 2 (8.0.10 &8.0.6). ios20627

ESO-ST commented 5 months ago

After using the nightly version of MAUI that has this fix, we now get this error

14:23:47:238    Unhandled Exception:
14:23:47:238    System.InvalidOperationException: VirtualView cannot be null here
14:23:47:238       at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IWebView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[WebKit.WKWebView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].get_VirtualView()
14:23:47:238       at Microsoft.Maui.Handlers.WebViewHandler.Microsoft.Maui.Handlers.IWebViewHandler.get_VirtualView()
14:23:47:238       at Microsoft.Maui.Platform.MauiWebViewNavigationDelegate.DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action`1 decisionHandler)
14:23:47:238       at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
14:23:47:238       at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKi
14:23:47:238    t/UIApplication.cs:line 94
14:23:47:238       at WebViewCrashMidLoad.Program.Main(String[] args) in C:\Users\Salem\source\repos\WebViewCrashMidLoad\WebViewCrashMidLoad\Platforms\iOS\Program.cs:line 13
14:23:47:238    2024-04-05 19:23:47.148522+0100 WebViewCrashMidLoad[39290:225312] Unhandled managed exception: VirtualView cannot be null here (System.InvalidOperationException)
14:23:47:238       at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IWebView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[WebKit.WKWebView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].get_VirtualView()
14:23:47:238       at Microsoft.Maui.Handlers.WebViewHandler.Microsoft.Maui.Handlers.IWebViewHandler.get_VirtualView()
14:23:47:238       at Microsoft.Maui.Platform.MauiWebViewNavigationDelegate.DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action`1 decisionHandler)
14:23:47:238       at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
14:23:47:238       at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/
14:23:47:238    src/UIKit/UIApplication.cs:line 94
14:23:47:238       at WebViewCrashMidLoad.Program.Main(String[] args) in C:\Users\Salem\source\repos\WebViewCrashMidLoad\WebViewCrashMidLoad\Platforms\iOS\Program.cs:line 13

I do want to note that our WebView is inside a Mopups Popup page, the repro project was also updated to reflect that and the issue is reproduceable there as well.