From edd0b8d7075bc6bb155bc9a9ebd08cdfff145e10 Mon Sep 17 00:00:00 2001 From: Vishvananda Abrams Date: Wed, 27 Aug 2025 19:44:58 +0000 Subject: [PATCH] tests: Stabilize TestLinkXdp against race condition The TestLinkXdp test was observed to be flaky in CI environments. The failure occurred because the test expects the second call to LinkSetXdpFdWithFlags with the XDP_FLAGS_UPDATE_IF_NOEXIST flag to fail with EBUSY, indicating that an XDP program is already attached. Due to a race condition, the kernel had not always completed the attachment from the first LinkSetXdpFd call before the second call was made. This caused the second call to succeed (returning a nil error) when it should have failed. The test logic correctly interpreted this unexpected success as a failure, but logged the nil error, leading to confusing output. This commit resolves the flakiness by introducing a retry loop with a short delay. The test now attempts the second call multiple times, giving the kernel sufficient time to update the link's XDP state. This ensures that the test reliably checks for the EBUSY error as intended. --- link_test.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/link_test.go b/link_test.go index bfe033b..3c656ad 100644 --- a/link_test.go +++ b/link_test.go @@ -2591,14 +2591,32 @@ func TestLinkXdp(t *testing.T) { if err != nil { t.Skipf("Loading bpf program failed: %s", err) } + t.Cleanup(func() { + _ = unix.Close(fd) + }) + if err := LinkSetXdpFd(testXdpLink, fd); err != nil { t.Fatal(err) } - if err := LinkSetXdpFdWithFlags(testXdpLink, fd, nl.XDP_FLAGS_UPDATE_IF_NOEXIST); !errors.Is(err, unix.EBUSY) { - t.Fatal(err) + t.Cleanup(func() { + _ = LinkSetXdpFd(testXdpLink, -1) + }) + var err2 error + // It can take a moment for the kernel to update the link state, so we retry here + attempts := 0 + for ; attempts < 10; attempts++ { + err2 = LinkSetXdpFdWithFlags(testXdpLink, fd, nl.XDP_FLAGS_UPDATE_IF_NOEXIST) + if err2 != nil { + break + } + time.Sleep(100 * time.Millisecond) } - if err := LinkSetXdpFd(testXdpLink, -1); err != nil { - t.Fatal(err) + + if err2 == nil { + t.Fatalf("expected EBUSY when reattaching XDP with UPDATE_IF_NOEXIST after %d attempts; got nil", attempts) + } + if !errors.Is(err2, unix.EBUSY) { + t.Fatalf("expected EBUSY when reattaching XDP with UPDATE_IF_NOEXIST; got %v", err2) } }