From ca3c23db50a94003d783b00ed7ae5af74ae884ba Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Wed, 15 Jan 2025 13:33:36 -0800 Subject: [PATCH] tests: Deflake socket_inet_loopback_isolated_test This test case creates one connection, then it closes one end of it. After that it waits for a linger timeout and tries to create the second connection with the same client source port. The test is flaky if the second half of the fist connection isn't closed before creating the second connection. PiperOrigin-RevId: 715924851 --- runsc/boot/network.go | 6 +++++- test/syscalls/linux/socket_inet_loopback_isolated.cc | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/runsc/boot/network.go b/runsc/boot/network.go index 08cecc9f4..19bf6d6f7 100644 --- a/runsc/boot/network.go +++ b/runsc/boot/network.go @@ -295,7 +295,11 @@ func (n *Network) CreateLinksAndRoutes(args *CreateLinksAndRoutesArgs, _ *struct nicID := n.Stack.NextNICID() nicids[link.Name] = nicID - linkEP := ethernet.New(loopback.New()) + var linkEP stack.LinkEndpoint + linkEP = ethernet.New(loopback.New()) + if args.LogPackets { + linkEP = sniffer.New(linkEP) + } log.Infof("Enabling loopback interface %q with id %d on addresses %+v", link.Name, nicID, link.Addresses) opts := stack.NICOptions{ diff --git a/test/syscalls/linux/socket_inet_loopback_isolated.cc b/test/syscalls/linux/socket_inet_loopback_isolated.cc index 1f3728ccb..6d771f752 100644 --- a/test/syscalls/linux/socket_inet_loopback_isolated.cc +++ b/test/syscalls/linux/socket_inet_loopback_isolated.cc @@ -210,6 +210,13 @@ TEST_P(SocketInetLoopbackIsolatedTest, TCPFinWait2Test) { ASSERT_THAT(bind(conn_fd2.get(), AsSockAddr(&conn_bound_addr), conn_addrlen), SyscallSucceeds()); + // Close the `accepted` end otherwise connect can return ECONNREFUSED. + constexpr int kTCPLingerTimeout0 = 0; + EXPECT_THAT(setsockopt(accepted.get(), IPPROTO_TCP, TCP_LINGER2, + &kTCPLingerTimeout0, sizeof(kTCPLingerTimeout0)), + SyscallSucceedsWithValue(0)); + shutdown(accepted.get(), SHUT_WR); + ASSERT_THAT( RetryEINTR(connect)(conn_fd2.get(), AsSockAddr(&conn_addr), conn_addrlen), SyscallSucceeds());