https://github.com/da-viper created 
https://github.com/llvm/llvm-project/pull/163821

The modules event requires the `APIMutex` to create the module load address. 
this may happen at the same time the `module request` is handled.

The modules request also requires the `APIMutex` and the `modules_mutex, set 
the order to acquire the mutexes.

>From b10c3b8725a7d219f3bae07d06105ec1a28ecd13 Mon Sep 17 00:00:00 2001
From: Ebuka Ezike <[email protected]>
Date: Thu, 16 Oct 2025 17:40:21 +0100
Subject: [PATCH] [lldb-dap] Fix mutex acquisition order for modules event.

The modules event requires the `APIMutex` to create the module
load address. this may happen at the same time the `module
request` is handled.

The modules request also requires the `APIMutex` and the `modules_mutex,
set the order to acquire the mutexes.
---
 lldb/tools/lldb-dap/DAP.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lldb/tools/lldb-dap/DAP.cpp b/lldb/tools/lldb-dap/DAP.cpp
index f76656e98ca01..003b55fa31610 100644
--- a/lldb/tools/lldb-dap/DAP.cpp
+++ b/lldb/tools/lldb-dap/DAP.cpp
@@ -1437,7 +1437,12 @@ void DAP::EventThread() {
           const bool remove_module =
               event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded;
 
-          std::lock_guard<std::mutex> guard(modules_mutex);
+          // NOTE: Both mutexes must be acquired (API mutex first, then modules
+          // mutex) to prevent deadlock when handling `modules_request`, which
+          // also requires both locks.
+          lldb::SBMutex api_mutex = GetAPIMutex();
+          std::lock_guard api_guard(api_mutex);
+          std::lock_guard modules_guard(modules_mutex);
           for (uint32_t i = 0; i < num_modules; ++i) {
             lldb::SBModule module =
                 lldb::SBTarget::GetModuleAtIndexFromEvent(i, event);

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to