[Lldb-commits] [lldb] [lldb][Windows] Lock the thread list when lldb-server adds a thread (PR #226974)
via lldb-commits
lldb-commits at lists.llvm.org
Mon Sep 28 05:52:58 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Charles Zablit (charles-zablit)
<details>
<summary>Changes</summary>
`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
```
---
Full diff: https://github.com/llvm/llvm-project/pull/226974.diff
1 Files Affected:
- (modified) lldb/source/Plugins/Process/Windows/Common/NativeProcessWindows.cpp (+2)
``````````diff
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));
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/226974
More information about the lldb-commits
mailing list