Skip to content

Commit b011856

Browse files
committed
Fix race condition
1 parent 275db02 commit b011856

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,26 @@ bool DAP::HandleObject(const Message &M) {
898898
}
899899

900900
void DAP::SendTerminatedEvent() {
901+
// The folloiwng is to prevent races between the request handler thread and
902+
// the event thread, where we are being asked to disconnect while the debugee
903+
// process exits.
904+
//
905+
// The request handler thread, when entering this method, would have already
906+
// acquired the API mutex (in BaseRequestHandler::Run). The next thing it
907+
// tries to do is to acquire the call_once() mutex below.
908+
//
909+
// A deadlock will happen if the event thread happens to acquire the
910+
// call_once() mutex first, then go into SBDebugger::Destroy() and eventually
911+
// Target::Destroy(), where it tries to acquire the API mutex.
912+
//
913+
// To avoid this deadlock, we require that both threads acquire the API mutex
914+
// first. Whoever gets it can go through the call_once() without issue.
915+
//
916+
// This is during the termination of a debug session, so performance loss
917+
// introduced by this additional lock are hopefully not too critical.
918+
lldb::SBMutex lock = GetAPIMutex();
919+
std::lock_guard<lldb::SBMutex> guard(lock);
920+
901921
// Prevent races if the process exits while we're being asked to disconnect.
902922
llvm::call_once(terminated_event_flag, [&] {
903923
RunTerminateCommands();

0 commit comments

Comments
 (0)