Skip to content

[Problem/Bug]: Certain actions in event handlers may crash the app hosting WebView2 #4573

Description

@equin0x80

What happened?

Consider the following code (Windows Forms), handling WebView2's NavigationStarting event:

private void BrwDoc_NavigationStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs e)
{
e.Cancel = true;
MessageBox.Show("You cannot navigate to " + e.Uri);
}

The intent is to cancel the navigation and show a messsage box to the user.

If you run this code, the entire hosting application will often crash (suddenly closing itself silently, with no error displayed, nor any exception caught in the Visual Studio debugger). It seems more likely to happen if you wait a long time before clicking OK in the message box, or if you switch around between windows, "minimize all", etc., before clicking OK.

Presumably we are not supposed to do long-running work in this event handler, but I don't think it should crash the entire application; and any known crash situations should at least be in the documentation.

By the way, it seems possible to work around the issue by invoking another function in the event handler, like this:
Task.Delay(1).ContinueWith((_) => this.Invoke(() => MyMessageBoxRoutine(e.Uri)));

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

124.0.2478.97

SDK Version

1.0.2478.35

Framework

Winforms

Operating System

Windows 10

OS Version

19045.4412

Repro steps

as described in "What happened" section above.

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions