Fix TSan data race in UDP Socket Tests

TSan flags concurrent socket operations on the identical host FD
as a data race. This commit uses dup() to duplicate the socket file
descriptor specifically for the background thread's connect() call,
so sendto and connect can execute concurrently from two different
threads without TSan data race on the fd struct.

PiperOrigin-RevId: 955479183
This commit is contained in:
Xin Zhong
2026-07-28 14:41:55 -07:00
committed by gVisor bot
parent 2ae334d8a1
commit f38a547aaa
+10 -2
View File
@@ -430,8 +430,16 @@ TEST_P(UdpSocketTest, ConnectSimultaneousWriteToInvalidPort) {
ASSERT_THAT(close(s.release()), SyscallSucceeds());
// Now connect to the port that we just released.
ScopedThread t([&] {
ASSERT_THAT(connect(sock_.get(), addr, addrlen_), SyscallSucceeds());
//
// Duplicate the socket file descriptor to prevent a TSan data race.
// TSan flags concurrent socket operations on the identical host FD
// (from the concurrent sendto() in the main thread) as a data race,
// so use a separate FD pointing to the same underlying socket.
int dup_fd = dup(sock_.get());
ASSERT_GE(dup_fd, 0);
ScopedThread t([&, dup_fd] {
ASSERT_THAT(connect(dup_fd, addr, addrlen_), SyscallSucceeds());
close(dup_fd);
});
char buf[512] = {};