MicrosoftEdge / WebView2Feedback

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

Uncaught exceptions in WebView2 event handlers don't trigger Application.ThreadException #1201

Open emimvi opened 3 years ago

emimvi commented 3 years ago

Description

When initialized with a Source, unhandled exceptions in WebView2 event handlers never trigger Application.ThreadException. Tested using WebView2 specific NavigationStarting event, and WinForms Control specific KeyDown. If Source is not set and the WebView2 is uninitialized, KeyDown events on the WebView properly triggers the ThreadException event.

Version SDK: 1.0.774.44 Runtime: Evergreen 90.0.818.39 Framework: WinForms OS: Win 10 Version 10.0.19041

Repro Steps

using System;
using System.Windows.Forms;
using Microsoft.Web.WebView2.WinForms;

namespace WinFormsApp1
{
    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.ThreadException += ExceptionHandler;
            AppDomain.CurrentDomain.UnhandledException += ExceptionHandler;

            var form = new Form();
            WebView2 webView = new WebView2();
            webView.Source = new Uri("http://www.example.org"); // If not set, ExceptionHandler is fired on exception.

            webView.Dock = DockStyle.Fill;
            webView.KeyDown += ThrowingEventHandler;
            //webView.NavigationStarting += ThrowingEventHandler;

            form.Controls.Add(webView);

            Application.Run(form);
        }

        static void ThrowingEventHandler(object sender, object eventArgs)
        {
            System.Diagnostics.Trace.WriteLine("Event fired from " + sender.GetType());
            throw new Exception("Test");
        }

        static void ExceptionHandler(object sender, object args)
        {
            System.Diagnostics.Trace.WriteLine("Unhandled exception!");
            Application.Exit();
        }
    }
}

Press some key to trigger, eg. F11:

Expected output: Event fired from Microsoft.Web.WebView2.WinForms.WebView2 Unhandled exception! Application exits

Actual output: Event fired from Microsoft.Web.WebView2.WinForms.WebView2

Additional context I don't know if this is expected behavior, or whether it relates to the interprocess communication more than the actual webview. I tried looking for some documentation regarding this behavior but without luck.

AB#32726576

champnic commented 3 years ago

Hey @emimvi - thanks for the bug report! I think I may be missing some context here. What is the stack you expect to see when hitting F11 in the initialized WebView2 control? Have you put a breakpoint in ThrowingEventHandler to verify that the F11-initialized-WebView2 scenario is getting hit?

emimvi commented 3 years ago

@champnic I'd like to have global exception handler for unhandled exceptions (bugs), that can eg. log the error and exit the application. In the example, there is an uncaught exception in the event handler, that is always hit when the event is triggered. The inconsistency I'm experiencing is what happens after the exception is thrown.

For the initialized webview in the example, event is triggered, the event fires, prints the trace message, the exception is thrown, and then nothing happens.

For the uninitialized case (and other builtin winforms controls), the event fires, prints the trace message, exception is thrown, and then the ExceptionHandler is triggered, allowing me to log the error and quit the application.

My issue is that to me, it appears as if the initialized WebView swallows the unhandled exception somehow, which makes me unable to log and exit the application.

champnic commented 3 years ago

Sounds good - I have opened this bug on our backlog to take a closer look. Thanks!