Repository: microsoft/terminal (language: cpp)

Issue:
Terminal ignores Profile termination behavior setting
### Windows Terminal version

1.19.2682.0

### Windows build number

10.0.22621.0

### Other Software

WSL

### Steps to reproduce

Set Profile termination behavior to "Close when process exits, fails, or crashes". Make profile exit with non-zero exit code. I can reproduce this in WSL, for example, by listing a non-existing file `ls 123`, then `exit`.


### Expected Behavior

Tab closes silently. (Terminal 1.18 closes tab silently in this case).

### Actual Behavior

This message is displayed:

```
[process exited with code 2 (0x00000002)]
You can now close this terminal with Ctrl+D, or press Enter to restart.
```


Discussion / hints:
I am in the same terminal version and I can confirm this issue. In the previous version, this wouldn't happen.
notes:

https://github.com/microsoft/terminal/blob/1669036e595bcc8a100e7b8b679dc8f4748f2eb7/src/cascadia/TerminalApp/Pane.cpp#L1069-L1076

When we get there as we're exiting, `previousConnectionState` is evaluated as... `NotConnected`? So we never propogated up the `Connecting`, `Connected`? weird....


oh derp
literally just above that:

https://github.com/microsoft/terminal/blob/1669036e595bcc8a100e7b8b679dc8f4748f2eb7/src/cascadia/TerminalApp/Pane.cpp#L1043-L1047


Fix patch:
diff --git a/src/cascadia/TerminalApp/Pane.cpp b/src/cascadia/TerminalApp/Pane.cpp
index 0d0fe1a71b1..fccd8b8c5d1 100644
--- a/src/cascadia/TerminalApp/Pane.cpp
+++ b/src/cascadia/TerminalApp/Pane.cpp
@@ -1040,6 +1040,7 @@ winrt::fire_and_forget Pane::_ControlConnectionStateChangedHandler(const winrt::
         newConnectionState = coreState.ConnectionState();
     }
 
+    const auto previousConnectionState = std::exchange(_connectionState, newConnectionState);
     if (newConnectionState < ConnectionState::Closed)
     {
         // Pane doesn't care if the connection isn't entering a terminal state.
@@ -1066,7 +1067,6 @@ winrt::fire_and_forget Pane::_ControlConnectionStateChangedHandler(const winrt::
         co_return;
     }
 
-    const auto previousConnectionState = std::exchange(_connectionState, newConnectionState);
     if (previousConnectionState < ConnectionState::Connected && newConnectionState >= ConnectionState::Failed)
     {
         // A failure to complete the connection (before it has _connected_) is not covered by "closeOnExit".


Test patch: