Repository: microsoft/terminal (language: cpp)

Issue:
WindowsTerminal!AppHost::_WindowInitializedHandler$_ResumeCoro$1+0x2cc -- access violation at Windows startup post Windows update restart
### Windows Terminal version

1.18.2310.9002

### Windows build number

_No response_

### Other Software

_No response_

### Steps to reproduce

Unknown.

### Expected Behavior

_No response_

### Actual Behavior

I have ProcDump set as an AeDebug debugger (/ma full dumps). After a restarting from a Windows update, a WindowsTerminal dump was generated. Here are some relevant details. Please let me know if you need any more details.
I can also supply the dump if you tell me where to send it.

```
0:021> .lastevent
Last event: 3944.3b10: Access violation - code c0000005 (first/second chance not available)
  debugger time: Fri Oct 27 15:02:12.989 2023 (UTC + 9:00)
0:021> .exr -1
ExceptionAddress: 00007ff6ed1eed5c (WindowsTerminal!AppHost::_WindowInitializedHandler$_ResumeCoro$1+0x00000000000002cc)
   ExceptionCode: c0000005 (Access violation)
  ExceptionFlags: 00000001
NumberParameters: 2
   Parameter[0]: 0000000000000000
   Parameter[1]: 0000000000000008
Attempt to read from address 0000000000000008
0:021> .ecxr
rax=0000017d8046a050 rbx=0000002a2deff1b0 rcx=0000000000000000
rdx=0000000000000005 rsi=0000000000000000 rdi=0000002a2defecc0
rip=00007ff6ed1eed5c rsp=0000002a2deff280 rbp=0000017d806f3680
 r8=0000017d8236e468  r9=0000017d8236e428 r10=00000fff47775dc4
r11=0100040010010010 r12=0000000000000000 r13=000000000000001c
r14=00007ffa955b70b0 r15=0000000000000000
iopl=0         nv up ei pl nz na po nc
cs=0033  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00010204
WindowsTerminal!AppHost::_WindowInitializedHandler$_ResumeCoro$1+0x2cc:
00007ff6`ed1eed5c 488b4908        mov     rcx,qword ptr [rcx+8] ds:00000000`00000008=????????????????

0:021> k
  *** Stack trace for last set context - .thread/.cxr resets it
 # Child-SP          RetAddr           Call Site
00 0000002a`2deff280 00007ff6`ed1e5370 WindowsTerminal!AppHost::_WindowInitializedHandler$_ResumeCoro$1+0x2cc [C:\__w\1\s\src\cascadia\WindowsTerminal\AppHost.cpp @ 1333] 
01 (Inline Function) --------`-------- WindowsTerminal!std::coroutine_handle<void>::resume+0xc [C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.37.32822\include\coroutine @ 85] 
02 (Inline Function) --------`-------- WindowsTerminal!wil::details::dispatcher_handler::Complete+0x1c [C:\__w\1\s\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\include\wil\cppwinrt_helpers.h @ 87] 
03 (Inline Function) --------`-------- WindowsTerminal!wil::details::dispatcher_handler::operator()+0x1c [C:\__w\1\s\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\include\wil\cppwinrt_helpers.h @ 81] 
04 0000002a`2deff350 00007ffa`8abf25e1 WindowsTerminal!winrt::impl::delegate<winrt::Windows::System::DispatcherQueueHandler,wil::details::dispatcher_handler>::Invoke+0x20 [C:\__w\1\s\src\cascadia\WindowsTerminal\Generated Files\winrt\Windows.system.h @ 1623] 
05 0000002a`2deff380 00007ffa`955b70c6 Windows_UI!Microsoft::WRL::Details::DelegateArgTraits<long (__cdecl Windows::System::IDispatcherQueueHandler::*)(void)>::DelegateInvokeHelper<Microsoft::WRL::Implements<Microsoft::WRL::RuntimeClassFlags<2>,Windows::System::IDispatcherQueueHandler,Microsoft::WRL::FtmBase>,<lambda_59517943c03487243f9bea31c6c1a784>,-1>::Invoke+0x71
06 0000002a`2deff3b0 00007ffa`9558529b CoreMessaging!Windows::System::DispatcherQueue::DeferInvokeCallback+0x16
07 0000002a`2deff3e0 00007ffa`95579d34 CoreMessaging!Microsoft__CoreUI__Dispatch__TimeoutHandler$CallbackThunk+0x11b
08 0000002a`2deff460 00007ffa`955795de CoreMessaging!Microsoft::CoreUI::Dispatch::DeferredCall::Callback_Dispatch+0x2c4
09 0000002a`2deff520 00007ffa`95589d84 CoreMessaging!Microsoft::CoreUI::Dispatch::DeferredCallDispatcher::Callback_OnDispatch+0x15e
0a 0000002a`2deff590 00007ffa`95588d36 CoreMessaging!Microsoft::CoreUI::Dispatch::EventLoop::Callback_RunCoreLoop+0xc04
0b 0000002a`2deff6b0 00007ffa`95586f71 CoreMessaging!Microsoft::CoreUI::Dispatch::UserAdapter::OnUserDispatch+0x1d6
0c 0000002a`2deff7a0 00007ffa`95586d9c CoreMessaging!Microsoft::CoreUI::Dispatch::UserAdapter_DoWork+0xf1
0d 0000002a`2deff880 00007ffa`9a63e858 CoreMessaging!Microsoft::CoreUI::Dispatch::UserAdapter_WindowProc+0xfc
0e 0000002a`2deff900 00007ffa`9a63e3dc user32!UserCallWinProcCheckWow+0x2f8
0f 0000002a`2deffa90 00007ffa`9a650c93 user32!DispatchClientMessage+0x9c
10 0000002a`2deffaf0 00007ffa`9a9b0e64 user32!_fnDWORD+0x33
11 0000002a`2deffb50 00007ffa`98401064 ntdll!KiUserCallbackDispatcherContinue
12 0000002a`2deffbd8 00007ffa`9a63a5c3 win32u!NtUserPeekMessage+0x14
13 0000002a`2deffbe0 00007ffa`9a63a523 user32!_PeekMessage+0x43
14 0000002a`2deffc50 00007ff6`ed21ec85 user32!PeekMessageW+0x143
15 0000002a`2deffcc0 00007ff6`ed21ec0a WindowsTerminal!WindowThread::_pumpRemainingXamlMessages+0x4d [C:\__w\1\s\src\cascadia\WindowsTerminal\WindowThread.cpp @ 52] 
16 0000002a`2deffd40 00007ff6`ed1d4762 WindowsTerminal!WindowThread::Refrigerate+0x5a [C:\__w\1\s\src\cascadia\WindowsTerminal\WindowThread.cpp @ 151] 
17 0000002a`2deffd80 00007ff6`ed1e59de WindowsTerminal!`WindowEmperor::_createNewWindowThread'::`2'::<lambda_1>::operator()+0x23a [C:\__w\1\s\src\cascadia\WindowsTerminal\WindowEmperor.cpp @ 252] 
18 (Inline Function) --------`-------- WindowsTerminal!std::invoke+0x5 [C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.37.32822\include\type_traits @ 1752] 
19 0000002a`2deffe30 00007ffa`98481bb2 WindowsTerminal!std::thread::_Invoke<std::tuple<`WindowEmperor::_createNewWindowThread'::`2'::<lambda_1> >,0>+0xe [C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.37.32822\include\thread @ 56] 
1a 0000002a`2deffe60 00007ffa`990e7344 ucrtbase!thread_start<unsigned int (__cdecl*)(void *),1>+0x42
1b 0000002a`2deffe90 00007ffa`9a9626b1 kernel32!BaseThreadInitThunk+0x14
1c 0000002a`2deffec0 00000000`00000000 ntdll!RtlUserThreadStart+0x21

