[Lldb-commits] [lldb] [lldb][Windows] Lock the thread list when lldb-server adds a thread (PR #226974)

Charles Zablit via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 28 05:52:19 PDT 2026


https://github.com/charles-zablit created https://github.com/llvm/llvm-project/pull/226974

`NativeProcessProtocol::Threads()` returns a `LockingAdaptedIterable` that holds `m_threads_mutex` for the lifetime of the iteration, so callers may hold references into `m_threads` across the loop body. On Windows, this is only done when removing threads:

```
  // OnCreateThread
  m_threads.push_back(std::move(thread));
...
  // OnExitThread
  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
  llvm::erase_if(m_threads, ...);
```

`m_threads` is a `std::vector<std::unique_ptr<NativeThreadProtocol>>`. When the debug-event thread appends and the vector reallocates, every reference held by a concurrent iteration on the main loop dangles. This takes lldb-server down with an access violation:

```
Exception Code: 0xC0000005
 #0 NativeThreadProtocol::GetID   NativeThreadProtocol.h:40
 #1 GetJSONThreadsInfo            GDBRemoteCommunicationServerLLGS.cpp:819
 #4 Handle_jThreadsInfo
 #8 GetPacketAndSendResponse
 #9 DataAvailableCallback         :1271
```

>From 374bd2c6191f7521ec46e4e65a4cb0fa619fab09 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Mon, 28 Sep 2026 13:49:45 +0100
Subject: [PATCH] [lldb][Windows] Lock the thread list when lldb-server adds a
 thread

---
 .../Plugins/Process/Windows/Common/NativeProcessWindows.cpp     | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp
index 849931eaec372..c24eebe82fd8d 100644
--- a/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp
@@ -558,6 +558,7 @@ void NativeProcessWindows::OnDebuggerConnected(lldb::addr_t image_base) {
   }
 
   // The very first one shall always be the main thread.
+  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
   assert(m_threads.empty());
   m_threads.push_back(std::make_unique<NativeThreadWindows>(
       *this, m_session_data->m_debugger->GetMainThread()));
@@ -783,6 +784,7 @@ void NativeProcessWindows::OnCreateThread(const HostThread &new_thread) {
     thread->SetStopReason(stop_info, "");
   }
 
+  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
   m_threads.push_back(std::move(thread));
 }
 



More information about the lldb-commits mailing list