Repository: microsoft/terminal (language: cpp)

Issue:
`ControlCore::_refreshSizeUnderLock` crash
### Windows Terminal version

1.11.3471.0

### Windows build number

10.0.22523.0

### Other Software

````
wsl --list -v

  NAME                   STATE           VERSION
* docker-desktop-data    Running         2
  Ubuntu                 Running         2
  docker-desktop         Running         2
````

Typically I'm using multiple panes, one or more tabs, with both Windows 11 Powershell and Ubuntu. For ubuntu:

````
cat /etc/lsb-release

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS"
````

### Steps to reproduce

Day-to-day use of the terminal. Sometimes WSL *and* Windows Powershell tabs/panes, sometimes just one or the other. Somtimes using software like k9s, other times just sitting at a prompt.

### Expected Behavior

Everything to remain responsive.

### Actual Behavior

After some nondescript amount of time, the window hangs, Terminal is unresponsive and must be quit. Windows crash reporting pops up.

If I leave it long enough (say a few hours), it's more likely to have occurred. Sometimes it's during active use though. If I'm using `k9s` (kubernetes tool) on a Windows powershell and press `e` to open Notepad to edit some yaml, recently that pane has then stopped responding to input (but output still updates). Unsure if related.

Feedback Hub link: https://aka.ms/AAfh1tt


Discussion / hints:
Sorry we apparently never triaged this one! If there were dumps in the FH link, then they've unfortunately timed out by now. 

> After some nondescript amount of time, the window hangs

Hmm. Some psychic debugging makes me think this might be #12607. We don't have a solid repro for that. That would only be if you had an nvidia graphics card. 


I think I might have been able to track down a crash that came from your device. One that repros on Terminal 1.13, but I'm not sure we have hits on 1.14... Bucket `9309a65f-d928-c838-cea1-5b6a7536dc5b`, failure `b25565e7-86c6-5d2e-1f06-ae88a038c74a`, MSFT:39723014, `NULL_POINTER_READ_c0000005_Microsoft.Terminal.Control.dll!winrt::Microsoft::Terminal::Control::implementation::ControlCore::_refreshSizeUnderLock`


Hmm. Stack looks like we're above the `AppHost` dtor, and fired off a `_SwapChainScaleChanged`, and that caused a resize, but ~hey guess what, the `TermControl` is null already~. er, no it's not, debugger is just trying.

`_SwapChainScaleChanged` is bound in XAML:

```xaml
                <SwapChainPanel x:Name="SwapChainPanel"
                                CompositionScaleChanged="_SwapChainScaleChanged"
                                SizeChanged="_SwapChainSizeChanged">

```

Frankly, the app is already closing here, so this might be a different crash than what you're seeing. 


I suppose `ControlCore::_refreshSizeUnderLock` might want to check `_IsClosing` - not sure it actually ever does. Ot if any of the callers ever do. Huh. 


Yea one of the dumps definitely had a `_closing=true` in it. 
Hi Mike - thanks very much.

I’m not sure if it’s the cause of this - but other apps hung in a similar way (especially Firefox) and the cause of that was audio redirection on RDC.

I was also using the k9s tool frequently with the console, and upgrading *that*.. I haven’t had a hang since.

So maybe not terminal related after all!

That's great to hear!

I might pivot this thread then, to the other crash I discovered. That's just about 1% of our crashes, so not super high, but we've got a beat on it so may as well keep tracking. Thanks!
bumping to 1.16 cause there's a bit of a spike in hits, and the fix seems kinda easy.
This is actually our top crash these days. Maybe not in the wild so much, but definitely on the test machines. Let's knock this out, and see if it goes away in 1.16. 

Fix patch:
diff --git a/src/cascadia/TerminalControl/ControlCore.cpp b/src/cascadia/TerminalControl/ControlCore.cpp
index c1c45094798..3fdeb2ae442 100644
--- a/src/cascadia/TerminalControl/ControlCore.cpp
+++ b/src/cascadia/TerminalControl/ControlCore.cpp
@@ -911,6 +911,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
     // - <none>
     void ControlCore::_refreshSizeUnderLock()
     {
+        if (_IsClosing())
+        {
+            return;
+        }
+
         auto cx = gsl::narrow_cast<til::CoordType>(_panelWidth * _compositionScale);
         auto cy = gsl::narrow_cast<til::CoordType>(_panelHeight * _compositionScale);
 


Test patch: