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

[Problem/Bug]: InvalidOperationException: CoreWebView2 members cannot be accessed after the WebView2 control is disposed. #4272

Closed
cuiliang opened this issue Jan 1, 2024 · 20 comments
Assignees
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@cuiliang
Copy link

cuiliang commented Jan 1, 2024

What happened?

Got some automatic exception report from multiple users.
The stacktracke shows it does not involve any user code.

InvalidOperationException: CoreWebView2 members cannot be accessed after the WebView2 control is disposed.
StackTrace:
   在 Microsoft.Web.WebView2.Core.CoreWebView2.get_Profile()
   在 Microsoft.Web.WebView2.Wpf.WebView2.Uninitialize(Boolean browserCrashed)
   在 Microsoft.Web.WebView2.Wpf.WebView2.Dispose(Boolean disposing)
   在 System.Windows.Interop.HwndHost.WeakEventDispatcherShutdown.OnShutdownFinished(Object sender, EventArgs e)
   在 System.EventHandler.Invoke(Object sender, EventArgs e)
   在 System.Windows.Threading.Dispatcher.ShutdownImplInSecurityContext(Object state)
   在 MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Windows.Threading.Dispatcher.ShutdownImpl()
   在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   在 System.Windows.Application.RunDispatcher(Object ignore)
   在 System.Windows.Application.RunInternal(Window window)
   在 Quicker.App.Main()


// inner exception
COMException: 组或资源的状态不是执行请求操作的正确状态。 (异常来自 HRESULT:0x8007139F)    // translation: The state of the group or resource is not the correct state to perform the requested operation.
StackTrace:
   在 Microsoft.Web.WebView2.Core.Raw.ICoreWebView2_13.get_Profile()
   在 Microsoft.Web.WebView2.Core.CoreWebView2.get_Profile()

Importance

Important. My app's user experience is significantly compromised.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

No response

SDK Version

1.0.2210.55

Framework

WPF

Operating System

Windows 10, Windows 11

OS Version

10.0.22000.0, 10.0.18363.0, 10.0.22631.0 ...

Repro steps

Sorry, It's automatic report, so I dont have this information.

Repros in Edge Browser

No

Regression

Don't know

Last working version (if regression)

No response

AB#48471924

@cuiliang cuiliang added the bug Something isn't working label Jan 1, 2024
@victorhuangwq
Copy link
Collaborator

This looks like a similar issue to #4206. It would be easier to debug if you can obtain a trace for us as well.

@victorhuangwq victorhuangwq self-assigned this Jan 2, 2024
@cuiliang
Copy link
Author

cuiliang commented Jan 2, 2024

@victorhuangwq Sorry I do not have this infomration.
But the stacktrace is obvious, it seems calling Profile property in under the Dispose() code path, maybe it is easy to find the problem.

@victorhuangwq
Copy link
Collaborator

victorhuangwq commented Jan 3, 2024

Based on #4206, it is very likely that the cause of the error is not something to do with WebView2, but rather the application using WebView2. As per the description of the stack trace, it is that the WebView2 control has been disposed, and yet the CoreWebView2 members are still being used by the application.

Without a trace we can't figure out what's the cause for you.

@cuiliang
Copy link
Author

cuiliang commented Jan 4, 2024

@victorhuangwq
Seems related manual dispose WebView2 control.
I want to release webview2 in the window unload event like this:

private void OnUnloaded(object sender, RoutedEventArgs e)
{
    if (webView == null)
    {
        return;
    }

    webView.NavigationCompleted -= WebViewOnNavigationCompleted;
    if (webView.CoreWebView2 != null)
    {
        webView.CoreWebView2.ProcessFailed -= WebView_ProcessFailed;
        webView.CoreWebView2.DocumentTitleChanged -= WebView_DocumentTitleChanged;
    }
    
    webView.Dispose();
    webView = null;
}

Maybe the webview2 is disposing at another code path?
Is this incorrect to release webview2 resource?

Seems related to the following issues:
#3792
#4263

Thank you.

@mikael-ekholm
Copy link

mikael-ekholm commented Jan 5, 2024

I'm having the same problem while using WebView2 with WPF. The problem occurs when I have a WebView2 inside a Window whose Owner is the WPF application MainWindow. When the MainWindow is closed I get the same error and stack trace as @cuiliang showed above. The error doesn't occur if the Owner of the webview window is not set.

Here is a sample wpf project that reproduces the error:
WebHelpOwnerCloseTest.zip

To reproduce the error, debug the application, click the button on the MainWindow (this opens the window that contains a WebView2) and then close the MainWindow.

@champnic champnic added the tracked We are tracking this work internally. label Jan 11, 2024
@champnic
Copy link
Member

