terminal/src/cascadia
Michael Niksa 2bd5791feb
Persist inbox conhost; delegate control activities to it via a pipe (#10415)
Persist inbox conhost; delegate control activities to it via a pipe

## PR Checklist
* [x] Closes #10194 - WSL Debug Tap doesn't work
* [x] Closes #10134 - WSL Parameter is Incorrect
* [x] Closes #10413 - Ctrl+C not passed to client
* [x] Closes #10414 - Leftover processes on abrupt termination
* [x] Might help #10251 - Win+X Powershell sometimes fails to attach
* [x] I work here
* [x] Manually tested with assorted launch scenarios

## Detailed Description of the Pull Request / Additional comments
It turns out that there's a bit of ownership that goes on with the original inbox `conhost.exe` and the operating system/driver. The PID of that original `conhost.exe` is stowed when the initial connection is established and it is verified for several activities. This means that the plan of letting it go completely away and having the `OpenConsole.exe` take over all of its activities must be slightly revised. 

I have tested the following two alternatives to keeping `conhost.exe` around and they do not work:
1. Replacing the original owner `conhost.exe` with `OpenConsole.exe` - A.) The driver does not allow this. Once the owner is registered, it cannot be replaced. B.) There's no way of updating this information inside the client process space and it is kept there too in the `kernelbase`/`conclnt` data from its initial connection.
2. Attempting to pick up the first packet (to determine headed/headless and other initial connection information that we use to determine whether handoff is appropriate or not) prior to registering any owner at all. - The driver doesn't allow this either. The owner must be registered prior to a packet coming through.

Put this mental model in your head:
CMD --> Conhost (inbox) --> OpenConsole (WT Package) --> Terminal (WT Package)

So since the `conhost.exe` needs to stick around, here's what I'm doing in this PR:
- `conhost.exe` in the OS will receive back the `OpenConsole.exe` process handle on a successful handoff and is expected to remain alive until the `OpenConsole.exe` exits. It's now waiting on that before it terminates itself.
- `conhost.exe` in the OS will establish a signal channel pipe and listen for control commands from `OpenConsole.exe` in a very similar fashion to how the `ConPTY` signal pipe operates between the Terminal and the PTY (provided by `OpenConsole.exe` in this particular example.) When `OpenConsole.exe` needs to do something that would be verified by the OS and rejected... it will instead signal the original `conhost.exe` to do that thing and it will go through.
- `conhost.exe` will give its own handle through to `OpenConsole.exe` so it can monitor its lifetime and cleanup. If the owner is gone, the session should end.
- Assorted handle cleanup that was leading to improper exits. I was confused between `.reset()` and `.release()` for some of the `wil::unique_any<T>` handling and it lead to leaked handles. The leaked handles meant that threads weren't aware of the other sides collapsing and wouldn't cleanup/terminate appropriately.

How does this fix things?
- For the WSL cases... WSL was specifically looking up the owner PID of the console session from the driver. That was the `conhost.exe` PID. If it exits, that PID isn't valid and is recycled. Thus the parameter is incorrect or other inappropriate WSL setup behaviors.
- Ctrl+C not passed... this is a signal the operating system rejects from a PID that is not the owner. This is now relayed through the original owner and it works.
- Leftover processes... I believe I explained this was both not-enough-monitoring of each others' process lifetimes coupled with mishandling of release/resetting handles and leaking them.
- Powershell sometimes fails to attach... my theory on this one is that it's a race that became upset when the `conhost.exe` disappeared while something about Powershell/.NET was still starting, much like the WSL one. I believe now that it is sticking around, it will be fine.

Also, this WILL require an OS update to complete improvement of functionality and I have revised the interface ID. This is considered an acceptable breaking change with no mitigation because we said this feature was an alpha preview.  

## Validation Steps Performed
- Launched WSL with defapp set, it works
- Launched WSL with defapp set and the debug tap on, it works and opens in two tabs
- Launched CMD, ran ping, did Ctrl+C, it now receives it
- Launched Win+X powershell a ton of times. It seems fine now
- Launched cmd, powershell, wsl, etc. Killed assorted processes in the chain (client/conhost/openconsole/windowsterminal) and observed in Process Explorer (with a long delta timer so I could see it) that they all successfully tear down now without leftovers.
2021-06-16 19:23:37 +00:00
..
CascadiaPackage Persist inbox conhost; delegate control activities to it via a pipe (#10415) 2021-06-16 19:23:37 +00:00
inc Add globalSummon action (#9854) 2021-04-28 17:13:28 -05:00
LocalTests_SettingsModel Bugfix: serialize iterable commands (#10373) 2021-06-10 18:25:27 +00:00
LocalTests_TerminalApp Restore embedded manifests to say 18362 (#10370) 2021-06-09 16:23:26 +00:00
PublicTerminalCore Exclude more rarely-used stuff from Windows headers (#8513) 2020-12-11 19:35:23 +00:00
Remoting Enable winrt::make<> detection (#10264) 2021-06-01 22:33:53 +00:00
ShellExtension Move Branding into common props (#9668) 2021-03-30 18:08:53 -05:00
TerminalApp Don't dispatch the Toggle Command Palette action to keep the just closed Command Palette closed. (#10423) 2021-06-14 23:32:31 +00:00
TerminalAzBridge Update C++/WinRT to 2.0.210309.3 (#9437) 2021-03-10 16:04:59 -06:00
TerminalConnection Ignore closeOnExit when a conn. moves from Connecting to Failed (#10263) 2021-05-28 14:22:43 -05:00
TerminalControl Lock for writing in ControlCore::SetBackgroundOpacity (#10357) 2021-06-09 16:55:41 +00:00
TerminalCore Upgrade Windows SDK to 19041 (#10118) 2021-05-20 16:04:25 +00:00
TerminalSettingsEditor Fix edit button color in light theme (#10412) 2021-06-14 16:51:15 +00:00
TerminalSettingsModel Add a language switcher using PrimaryLanguageOverride (#10309) 2021-06-10 23:24:21 +00:00
UnitTests_Control Upgrade Windows SDK to 19041 (#10118) 2021-05-20 16:04:25 +00:00
UnitTests_Remoting Upgrade Windows SDK to 19041 (#10118) 2021-05-20 16:04:25 +00:00
UnitTests_TerminalCore Introduce til::rle - a run length encoded vector (#10099) 2021-05-20 17:27:50 +00:00
ut_app Restore embedded manifests to say 18362 (#10370) 2021-06-09 16:23:26 +00:00
WindowsTerminal Summon, not toggle visibility, window on command line dispatch (#10396) 2021-06-10 17:22:22 +00:00
WindowsTerminal_UIATests Implement PGO in pipelines for AMD64 architecture; supply training test scenarios (#10071) 2021-05-13 21:12:30 +00:00
WindowsTerminalUniversal Upgrade Windows SDK to 19041 (#10118) 2021-05-20 16:04:25 +00:00
WinRTUtils Remove bug fixes which aren't needed in VS 16.9 (#9953) 2021-04-28 10:43:05 +00:00
WpfTerminalControl [WPF] Allows setting the WPF control background when setting the terminal theme (#10026) 2021-05-04 21:18:25 +00:00
WpfTerminalTestNetCore Auto-format our XAML files and enforce in CI (#9589) 2021-03-29 17:09:38 -05:00
wt Upgrade Windows SDK to 19041 (#10118) 2021-05-20 16:04:25 +00:00
CascadiaResources.build.items Introduce TerminalSettingsModel project (#7667) 2020-10-06 09:56:59 -07:00