[lldb-vscode] Fix data race in lldb-vscode when running with ThreadSanitizer
authorWalter Erquinigo <wallace@fb.com>
Tue, 17 May 2022 15:53:51 +0000 (08:53 -0700)
committerWalter Erquinigo <wallace@fb.com>
Tue, 17 May 2022 16:11:45 +0000 (09:11 -0700)
This patch fixes https://github.com/llvm/llvm-project/issues/54768. A ProgressEventReporter creates a dedicated thread that keeps checking whether there are new events that need to be sent to IDE as long as m_thread_should_exit is true. When the VSCode instance is destructed, it will set m_thread_should_exit to false, which caused a data race because at the same time its ProgressEventReporter is reading this value to determine whether it should quit. This fix simply uses mutex to ensure they cannot read and write this value at the same time.

Committed on behalf of PRESIDENT810

Reviewed By: clayborg, wallace

Differential Revision: https://reviews.llvm.org/D125073

lldb/tools/lldb-vscode/ProgressEvent.h

index 627d723..7fa79ff 100644 (file)
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <atomic>
 #include <mutex>
 #include <queue>
 #include <thread>
@@ -149,7 +150,7 @@ private:
   std::queue<ProgressEventManagerSP> m_unreported_start_events;
   /// Thread used to invoke \a ReportStartEvents periodically.
   std::thread m_thread;
-  bool m_thread_should_exit;
+  std::atomic<bool> m_thread_should_exit;
   /// Mutex that prevents running \a Push and \a ReportStartEvents
   /// simultaneously, as both read and modify the same underlying objects.
   std::mutex m_mutex;