Repository: microsoft/terminal (language: cpp)

Issue:
Sporadic cannot ctrl+D to or enter to close the terminal 
### Windows Terminal version

Version: 1.21.1772.0

### Windows build number

Microsoft Windows [Version 10.0.22631.3880]

### Other Software

N/A

### Steps to reproduce

No idea how to reproduce. Sometimes, after a long time open and running I notice the "track my mouse" feature stops working between panes and then when i exit the terminal it ends up printing:
```
[process exited with code 130 (0x00000082)]
You can now close this terminal with Ctrl+D, or press Enter to restart.
```

but ctrl+d or enter don't exit the pane:
![terminal-pane-example](https://github.com/user-attachments/assets/7b66b6b6-39a4-4ad6-8de0-e95adf8bfb50)


### Expected Behavior

I want the "track my mouse" feature to keep working and if i have to close the terminal, i expect the pane to actually close.


### Actual Behavior

mouse tracking fails and ctrl+d/enter won't actually close the pane. the whole terminal window needs to be relaunched. 


Discussion / hints:
I can't reproduce it on demand but it keeps happening. i did however go back to the released Version: 1.20.11781.0 and this is not a problem. I've moved away from the 'preview' for now because of this issue.

It might have to do with mouse tracking. Another example of how the mouse tracking seems to have failed and when i exit the terminal i get the Ctrl+D message

![track](https://github.com/user-attachments/assets/1643cc27-3c36-4b63-9d89-9e0a449599c7)

CTRL-D doesn't close the window either on the preview version. I can confirm that the 'stable' terminal `1.20.11781.0` properly responds to CTRL-D and closes the pane
I can repro (every time) like this.

Start WT normally (one tab, normal window).
Go full-screen (F11).
Split vertical (Alt+Shift++)
"exit" the shell on the right (this is OK)
"exit" the remaining shell (error occurs)

Note that exiting the lefthand shell then the other one does not cause the error.

Ditto for Split Horizontal with "bottom" replacing "right" and the analogous note about which is exitted first.
It happens in WindowsTerminal 1.21.2406.25002 (preview) and not in WindowsTerminal 1.20.2406.26001.
This regressed in 5d46e317b29a709a4b585ab9fe83ad71c456b5df. Because the content isn't being reset anymore, after a split-pane two panes may share the same content object. Closing one pane will then cause both to be closed.
@lhecker, If I understand your comment correctly (which may not be the case), then I don't see that.  In my experiment, closing either pane leaves the other pane functional.  As noted before, closing the new pane causes the original pane to exhibit the problem under discussion _when it's closed_.  But closing the original pane does not cause the new pane to exhibit the problem _when it's closed_.
When I said "closed" I was referring to an internal function called "Close" that removes some event handlers from the "content" of a pane. This is what causes Ctrl-D and Enter to seemingly not work. (It was confusingly worded. I'm sorry. 😣)

Fix patch:
diff --git a/src/cascadia/TerminalApp/Pane.cpp b/src/cascadia/TerminalApp/Pane.cpp
index 96667a60fa1..a210b701528 100644
--- a/src/cascadia/TerminalApp/Pane.cpp
+++ b/src/cascadia/TerminalApp/Pane.cpp
@@ -1711,9 +1711,7 @@ void Pane::_SetupChildCloseHandlers()
 IPaneContent Pane::_takePaneContent()
 {
     _closeRequestedRevoker.revoke();
-    // we cannot return std::move(_content) because we don't want _content to be null,
-    // since _content gets accessed even after Close is called
-    return _content;
+    return std::move(_content);
 }
 
 // This method safely sets the content of the Pane. It'll ensure to revoke and
@@ -1723,9 +1721,9 @@ void Pane::_setPaneContent(IPaneContent content)
 {
     // The IPaneContent::Close() implementation may be buggy and raise the CloseRequested event again.
     // _takePaneContent() avoids this as it revokes the event handler.
-    if (_takePaneContent())
+    if (const auto c = _takePaneContent())
     {
-        _content.Close();
+        c.Close();
     }
 
     if (content)


Test patch: