MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
452 stars 55 forks source link

Application crash randomly #774

Closed orrindeng closed 3 years ago

orrindeng commented 3 years ago

We integrated WebView2 into our WPF application. The WebView2 control is put in a zero width and zero height Border and it will navigate to a initial URL to preload the page. But we receive some feedback from our mates that our application will crash randomly when we did some operation(not regular) to application.

We found the crash information from Event Viewer.

Faulting module name: KERNELBASE.dll, version: 10.0.19041.572, time stamp: 0x1183946c
Exception code: 0xe0434352
Fault offset: 0x0000000000023e49
Faulting process id: 0x4c88
Faulting application start time: 0x01d6ddc5a39843e3
Faulting application path: E:\Bitbucket\forguncy\Forguncy.Server2\designerBin\Forguncy.exe
Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
Report Id: ea973309-8136-459a-8374-f3b0792fed1f
Faulting package full name: 
Faulting package-relative application ID: 
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.InteropServices.COMException
   at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(Microsoft.Web.WebView2.Core.Raw.tagRECT)
   at Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(System.Drawing.Rectangle)
   at Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(System.Windows.Rect)
   at System.Windows.Interop.HwndHost.UpdateWindowPos()
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.Interop.HwndSource.SetLayoutSize()
   at System.Windows.Interop.HwndSource.set_RootVisualInternal(System.Windows.Media.Visual)
   at System.Windows.Window.SetRootVisual()
   at System.Windows.Window.SetRootVisualAndUpdateSTC()
   at System.Windows.Window.SetupInitialState(Double, Double, Double, Double)
   at System.Windows.Window.CreateSourceWindow(Boolean)
   at System.Windows.Window.ShowHelper(System.Object)
   at System.Windows.Window.ShowDialog()
   at Forguncy.App.app_DispatcherUnhandledException(System.Object, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs)
   at System.Windows.Threading.Dispatcher.CatchException(System.Exception)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)

Runtime: 87.0.664.66 Framework: WPF OS: Win 10


Updated: This stack trace happens when another unhandled exception(no matter what it is) happens, and we want to show the exception information dialog(you see app_DispatcherUnhandledException and ShowDialog in stack trace), but showing dialog causes WebView2 crashes. So our application crashes.

AB#31292515

orrindeng commented 3 years ago

Another StackTrace:

   場所 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   場所 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   場所 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   場所 System.Windows.Interop.HwndHost.UpdateWindowPos()
   場所 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   場所 System.Windows.ContextLayoutManager.UpdateLayout()
   場所 System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   場所 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   場所 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   場所 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   場所 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   場所 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
orrindeng commented 3 years ago

When this exception happens, it will happen for many many many times. And we just can't do anything on our UI thread (stuck) if we catch it in UnhandledException event. I will really appreciate if anyone tell me how to solve it.

kaansengul commented 3 years ago

