Skip to content

Commit 0bdbe7b

Browse files
committed
[lldb] Fix data race in ConnectionFileDescriptor
TSan reports the following data race: Write of size 4 at 0x000109e0b160 by thread T2 (mutexes: write M0, write M1): #0 NativeFile::Close() File.cpp:329 #1 ConnectionFileDescriptor::Disconnect(lldb_private::Status*) ConnectionFileDescriptorPosix.cpp:232 rust-lang#2 Communication::Disconnect(lldb_private::Status*) Communication.cpp:61 rust-lang#3 process_gdb_remote::ProcessGDBRemote::DidExit() ProcessGDBRemote.cpp:1164 rust-lang#4 Process::SetExitStatus(int, char const*) Process.cpp:1097 rust-lang#5 process_gdb_remote::ProcessGDBRemote::MonitorDebugserverProcess(...) ProcessGDBRemote.cpp:3387 Previous read of size 4 at 0x000109e0b160 by main thread (mutexes: write M2): #0 NativeFile::IsValid() const File.h:393 #1 ConnectionFileDescriptor::IsConnected() const ConnectionFileDescriptorPosix.cpp:121 rust-lang#2 Communication::IsConnected() const Communication.cpp:79 rust-lang#3 process_gdb_remote::GDBRemoteCommunication::WaitForPacketNoLock(...) GDBRemoteCommunication.cpp:256 rust-lang#4 process_gdb_remote::GDBRemoteCommunication::WaitForPacketNoLock(...l) GDBRemoteCommunication.cpp:244 rust-lang#5 process_gdb_remote::GDBRemoteClientBase::SendPacketAndWaitForResponseNoLock(llvm::StringRef, StringExtractorGDBRemote&) GDBRemoteClientBase.cpp:246 The problem is that in WaitForPacketNoLock's run loop, it checks that the connection is still connected. This races with the ConnectionFileDescriptor disconnecting. Most (but not all) access to the IOObject in ConnectionFileDescriptorPosix is already gated by the mutex. This patch just protects IsConnected in the same way. Differential revision: https://reviews.llvm.org/D157347
1 parent 0664db5 commit 0bdbe7b

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

lldb/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class ConnectionFileDescriptor : public Connection {
131131
lldb::IOObjectSP m_io_sp;
132132

133133
Pipe m_pipe;
134-
std::recursive_mutex m_mutex;
134+
mutable std::recursive_mutex m_mutex;
135135
std::atomic<bool> m_shutting_down; // This marks that we are shutting down so
136136
// if we get woken up from
137137
// BytesAvailable to disconnect, we won't try to read again.

lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ void ConnectionFileDescriptor::CloseCommandPipe() {
118118
}
119119

120120
bool ConnectionFileDescriptor::IsConnected() const {
121+
std::lock_guard<std::recursive_mutex> guard(m_mutex);
121122
return m_io_sp && m_io_sp->IsValid();
122123
}
123124

0 commit comments

Comments
 (0)