From ab1519acef090010bc64189b2d3297d093832213 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 9 Apr 2019 21:09:27 -0500 Subject: [PATCH] Fix high CPU usage in subsequent select(2) calls The timeout was being reset to zero, so `select` was being called in a very tight loop. Closes #5761. --- src/io.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/io.cpp b/src/io.cpp index 727796c96..535be1ea0 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -76,6 +76,9 @@ void io_buffer_t::run_background_fillthread(autoclose_fd_t readfd) { FD_ZERO(&fds); FD_SET(fd, &fds); int ret = select(fd + 1, &fds, NULL, NULL, &tv); + // select(2) is allowed to (and does) update `tv` to indicate how much time was left, so we + // need to restore the desired value each time. + tv.tv_usec = poll_timeout_usec; readable = ret > 0; if (ret < 0 && errno != EINTR) { // Surprising error.