Files
llvm-project/lldb/source/Core/ThreadedCommunication.cpp
Charles Zablit 796a1ea79f [lldb][windows] fix a race condition when closing the ConPTY (#182302)
This patch fixes a race condition when closing the ConPTY of a process
on Windows.

The read operation in `ConnectionGenericFile::Read` is asynchronous
because the ConPTY does not work with synchronous reads. However, this
is prone to a race condition if the ConPTY is closed between the
`ReadFile` and the `WaitForMultipleObjects`. This eventually leads to
the data being truncated, i.e some of the STDOUT is missing.

The fix is to introduce a mutex which prevents the ConPTY from closing
while a Read loop is in progress.

At the end of the pipe, `WaitForMultipleObjects` hangs until it receives
a `ERROR_BROKEN_PIPE` event, which is signaled when closing the ConPTY.
It's important to close the Read thread before closing the ConPTY,
otherwise the thread deadlocks waiting for the `ERROR_BROKEN_PIPE` which
is never sent because the ConPTY is waiting for the thread to exit
before closing.

All of this logic is encapsulated in the `ConnectionConPTY` class which
is a subclass of `ConnectionGenericFile`. `ConnectionConPTY` only
supports reading from the ConPTY. To write to it,
`IOHandlerProcessSTDIOWindows` should be used instead.

This patch depends on:
- https://github.com/llvm/llvm-project/pull/182536

This patch replaces https://github.com/llvm/llvm-project/pull/182109.

---------

Co-authored-by: Nerixyz <nero.9@hotmail.de>
2026-02-23 17:21:06 +00:00

13 KiB