Skip to content

Commit 5aa416f

Browse files
polycubed: fix busy wait in netlink socket
select() updates the timeout structure, so if this is not intialized it is going to be 0 at some point and then create a busy wait loop, using 100% of a cpu core. This commit moves the assignent of the timeout structure to its original location and adds the comment that was removed by mistake. Fixes: 553afa7 ("polycubed: use raw socket for netlink") Signed-off-by: Mauricio Vasquez B <mauriciovasquezbernal@gmail.com>
1 parent 743a2c0 commit 5aa416f

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/polycubed/src/netlink.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,19 @@ class Netlink::NetlinkNotification {
6262
}
6363

6464
void execute_wait() {
65-
int socket_fd, result;
65+
int result;
6666
fd_set readset;
6767
struct timeval tv;
6868

69-
tv.tv_sec = NETLINK_TIMEOUT;
70-
tv.tv_usec = 0;
71-
7269
while (running) {
7370
do {
71+
tv.tv_sec = NETLINK_TIMEOUT;
72+
tv.tv_usec = 0;
7473
FD_ZERO(&readset);
7574
FD_SET(sock, &readset);
75+
// The struct tv is decremented every time the select terminates.
76+
// If the value is not updated, the next time select is called uses
77+
// 0 as timeout value, behaving as a non-blocking socket.
7678
result = select(sock + 1, &readset, NULL, NULL, &tv);
7779
} while (result < 0 && errno == EINTR && running);
7880

0 commit comments

Comments
 (0)