Skip to content

[lldb-dap] fix crash if disconnect request is not sent. #148878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/20.x
Choose a base branch
from

Conversation

da-viper
Copy link
Contributor

It crashes because the event_thread and progress_event_thread not joined before calling DAP struct deconstructor.

It now joins when we leave the DAP::Loop function.

from Discourse

@da-viper da-viper added this to the LLVM 20.X Release milestone Jul 15, 2025
@da-viper da-viper requested a review from ashgti July 15, 2025 16:02
@da-viper da-viper requested a review from JDevlieghere as a code owner July 15, 2025 16:02
@llvmbot llvmbot added the lldb label Jul 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 15, 2025

@llvm/pr-subscribers-lldb

Author: Ebuka Ezike (da-viper)

Changes

It crashes because the event_thread and progress_event_thread not joined before calling DAP struct deconstructor.

It now joins when we leave the DAP::Loop function.

from Discourse


Full diff: https://github.com/llvm/llvm-project/pull/148878.diff

2 Files Affected:

  • (modified) lldb/tools/lldb-dap/DAP.cpp (+12)
  • (modified) lldb/tools/lldb-dap/lldb-dap.cpp (-9)
diff --git a/lldb/tools/lldb-dap/DAP.cpp b/lldb/tools/lldb-dap/DAP.cpp
index a67abe582abd4..134ddaa4c8f59 100644
--- a/lldb/tools/lldb-dap/DAP.cpp
+++ b/lldb/tools/lldb-dap/DAP.cpp
@@ -22,6 +22,7 @@
 #include "lldb/lldb-defines.h"
 #include "lldb/lldb-enumerations.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/Error.h"
@@ -224,6 +225,16 @@ llvm::Error DAP::ConfigureIO(std::FILE *overrideOut, std::FILE *overrideErr) {
 void DAP::StopIO() {
   out.Stop();
   err.Stop();
+
+  if (event_thread.joinable()) {
+    broadcaster.BroadcastEventByType(eBroadcastBitStopEventThread);
+    event_thread.join();
+  }
+
+  if (progress_event_thread.joinable()) {
+    broadcaster.BroadcastEventByType(eBroadcastBitStopProgressThread);
+    progress_event_thread.join();
+  }
 }
 
 // Send the JSON in "json_str" to the "out" stream. Correctly send the
@@ -794,6 +805,7 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
 }
 
 llvm::Error DAP::Loop() {
+  auto stop_io = llvm::make_scope_exit([this]() { StopIO(); });
   while (!disconnecting) {
     llvm::json::Object object;
     lldb_dap::PacketStatus status = GetNextObject(object);
diff --git a/lldb/tools/lldb-dap/lldb-dap.cpp b/lldb/tools/lldb-dap/lldb-dap.cpp
index 9e0e7f21ce4fc..02688fc858f6b 100644
--- a/lldb/tools/lldb-dap/lldb-dap.cpp
+++ b/lldb/tools/lldb-dap/lldb-dap.cpp
@@ -1096,15 +1096,6 @@ void request_disconnect(DAP &dap, const llvm::json::Object &request) {
   }
   SendTerminatedEvent(dap);
   dap.SendJSON(llvm::json::Value(std::move(response)));
-  if (dap.event_thread.joinable()) {
-    dap.broadcaster.BroadcastEventByType(eBroadcastBitStopEventThread);
-    dap.event_thread.join();
-  }
-  if (dap.progress_event_thread.joinable()) {
-    dap.broadcaster.BroadcastEventByType(eBroadcastBitStopProgressThread);
-    dap.progress_event_thread.join();
-  }
-  dap.StopIO();
   dap.disconnecting = true;
 }
 

It crashes because the event_thread and progress_event_thread not joined before calling DAP struct deconstructor.

It now joins when we leave the main loop.
@da-viper da-viper force-pushed the join-event-threads branch from 3dcb5ff to 5087ed5 Compare July 15, 2025 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants