Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: handle zero-length udp datagram #4344

Open
wants to merge 1 commit into
base: v1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions test/test-udp-recv-in-a-row.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ static void sv_recv_cb(uv_udp_t* handle,
const uv_buf_t* rcvbuf,
const struct sockaddr* addr,
unsigned flags) {
if (++ recv_cnt < N) {
ASSERT_EQ(sizeof(send_data), nread);
} else {
ASSERT_OK(nread);
}
/* |nread| can be zero when the kernel drops an incoming datagram after
* marking the file descriptor as readable but before libuv has a chance
* to receive it. Libuv still invokes the uv_udp_recv_cb callback to give
* back the memory from the uv_alloc_cb callback.
*
* See https://github.com/libuv/libuv/issues/4219.
*/
recv_cnt++;
bnoordhuis marked this conversation as resolved.
Show resolved Hide resolved
if (nread > 0)
ASSERT_EQ(nread, sizeof(send_data));
}

static void check_cb(uv_check_t* handle) {
Expand All @@ -63,9 +68,11 @@ static void check_cb(uv_check_t* handle) {
/**
* sv_recv_cb() is called with nread set to zero to indicate
* there is no more udp packet in the kernel, so the actual
* recv_cnt is one larger than N.
* recv_cnt is up to one larger than N. UDP being what it is,
* packets can get dropped so don't assume an exact count.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vtjnash after looking at the code some more, I came the conclusion that the "is equal to N+1" check is wrong because any number of packets can get dropped. Even assuming a single packet makes it through is already a shaky assumption, just one that is true most of the time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the kernel does end up guaranteeing reliable UDP delivery to localhost. It is sort of by-accident, but it would be more work for it to discard messages because it goes through loopback directly instead of the making a whole roundtrip through the hardware.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux maybe but XNU? The CI failures are on macos buildbots.

FWIW, I can sort of reproduce it on Linux with a traffic-shaped device but at that point it's not really localhost traffic anymore so maybe not completely apples to apples.

*/
ASSERT_EQ(N+1, recv_cnt);
ASSERT_GE(recv_cnt, 1);
ASSERT_LE(recv_cnt, N+1);
check_cb_called = 1;

/* we are done */
Expand Down