From 9d6e03aba0066f7760885eff0389203721b38c0a Mon Sep 17 00:00:00 2001 From: mehrdadn Date: Tue, 17 Dec 2019 02:33:37 -0800 Subject: [PATCH] Fix use of select() instead of poll() (#6477) * Fix Arrow poll() patch - Negative timeout for poll() was not translated to infinite timeout for select() - Only use select() on Windows, as other systems limit the range of the file descriptors * Apply poll() -> select() patch to Redis's ae.c as well --- bazel/ray_deps_setup.bzl | 1 + thirdparty/patches/arrow-windows-poll.patch | 29 ++++++----- thirdparty/patches/redis-windows-poll.patch | 55 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 thirdparty/patches/redis-windows-poll.patch diff --git a/bazel/ray_deps_setup.bzl b/bazel/ray_deps_setup.bzl index 4a5fb69c0..f7e04140f 100644 --- a/bazel/ray_deps_setup.bzl +++ b/bazel/ray_deps_setup.bzl @@ -78,6 +78,7 @@ def ray_deps_setup(): sha256 = "8e5997b447b1afdd1efd33731968484d2fe71c271fa7f1cd6b2476367e964e0e", patches = [ "//thirdparty/patches:hiredis-async-include-dict.patch", + "//thirdparty/patches:redis-windows-poll.patch", ], ) diff --git a/thirdparty/patches/arrow-windows-poll.patch b/thirdparty/patches/arrow-windows-poll.patch index c767c7782..b7f0bdebc 100644 --- a/thirdparty/patches/arrow-windows-poll.patch +++ b/thirdparty/patches/arrow-windows-poll.patch @@ -1,19 +1,13 @@ diff --git cpp/src/plasma/thirdparty/ae/ae.c cpp/src/plasma/thirdparty/ae/ae.c --- cpp/src/plasma/thirdparty/ae/ae.c +++ cpp/src/plasma/thirdparty/ae/ae.c -@@ -428,19 +428,33 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags) +@@ -428,20 +428,43 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags) /* Wait for milliseconds until the given file descriptor becomes * writable/readable/exception */ int aeWait(int fd, int mask, long long milliseconds) { -- struct pollfd pfd; ++ int retmask = 0, retval; + short revents = 0; -+ struct timeval tv = { milliseconds / 1000, (milliseconds % 1000) * 1000 }; - int retmask = 0, retval; - -- memset(&pfd, 0, sizeof(pfd)); -- pfd.fd = fd; -- if (mask & AE_READABLE) pfd.events |= POLLIN; -- if (mask & AE_WRITABLE) pfd.events |= POLLOUT; ++#ifdef _WINSOCKAPI_ + fd_set rset, wset; + FD_ZERO(&rset); + FD_ZERO(&wset); @@ -22,8 +16,8 @@ diff --git cpp/src/plasma/thirdparty/ae/ae.c cpp/src/plasma/thirdparty/ae/ae.c + } else if (mask & AE_WRITABLE) { + FD_SET(fd, &wset); + } -+ -+ if ((retval = select(fd + 1, &rset, &wset, NULL, &tv)) > 0) { ++ struct timeval tv = { milliseconds / 1000, (milliseconds % 1000) * 1000 }; ++ if ((retval = select(fd + 1, &rset, &wset, NULL, milliseconds >= 0 ? &tv : NULL)) > 0) { + if (FD_ISSET(fd, &rset)) { + revents |= POLLIN; + } @@ -31,12 +25,23 @@ diff --git cpp/src/plasma/thirdparty/ae/ae.c cpp/src/plasma/thirdparty/ae/ae.c + revents |= POLLOUT; + } + } ++#else + struct pollfd pfd; +- int retmask = 0, retval; + memset(&pfd, 0, sizeof(pfd)); + pfd.fd = fd; + if (mask & AE_READABLE) pfd.events |= POLLIN; + if (mask & AE_WRITABLE) pfd.events |= POLLOUT; + ++ retval = poll(&pfd, 1, milliseconds); ++ revents = pfd.revents; - if ((retval = poll(&pfd, 1, milliseconds))== 1) { - if (pfd.revents & POLLIN) retmask |= AE_READABLE; - if (pfd.revents & POLLOUT) retmask |= AE_WRITABLE; - if (pfd.revents & POLLERR) retmask |= AE_WRITABLE; - if (pfd.revents & POLLHUP) retmask |= AE_WRITABLE; ++#endif + if (retval== 1) { + if (revents & POLLIN) retmask |= AE_READABLE; + if (revents & POLLOUT) retmask |= AE_WRITABLE; @@ -45,4 +50,6 @@ diff --git cpp/src/plasma/thirdparty/ae/ae.c cpp/src/plasma/thirdparty/ae/ae.c return retmask; } else { return retval; + } + } -- diff --git a/thirdparty/patches/redis-windows-poll.patch b/thirdparty/patches/redis-windows-poll.patch new file mode 100644 index 000000000..a6a369841 --- /dev/null +++ b/thirdparty/patches/redis-windows-poll.patch @@ -0,0 +1,55 @@ +diff --git src/ae.c src/ae.c +--- src/ae.c ++++ src/ae.c +@@ -474,21 +474,44 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags) + /* Wait for milliseconds until the given file descriptor becomes + * writable/readable/exception */ + int aeWait(int fd, int mask, long long milliseconds) { ++ int retmask = 0, retval; ++ short revents = 0; ++#ifdef _WINSOCKAPI_ ++ fd_set rset, wset; ++ FD_ZERO(&rset); ++ FD_ZERO(&wset); ++ if (mask & AE_READABLE) { ++ FD_SET(fd, &rset); ++ } else if (mask & AE_WRITABLE) { ++ FD_SET(fd, &wset); ++ } ++ struct timeval tv = { milliseconds / 1000, (milliseconds % 1000) * 1000 }; ++ if ((retval = select(fd + 1, &rset, &wset, NULL, milliseconds >= 0 ? &tv : NULL)) > 0) { ++ if (FD_ISSET(fd, &rset)) { ++ revents |= POLLIN; ++ } ++ if (FD_ISSET(fd, &wset)) { ++ revents |= POLLOUT; ++ } ++ } ++#else + struct pollfd pfd; +- int retmask = 0, retval; + + memset(&pfd, 0, sizeof(pfd)); + pfd.fd = fd; + if (mask & AE_READABLE) pfd.events |= POLLIN; + if (mask & AE_WRITABLE) pfd.events |= POLLOUT; + ++ retval = poll(&pfd, 1, milliseconds); ++ revents = pfd.revents; +- if ((retval = poll(&pfd, 1, milliseconds))== 1) { +- if (pfd.revents & POLLIN) retmask |= AE_READABLE; +- if (pfd.revents & POLLOUT) retmask |= AE_WRITABLE; +- if (pfd.revents & POLLERR) retmask |= AE_WRITABLE; +- if (pfd.revents & POLLHUP) retmask |= AE_WRITABLE; ++#endif ++ if (retval== 1) { ++ if (revents & POLLIN) retmask |= AE_READABLE; ++ if (revents & POLLOUT) retmask |= AE_WRITABLE; ++ if (revents & POLLERR) retmask |= AE_WRITABLE; ++ if (revents & POLLHUP) retmask |= AE_WRITABLE; + return retmask; + } else { + return retval; + } + } +--