Skip to content

[lldb-dap] Fix a race during shutdown #91591

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

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lldb/tools/lldb-dap/DAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ DAP::DAP()
{"objc_throw", "Objective-C Throw", lldb::eLanguageTypeObjC},
{"swift_catch", "Swift Catch", lldb::eLanguageTypeSwift},
{"swift_throw", "Swift Throw", lldb::eLanguageTypeSwift}}),
focus_tid(LLDB_INVALID_THREAD_ID), sent_terminated_event(false),
stop_at_entry(false), is_attach(false),
focus_tid(LLDB_INVALID_THREAD_ID), stop_at_entry(false), is_attach(false),
enable_auto_variable_summaries(false),
enable_synthetic_child_debugging(false),
restarting_process_id(LLDB_INVALID_PROCESS_ID),
Expand Down Expand Up @@ -623,7 +622,7 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
}

llvm::Error DAP::Loop() {
while (!sent_terminated_event) {
while (!disconnecting) {
llvm::json::Object object;
lldb_dap::PacketStatus status = GetNextObject(object);

Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-dap/DAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct DAP {
// arguments if we get a RestartRequest.
std::optional<llvm::json::Object> last_launch_or_attach_request;
lldb::tid_t focus_tid;
std::atomic<bool> sent_terminated_event;
bool disconnecting = false;
bool stop_at_entry;
bool is_attach;
bool enable_auto_variable_summaries;
Expand Down
25 changes: 7 additions & 18 deletions lldb/tools/lldb-dap/lldb-dap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,26 +226,14 @@ void SendContinuedEvent() {
// Send a "terminated" event to indicate the process is done being
// debugged.
void SendTerminatedEvent() {
// If an inferior exits prior to the processing of a disconnect request, then
// the threads executing EventThreadFunction and request_discontinue
// respectively may call SendTerminatedEvent simultaneously. Without any
// synchronization, the thread executing EventThreadFunction may set
// g_dap.sent_terminated_event before the thread executing
// request_discontinue has had a chance to test it, in which case the latter
// would move ahead to issue a response to the disconnect request. Said
// response may get dispatched ahead of the terminated event compelling the
// client to terminate the debug session without consuming any console output
// that might've been generated by the execution of terminateCommands. So,
// synchronize simultaneous calls to SendTerminatedEvent.
// Prevent races if the process exits while we're being asked to disconnect.
static std::mutex mutex;
std::lock_guard<std::mutex> locker(mutex);
if (!g_dap.sent_terminated_event) {
g_dap.sent_terminated_event = true;
g_dap.RunTerminateCommands();
// Send a "terminated" event
llvm::json::Object event(CreateTerminatedEventObject());
g_dap.SendJSON(llvm::json::Value(std::move(event)));
}

g_dap.RunTerminateCommands();
// Send a "terminated" event
llvm::json::Object event(CreateTerminatedEventObject());
g_dap.SendJSON(llvm::json::Value(std::move(event)));
}

// Send a thread stopped event for all threads as long as the process
Expand Down Expand Up @@ -1003,6 +991,7 @@ void request_disconnect(const llvm::json::Object &request) {
g_dap.broadcaster.BroadcastEventByType(eBroadcastBitStopProgressThread);
g_dap.progress_event_thread.join();
}
g_dap.disconnecting = true;
}

void request_exceptionInfo(const llvm::json::Object &request) {
Expand Down
Loading