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.
This commit is contained in:
Vishvananda Abrams
2025-08-27 19:44:58 +00:00
parent 349c84c717
commit edd0b8d707

View File

@@ -2591,14 +2591,32 @@ func TestLinkXdp(t *testing.T) {
if err != nil { if err != nil {
t.Skipf("Loading bpf program failed: %s", err) t.Skipf("Loading bpf program failed: %s", err)
} }
t.Cleanup(func() {
_ = unix.Close(fd)
})
if err := LinkSetXdpFd(testXdpLink, fd); err != nil { if err := LinkSetXdpFd(testXdpLink, fd); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := LinkSetXdpFdWithFlags(testXdpLink, fd, nl.XDP_FLAGS_UPDATE_IF_NOEXIST); !errors.Is(err, unix.EBUSY) { t.Cleanup(func() {
t.Fatal(err) _ = 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)
} }
} }