Repository: microsoft/terminal (language: cpp)

Issue:
Crash when choosing 'Close all other panes'
### Windows Terminal version

1.22.2362.0

### Windows build number

10.0.27699.0

### Other Software

_No response_

### Steps to reproduce

1.	Open Windows Terminal.
2.	Split panes so there are at least two panes.
3.	Ctrl+Shift+P and choose ‘Close all other panes’


### Expected Behavior

No crash.

### Actual Behavior

Terminal crashes with an access violation:

```
(3c78.4aa0): Access violation - code c0000005 (first/second chance not available)
For analysis of this file, run !analyze -v
TerminalApp!winrt::impl::consume_TerminalApp_IPaneContent<winrt::TerminalApp::IPaneContent>::TaskbarProgress+0x34 [inlined in TerminalApp!Pane::CollectTaskbarStates+0x82]:
00007ffb`45483032 488b01          mov     rax,qword ptr [rcx] ds:00000000`00000000=????????????????
```

Here is the call stack:

```
TerminalApp!winrt::impl::consume_TerminalApp_IPaneContent<winrt::TerminalApp::IPaneContent>::TaskbarProgress
TerminalApp!Pane::CollectTaskbarStates
TerminalApp!Pane::CollectTaskbarStates
TerminalApp!winrt::TerminalApp::implementation::TerminalTab::GetCombinedTaskbarState
TerminalApp!winrt::TerminalApp::implementation::TerminalPage::TaskbarState
TerminalApp!winrt::TerminalApp::implementation::TerminalWindow::TaskbarState
TerminalApp!winrt::impl::produce<winrt::TerminalApp::implementation::TerminalWindow,winrt::TerminalApp::ITerminalWindow>::get_TaskbarState
WindowsTerminal
WindowsTerminal
TerminalApp!winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Foundation::IInspectable,winrt::Windows::Foundation::IInspectable>::operator()
TerminalApp!wil::details::dispatcher_handler::operator()
TerminalApp!winrt::impl::delegate<winrt::Windows::UI::Core::DispatchedHandler,wil::details::dispatcher_handler>::Invoke
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_980f345bc6e4ac8a906323c24a5fe9b1>,-1>::Invoke
CoreMessaging!Windows::System::DispatcherQueue::DeferInvokeCallback
CoreMessaging!CFlat::SehSafe::Execute<<lambda_654db17c35df07198786f0867aa10de6> >
CoreMessaging!Microsoft::CoreUI::ActionCallback::ImportAdapter$
CoreMessaging!Microsoft::CoreUI::Dispatch::DeferredCall::Callback_Dispatch
CoreMessaging!Microsoft::CoreUI::Dispatch::DeferredCallDispatcher::Callback_OnDispatch
CoreMessaging!Microsoft::CoreUI::Dispatch::Dispatcher::Callback_DispatchLoop
CoreMessaging!Microsoft::CoreUI::Dispatch::EventLoop::Callback_RunCoreLoop
user32!_fnDWORD
ntdll!KiUserCallbackDispatcherContinue
win32u!NtUserGetMessage
user32!GetMessageW
WindowsTerminal
WindowsTerminal
WindowsTerminal
ucrtbase!thread_start<unsigned int (__cdecl*)(void *),1>
KERNEL32!BaseThreadInitThunk
ntdll!RtlUserThreadStart
```


Discussion / hints:
I suspect this is the same underlying bug as #17869. If I try and reproduce that one in the debugger I get a very similar stack trace to this crash.
This must've regressed in #17750. Since we never wrote down what made it originally crash, I guessed it happened by simply closing panes, but this shows it was via the "Close all other panes" action. Not 100% sure how to best fix it. There were two simple options and neither worked. The correct approach would be to not tie the model (binary tree of panes) to the animations but fixing that is not an option for a hotfix.

Fix patch:
diff --git a/src/cascadia/TerminalApp/Pane.cpp b/src/cascadia/TerminalApp/Pane.cpp
index a210b701528..dc235b06024 100644
--- a/src/cascadia/TerminalApp/Pane.cpp
+++ b/src/cascadia/TerminalApp/Pane.cpp
@@ -2956,13 +2956,13 @@ bool Pane::ContainsReadOnly() const
 // - <none>
 void Pane::CollectTaskbarStates(std::vector<winrt::TerminalApp::TaskbarState>& states)
 {
-    if (_IsLeaf())
+    if (_content)
     {
         auto tbState{ winrt::make<winrt::TerminalApp::implementation::TaskbarState>(_content.TaskbarState(),
                                                                                     _content.TaskbarProgress()) };
         states.push_back(tbState);
     }
-    else
+    else if (_firstChild && _secondChild)
     {
         _firstChild->CollectTaskbarStates(states);
         _secondChild->CollectTaskbarStates(states);


Test patch: