diff --git a/lldb/tools/lldb-vscode/lldb-vscode.cpp b/lldb/tools/lldb-vscode/lldb-vscode.cpp --- a/lldb/tools/lldb-vscode/lldb-vscode.cpp +++ b/lldb/tools/lldb-vscode/lldb-vscode.cpp @@ -179,6 +179,19 @@ // 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_vsc.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. + static std::mutex mutex; + std::lock_guard locker(mutex); if (!g_vsc.sent_terminated_event) { g_vsc.sent_terminated_event = true; g_vsc.RunTerminateCommands();