From 16090939325af3d5b563d19f01fb0d3e5091f540 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Mon, 3 Sep 2018 12:10:49 +0300 Subject: [PATCH 1/3] bpo-29386: Cleanup select_epoll_poll_impl() --- Modules/selectmodule.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index 836af5429af582..f8289629309c09 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -1498,17 +1498,12 @@ select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj, int nfds, i; PyObject *elist = NULL, *etuple = NULL; struct epoll_event *evs = NULL; - _PyTime_t timeout, ms, deadline; + _PyTime_t timeout = -1, ms = -1, deadline = 0; if (self->epfd < 0) return pyepoll_err_closed(); - if (timeout_obj == Py_None) { - timeout = -1; - ms = -1; - deadline = 0; /* initialize to prevent gcc warning */ - } - else { + if (timeout_obj != NULL && timeout_obj != Py_None) { /* epoll_wait() has a resolution of 1 millisecond, round towards infinity to wait at least timeout seconds. */ if (_PyTime_FromSecondsObject(&timeout, timeout_obj, @@ -1526,7 +1521,19 @@ select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj, return NULL; } - deadline = _PyTime_GetMonotonicClock() + timeout; + if (timeout >= 0) { + deadline = _PyTime_GetMonotonicClock() + timeout; + } + } + + /* Kernel doesn't make any difference for negative values, but -1 is + documented as a way to block indefinitely in the epoll_wait() + documentation, so set ms to -1 if the value is negative. + + Note that we didn't use INFTIM here since it's non-standard and + doesn't available under Linux. */ + if (ms < 0) { + ms = -1; } if (maxevents == -1) { From 6c69cad5ee6820526cc4372edf90a4697e41abf2 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Mon, 3 Sep 2018 16:12:17 +0300 Subject: [PATCH 2/3] address review comments --- Modules/selectmodule.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index f8289629309c09..edf89e14878182 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -1503,7 +1503,7 @@ select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj, if (self->epfd < 0) return pyepoll_err_closed(); - if (timeout_obj != NULL && timeout_obj != Py_None) { + if (timeout_obj != Py_None) { /* epoll_wait() has a resolution of 1 millisecond, round towards infinity to wait at least timeout seconds. */ if (_PyTime_FromSecondsObject(&timeout, timeout_obj, @@ -1520,22 +1520,22 @@ select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj, PyErr_SetString(PyExc_OverflowError, "timeout is too large"); return NULL; } + /* epoll_wait(2) treats all arbitrary negative numbers the same + for the timeout argument, but -1 is the documented way to block + indefinitely in the epoll_wait(2) documentation, so we set ms + to -1 if the value of ms is a negative number. + + Note that we didn't use INFTIM here since it's non-standard and + doesn't available under Linux. */ + if (ms < 0) { + ms = -1; + } if (timeout >= 0) { deadline = _PyTime_GetMonotonicClock() + timeout; } } - /* Kernel doesn't make any difference for negative values, but -1 is - documented as a way to block indefinitely in the epoll_wait() - documentation, so set ms to -1 if the value is negative. - - Note that we didn't use INFTIM here since it's non-standard and - doesn't available under Linux. */ - if (ms < 0) { - ms = -1; - } - if (maxevents == -1) { maxevents = FD_SETSIZE-1; } From a968c3ff46ca056e88e6b06a031e877452d6c875 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Sat, 8 Sep 2018 22:46:28 +0300 Subject: [PATCH 3/3] fix typo --- Modules/selectmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index edf89e14878182..d86727a8978dc0 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -1526,7 +1526,7 @@ select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj, to -1 if the value of ms is a negative number. Note that we didn't use INFTIM here since it's non-standard and - doesn't available under Linux. */ + isn't available under Linux. */ if (ms < 0) { ms = -1; }