I also have same problem (at 4 different PC's with same config), but mine crashes at set_IsVisible:

Application: Desktop.exe CoreCLR Version: 5.0.120.57516 .NET Version: 5.0.1 Description: The process was terminated due to an unhandled exception. Exception Info: System.Runtime.InteropServices.COMException (0x8007139F): Grup veya kaynak istenen işlemi gerçekleştirmek için doğru durumda değil. (0x8007139F) at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_IsVisible(Int32 IsVisible) at Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_IsVisible(Boolean value) at Microsoft.Web.WebView2.Wpf.WebView2.UIElement_IsVisibleChanged(Object sender, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyPropertyChangedEventHandler.Invoke(Object sender, DependencyPropertyChangedEventArgs e) at System.Windows.UIElement.OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.UIElement.UpdateIsVisibleCache() at System.Windows.UIElement.InvalidateForceInheritPropertyOnChildren(Visual v, DependencyProperty property) at System.Windows.FrameworkElement.InvalidateForceInheritPropertyOnChildren(DependencyProperty property) at System.Windows.UIElement.OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.UIElement.UpdateIsVisibleCache() at System.Windows.UIElement.InvalidateForceInheritPropertyOnChildren(Visual v, DependencyProperty property) at

champnic commented 3 years ago

@orrindeng Are you saying you only see this crash if you there is another (unrelated to WebView2) crash, you try to display the dialog, and that's when WebView2 crashes? Do you know what that other exception is?

dujianxin commented 3 years ago

I got the same problem. It crashs randomly when I open a dialog that contains WebView2 control.

2021-01-07 15:02:12.882 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   在 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   在 System.Windows.Interop.HwndHost.UpdateWindowPos()
   在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.Interop.HwndSource.SetLayoutSize()
   在 System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
   在 System.Windows.Window.SetRootVisual()
   在 System.Windows.Window.SetRootVisualAndUpdateSTC()
   在 System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
   在 System.Windows.Window.CreateSourceWindow(Boolean duringShow)
   在 System.Windows.Window.ShowHelper(Object booleanBox)
   在 System.Windows.Window.ShowDialog()
   在 HHMedic.Pasteur.App.Views.DialogWindow.Prism.Services.Dialogs.IDialogWindow.ShowDialog()
   在 Prism.Services.Dialogs.DialogService.ShowDialog(String name, IDialogParameters parameters, Action`1 callback)
   在 HHMedic.Pasteur.Module.Order.ViewModels.OrderSearcherViewModel.RecordCalleeAcceptHandle(DoctorOrderModel order)
   在 Prism.Events.EventSubscription`1.<>c__DisplayClass11_0.<GetExecutionStrategy>b__0(Object[] arguments)
   在 Prism.Events.EventBase.InternalPublish(Object[] arguments)
   在 HHMedic.Pasteur.Module.Order.ViewModels.RecordEditorViewModel.<RTCCalleeAcceptHandle>d__144.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
2021-01-07 15:02:12.882 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   在 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   在 System.Windows.Interop.HwndHost.UpdateWindowPos()
   在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.Interop.HwndSource.Process_WM_SIZE(UIElement rootUIElement, IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam)
   在 System.Windows.Interop.HwndSource.LayoutFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
2021-01-07 15:02:12.882 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   在 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   在 System.Windows.Interop.HwndHost.UpdateWindowPos()
   在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.Interop.HwndSource.Process_WM_SIZE(UIElement rootUIElement, IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam)
   在 System.Windows.Interop.HwndSource.LayoutFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
2021-01-07 15:02:12.882 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   在 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   在 System.Windows.Interop.HwndHost.UpdateWindowPos()
   在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
2021-01-07 15:02:12.909 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   在 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   在 System.Windows.Interop.HwndHost.UpdateWindowPos()
   在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
orrindeng commented 3 years ago

@champnic Thanks for your reply. We are still developing our product and it's unstable. These exceptions won't cause crash because we will handle them and show it in a Exception Dialog in app_DispatcherUnhandledException. (These exceptions are for example, operate CollectionView in another thread, or argument null exception etc..) I 've tried to just put a button on our window, click button and throw a new exception. The Exception Dialog show, but the crash didn't reproduce.

I'm pretty sure the crash happens only when another exception happens and we want to show it with a dialog in our application. But we found some case is that the same exception happens (which has different stack trace) when the application is in background. And we catch this exception and want to show it. Then application crashes. :(

The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)
   at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   at Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   at Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   at System.Windows.Interop.HwndHost.UpdateWindowPos()
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
champnic commented 3 years ago

Thanks for the added info - I've opened a bug with this info on our backlog and we'll take a closer look.

orrindeng commented 3 years ago

Hello @champnic , I found a way to reproduce my problem by a simple demo with simple steps. XAML Code:

<Window x:Class="WpfApp15.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp15" 
        xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf" xmlns:fluent="urn:fluent-ribbon"
        mc:Ignorable="d"
        Title="MainWindow">
    <StackPanel>
        <wv2:WebView2 Name="wv2" Height="400" Source="https://www.microsoft.com"></wv2:WebView2>
    </StackPanel>
</Window>

C# Code:

namespace WpfApp15
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
        }

        private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            Window window = new Window()
            {
                Width = 400,
                Height = 100,
            };
            window.Content = JsonConvert.SerializeObject(e.Exception);
            window.ShowDialog();

            e.Handled = true;
        }
    }
}

Reproduce step: Kill all msedgewebview2.exe processes. And then firstly throws Exception:

The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)
   at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   at Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   at Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   at System.Windows.Interop.HwndHost.UpdateWindowPos()
   at System.Windows.Interop.HwndHost.OnLayoutUpdated(Object sender, EventArgs e)
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Then the above Dispatcher_UnhandledException catch exception then want to show it. It crashes exception again:

