Skip to content

Commit a803ad2

Browse files
committed
Move debugger destroy to the end of the DAP lifecycle
1 parent b011856 commit a803ad2

File tree

1 file changed

+5
-26
lines changed

1 file changed

+5
-26
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -898,38 +898,12 @@ 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-
921901
// Prevent races if the process exits while we're being asked to disconnect.
922902
llvm::call_once(terminated_event_flag, [&] {
923903
RunTerminateCommands();
924-
925904
// Send a "terminated" event
926905
llvm::json::Object event(CreateTerminatedEventObject(target));
927906
SendJSON(llvm::json::Value(std::move(event)));
928-
929-
// Destroy the debugger when the session ends. This will trigger the
930-
// debugger's destroy callbacks for earlier logging and clean-ups, rather
931-
// than waiting for the termination of the lldb-dap process.
932-
lldb::SBDebugger::Destroy(debugger);
933907
});
934908
}
935909

@@ -1094,6 +1068,11 @@ llvm::Error DAP::Loop() {
10941068
out.Stop();
10951069
err.Stop();
10961070
StopEventHandlers();
1071+
1072+
// Destroy the debugger when the session ends. This will trigger the
1073+
// debugger's destroy callbacks for earlier logging and clean-ups, rather
1074+
// than waiting for the termination of the lldb-dap process.
1075+
lldb::SBDebugger::Destroy(debugger);
10971076
});
10981077

10991078
while (true) {

0 commit comments

Comments
 (0)