[Lldb-commits] [PATCH] D132577: [lldb] [Core] Pass error/status from Communication::ReadThread()

Michał Górny via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Aug 26 10:14:33 PDT 2022


mgorny updated this revision to Diff 455950.
mgorny added a comment.

Rebase, move `StopReadThread()` test to the other patch.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132577/new/

https://reviews.llvm.org/D132577

Files:
  lldb/include/lldb/Core/Communication.h
  lldb/source/Core/Communication.cpp
  lldb/unittests/Core/CommunicationTest.cpp


Index: lldb/unittests/Core/CommunicationTest.cpp
===================================================================
--- lldb/unittests/Core/CommunicationTest.cpp
+++ lldb/unittests/Core/CommunicationTest.cpp
@@ -15,6 +15,9 @@
 #include "TestingSupport/Host/SocketTestUtilities.h"
 #include "TestingSupport/SubsystemRAII.h"
 
+#include <chrono>
+#include <condition_variable>
+#include <mutex>
 #include <thread>
 
 #if LLDB_ENABLE_POSIX
@@ -78,9 +81,31 @@
   EXPECT_EQ(status, lldb::eConnectionStatusEndOfFile);
   EXPECT_THAT_ERROR(error.ToError(), llvm::Succeeded());
 
-  // JoinReadThread() should just return immediately since there was no read
+  // JoinReadThread() should just return immediately if there was no read
   // thread started.
   EXPECT_TRUE(comm.JoinReadThread());
+
+  // Test using Communication that is disconnected.
+  ASSERT_EQ(comm.Disconnect(), lldb::eConnectionStatusSuccess);
+  if (use_read_thread)
+    ASSERT_TRUE(comm.StartReadThread());
+  error.Clear();
+  EXPECT_EQ(
+      comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 0U);
+  EXPECT_EQ(status, lldb::eConnectionStatusLostConnection);
+  EXPECT_THAT_ERROR(error.ToError(), llvm::Failed());
+  EXPECT_TRUE(comm.JoinReadThread());
+
+  // Test using Communication without a connection.
+  comm.SetConnection(nullptr);
+  if (use_read_thread)
+    ASSERT_TRUE(comm.StartReadThread());
+  error.Clear();
+  EXPECT_EQ(
+      comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 0U);
+  EXPECT_EQ(status, lldb::eConnectionStatusNoConnection);
+  EXPECT_THAT_ERROR(error.ToError(), llvm::Failed());
+  EXPECT_TRUE(comm.JoinReadThread());
 }
 
 TEST_F(CommunicationTest, Read) {
Index: lldb/source/Core/Communication.cpp
===================================================================
--- lldb/source/Core/Communication.cpp
+++ lldb/source/Core/Communication.cpp
@@ -162,14 +162,10 @@
 
       if (event_type & eBroadcastBitReadThreadDidExit) {
         // If the thread exited of its own accord, it either means it
-        // hit an end-of-file condition or lost connection
-        // (we verified that we had an connection above).
-        if (!m_connection_sp) {
-          if (error_ptr)
-            error_ptr->SetErrorString("Lost connection.");
-          status = eConnectionStatusLostConnection;
-        } else
-          status = eConnectionStatusEndOfFile;
+        // hit an end-of-file condition or an error.
+        status = m_pass_status;
+        if (error_ptr)
+          *error_ptr = std::move(m_pass_error);
 
         if (GetCloseOnEOF())
           Disconnect(nullptr);
@@ -384,7 +380,8 @@
       break;
     }
   }
-  log = GetLog(LLDBLog::Communication);
+  m_pass_status = status;
+  m_pass_error = std::move(error);
   LLDB_LOG(log, "Communication({0}) thread exiting...", this);
 
   // Handle threads wishing to synchronize with us.
Index: lldb/include/lldb/Core/Communication.h
===================================================================
--- lldb/include/lldb/Core/Communication.h
+++ lldb/include/lldb/Core/Communication.h
@@ -324,6 +324,9 @@
       m_bytes; ///< A buffer to cache bytes read in the ReadThread function.
   std::recursive_mutex m_bytes_mutex; ///< A mutex to protect multi-threaded
                                       ///access to the cached bytes.
+  lldb::ConnectionStatus m_pass_status; ///< Connection status passthrough
+                                        ///from read thread.
+  Status m_pass_error; ///< Error passthrough from read thread.
   std::mutex
       m_write_mutex; ///< Don't let multiple threads write at the same time...
   std::mutex m_synchronize_mutex;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132577.455950.patch
Type: text/x-patch
Size: 3669 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220826/74286ca0/attachment-0001.bin>


More information about the lldb-commits mailing list