epoll_wait() returns EINTR infinitely

1.5k views Asked by At

I have a thread responsible for polling various fds. I am using epoll_wait with timeout set. Following is the code snippet:

do {
    n = epoll_wait(epollFd, eventsList, eventsTotal, timeoutMS);
} 
while ((n<0) && (errno == EINTR));

eventsList memory contains timerfd, signalfd and socket fd.

The thread works well & handles timer event, socket open/read/write/close event & user-defined signal events.

But there are times when thread goes in infinite do-while loop as errno always returns EINTR.
Top -H of thread shows status as sleep. strace reveals it's calling epoll_wait() in loop.

So what could go wrong as I am using well-accepted way of handling epoll_wait & EINTR? Could there be anything wrong with socket read/write/close that can cause above issue? Or with timerfd?

Update: strace -p output:

epoll_wait(8, {}, 8192, 10)             = 0
epoll_wait(8, {}, 8192, 10)             = 0
epoll_wait(8, {}, 8192, 10)             = 0
epoll_wait(8, {}, 8192, 10)             = 0

Then I took gcore and tried to get errno returned by epoll_wait(). It is 4 (EINTR)

1

There are 1 answers

0
Shachar Shemesh On BEST ANSWER

It would have been more helpful if you showed us at least a snippet of the strace output. At the very least, it would tell us which interrupt is being received.

Without that information, I cannot tell you what is wrong with your program. I can, however, tell you how to find out.

First, look at the strace output. It will tell you what signal it is that the process receives.

Then look at the signal handler for that signal. If you don't have one, register one. Make sure you are using sigaction and not signal to do so. Also, make sure you are using the newer sa_sigaction function format.

With this format, your signal handler receives information about who sent that signal. This includes PID of sender, if it is a timer, the timer ID etc. Use that information to determine where the signal is coming from.