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
This commit is contained in:
Peter Johnston
2024-07-08 07:20:38 -07:00
committed by gVisor bot
parent 222258a585
commit db9fab290c
+2 -2
View File
@@ -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;