The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)
   at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   at Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   at Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   at System.Windows.Interop.HwndHost.UpdateWindowPos()
   at System.Windows.Interop.HwndHost.OnLayoutUpdated(Object sender, EventArgs e)
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.UIElement.UpdateLayout()
   at System.Windows.Interop.HwndSource.SetLayoutSize()
   at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
   at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
   at System.Windows.Window.SetRootVisual()
   at System.Windows.Window.SetRootVisualAndUpdateSTC()
   at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
   at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
   at System.Windows.Window.CreateSourceWindowDuringShow()
   at System.Windows.Window.SafeCreateWindowDuringShow()
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()
   at System.Windows.Window.ShowDialog()
   at WpfApp15.MainWindow.Dispatcher_UnhandledException(Object sender, DispatcherUnhandledExceptionEventArgs e) in C:\Users\orrindeng\source\repos\WpfApp15\MainWindow.xaml.cs:line 43
   at System.Windows.Threading.Dispatcher.CatchException(Exception e)
   at System.Windows.Threading.Dispatcher.CatchExceptionStatic(Object source, Exception e)
   at System.Windows.Threading.ExceptionWrapper.CatchException(Object source, Exception e, Delegate catchHandler)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

I know it's meaningless to kill all msedgewebview2.exe processes in the reproduce steps. But we guess some other things may cause msedgewebview2.exe processes crashing in our application. We really want this issue to be fixed. So I'd like to know where I can find the msedgewebview2.exe log or crash information. And maybe these logs may help you (or us) to fix this issue.

Thanks.

johna-ms commented 3 years ago

Hi I'm investigating this and have a similar repro going. This is a bug in the WebView control and we are working on a fix. In the mean time, we have an event you can listen for when the browser crashes with some limited information about the reason for the crash. It's called ProcessFailed

You can listen for this event and remove, nullify, or recreate the control upon the browser process crashing to avoid the COM exception you're seeing. The crash is coming from the implementation for the control so you'll have to dispose of it somehow.

Regarding logging, it is possible to enable logging for the browser but not from the control. You'd need to be creating the WebView from the Core APIs and passing in the command line argument "--enable-logging=stderr --v=1 > log.txt 2>&1" to the CoreWebView2 Environment via the CoreWebView2EnvironmentOptions. log.txt should be the path to your logfile

It may be more effort than it's worth. If you decide to go that route I'm here to support that, but hopefully we have a fix out soon and you don't need to redo how you create WebView in order to enable logging. Please also let me know if you have questions about the workaround.

timothymcgrath commented 3 years ago

I'm experiencing a similar issue to this. Because the browser is in a tab control with other WPF controls, it causes all the WPF tabs to also stop rendering until the app is restarted. I'm not sure what is causing the edge processes to crash, but it completely breaks our app when it occurs.

Is there an ETA on this fix? I need to decide if we need to revert to the original browser control for now or if we can wait for this fix.

champnic commented 3 years ago

@timothymcgrath Can you try using the CoreWebView2.ProcessFailed event John mentioned above? Normally we see crashes in a renderer process, which is per WebView2 and shouldn't affect other WebView2 controls. However, if the browser crash is in the main browser process that will take down all WebView2 that share that browser process, which is any WebView2 using the same User Data Folder (if you aren't specifying a specific UDF then the default will be next to your .exe and all of your WV2 controls will share it).

If you are able to find the reproducible steps for the browser crash we'd be interested in trying to investigate the underlying cause further.

timothymcgrath commented 3 years ago

The ProcessFailed event works well. I handle that event, clear the broken WebView out of the visual tree and insert a new one and the app works normally again.

I added some more logging, I'll try to figure out what is causing the process crash.

Thanks for the advice!

timothymcgrath commented 3 years ago

Here is a link to an example of the changes I had to make if anyone is interested: https://github.com/timothymcgrath/wpf-web-view-fixes

lggroapa commented 3 years ago

Any update on this issue? Reproduced on 1.0.705.50. Randomly crashed when calling show on the form control.

Fatal exception of type: System.Runtime.InteropServices.COMException System.Runtime.InteropServices.COMException (0x8007139F): The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F) at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_IsVisible(Int32 IsVisible) at Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_IsVisible(Boolean value) at Microsoft.Web.WebView2.WinForms.WebView2.OnVisibleChanged(EventArgs e) at System.Windows.Forms.Control.OnParentVisibleChanged(EventArgs e) at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e) at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e) at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

champnic commented 3 years ago

