Just encountered an undocumented error. The call to poll() returned ECHILD.
I have a very simple code, almost directly from the man:
struct pollfd fds[] = { fd, POLLIN, 0 };
while(should_continue) {
int rc=poll(fds, 1, 1000);
if(rc==0) {
// exit by timeout, repeat waiting
continue;
} else if(rc<0) {
printf("poll error %d\n", errno);
exit(1);
}
// read from fd
}
This code suddenly exited with "poll error 10". But man 2 poll does not even mention ECHILD as possible error. What could be the cause?
I suspect that you have a signal handler that clobbers
errno. Specifically, aSIGCHLDhandler that callswaituntil it returns-1.What happens is this:
polland it blocks.pollreturns errorEINTRto allow your signal handler a chance to run.SIGCHLDhandler is called.waitrepeatedly to reap every child waiting to be reaped.wait, having nothing left to reap, returns errorECHILD.SIGCHLDhandler returns without restoringerrno.pollappears to have returned errorECHILDwhen it really returned errorEINTR.Fix:
errnowhen your signal handler is called, and restore it before exiting.pollwhen it returns errorEINTR, or make it so it's automatically restarted after signal processing by setting up your signal handler usingsigactionand passing the appropriate flags.Very few things return error
ECHILD, so the possible explanations are very limited. As such, I'm pretty confident in my guess.Notes:
EXXX" is used as shorthand for "returns-1after settingerrnotoEXXX".