Skip to content

Commit

Permalink
fdReady: use poll() instead of select()
Browse files Browse the repository at this point in the history
select() is limited to 1024 file descriptors.  This actually blew up
in a very hard-to-debug way in our production system when using the
hinotify package.

Test Plan:
libraries/tests pass, paricularly hGetBuf001 which exercises this
code.

Reviewers: niteria, erikd, austin, hvr, bgamari

Reviewed By: bgamari

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D2785

GHC Trac Issues: #12912
  • Loading branch information
simonmar authored and bgamari committed Dec 2, 2016
1 parent 27731f1 commit f46369b
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions libraries/base/cbits/inputReady.c
Expand Up @@ -7,6 +7,9 @@
/* select and supporting types is not Posix */
/* #include "PosixSource.h" */
#include "HsBase.h"
#if !defined(_WIN32)
#include <poll.h>
#endif

/*
* inputReady(fd) checks to see whether input is available on the file
Expand All @@ -16,19 +19,41 @@
int
fdReady(int fd, int write, int msecs, int isSock)
{
if
#if defined(_WIN32)
( isSock ) {

#if !defined(_WIN32)

// We only handle msecs == 0 on non-Windows, because this is the
// only case we need. Non-zero waiting is handled by the IO manager.
if (msecs != 0) {
fprintf(stderr, "fdReady: msecs != 0, this shouldn't happen");
abort();
}

struct pollfd fds[1];

fds[0].fd = fd;
fds[0].events = write ? POLLOUT : POLLIN;
fds[0].revents = 0;

int res;
while ((res = poll(fds, 1, 0)) < 0) {
if (errno != EINTR) {
return (-1);
}
}

// res is the number of FDs with events
return (res > 0);

#else
( 1 ) {
#endif

if (isSock) {
int maxfd, ready;
fd_set rfd, wfd;
struct timeval tv;
if ((fd >= (int)FD_SETSIZE) || (fd < 0)) {
/* avoid memory corruption on too large FDs */
errno = EINVAL;
return -1;
fprintf(stderr, "fdReady: fd is too big");
abort();
}
FD_ZERO(&rfd);
FD_ZERO(&wfd);
Expand All @@ -54,7 +79,6 @@ fdReady(int fd, int write, int msecs, int isSock)
/* 1 => Input ready, 0 => not ready, -1 => error */
return (ready);
}
#if defined(_WIN32)
else {
DWORD rc;
HANDLE hFile = (HANDLE)_get_osfhandle(fd);
Expand Down

0 comments on commit f46369b

Please sign in to comment.