Skip to content

Commit c263fc6

Browse files
committed
[mi_datagram] turn the RX sockets to non-blocking
As we have multiple procs reading from the same datagram sockets, we may end up with a mixing between the procs woken up by OS and the procs doing the reading. So some procs (even if were woken up) may have nothing to read. To be resilient, better do non-blocking reading and igonre the EAGAIN or EWOULDBLOCK. (cherry picked from commit 12e705e)
1 parent 75645be commit c263fc6

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

modules/mi_datagram/datagram_fnc.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ int mi_init_datagram_server(sockaddr_dtgram *addr, unsigned int socket_domain,
113113
return -1;
114114
}
115115

116+
/* Turn non-blocking mode on for rx*/
117+
flags = fcntl(socks->rx_sock, F_GETFL);
118+
if (flags == -1) {
119+
LM_ERR("fcntl failed on RX socket: %s\n", strerror(errno));
120+
goto err_both;
121+
}
122+
if (fcntl(socks->rx_sock, F_SETFL, flags | O_NONBLOCK) == -1) {
123+
LM_ERR("fcntl: set non-blocking failed for RX socket: %s\n",strerror(errno));
124+
goto err_both;
125+
}
126+
116127
switch(socket_domain) {
117128
case AF_LOCAL:
118129
LM_DBG("we have a unix socket: %s\n", addr->unix_addr.sun_path);
@@ -451,15 +462,14 @@ int mi_datagram_callback(int rx_sock, void *_tx_sock, int was_timeout)
451462
(struct sockaddr*)&reply_addr, &reply_addr_len);
452463

453464
if (ret < 0) {
454-
LM_ERR("recvfrom %d: (%d) %s\n", ret, errno, strerror(errno));
455465
if ((errno == EINTR) ||
456466
(errno == EAGAIN) ||
457467
(errno == EWOULDBLOCK) ||
458468
(errno == ECONNREFUSED)) {
459469
LM_DBG("got %d (%s), going on\n", errno, strerror(errno));
460470
return 0;
461471
}
462-
LM_DBG("error in recvfrom\n");
472+
LM_ERR("recvfrom %d: (%d) %s\n", ret, errno, strerror(errno));
463473
return -1;
464474
}
465475

0 commit comments

Comments
 (0)