terminal: ClosePseudoConsole API hanging
Environment
Windows build number: Microsoft Windows [Version 10.0.18362.175]
Any other software? Visual Studio Code Insiders
Steps to reproduce
- Launch VS Code Insiders.
- Ensure using conpty with setting:
terminal.integrated.windowsEnableConpty - Open a terminal with Ctrl+`
- Close the terminal with the trash can icon
- Repeat 3 and 4 until the window hangs. It takes around 10 retries on my machine.
Expected behavior
No hanging.
Actual behavior
The window hangs. Using windbg, I see the following:

Notice the many leftover conhosts in the task manager as well as the hang in ClosePseudoConsole.
Here is the link to the code in node-pty calling into the PseudoConsole API. https://github.com/microsoft/node-pty/blob/04445ed76f90b4f56a190982ea2d4fcdd22a0ee7/src/win/conpty.cc#L429
/cc @daimms
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 16
- Comments: 58 (40 by maintainers)
Commits related to this issue
- Fix a deadlock during ConPTY shutdown (#14160) Problem: * Calling `RundownAndExit` tries to flush out the last frame from `VtEngine` * `VtEngine` indirectly calls `RundownAndExit` if the pipe is go... — committed to microsoft/terminal by lhecker 2 years ago
- Fix a deadlock during ConPTY shutdown (#14160) Problem: * Calling `RundownAndExit` tries to flush out the last frame from `VtEngine` * `VtEngine` indirectly calls `RundownAndExit` if the pipe is gone... — committed to microsoft/terminal by lhecker 2 years ago
- Fix a deadlock during ConPTY shutdown (#14160) Problem: * Calling `RundownAndExit` tries to flush out the last frame from `VtEngine` * `VtEngine` indirectly calls `RundownAndExit` if the pipe is gone... — committed to microsoft/terminal by lhecker 2 years ago
- Fix a deadlock during ConPTY shutdown (#14160) Problem: * Calling `RundownAndExit` tries to flush out the last frame from `VtEngine` * `VtEngine` indirectly calls `RundownAndExit` if the pipe is go... — committed to microsoft/terminal by lhecker 2 years ago
- Fix a deadlock during ConPTY shutdown (#14160) Problem: * Calling `RundownAndExit` tries to flush out the last frame from `VtEngine` * `VtEngine` indirectly calls `RundownAndExit` if the pipe is gone... — committed to microsoft/terminal by lhecker 2 years ago
@miniksa hopefully it can get sorted, it currently causes all kinds of hard locks in Visual Studio Code at the moment in any terminal. To reproduce, just type ‘a’ and kill the process.
Quite frustrating 😕
Probably tomorrow’s insiders, follow https://github.com/microsoft/vscode/pull/116185 for updates
I am currently actively looking into finally working around this ConPTY behavior.
Honestly, if your above quoted workaround is the only way to work around this, I’d rather drop Windows support (sadly) or just wait until maybe or maybe not eventually ConPTY can handle with read/write operations in one thread. The reasoning is not that I want to be stubborn but rather that ConPTY (windows) is the only platform that requires this architectural change just to not freeze at shutdown.
I’m trying it though, but it seems like not worth it for now. 😃
UPDATE: Okay, I’ve actually forgotten my own source code, and I remember why I did already split up PTY reads from writes (because ConPTY does not support non-blocking I/O).
My problem was, that if the connected process behind ConPTY terminated, the already started ConPTY’s
read()call did not return and does hang forever. This is still the case with latest software updates, and I actually do consider this a bad (or buggy?) behaviour compared to the other PTY implementations cancelling any active I/O operation when the other end disconnects. I did add a process exit watcher that does explicitly close the terminal’s end of the PTY, and now theread-call is not hanging anymore.Thanks 😃
Is there any movement on this issue? It causes hangs for many vscode users.
VS Code update: Electron isn’t going to support
worker_threadsin the renderer thread, so the fix is blocked on moving where we host node-pty/conpty to a real node process: https://github.com/microsoft/node-pty/pull/415#issuecomment-688870219OK. We’ll see. Thanks for the pointer to the code. We’ll have to see where/when we can find resources for that and for this issue.
Next time you hit the hang or any other hang like this, can you please take a process dump of the supposedly hung conhost so I can check the stacks?
If you’re hitting a similar situation without the flag set, then you’re getting hung in a different situation which I’m not necessarily working on fixing right now in this issue.
@miniksa installer/zip at the top: https://code.visualstudio.com/updates/v1_36, you can set
"update.mode": "none"to disable automatic updatesThis is because the ConPTY is being started with
CreatePseudoConsole()with the flagPSEUDOCONSOLE_INHERIT_CURSOR. There’s an indefinite wait for the calling terminal to respond with the cursor position before the startup lock is freed for other threads to use. A different thread gets the shutdown message and is attempting to acquire the same lock to clean things up before exiting.It looks like if the PTY starts up fast enough so that the shutdown message comes after the cursor is inherited OR if the shutdown happens before the PTY starts asking for the cursor inherit, then everything is good.
Given this is a race condition, I can’t really blame it on the caller holding it wrong. I’ll work to resolve it such that a shutdown is a valid way of halting the request for the cursor position while it is starting up.