diff --git a/lldb/tools/lldb-dap/DAP.h b/lldb/tools/lldb-dap/DAP.h index bbd9d46ba3a04..a88ee3e1dec6b 100644 --- a/lldb/tools/lldb-dap/DAP.h +++ b/lldb/tools/lldb-dap/DAP.h @@ -26,6 +26,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/JSON.h" +#include "llvm/Support/Threading.h" #include "llvm/Support/raw_ostream.h" #include "lldb/API/SBAttachInfo.h" @@ -169,6 +170,7 @@ struct DAP { std::optional last_launch_or_attach_request; lldb::tid_t focus_tid; bool disconnecting = false; + llvm::once_flag terminated_event_flag; bool stop_at_entry; bool is_attach; bool enable_auto_variable_summaries; diff --git a/lldb/tools/lldb-dap/lldb-dap.cpp b/lldb/tools/lldb-dap/lldb-dap.cpp index 170fa88f1e8b8..7746afb6cbbf3 100644 --- a/lldb/tools/lldb-dap/lldb-dap.cpp +++ b/lldb/tools/lldb-dap/lldb-dap.cpp @@ -227,13 +227,12 @@ void SendContinuedEvent() { // debugged. void SendTerminatedEvent() { // Prevent races if the process exits while we're being asked to disconnect. - static std::mutex mutex; - std::lock_guard locker(mutex); - - g_dap.RunTerminateCommands(); - // Send a "terminated" event - llvm::json::Object event(CreateTerminatedEventObject()); - g_dap.SendJSON(llvm::json::Value(std::move(event))); + llvm::call_once(g_dap.terminated_event_flag, [&] { + g_dap.RunTerminateCommands(); + // Send a "terminated" event + llvm::json::Object event(CreateTerminatedEventObject()); + g_dap.SendJSON(llvm::json::Value(std::move(event))); + }); } // Send a thread stopped event for all threads as long as the process