From db9fab290c26e0cccf49386c9f6fc075b7691b8d Mon Sep 17 00:00:00 2001 From: Peter Johnston Date: Mon, 8 Jul 2024 07:16:58 -0700 Subject: [PATCH] Fix a race condition in TCPDeferAcceptTimeout The test is timing-dependent and it's possible for it to sleep for longer than the TCP_DEFER_ACCEPT timeout. We currently check if this happened after sleeping and skip the test if so, but we call `accept` _after_ this check, so the race still exists. Move the `accept` call to before the check. PiperOrigin-RevId: 650240216 --- test/syscalls/linux/socket_inet_loopback.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/syscalls/linux/socket_inet_loopback.cc b/test/syscalls/linux/socket_inet_loopback.cc index 2d6516178..e2c3ae624 100644 --- a/test/syscalls/linux/socket_inet_loopback.cc +++ b/test/syscalls/linux/socket_inet_loopback.cc @@ -1525,13 +1525,13 @@ TEST_P(SocketInetLoopbackTest, TCPDeferAcceptTimeout) { // timeout is hit. const auto start = absl::Now(); absl::SleepFor(absl::Seconds(kTCPDeferAccept - 1)); + const int result = accept(listen_fd.get(), nullptr, nullptr); // It's possible that we ended up sleeping for longer than the // TCP_DEFER_ACCEPT timeout. If this happens, skip this test. if (absl::Now() >= start + absl::Seconds(kTCPDeferAccept)) { GTEST_SKIP(); } - ASSERT_THAT(accept(listen_fd.get(), nullptr, nullptr), - SyscallFailsWithErrno(EWOULDBLOCK)); + ASSERT_THAT(result, SyscallFailsWithErrno(EWOULDBLOCK)); // Set FD back to blocking. opts &= ~O_NONBLOCK;