Skip to content

Commit

Permalink
Handle return events for poll() (#836)
Browse files Browse the repository at this point in the history
On MacOS, there was a situation where poll() had POLLHUP in revents,
which was not being handled by WaitForEvent. This caused poll to
continuously run, taking CPU time (up to 100%).

In spite of requesting POLLERR | POLLIN or POLLERR | POLLOUT,
poll will also potentially return POLLHUP and POLLNVAL.
[From the opengroup poll spec:](https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html)
"In addition, poll() shall set the POLLHUP, POLLERR, and POLLNVAL flag in
revents if the condition is true, even if the application did not set the
corresponding bit in events."

We've removed the request for POLLERR from the file descriptor as it is
unnecessary and misleading.

This fix addresses the CPU usage on MacOS 11.6 when running a monitor
that cannot connect to its nodes.

Co-authored-by: Jacob Champion <pchampion@vmware.com>
  • Loading branch information
Rachel Heaton and jchampio committed Nov 23, 2021
1 parent fc92546 commit 37a2496
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/monitor/health_check_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,11 +748,11 @@ WaitForEvent(List *healthCheckList)

if (healthCheck->pollingStatus == PGRES_POLLING_READING)
{
pollEventMask = POLLERR | POLLIN;
pollEventMask = POLLIN;
}
else if (healthCheck->pollingStatus == PGRES_POLLING_WRITING)
{
pollEventMask = POLLERR | POLLOUT;
pollEventMask = POLLOUT;
}

pollFileDescriptor->fd = PQsocket(connection);
Expand Down Expand Up @@ -786,8 +786,7 @@ WaitForEvent(List *healthCheckList)
HealthCheck *healthCheck = (HealthCheck *) lfirst(healthCheckCell);
struct pollfd *pollFileDescriptor = &pollFDs[healthCheckIndex];

healthCheck->readyToPoll = pollFileDescriptor->revents &
pollFileDescriptor->events;
healthCheck->readyToPoll = pollFileDescriptor->revents != 0;

healthCheckIndex++;
}
Expand Down

0 comments on commit 37a2496

Please sign in to comment.