llvm / llvm/llvm-project

[lldb-dap] Deadlock when disconnecting on DAP::SendTerminatedEvent()

Open
#194,670 5 comments 0 reactions 0 assignees View on GitHub
lldb-dap
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

We currently call `DAP::SendTerminatedEvent` once for every DAP session.

```cpp
void DAP::SendTerminatedEvent() {
// Prevent races if the process exits while we're being asked to disconnect.
llvm::call_once(terminated_event_flag, [&] {
RunTerminateCommands();
// Send a "terminated" event
llvm::json::Object event(CreateTerminatedEventObject(target));
SendJSON(llvm::json::Value(std::move(event)));
});
}
```
But It deadlocks on high CPU load if DAP disconnects in the following sequence.
```
[main thread] DisconnectRequestHandler (Holds API mutex)
[event thread] Process Exited dap.SendTerminatedEvent. (holds call_once mutex and tries to acquire API mutex).
[main thread] Disconnect dap.SendTerminatedEvent. (already holds API mutex and waits for call_once to complete).
```

Looking at our [docs](https://lldb.llvm.org/use/lldbdap.html#configuration-settings-reference).
```
terminateCommands | [string] | LLDB commands executed when the debugging session ends.
```
We should only execute the `terminateCommands` when the session ends not when the process ends. It would make sense only run the terminate commands when we disconnect (in the Main thread). unless I am missing something ?.

cc: @ashgti @DrSergei @walter-erquinigo

Contributor guide

Open the contributing guide

Research direction

Start by tracing DAP::SendTerminatedEvent from DisconnectRequestHandler and the process-exit path, focusing on RunTerminateCommands, SendJSON, and the API mutex/call_once interaction. Check the lldbdap configuration settings reference for the intended terminateCommands behavior. Done means disconnecting under load cannot deadlock and terminateCommands run only when the debugging session ends.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.