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; 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/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 +#include +#include #include #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) {