Possible pipe handle (hOutput) leak in OpenConsole #17589

Open
opened 2026-01-31 05:46:54 +00:00 by claunia · 0 comments
Owner

Originally created by @o-sdn-o on GitHub (May 27, 2022).

When working within the Win32 API, when two processes interact through a pipe (::CreatePipe), closing the last writing handle of the pipe on the side of one process automatically unlocks the blocked ::ReadFile() on the side of the other process.

With ConPTY, the parent process has to additionally keep track of the state of the child process, since a blocked parent ::ReadFile() is not automatically unlocked when the child process exits.

A possible handle leak could be as follows.

When creating a pPty->hConPtyProcess process, three handles are passed to the ::CreateProcess():

11b810e403/src/winconpty/winconpty.cpp (L148-L161)

::CreateProcess() duplicates them and makes three new independent full-fledged handles that need to be closed.

But the OpenConsole.exe process only operates on two handles, ignoring hStdError, which is currently an independent handle.

11b810e403/src/host/exe/exemain.cpp (L257-L261)

Subsequently, the first two handles: _hInput and _hOutput are closed automatically, but hStdError is not

11b810e403/src/host/VtIo.cpp (L117-L119)

I think as a result, an application using ConPTY needs to track the termination of the child process, since the unclosed handle (which is a duplicate of hStdOutput) remains open. Perhaps this open handle is blocking the parent's ::ReadFile() even after the child terminates.

Originally created by @o-sdn-o on GitHub (May 27, 2022). When working within the Win32 API, when two processes interact through a pipe (::CreatePipe), closing the last writing handle of the pipe on the side of one process automatically unlocks the blocked ::ReadFile() on the side of the other process. With ConPTY, the parent process has to additionally keep track of the state of the child process, since a blocked parent ::ReadFile() is not automatically unlocked when the child process exits. A possible handle leak could be as follows. When creating a pPty->hConPtyProcess process, three handles are passed to the ::CreateProcess(): https://github.com/microsoft/terminal/blob/11b810e4036b669e5f223cce447f76d56044be0f/src/winconpty/winconpty.cpp#L148-L161 ::CreateProcess() duplicates them and makes three new independent full-fledged handles that need to be closed. But the OpenConsole.exe process only operates on two handles, ignoring hStdError, which is currently an independent handle. https://github.com/microsoft/terminal/blob/11b810e4036b669e5f223cce447f76d56044be0f/src/host/exe/exemain.cpp#L257-L261 Subsequently, the first two handles: _hInput and _hOutput are closed automatically, but hStdError is not https://github.com/microsoft/terminal/blob/11b810e4036b669e5f223cce447f76d56044be0f/src/host/VtIo.cpp#L117-L119 I think as a result, an application using ConPTY needs to track the termination of the child process, since the unclosed handle (which is a duplicate of hStdOutput) remains open. Perhaps this open handle is blocking the parent's ::ReadFile() even after the child terminates.
claunia added the Needs-TriageNeeds-Tag-Fix labels 2026-01-31 05:46:54 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#17589