I've opened a bug for this on our backlog and am working on a fix now.

@pdolanjski
Copy link

@champnic we're seeing an issue with this in our production app that is causing a crash. Could you please provide on update on an ETA for a fix?

@champnic
Copy link
Member

Hey @pdolanjski - the fix is almost in and should be in the next prerelease SDK, which should ship around the end of this month.

@benhbell
Copy link

benhbell commented Feb 5, 2024

Since my ticket is linked to in this thread, is there any chance that this bug fix will resolve this issue? Can I know which pre-release build # I should look out for to test?

@champnic
Copy link
Member

champnic commented Feb 6, 2024

@pdolanjski @benhbell It was just released recently in a prerelease SDK - can you test your scenarios with 1.0.2357-prerelease SDK and let me know if you run into the failure still, or other problems?
https://www.nuget.org/packages/Microsoft.Web.WebView2/1.0.2357-prerelease

@benhbell
Copy link

benhbell commented Feb 7, 2024

Hi! I have added the pre-release to my project in Nuget

Screenshot 2024-02-07 at 4 41 34 PM

but am running into the following problems trying to evaluate if it fixes the issue.

  1. I was getting the issue referenced here when building (i set it to the answer to get around the build issue)
    return $"PollEvPresenter/{Version(VersionFormat.Clean)} NoRedirect/{Version(VersionFormat.Clean)} Office/{OfficeVersion} PowerPoint/{OfficeVersion} ({OsIdentifier}) WebView2Browser/{CoreWebView2Environment.GetAvailableBrowserVersionString(null)}";
  2. my logs I still see the old runtime running and see an uncaught exception when initializing webview.
    'options.ChannelSearchKind' threw an exception of type 'System.NullReferenceException'
Screenshot 2024-02-07 at 4 45 00 PM Screenshot 2024-02-07 at 4 45 14 PM then when the webview is initialized, I see the older build Screenshot 2024-02-07 at 4 47 43 PM

@champnic
Copy link
Member

champnic commented Feb 8, 2024

When using a prerelease package you'll want to use a prerelease runtime as well (like Edge Canary), generally speaking. Did you make changes that use the new "ChannelSearchKind" property?
https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/set-preview-channel#approaches-to-making-your-app-use-a-specific-browser-channel

@benhbell
Copy link

benhbell commented Feb 8, 2024

you are right! Thanks. Will do that.

@benhbell
Copy link

Hey @champnic
I ran the regex command, but for some reason, when I build and debug I still see the latest non-release running

REG ADD HKLM\Software\Policies\Microsoft\Edge\WebView2\ReleaseChannelPreference /v * /t REG_SZ /d "1"

I still see it loading the .22277 version loaded and the ChannelSearchKind issue.

When you say changes, do you mean that there are more changes in the code needed than just referencing the canary build I installed using nugat?

@champnic
Copy link
Member

Did you install a non-release runtime, like the Edge Canary or Edge Dev browser?
https://www.microsoft.com/en-us/edge/download/insider?form=MA13FJ

@benhbell
Copy link

I did not, but after re-installing, I still see that channelSearchKind error.

I also see an LUID error
Screenshot 2024-02-13 at 12 32 41 PM
Screenshot 2024-02-13 at 12 32 01 PM

@benhbell
Copy link

I managed to get a bit further in testing the pre-release, but not sure I have been successful in confirming that the pre-release is actually runnng (i still replicate the issue)

#4206 (comment)

@pdolanjski
Copy link

@pdolanjski @benhbell It was just released recently in a prerelease SDK - can you test your scenarios with 1.0.2357-prerelease SDK and let me know if you run into the failure still, or other problems? https://www.nuget.org/packages/Microsoft.Web.WebView2/1.0.2357-prerelease

I can confirmed that the issue looks to be addressed on our end when we test the new prerelease version. Thank you.

@benhbell
Copy link

@pdolanjski any chance you can validate or run the steps you took to update to the prerelease version? As well as how you verified it was using that version? Thanks.

@pdolanjski
Copy link

@pdolanjski any chance you can validate or run the steps you took to update to the prerelease version? As well as how you verified it was using that version? Thanks.

Sure. Note that we're bundling WebView2 with our app, not using the system one (and in this case we used 121.0.2277.106).
The symptom that we previously saw was our app crashing when a user did an uninstall (or an update).

From our developer:
Steps to update:

  • Update WebView2 SDK to version 1.0.2357-prerelease
  • Rebuild and repackage our app

Steps to test:

  • Install app
  • Run app
  • Uninstall app -> No crash

@champnic champnic closed this as completed Apr 3, 2024
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