[PR #14282] [MERGED] Wait for clients to exit on ConPTY shutdown #30039

Open
opened 2026-01-31 09:38:18 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/microsoft/terminal/pull/14282
Author: @lhecker
Created: 10/23/2022
Status: Merged
Merged: 11/29/2022
Merged by: @lhecker

Base: mainHead: dev/lhecker/14132-shutdown-deadlock-part2


📝 Commits (10+)

  • 8bccc33 Wait for clients to exit on ConPTY shutdown
  • 6f177cf Fix code that used to rely on noreturn, Fix hPtyReference
  • a7db7a9 Fix build
  • 688a587 Fix typo
  • d693df4 Fix another deadlock on shutdown
  • f5f22ff Merge remote-tracking branch 'origin/main' into dev/lhecker/14132-shutdown-deadlock-part2
  • f24689f Merge remote-tracking branch 'origin/main' into dev/lhecker/14132-shutdown-deadlock-part2
  • 7fb6ffa Fix merge origin/main
  • bfd9004 Address feedback
  • 0a1da70 Fix build failure

📊 Changes

11 files changed (+73 additions, -138 deletions)

View changed files

📝 src/host/PtySignalInputThread.cpp (+39 -39)
📝 src/host/PtySignalInputThread.hpp (+2 -2)
📝 src/host/VtInputThread.cpp (+1 -4)
📝 src/host/VtInputThread.hpp (+1 -1)
📝 src/host/VtIo.cpp (+0 -12)
📝 src/host/VtIo.hpp (+2 -2)
📝 src/host/output.cpp (+4 -10)
📝 src/interactivity/base/ServiceLocator.cpp (+9 -1)
📝 src/winconpty/ft_pty/ConPtyTests.cpp (+4 -59)
📝 src/winconpty/winconpty.cpp (+10 -8)
📝 src/winconpty/winconpty.h (+1 -0)

📄 Description

#14160 didn't fix #14132 entirely. There seems to be a race condition left
where (on my system) 9 out of 10 times everything works correctly,
but sometimes OpenConsole exits, while pwsh and bash keep running.

My leading theory is that the new code is exiting OpenConsole faster than the
old code. This prevents clients from calling console APIs, etc. causing them
to get stuck. The old code (and new code) calls ExitProcess when the ConPTY
pipes break and I think this is wrong: In conhost when you close the window we
only call CloseConsoleProcessState via the WM_CLOSE event and that's it.
Solution: Remove the call to RundownAndExit for ConPTY.

During testing I found that continuously printing text inside msys2 will cause
child processes to only exit slowly one by one every 5 seconds.
This happens because CloseConsoleProcessState calls HandleCtrlEvent without
holding the console lock. This creates a race condition where most of the time
the console IO thread is the one picking up the CTRL_CLOSE_EVENT. But that's
problematic because the CTRL_CLOSE_EVENT leads to a ConsoleControl call of
type ConsoleEndTask which calls back into conhost's IO thread and
so you got the IO thread waiting on itself to respond.
Solution: Don't race conditions.

Validation Steps Performed

  • Enter-VsDevShell and close the tab
    Everything exits after 5s
  • Run msys2 bash from within pwsh and close the tab
    Everything exits instantly
  • Run cat bigfile.txt and close the tab
    Everything exits instantly
  • Patch conhost.exe with sfpcopy, as well as KernelBase.dll
    with the recent changes to winconpty, then launch and exit
    shells and applications via VS Code's terminal
  • On the main branch without this modification remove the call to
    TriggerTeardown in RundownAndExit (this speeds up the shutdown).
    Run (msys2's) bash.exe --login and hold enter and then press Ctrl+Shift+W
    simultaneously. The tab should close and randomly OpenConsole should exit
    early while pwsh/bash keep running. Then retry this with this branch and
    observe how the child processes don't stick around forever anymore.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/microsoft/terminal/pull/14282 **Author:** [@lhecker](https://github.com/lhecker) **Created:** 10/23/2022 **Status:** ✅ Merged **Merged:** 11/29/2022 **Merged by:** [@lhecker](https://github.com/lhecker) **Base:** `main` ← **Head:** `dev/lhecker/14132-shutdown-deadlock-part2` --- ### 📝 Commits (10+) - [`8bccc33`](https://github.com/microsoft/terminal/commit/8bccc33f9aac00caf5ef66b06bcbe66019da23c1) Wait for clients to exit on ConPTY shutdown - [`6f177cf`](https://github.com/microsoft/terminal/commit/6f177cf78b193cbe88595c8dd7165ebbc9f60a4e) Fix code that used to rely on [[noreturn]], Fix hPtyReference - [`a7db7a9`](https://github.com/microsoft/terminal/commit/a7db7a9309194679f95e869147489b7977e0ee4c) Fix build - [`688a587`](https://github.com/microsoft/terminal/commit/688a58721f090053326256ad18e6e58302bd7d4b) Fix typo - [`d693df4`](https://github.com/microsoft/terminal/commit/d693df4a6dea75842161409f2afc4fa76d60ce38) Fix another deadlock on shutdown - [`f5f22ff`](https://github.com/microsoft/terminal/commit/f5f22ff0df93e57ce0095a127871a7b53bffe9c7) Merge remote-tracking branch 'origin/main' into dev/lhecker/14132-shutdown-deadlock-part2 - [`f24689f`](https://github.com/microsoft/terminal/commit/f24689f027cbabe2aab19b3d0c9d892998a9b8fc) Merge remote-tracking branch 'origin/main' into dev/lhecker/14132-shutdown-deadlock-part2 - [`7fb6ffa`](https://github.com/microsoft/terminal/commit/7fb6ffa6a938a522d74ef70d5e217b153c566df2) Fix merge origin/main - [`bfd9004`](https://github.com/microsoft/terminal/commit/bfd900492b788c849964b89f497ff879df5d9749) Address feedback - [`0a1da70`](https://github.com/microsoft/terminal/commit/0a1da70708884d4b07fbaf66ac9df4515ff3b421) Fix build failure ### 📊 Changes **11 files changed** (+73 additions, -138 deletions) <details> <summary>View changed files</summary> 📝 `src/host/PtySignalInputThread.cpp` (+39 -39) 📝 `src/host/PtySignalInputThread.hpp` (+2 -2) 📝 `src/host/VtInputThread.cpp` (+1 -4) 📝 `src/host/VtInputThread.hpp` (+1 -1) 📝 `src/host/VtIo.cpp` (+0 -12) 📝 `src/host/VtIo.hpp` (+2 -2) 📝 `src/host/output.cpp` (+4 -10) 📝 `src/interactivity/base/ServiceLocator.cpp` (+9 -1) 📝 `src/winconpty/ft_pty/ConPtyTests.cpp` (+4 -59) 📝 `src/winconpty/winconpty.cpp` (+10 -8) 📝 `src/winconpty/winconpty.h` (+1 -0) </details> ### 📄 Description #14160 didn't fix #14132 entirely. There seems to be a race condition left where (on my system) 9 out of 10 times everything works correctly, but sometimes OpenConsole exits, while pwsh and bash keep running. My leading theory is that the new code is exiting OpenConsole faster than the old code. This prevents clients from calling console APIs, etc. causing them to get stuck. The old code (and new code) calls `ExitProcess` when the ConPTY pipes break and I think this is wrong: In conhost when you close the window we only call `CloseConsoleProcessState` via the `WM_CLOSE` event and that's it. Solution: Remove the call to `RundownAndExit` for ConPTY. During testing I found that continuously printing text inside msys2 will cause child processes to only exit slowly one by one every 5 seconds. This happens because `CloseConsoleProcessState` calls `HandleCtrlEvent` without holding the console lock. This creates a race condition where most of the time the console IO thread is the one picking up the `CTRL_CLOSE_EVENT`. But that's problematic because the `CTRL_CLOSE_EVENT` leads to a `ConsoleControl` call of type `ConsoleEndTask` which calls back into conhost's IO thread and so you got the IO thread waiting on itself to respond. Solution: Don't race conditions. ## Validation Steps Performed * `Enter-VsDevShell` and close the tab Everything exits after 5s ✅ * Run msys2 bash from within pwsh and close the tab Everything exits instantly ✅ * Run `cat bigfile.txt` and close the tab Everything exits instantly ✅ * Patch `conhost.exe` with `sfpcopy`, as well as `KernelBase.dll` with the recent changes to `winconpty`, then launch and exit shells and applications via VS Code's terminal ✅ * On the main branch without this modification remove the call to `TriggerTeardown` in `RundownAndExit` (this speeds up the shutdown). Run (msys2's) `bash.exe --login` and hold enter and then press Ctrl+Shift+W simultaneously. The tab should close and randomly OpenConsole should exit early while pwsh/bash keep running. Then retry this with this branch and observe how the child processes don't stick around forever anymore. ✅ --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-31 09:38:18 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#30039