Skip to content

Commit ee86691

Browse files
da-viperaokblast
authored andcommitted
[lldb-dap] Fix mutex acquisition order for modules event. (llvm#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.
1 parent e08226b commit ee86691

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "lldb/API/SBEvent.h"
2727
#include "lldb/API/SBLanguageRuntime.h"
2828
#include "lldb/API/SBListener.h"
29+
#include "lldb/API/SBMutex.h"
2930
#include "lldb/API/SBProcess.h"
3031
#include "lldb/API/SBStream.h"
3132
#include "lldb/Host/JSONTransport.h"
@@ -1452,7 +1453,11 @@ void DAP::EventThread() {
14521453
const bool remove_module =
14531454
event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded;
14541455

1455-
std::lock_guard<std::mutex> guard(modules_mutex);
1456+
// NOTE: Both mutexes must be acquired to prevent deadlock when
1457+
// handling `modules_request`, which also requires both locks.
1458+
lldb::SBMutex api_mutex = GetAPIMutex();
1459+
const std::scoped_lock<lldb::SBMutex, std::mutex> guard(
1460+
api_mutex, modules_mutex);
14561461
for (uint32_t i = 0; i < num_modules; ++i) {
14571462
lldb::SBModule module =
14581463
lldb::SBTarget::GetModuleAtIndexFromEvent(i, event);

0 commit comments

Comments
 (0)