Skip to content

Commit 3dcb5ff

Browse files
committed
[lldb-dap] fix crash if disconnect request is not sent.
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.
1 parent 6146a88 commit 3dcb5ff

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "lldb/lldb-defines.h"
2323
#include "lldb/lldb-enumerations.h"
2424
#include "llvm/ADT/ArrayRef.h"
25+
#include "llvm/ADT/ScopeExit.h"
2526
#include "llvm/ADT/StringExtras.h"
2627
#include "llvm/ADT/Twine.h"
2728
#include "llvm/Support/Error.h"
@@ -224,6 +225,16 @@ llvm::Error DAP::ConfigureIO(std::FILE *overrideOut, std::FILE *overrideErr) {
224225
void DAP::StopIO() {
225226
out.Stop();
226227
err.Stop();
228+
229+
if (event_thread.joinable()) {
230+
broadcaster.BroadcastEventByType(eBroadcastBitStopEventThread);
231+
event_thread.join();
232+
}
233+
234+
if (progress_event_thread.joinable()) {
235+
broadcaster.BroadcastEventByType(eBroadcastBitStopProgressThread);
236+
progress_event_thread.join();
237+
}
227238
}
228239

229240
// 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) {
794805
}
795806

796807
llvm::Error DAP::Loop() {
808+
auto stop_io = llvm::make_scope_exit([this]() { StopIO(); });
797809
while (!disconnecting) {
798810
llvm::json::Object object;
799811
lldb_dap::PacketStatus status = GetNextObject(object);

lldb/tools/lldb-dap/lldb-dap.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,15 +1096,6 @@ void request_disconnect(DAP &dap, const llvm::json::Object &request) {
10961096
}
10971097
SendTerminatedEvent(dap);
10981098
dap.SendJSON(llvm::json::Value(std::move(response)));
1099-
if (dap.event_thread.joinable()) {
1100-
dap.broadcaster.BroadcastEventByType(eBroadcastBitStopEventThread);
1101-
dap.event_thread.join();
1102-
}
1103-
if (dap.progress_event_thread.joinable()) {
1104-
dap.broadcaster.BroadcastEventByType(eBroadcastBitStopProgressThread);
1105-
dap.progress_event_thread.join();
1106-
}
1107-
dap.StopIO();
11081099
dap.disconnecting = true;
11091100
}
11101101

0 commit comments

Comments
 (0)