0:021> lm v m WindowsTerminal
Browse full module list
start             end                 module name
00007ff6`ed1d0000 00007ff6`ed282000   WindowsTerminal   (private pdb symbols)  c:\symbols\cache\WindowsTerminal.pdb\EDF2737A9E7C4F5DB889021C6A7F7E1D1\WindowsTerminal.pdb
    Loaded symbol image file: WindowsTerminal.exe
    Image path: C:\Program Files\WindowsApps\Microsoft.WindowsTerminal_1.18.2822.0_x64__8wekyb3d8bbwe\WindowsTerminal.exe
    Image name: WindowsTerminal.exe
    Browse all global symbols  functions  data
    Timestamp:        Tue Oct 10 08:21:51 2023 (65248B0F)
    CheckSum:         000B806A
    ImageSize:        000B2000
    File version:     1.18.2310.9002
    Product version:  1.18.2310.9002
    File flags:       0 (Mask 3F)
    File OS:          4 Unknown Win32
    File type:        1.0 App
    File date:        00000000.00000000
    Translations:     0000.04b0
    Information from resource tables:

```
Best regards.


Discussion / hints:
Looks like this is MSFT:46763065 internally. Dumps show this repros on 1.19 too. 

I think this was #16061 which had a theoretical fix in #16065. Looks like you're on Terminal Stable v1.18.2822.0, and https://github.com/microsoft/terminal/releases/tag/v1.18.2822.0 is supposed to have had that fix in it. Dang. 

I'll try and load up a dump. 
ohp well this is embarrassing

https://github.com/microsoft/terminal/blob/d0d3039963cedc9f3de38c3772a672e82955fa86/src/cascadia/WindowsTerminal/AppHost.cpp#L1433-L1445

I never actually checked if we _still had a `_window`_. We're alive, yay! But we're still in the middle of refrigerating. So, there's no HWND anymore 🤦 

Fix patch:
diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp
index 5af3a80d50a..7cae9959998 100644
--- a/src/cascadia/WindowsTerminal/AppHost.cpp
+++ b/src/cascadia/WindowsTerminal/AppHost.cpp
@@ -979,7 +979,8 @@ winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> AppHost::_GetWindowL
     co_await wil::resume_foreground(_windowLogic.GetRoot().Dispatcher());
 
     const auto strongThis = weakThis.lock();
-    if (!strongThis)
+    // GH #16235: If we don't have a window logic, we're already refrigerating, and won't have our _window either.
+    if (!strongThis || _windowLogic == nullptr)
     {
         co_return layoutJson;
     }
@@ -1267,7 +1268,8 @@ winrt::fire_and_forget AppHost::_QuitRequested(const winrt::Windows::Foundation:
     co_await wil::resume_foreground(_windowLogic.GetRoot().Dispatcher());
 
     const auto strongThis = weakThis.lock();
-    if (!strongThis)
+    // GH #16235: If we don't have a window logic, we're already refrigerating, and won't have our _window either.
+    if (!strongThis || _windowLogic == nullptr)
     {
         co_return;
     }
@@ -1431,7 +1433,7 @@ winrt::fire_and_forget AppHost::_WindowInitializedHandler(const winrt::Windows::
 
     // If we're gone on the other side of this co_await, well, that's fine. Just bail.
     const auto strongThis = weakThis.lock();
-    if (!strongThis)
+    if (!strongThis || _window == nullptr)
     {
         co_return;
     }


Test patch: