File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -898,6 +898,26 @@ bool DAP::HandleObject(const Message &M) {
898898}
899899
900900void 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 ();
You can’t perform that action at this time.
0 commit comments