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

App Crash on WebView2 background finalize #955

Open
timothymcgrath opened this issue Feb 18, 2021 · 11 comments
Open

App Crash on WebView2 background finalize #955

timothymcgrath opened this issue Feb 18, 2021 · 11 comments
Assignees

Comments

@timothymcgrath
Copy link

timothymcgrath commented Feb 18, 2021

Description
I have a WPF app using .NET Framework 4.7.2, that contains multiple WebView2 instances. Intermittently, we get an app crash during the finalize of the WebView2 control
Here is the error from Windows Event Viewer.

Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
at System.Windows.Threading.Dispatcher.VerifyAccess()
at Microsoft.Web.WebView2.Wpf.WebView2.get_CoreWebView2()
at Microsoft.Web.WebView2.Wpf.WebView2.Dispose(Boolean)
at System.Windows.Interop.HwndHost.Finalize()

I can't find any way to prevent this from happening.

Version
WebView2 1.0.721-prerelease
.NET Framework 4.7.2

Repro Steps
I believe it eventually happens after a user opens a window with a WebView2 and then closes it. We dispose of the WebView2 instance, but a little while later, the app crashes (during the finalize process).

Screenshots

Additional context

AB#31292515

@timothymcgrath timothymcgrath added the bug Something isn't working label Feb 18, 2021
@jozefizso
Copy link

What thread do you dispose the WebView2 instance on?

@timothymcgrath
Copy link
Author

It is on the UI thread when I call .Dispose(), but it seems like when the Finalize() rolls through it is on a different thread?

Here are the two places I dispose of the web control -- pardon the VB, it's an old app :) --
First one handles an issue with the WebView process crashing (posted in this Github project a few weeks ago):

           ' When the WebView process crashes, throw away this WebView and replace it with a new one.
           ' This is an issue that the WebView team is working on.
           ' https://github.com/MicrosoftEdge/WebView2Feedback/issues/774
           Dispatcher.BeginInvoke(Async Sub()
                                      Try
                                          If _host.Children.Count > 0 Then
                                              Try
                                                  _host.Children.Clear()
                                              Catch
                                              End Try
                                          End If

                                          ' Dispose here because if the Finalize calls Dispose, it can crash the app due to a non-UI thread call.
                                          Try
                                              _browser.Dispose()
                                          Catch
                                          End Try

The other one is for when a window with a browser closes:

                AddHandler _browser.Unloaded,
                    Sub()
                        Try
                            _browser.Dispose()
                        Catch
                        End Try
                    End Sub

@champnic champnic removed the bug Something isn't working label Mar 2, 2021
@champnic
Copy link
Member

champnic commented Mar 2, 2021

Based on that stack it looks like when the finalizer is calling Dispose() from the background thread Dispose() hasn't yet been called on the WebView2. Can you put breakpoints on where you are disposing of the WebView2 and verify that it's getting called before the finalizer?

@champnic champnic self-assigned this Mar 2, 2021
@timothymcgrath
Copy link
Author

I checked it and I do call Dispose() from the ProcessFailedHandler, but the _browser.Dispose() throws with:
{"The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)"}

So, I'm guessing the .Dispose() never actually happens, so the Finalizer picks it up later and then fails and it crashes my entire app.

I can't figure out any way to prevent this app crash.

I believe this all stems from the original problem of handling the process crashes:
#774 (comment)

@champnic
Copy link
Member

champnic commented Mar 5, 2021

Ya that seems related to me. We have a fix for #774 that improves this experience and I think it should fix this as well. It should make Dispose() not cause an exception when called after the browser has crashed. I'll respond to this thread when the fix is available to try in the next prerelease package.

@timothymcgrath
Copy link
Author

Do you have a timeline on this?

If I keep a list of references to my crashed WebView2 objects that error in disposal, would that prevent the Finalizer from running and crashing the app? Until your fix goes live.

@champnic
Copy link
Member

champnic commented Mar 9, 2021

The next prerelease package (that will include this fix) will go out soon, within the next week or two.

That workaround sounds reasonable to try.

@champnic
Copy link
Member

This should be fixed in the 1.0.824-prerelease SDK. Please give it a try and let us know if you run into any further issues. Thanks!

@rhickmott
Copy link

This seems to still be the case in 1.0.1774.30

I can call a dispose on Unloaded and then quit the application and it works fine but just closing the app or calling an Application.Exit() with an Active WPF Window Open (and a webview active) causes the application to immediately crash out

Exception Occured - Unhandled Exception
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{4D00C0D1-9434-4EB6-8078-8697A560334F}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.get_CoreWebView2()
at Microsoft.Web.WebView2.Core.CoreWebView2Controller.get_CoreWebView2()
at Microsoft.Web.WebView2.Wpf.WebView2.Uninitialize(Boolean browserCrashed)
at Microsoft.Web.WebView2.Wpf.WebView2.Dispose(Boolean disposing)
at System.Windows.Interop.HwndHost.Finalize()

I can't capture the Finalize method (by design) to gracefully dispose the control.
.NET Version is 4.5

@Daveric
Copy link

Daveric commented Dec 18, 2024

It seems that the error still occurs with the current version: 1.0.2903.40
Here is the snippet from Event Viewer:

Provider: .NET Runtime
Application: MyApp.exe
Framework version: v4.0.30319
Description: The process terminated due to an unhandled exception.
Exception information: System.InvalidCastException at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(System.Object, IntPtr, IntPtr ByRef, Boolean ByRef) at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller.get_CoreWebView2() at Microsoft.Web.WebView2.Core.CoreWebView2Controller.get_CoreWebView2() at Microsoft.Web.WebView2.Wpf.WebView2Base.Uninitialize(Boolean) at Microsoft.Web.WebView2.Wpf.WebView2.Dispose(Boolean) bei System.Windows.Interop.HwndHost.Finalize()

Framework: .NET Framework 4.8, WPF
OS: Windows 11 24H2

Calling Dispose() on Unloaded works but it shouldn't be necessary, why do we need manually to Dispose this?

@rhickmott
Copy link

Looks like they closed this so it would have to be re-opened as a new bug.

@sivMSFT sivMSFT reopened this Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants