terminal/src/server
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
..
lib Create a control unittesting project (#9677) 2021-04-05 16:07:55 +00:00
ApiDispatchers.cpp Set keyword flags on all tracelog events (#10098) 2021-05-14 23:14:26 +00:00
ApiDispatchers.h Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
ApiDispatchersInternal.cpp Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
ApiMessage.cpp Remove CONSOLE_API_MSG::UpdateUserBufferPointers hack (#10326) 2021-06-14 19:52:40 +00:00
ApiMessage.h Remove CONSOLE_API_MSG::UpdateUserBufferPointers hack (#10326) 2021-06-14 19:52:40 +00:00
ApiMessageState.cpp add clang-format conf to the project, format the c++ code (#1141) 2019-06-11 13:27:09 -07:00
ApiMessageState.h add clang-format conf to the project, format the c++ code (#1141) 2019-06-11 13:27:09 -07:00
ApiSorter.cpp add clang-format conf to the project, format the c++ code (#1141) 2019-06-11 13:27:09 -07:00
ApiSorter.h add clang-format conf to the project, format the c++ code (#1141) 2019-06-11 13:27:09 -07:00
ConDrvDeviceComm.cpp ci: fix spelling 2021-02-11 14:08:37 -08:00
ConDrvDeviceComm.h Merged PR 5677497: [Git2Git] Merged PR 5655213: Allow conhost to handoff to registered default app handler 2021-02-11 21:07:50 +00:00
ConsoleShimPolicy.cpp Reintroduce a color compatibility hack, but only for PowerShells (#6810) 2020-07-10 15:25:39 -07:00
ConsoleShimPolicy.h Reintroduce a color compatibility hack, but only for PowerShells (#6810) 2020-07-10 15:25:39 -07:00
DeviceComm.h Merged PR 5677497: [Git2Git] Merged PR 5655213: Allow conhost to handoff to registered default app handler 2021-02-11 21:07:50 +00:00
DeviceHandle.cpp Apply audit mode to TerminalConnection/Core/Settings and WinCon… (#4016) 2020-01-03 10:44:27 -08:00
DeviceHandle.h add clang-format conf to the project, format the c++ code (#1141) 2019-06-11 13:27:09 -07:00
dirs Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
Entrypoints.cpp Change NULL to nullptr since they are pointers (#4960) 2020-03-20 20:35:12 +00:00
Entrypoints.h add clang-format conf to the project, format the c++ code (#1141) 2019-06-11 13:27:09 -07:00
IApiRoutines.h Replace basic_string_view<T> with span<const T> (#6921) 2020-07-15 16:40:42 +00:00
IoDispatchers.cpp Persist inbox conhost; delegate control activities to it via a pipe (#10415) 2021-06-16 19:23:37 +00:00
IoDispatchers.h Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
IoSorter.cpp Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
IoSorter.h Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
IoThread.h add clang-format conf to the project, format the c++ code (#1141) 2019-06-11 13:27:09 -07:00
IWaitRoutine.h Merged PR 6034984: Fix a crash caused by improper buffer management w/ multiple clients 2021-05-11 16:56:43 +00:00
ObjectHandle.cpp Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
ObjectHandle.h Prevent cleanup of object given to handle that failed access check (#3414) 2019-11-05 14:22:55 -08:00
ObjectHeader.cpp Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
ObjectHeader.h Prevent cleanup of object given to handle that failed access check (#3414) 2019-11-05 14:22:55 -08:00
precomp.cpp Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
precomp.h Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
ProcessHandle.cpp Convert DeviceComm into an interface and add handle exchange (#8367) 2020-12-15 23:07:43 +00:00
ProcessHandle.h Convert DeviceComm into an interface and add handle exchange (#8367) 2020-12-15 23:07:43 +00:00
ProcessList.cpp Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
ProcessList.h Revert locking changes (#3488) 2019-11-08 13:44:52 -08:00
ProcessPolicy.cpp Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
ProcessPolicy.h Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
sources.inc Merged PR 5677497: [Git2Git] Merged PR 5655213: Allow conhost to handoff to registered default app handler 2021-02-11 21:07:50 +00:00
WaitBlock.cpp Remove CONSOLE_API_MSG::UpdateUserBufferPointers hack (#10326) 2021-06-14 19:52:40 +00:00
WaitBlock.h Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
WaitQueue.cpp Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
WaitQueue.h Change backslashes in include statements to forward slashes (#8205) 2020-11-25 21:02:10 +00:00
WaitTerminationReason.h Initial release of the Windows Terminal source code 2019-05-02 15:29:04 -07:00
winbasep.h add clang-format conf to the project, format the c++ code (#1141) 2019-06-11 13:27:09 -07:00
WinNTControl.cpp Apply audit mode to TerminalConnection/Core/Settings and WinCon… (#4016) 2020-01-03 10:44:27 -08:00
WinNTControl.h Apply audit mode to TerminalConnection/Core/Settings and WinCon… (#4016) 2020-01-03 10:44:27 -08:00