Skip to content

Commit

Permalink
udp: fix race, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Oct 13, 2024
1 parent 175a134 commit b61285d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/sp/transport/udp/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ udp_pipe_send(void *arg, nni_aio *aio)
udp_ep *ep;
udp_sp_data dreq;
nng_msg *msg;
size_t count = 0;

if (nni_aio_begin(aio) != 0) {
// No way to give the message back to the protocol,
Expand All @@ -981,6 +982,10 @@ udp_pipe_send(void *arg, nni_aio *aio)
msg = nni_aio_get_msg(aio);
ep = p->ep;

if (msg != NULL) {
count = nni_msg_len(msg) + nni_msg_header_len(msg);
}

nni_mtx_lock(&ep->mtx);
if ((nni_msg_len(msg) + nni_msg_header_len(msg)) > p->sndmax) {
nni_mtx_unlock(&ep->mtx);
Expand All @@ -999,14 +1004,13 @@ udp_pipe_send(void *arg, nni_aio *aio)
dreq.us_sender_id = p->self_id;
dreq.us_peer_id = p->peer_id;
dreq.us_sequence = p->self_seq++;
dreq.us_length =
msg != NULL ? nni_msg_len(msg) + nni_msg_header_len(msg) : 0;
dreq.us_length = (uint16_t) count;

// Just queue it, or fail it.
udp_queue_tx(ep, &p->peer_addr, (void *) &dreq, msg);
nni_mtx_unlock(&ep->mtx);
nni_aio_finish(
aio, 0, msg ? nni_msg_len(msg) + nni_msg_header_len(msg) : 0);

nni_aio_finish(aio, 0, count);
}

static void
Expand Down
6 changes: 6 additions & 0 deletions src/sp/transport/udp/udp_tran_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ test_udp_multi_small_burst(void)
int burst = 4;
int count = 20;

#ifdef NNG_PLATFORM_WINDOWS
// Windows seems to drop a lot - maybe because of virtualization
burst = 2;
count = 10;
#endif

// Experimentally at least on Darwin, we see some packet losses
// even for loopback. Loss rates appear depressingly high.
for (int i = 0; i < count; i++) {
Expand Down

0 comments on commit b61285d

Please sign in to comment.