[Lldb-commits] [lldb] 2c185ee - [lldb][Windows] Don't hang unregistering a pipe before its monitor reads (#228111)
via lldb-commits
lldb-commits at lists.llvm.org
Tue Oct 6 06:51:44 PDT 2026
Author: Charles Zablit
Date: 2026-10-06T14:51:33+01:00
New Revision: 2c185eed2f53171bf5da6df59c796153d03d01e6
URL: https://github.com/llvm/llvm-project/commit/2c185eed2f53171bf5da6df59c796153d03d01e6
DIFF: https://github.com/llvm/llvm-project/commit/2c185eed2f53171bf5da6df59c796153d03d01e6.diff
LOG: [lldb][Windows] Don't hang unregistering a pipe before its monitor reads (#228111)
On Windows, `MainLoop` watches each pipe with a helper thread that waits
in a `ReadFile`. On shutdown, `~PipeEvent` cancels that read once and
then waits for the thread. If the thread hadn't started its read yet,
the cancel misses it, and the shutdown waits forever while the pipe
stays open.
`lldb-dap` hit this at exit, which causes the `TestDAP_io` timeouts on
Windows CI.
This patch makes it so overlapped reads now also wait on a stop event,
so they can't miss a stop. Synchronous reads can't be interrupted that
way, so the destructor keeps cancelling until the thread exits. It now
waits for an event the thread sets on exit, not the thread handle.
The new unit tests hang without the fix, and `lldb-dap` exits in 10,000
of 10,000 runs under load.
rdar://188800539
Added:
Modified:
lldb/source/Host/windows/MainLoopWindows.cpp
lldb/unittests/Host/MainLoopTest.cpp
Removed:
################################################################################
diff --git a/lldb/source/Host/windows/MainLoopWindows.cpp b/lldb/source/Host/windows/MainLoopWindows.cpp
index 113cc15b99130..0bdb42f02eae4 100644
--- a/lldb/source/Host/windows/MainLoopWindows.cpp
+++ b/lldb/source/Host/windows/MainLoopWindows.cpp
@@ -47,8 +47,14 @@ class PipeEvent : public MainLoopWindows::IOEvent {
/*bInitialState=*/FALSE, nullptr)),
m_handle(handle),
m_ready(CreateEventW(nullptr, /*bManualReset=*/TRUE,
- /*bInitialState=*/FALSE, nullptr)) {
- assert(m_event && m_ready);
+ /*bInitialState=*/FALSE, nullptr)),
+ m_stop(CreateEventW(nullptr, /*bManualReset=*/TRUE,
+ /*bInitialState=*/FALSE, nullptr)),
+ m_read_done(CreateEventW(nullptr, /*bManualReset=*/TRUE,
+ /*bInitialState=*/FALSE, nullptr)),
+ m_exited(CreateEventW(nullptr, /*bManualReset=*/TRUE,
+ /*bInitialState=*/FALSE, nullptr)) {
+ assert(m_event && m_ready && m_stop && m_read_done && m_exited);
m_monitor_thread = std::thread(&PipeEvent::Monitor, this);
}
@@ -58,12 +64,18 @@ class PipeEvent : public MainLoopWindows::IOEvent {
std::lock_guard<std::mutex> guard(m_mutex);
m_stopped = true;
SetEvent(m_ready);
- CancelIoEx(m_handle, &m_ov);
+ SetEvent(m_stop);
}
+ do {
+ CancelIoEx(m_handle, &m_ov);
+ } while (WaitForSingleObject(m_exited, 1) == WAIT_TIMEOUT);
m_monitor_thread.join();
}
CloseHandle(m_event);
CloseHandle(m_ready);
+ CloseHandle(m_stop);
+ CloseHandle(m_read_done);
+ CloseHandle(m_exited);
}
void WillPoll() override {
@@ -93,10 +105,11 @@ class PipeEvent : public MainLoopWindows::IOEvent {
// Wait until the MainLoop tells us to start.
WaitForSingleObject(m_ready, INFINITE);
- do {
+ while (!m_stopped) {
char buf[1];
DWORD bytes_read = 0;
ZeroMemory(&m_ov, sizeof(m_ov));
+ m_ov.hEvent = m_read_done;
// Block on a 0-byte read; this will only resume when data is
// available in the pipe. The pipe must be PIPE_WAIT or this thread
// will spin.
@@ -105,8 +118,17 @@ class PipeEvent : public MainLoopWindows::IOEvent {
DWORD bytes_available = 0;
DWORD err = GetLastError();
if (!success && err == ERROR_IO_PENDING) {
+ // m_stop stays signaled, so a stop requested before the read was
+ // issued is still seen here.
+ HANDLE handles[2] = {m_read_done, m_stop};
+ if (WaitForMultipleObjects(2, handles, /*bWaitAll=*/FALSE, INFINITE) !=
+ WAIT_OBJECT_0) {
+ CancelIoEx(m_handle, &m_ov);
+ GetOverlappedResult(m_handle, &m_ov, &bytes_read, /*bWait=*/TRUE);
+ break;
+ }
success = GetOverlappedResult(m_handle, &m_ov, &bytes_read,
- /*bWait=*/TRUE);
+ /*bWait=*/FALSE);
err = GetLastError();
}
if (success) {
@@ -144,12 +166,16 @@ class PipeEvent : public MainLoopWindows::IOEvent {
// Wait until the current read is consumed before doing the next read.
WaitForSingleObject(m_ready, INFINITE);
- } while (!m_stopped);
+ }
+ SetEvent(m_exited);
}
private:
HANDLE m_handle;
HANDLE m_ready;
+ HANDLE m_stop;
+ HANDLE m_read_done;
+ HANDLE m_exited;
OVERLAPPED m_ov;
std::thread m_monitor_thread;
std::atomic<bool> m_stopped = false;
diff --git a/lldb/unittests/Host/MainLoopTest.cpp b/lldb/unittests/Host/MainLoopTest.cpp
index 9fb8032f48d3b..83da90a280634 100644
--- a/lldb/unittests/Host/MainLoopTest.cpp
+++ b/lldb/unittests/Host/MainLoopTest.cpp
@@ -21,6 +21,11 @@
#include <future>
#include <thread>
+#ifdef _WIN32
+#include <fcntl.h>
+#include <io.h>
+#endif
+
using namespace lldb_private;
namespace {
@@ -150,6 +155,44 @@ TEST_F(MainLoopTest, MultipleReadsPipeObject) {
}
#endif
+// Registers read_fd, optionally lets the loop poll it once, and drops the
+// handle while the write end of the pipe is still open.
+static void UnregisterWithOpenWriter(int read_fd, bool poll) {
+ MainLoop loop;
+ Status error;
+ auto handle = loop.RegisterReadObject(
+ std::make_shared<NativeFile>(read_fd, File::eOpenOptionReadOnly, false),
+ [](MainLoopBase &) {}, error);
+ ASSERT_THAT_ERROR(error.ToError(), llvm::Succeeded());
+ if (poll) {
+ loop.AddPendingCallback(
+ [](MainLoopBase &loop) { loop.RequestTermination(); });
+ ASSERT_THAT_ERROR(loop.Run().ToError(), llvm::Succeeded());
+ }
+}
+
+TEST_F(MainLoopTest, UnregisterPipeWithOpenWriter) {
+ for (int i = 0; i < 100; ++i) {
+ Pipe pipe;
+ ASSERT_THAT_ERROR(pipe.CreateNew().ToError(), llvm::Succeeded());
+ ASSERT_NO_FATAL_FAILURE(
+ UnregisterWithOpenWriter(pipe.GetReadFileDescriptor(), i % 2));
+ }
+}
+
+#ifdef _WIN32
+// The CRT's pipes, unlike lldb's, are not opened for overlapped I/O.
+TEST_F(MainLoopTest, UnregisterSynchronousPipeWithOpenWriter) {
+ for (int i = 0; i < 100; ++i) {
+ int fds[2];
+ ASSERT_EQ(_pipe(fds, 4096, _O_BINARY), 0);
+ ASSERT_NO_FATAL_FAILURE(UnregisterWithOpenWriter(fds[0], i % 2));
+ _close(fds[0]);
+ _close(fds[1]);
+ }
+}
+#endif
+
TEST_F(MainLoopTest, PipeDelayBetweenRegisterAndRun) {
Pipe pipe;
More information about the lldb-commits
mailing list