@lggroapa Can you try with the latest SDK to see if your issue persists? Are you handling the ProcessFailed event as mentioned earlier in this thread, and see it getting hit?

johna-ms commented 3 years ago

Also what is your runtime version? This should be fixed in Edge Dev version ie. 90.* It will be in the runtime by mid-April. Please let us know if the workaround works for you

lggroapa commented 3 years ago

Thank you for the response,

I was using the 88.0.705.74 runtime with fixed version.

I have switched now to the new SDK 1.0.774.44 and runtime 89.0.774.48. Are there any chances that this is fixed by this combination?

This issue did reproduce very rarely (once in thousands of show calls) I will continue to monitor it.

Other than waiting for mid-April is there any workaround for this that does not involve restarting the process? Like waiting a fixed amount of time between different operations to avoid this threading (I suspect) issue?

johna-ms commented 3 years ago

The issue is expected to repro with your 1.0.744.44 and 89.0.774.48 combo. The issue should be fixed with runtime 90 and above. You can use Edge Canary or Dev as the runtime to confirm.

Our understanding is this bug is a result of the browser process failing so I cannot think of a workaround that would not involve restarting the process/reinitializing the control. Are you encountering this exception with no browser crash?

lggroapa commented 3 years ago

It always crashes the browser, I was just hoping for a miraculous workaround :) I will also try out the 90.

johna-ms commented 3 years ago

Want to clarify -- are you saying that there is some issue in webview that always crashes the browser process? Or confirming that this exception 'always' happens upon browser process crash?

lggroapa commented 3 years ago

The latter, the exception always happens upon a browser process crash.

I do not know why the browser is crashing.

johna-ms commented 3 years ago

Ok got it. Yes the browser may crash for a variety of reasons, good to be clear that this isn't a new bug. Please let me know how it goes trying out 90 :)

champnic commented 3 years ago

We've improved the experience and made the exception message clearer when trying to use the control when the browser process has crashed. This should be in the 1.0.824-prerelease SDK. Please give it a try and let us know if you run into any further issues. Thanks!

dujianxin commented 3 years ago

I have tried out the runtime v90.0.818.46, and it crashes again.

2021-04-25 12:43:49.516 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON reason)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.MoveFocus(CoreWebView2MoveFocusReason reason)
   在 Microsoft.Web.WebView2.Wpf.WebView2.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 System.Windows.Interop.HwndHost.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
2021-04-25 12:43:51.567 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   在 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   在 System.Windows.Interop.HwndHost.UpdateWindowPos()
   在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
2021-04-25 12:43:51.567 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   在 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   在 System.Windows.Interop.HwndHost.UpdateWindowPos()
   在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
2021-04-25 12:43:51.567 [AppLogger] [1] ERROR - [APP]Application_DispatcherUnhandledException. exception:System.Runtime.InteropServices.COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.set_Bounds(tagRECT Bounds)
   在 Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_Bounds(Rectangle value)
   在 Microsoft.Web.WebView2.Wpf.WebView2.OnWindowPositionChanged(Rect rcBoundingBox)
   在 System.Windows.Interop.HwndHost.UpdateWindowPos()
   在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   在 System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
champnic commented 3 years ago

@dujianxin Are you listening for the ProcessFailed event and recreating the WebView2 control?

johna-ms commented 3 years ago

Hi, sorry to be misleading -- I was mistaken in my comments above regarding the fix showing up in the runtime. As Nic mentioned, this fix should be in the SDK. The SDK and runtime versions are related so I meant the relevant fixes would appear in SDK releases aligned with the runtime version 90. In this case that would be SDK version 824-prerelease or 818-release

dujianxin commented 3 years ago

sorry, I don't listen for the ProcessFailed event now. I thought the bug had been fixed when the runtime update to 90+. I will change my code and add the ProcessFailed event handler. Thank you!

champnic commented 3 years ago

@dujianxin Gotcha! There will always be browser process crashes (and we will always try to fix the worst ones), so you should always be listening for the ProcessFailed event. We're going to be publishing a "Best Practices" doc soon that includes this info.

Velok commented 3 years ago

Hello @champnic : I would like to ask if the "Best Practices" doc has been published? Thanks.

champnic commented 3 years ago

@Velok Yes, it's been added: https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/developer-guide

Although I just checked and it looks like it's missing that info, so I've just opened a bug to update it (AB#33392905). Thanks!