Repository: microsoft/terminal (language: cpp)

Issue:
Crash in `AppHost::_QuitRequested`
From 1.21 stable crash dumps. 

`891ec77c-0eb3-2890-9aa6-ed3abd60948b`

MSFT:50659255

13.41% of our crashes currently(!)

"INVALID_POINTER_READ_c0000005_WindowsTerminal.exe!winrt::impl::consume_Windows_UI_Xaml_IDependencyObject_winrt::Windows::UI::Xaml::UIElement_::Dispatcher"

https://github.com/microsoft/terminal/blob/e8c15683a726eccf416d28d9159350ecbe555796/src/cascadia/WindowsTerminal/AppHost.cpp#L1201-L1227





Discussion / hints:

Fix patch:
diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp
index 7c538c71cde..86d70a4d81d 100644
--- a/src/cascadia/WindowsTerminal/AppHost.cpp
+++ b/src/cascadia/WindowsTerminal/AppHost.cpp
@@ -1221,11 +1221,23 @@ void AppHost::_IsQuakeWindowChanged(const winrt::Windows::Foundation::IInspectab
 // Raised from our Peasant. We handle by propagating the call to our terminal window.
 void AppHost::_QuitRequested(const winrt::Windows::Foundation::IInspectable&, const winrt::Windows::Foundation::IInspectable&)
 {
+    const auto root = _windowLogic.GetRoot();
+    if (!root)
+    {
+        return;
+    }
+
+    const auto dispatcher = root.Dispatcher();
+    if (!dispatcher)
+    {
+        return;
+    }
+
     // We process the shutdown synchronously here, because otherwise the
     // AutomaticShutdownRequested() logic wouldn't run synchronously either.
     til::latch latch{ 1 };
 
-    _windowLogic.GetRoot().Dispatcher().RunAsync(winrt::Windows::UI::Core::CoreDispatcherPriority::Normal, [&latch, weakThis = weak_from_this()]() {
+    dispatcher.RunAsync(winrt::Windows::UI::Core::CoreDispatcherPriority::Normal, [&latch, weakThis = weak_from_this()]() {
         const auto countDownOnExit = wil::scope_exit([&latch] {
             latch.count_down();
         });


Test patch: