[Lldb-commits] [lldb] [lldb][Windows] Fix lldb-server exit deadlock with pipe-mode stdio (PR #227273)
via lldb-commits
lldb-commits at lists.llvm.org
Tue Oct 6 08:53:54 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Charles Zablit (charles-zablit)
<details>
<summary>Changes</summary>
When the inferior exits, lldb-server waits for its stdio reader thread before letting the exit finish, so the inferior still holds the pipe open and `EOF` can never arrive.
In pipe mode, closing the pipe only cancelled a read that was already in progress. A read that started right after was retried at every 5 s timeout, so lldb-server waited forever and never reported the exit.
On Windows CI this shows up as tests that continue the inferior to its exit timing out after 600 s, and as `TestGdbRemoteExitCode` timing out waiting for the exit reply.
Closing the pipe now also stops the reader from starting another read, and lldb-server wakes the reader so it notices the close right away instead of at its next timeout.
rdar://188700912
---
Full diff: https://github.com/llvm/llvm-project/pull/227273.diff
6 Files Affected:
- (modified) lldb/include/lldb/Host/windows/PseudoConsole.h (+11-24)
- (modified) lldb/source/Host/windows/ConnectionConPTYWindows.cpp (+6-8)
- (modified) lldb/source/Host/windows/PseudoConsole.cpp (+6-6)
- (modified) lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp (+8)
- (modified) lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp (+3-25)
- (modified) lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h (+3-1)
``````````diff
diff --git a/lldb/include/lldb/Host/windows/PseudoConsole.h b/lldb/include/lldb/Host/windows/PseudoConsole.h
index b98402922bfb3..fa170caa11d00 100644
--- a/lldb/include/lldb/Host/windows/PseudoConsole.h
+++ b/lldb/include/lldb/Host/windows/PseudoConsole.h
@@ -11,7 +11,6 @@
#include "llvm/Support/Error.h"
#include <atomic>
-#include <condition_variable>
#include <mutex>
#include <string>
@@ -60,7 +59,13 @@ class PseudoConsole {
/// Closes the ConPTY and invalidates its handle, without closing the STDIN
/// and STDOUT pipes. Closing the ConPTY signals EOF to any process currently
- /// attached to it.
+ /// attached to it. The console host then writes its last frame to the STDOUT
+ /// pipe and closes its end, so reading the pipe up to EOF gets all of the
+ /// output.
+ ///
+ /// In pipe mode there is no ConPTY to close: this cancels the read pending
+ /// on the STDOUT pipe and marks the pipes closed, so IsConnected() returns
+ /// false from then on, as it does once a ConPTY is closed.
void Close();
/// Closes the STDIN and STDOUT pipe handles and invalidates them.
@@ -113,26 +118,6 @@ class PseudoConsole {
Mode GetMode() const { return m_mode; };
- /// Returns a reference to the mutex used to synchronize access to the
- /// ConPTY state.
- std::mutex &GetMutex() { return m_mutex; };
-
- /// Returns a reference to the condition variable used to signal state changes
- /// to threads waiting on the ConPTY (e.g. waiting for output or shutdown).
- std::condition_variable &GetCV() { return m_cv; };
-
- /// Returns whether the ConPTY is in the process of shutting down.
- ///
- /// \return
- /// A reference to the atomic bool that is set to true when the ConPTY
- /// is stopping. Callers should check this in their read/write loops to
- /// exit gracefully.
- bool IsStopping() const { return m_stopping.load(); };
-
- /// Sets the stopping flag to \p value, signalling to threads waiting on the
- /// ConPTY that they should stop.
- void SetStopping(bool value) { m_stopping = value; };
-
protected:
HANDLE m_conpty_handle = reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
HANDLE m_conpty_output = reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
@@ -142,10 +127,12 @@ class PseudoConsole {
reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
HANDLE m_pipe_child_stdout =
reinterpret_cast<HANDLE>(static_cast<intptr_t>(-1));
+ // Pipe mode: set by Close(). Its CancelIoEx only reaches a read that is
+ // already pending, so a reader has to be able to tell that the pipes were
+ // closed before it starts the next one.
+ std::atomic<bool> m_pipes_closed = false;
Mode m_mode = Mode::None;
std::mutex m_mutex{};
- std::condition_variable m_cv{};
- std::atomic<bool> m_stopping = false;
};
} // namespace lldb_private
diff --git a/lldb/source/Host/windows/ConnectionConPTYWindows.cpp b/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
index 22f8d42a8beca..2460532e4d6d8 100644
--- a/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
+++ b/lldb/source/Host/windows/ConnectionConPTYWindows.cpp
@@ -34,14 +34,12 @@ size_t ConnectionConPTY::Read(void *dst, size_t dst_len,
const Timeout<std::micro> &timeout,
lldb::ConnectionStatus &status,
Status *error_ptr) {
- {
- std::unique_lock<std::mutex> guard(m_pty->GetMutex());
- if (m_pty->IsStopping())
- m_pty->GetCV().wait(guard, [this] { return !m_pty->IsStopping(); });
- if (!m_pty->IsConnected()) {
- status = eConnectionStatusEndOfFile;
- return 0;
- }
+ bool open = m_pty->GetMode() == PseudoConsole::Mode::ConPTY
+ ? m_pty->GetSTDOUTHandle() != INVALID_HANDLE_VALUE
+ : m_pty->IsConnected();
+ if (!open) {
+ status = eConnectionStatusEndOfFile;
+ return 0;
}
char *out = static_cast<char *>(dst);
diff --git a/lldb/source/Host/windows/PseudoConsole.cpp b/lldb/source/Host/windows/PseudoConsole.cpp
index 3a28adacfea53..131bbaf2a936a 100644
--- a/lldb/source/Host/windows/PseudoConsole.cpp
+++ b/lldb/source/Host/windows/PseudoConsole.cpp
@@ -176,7 +176,7 @@ llvm::Error PseudoConsole::OpenPseudoConsole(uint16_t req_cols,
bool PseudoConsole::IsConnected() const {
if (m_mode == Mode::Pipe)
- return m_conpty_input != INVALID_HANDLE_VALUE &&
+ return !m_pipes_closed && m_conpty_input != INVALID_HANDLE_VALUE &&
m_conpty_output != INVALID_HANDLE_VALUE;
return m_conpty_handle != INVALID_HANDLE_VALUE &&
m_conpty_input != INVALID_HANDLE_VALUE &&
@@ -184,15 +184,14 @@ bool PseudoConsole::IsConnected() const {
}
void PseudoConsole::Close() {
- SetStopping(true);
- std::unique_lock<std::mutex> guard(m_mutex);
+ std::lock_guard<std::mutex> guard(m_mutex);
if (m_conpty_handle != INVALID_HANDLE_VALUE)
kernel32.ClosePseudoConsole(m_conpty_handle);
- if (m_mode == Mode::Pipe && m_conpty_output != INVALID_HANDLE_VALUE)
+ if (m_mode == Mode::Pipe && m_conpty_output != INVALID_HANDLE_VALUE) {
+ m_pipes_closed = true;
CancelIoEx(m_conpty_output, nullptr);
+ }
m_conpty_handle = INVALID_HANDLE_VALUE;
- SetStopping(false);
- m_cv.notify_all();
}
void PseudoConsole::ClosePseudoConsolePipes() {
@@ -219,6 +218,7 @@ void PseudoConsole::Reset() {
Close();
ClosePseudoConsolePipes();
CloseAnonymousPipes();
+ m_pipes_closed = false;
m_mode = Mode::None;
}
diff --git a/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp
index 90c9102bcea58..db0271b4f9346 100644
--- a/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp
@@ -979,9 +979,17 @@ void NativeProcessWindows::StopStdioForwarding() {
if (!m_stdio_communication.HasConnection())
return;
+ m_stdio_communication.SynchronizeWithReadThread();
+
if (m_pty)
m_pty->Close();
+ // Close() cancels the read pending on the pipe, but one that the read thread
+ // is about to start would only return at its 5s timeout: EOF cannot come
+ // while the inferior is held at its exit debug event. Wake the thread so that
+ // it sees the closed PTY right away.
+ m_stdio_communication.InterruptRead();
+
if (m_stdio_communication.ReadThreadIsRunning())
m_stdio_communication.JoinReadThread();
diff --git a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
index c01589e3ed812..1bc6e87a5d0ec 100644
--- a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
@@ -606,10 +606,9 @@ void ProcessWindows::OnExitProcess(uint32_t exit_code) {
if (m_pty) {
DrainProcessStdout();
- m_pty->SetStopping(true);
m_pty->Close();
m_stdio_communication.InterruptRead();
- m_stdio_communication.StopReadThread();
+ m_stdio_communication.JoinReadThread();
}
TargetSP target = CalculateTarget();
@@ -627,29 +626,8 @@ void ProcessWindows::OnExitProcess(uint32_t exit_code) {
}
void ProcessWindows::DrainProcessStdout() {
- if (!m_stdio_communication.ReadThreadIsRunning())
- return;
- m_stdio_communication.SynchronizeWithReadThread();
- if (!m_pty || m_pty->GetMode() != PseudoConsole::Mode::ConPTY)
- return;
-
- HANDLE pipe = m_pty->GetSTDOUTHandle();
- for (int consec_empty = 0; consec_empty < 3;) {
- if (!m_stdio_communication.ReadThreadIsRunning())
- break;
- DWORD avail = 0;
- // PeekNamedPipe is thread safe.
- if (!::PeekNamedPipe(pipe, nullptr, 0, nullptr, &avail, nullptr))
- break;
- if (avail > 0) {
- consec_empty = 0;
- m_stdio_communication.SynchronizeWithReadThread();
- } else {
- ++consec_empty;
- if (consec_empty < 3)
- ::SleepEx(1, FALSE);
- }
- }
+ if (m_stdio_communication.ReadThreadIsRunning())
+ m_stdio_communication.SynchronizeWithReadThread();
}
void ProcessWindows::OnDebuggerConnected(lldb::addr_t image_base) {
diff --git a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h
index e1361dc31fda2..0ac63e9c36743 100644
--- a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h
+++ b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h
@@ -113,7 +113,9 @@ class ProcessWindows : public Process, public ProcessDebugger {
protected:
/// Block until the stdio read thread has surfaced everything currently
- /// buffered in the ConPTY/pipe to the process's STDOUT cache.
+ /// buffered in the pipe to the process's STDOUT cache. With the process
+ /// stopped, that is all it wrote to a pipe. A ConPTY may still be rendering.
+ /// That output arrives after the stop.
void DrainProcessStdout();
size_t PutSTDIN(const char *src, size_t src_len, Status &error) override;
``````````
</details>
https://github.com/llvm/llvm-project/pull/227273
More information about the lldb-commits
mailing list