Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing Wpf app with WebView2 gives exception #640

Closed
njannink opened this issue Nov 19, 2020 · 20 comments
Closed

Closing Wpf app with WebView2 gives exception #640

njannink opened this issue Nov 19, 2020 · 20 comments
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@njannink
Copy link

njannink commented Nov 19, 2020

Description
When closing my application I get a COMException - The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F). And I don't seem to be able to catch it anywhere.

Version
SDK: 1.0.674-prerelease
Runtime: 86.0.622.69
Framework: Wpf
OS: Win10

Repro Steps
Close application

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.Wpf.WebView2.UIElement_IsVisibleChanged(Object sender, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyPropertyChangedEventHandler.Invoke(Object sender, DependencyPropertyChangedEventArgs e)
   at System.Windows.UIElement.RaiseDependencyPropertyChanged(EventPrivateKey key, DependencyPropertyChangedEventArgs args)
   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 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 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 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 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 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.PresentationSource.RootChanged(Visual oldRoot, Visual newRoot)
   at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
   at System.Windows.Interop.HwndSource.Dispose(Boolean disposing)
   at System.Windows.Interop.HwndSource.OnHwndDisposed(Object sender, EventArgs args)
   at MS.Win32.HwndWrapper.Dispose(Boolean disposing, Boolean isHwndBeingDestroyed)
   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)

AB#30787253

@njannink njannink added the bug Something isn't working label Nov 19, 2020
@njannink
Copy link
Author

As workaround I now subscribe on the form closing event and explicity dispose the WebView2 control. This should not be needed, because Wpf doesn't really support lifetime management / dispose pattern like WinForms does

@champnic champnic added the tracked We are tracking this work internally. label Nov 21, 2020
@champnic
Copy link
Member

Glad you have a workaround - I've added this bug to our backlog and we'll try to improve the experience. Thanks!

@GeorgHue
Copy link

GeorgHue commented Jan 4, 2021

Observed the same error message. Does a time table already exist to fix it?
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_ParentWindow(IntPtr ParentWindow) at Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_ParentWindow(IntPtr value) at Microsoft.Web.WebView2.WinForms.WebView2.WebView2_HandleDestroyed(Object sender, EventArgs e) at System.Windows.Forms.Control.OnHandleDestroyed(EventArgs e) at System.Windows.Forms.Control.WmDestroy(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

@dqwork
Copy link

dqwork commented Mar 8, 2021

I experience the same error. If a user navigated to 'edge://'and then closes the control, it throws this exception when it tries to dispose the 'host control'

@champnic
Copy link
Member

This type of error can come from a few different sources.

  1. The control is not initialized yet.
  2. The browser/runtime process crashed.
  3. The control is being accessed on a different thread than the one it was created on.
  4. Shutdown path when the browser/runtime process gets cleaned up first.

@GeorgHue I think you are seeing an instance of 4) - we recently put in a fix for this type of issue and it should be available to test in our next prerelease package (hopefully this week or early next).

@dqwork You are seeing an instance of 2) due to a browser crash mentioned in #604. We have a fix for that and it will also be available in the next prerelease package.

@dqwork
Copy link

dqwork commented Mar 11, 2021

@champnic Thanks for the info

@GeorgHue
Copy link

@champnic Also many thanks for your infos! Additional info to my error message above: It happened when logging off my system while WebView2 is in use.

@njannink
Copy link
Author

One of the issues I ran into is that when the WebView2 isn't done yet initializing and you then dispose it it also crashes. This happens, because I have the webview2 on a dockable panel and if I toggle this panel very quickly on and off this happens

@LeifHwang
Copy link

If u called the AddScriptToExecuteOnDocumentCreatedAsync, then u could call the "RemoveScriptToExecuteOnDocumentCreated" before the win closed.

@GeorgHue
Copy link

GeorgHue commented Oct 1, 2021

In WebView 2 runtime 94.0.992.31 I still get "The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)" during logoff. Is there any fix or workaround in sight?

@champnic
Copy link
Member

champnic commented Oct 1, 2021

@GeorgHue Is it still the same stack? Is the WebView2 only being disposed once, and being torn down on the thread it was created on? Notable, the finalizer often calls dispose on a background thread which can cause issues.

@GeorgHue
Copy link

GeorgHue commented Oct 8, 2021

@njannink We assume that if we log off from the system, Outlook is terminated and then it is not able calling shutdown to our addin. Here is our current stack:
************** Exception Text ************** 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_ParentWindow(IntPtr ParentWindow) at Microsoft.Web.WebView2.Core.CoreWebView2Controller.set_ParentWindow(IntPtr value) at Microsoft.Web.WebView2.WinForms.WebView2.WebView2_HandleDestroyed(Object sender, EventArgs e) at System.Windows.Forms.Control.OnHandleDestroyed(EventArgs e) at System.Windows.Forms.Control.WmDestroy(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

@njannink
Copy link
Author

njannink commented Oct 8, 2021

I have no idea what your are trying to ask and what Outlook has to do with it.

@GeorgHue
Copy link

GeorgHue commented Oct 8, 2021

@njannink Sorry for my unclear comment above! 😳 In fact we use WebView2 as Outlook addin viewing component. If we close Outlook, WebView2 is also closed with no problems. But logging of from the system closes Outlook immediately and WebView2 crashes.

@fdzatdtv
Copy link

fdzatdtv commented Nov 9, 2021

I can confirm the same error and callstack as posted by @njannink. Please find attached a zip file with a minimal repro solution. I also can confirm that the work around helps. But this is not practical at all.
Note: WinForms doesn't have the error. See repro solution also including a WinForms project demonstrating same handling with no errors.

Version
SDK: 1.0.1020.30
Runtime: 93.0.961.38
Framework: Wpf
.NET: 5.0
OS: Win10

Repro Steps

  1. Start WV2BugDemo1Wpf
  2. Click on "Open WebView2 dialog" (will open modeless child window)
  3. Close main window without closing child window. => Exception.
    WV2BugDemo1.zip

@Shujee
Copy link

Shujee commented Aug 13, 2022

Has this ever been fixed? This seemed to have been fixed in 1.0.1072.54. I recently upgraded to 1.0.1293.44 and this one has reared its ugly head again. Even reverting back to 1.0.1072.54 doesn't fix it for me anymore.

@champnic
Copy link
Member

champnic commented Aug 15, 2022

Hey @Shujee - No I don't think this had been fixed before. I'm not sure what might have changed in the running of your app, but that would explain why going back to 1.0.1072.54 didn't fix it. However, we did just put in a fix, and it should be available in the next prerelease SDK.

@Shujee
Copy link

Shujee commented Aug 16, 2022

Thanks @champnic . Just in case this helps you guys, I just found that this problem can be avoided by explicitly calling Dispose() on my WebView2 instance. Note that I'm using WebView2 in my custom task pane (using ElementHost). Showing the task pane and then simply pressing Alt + F4 on Word window would trigger this issue, but explicitly calling Dispose() in OnShutdown() fixed the problem for me.

Still looks like a bug to me that I don't have to Dispose() any other WPF controls, but at least I have a workaround now.

@champnic
Copy link
Member

Yup this is a known workaround, though most folks don't want to have to call Dispose() - as you noted, it's not common in WPF. We explicitly need Dispose to be called at some point, because the instance of a WebView2 WPF control is tied to large/multiple browser processes that should be cleaned up when possible.

@champnic
Copy link
Member

This should be fixed in SDK 1.0.1369-prerelease. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

7 participants