Skip to content

Commit b690b9b

Browse files
authored
bpo-29386: Pass -1 to epoll_wait() when timeout is < -1 (GH-9040)
Although the kernel accepts any negative value for timeout, the documented value to block indefinitely is -1. This commit also makes the code similar to select.poll.poll().
1 parent 0baa72f commit b690b9b

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

Modules/selectmodule.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,17 +1498,12 @@ select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj,
14981498
int nfds, i;
14991499
PyObject *elist = NULL, *etuple = NULL;
15001500
struct epoll_event *evs = NULL;
1501-
_PyTime_t timeout, ms, deadline;
1501+
_PyTime_t timeout = -1, ms = -1, deadline = 0;
15021502

15031503
if (self->epfd < 0)
15041504
return pyepoll_err_closed();
15051505

1506-
if (timeout_obj == Py_None) {
1507-
timeout = -1;
1508-
ms = -1;
1509-
deadline = 0; /* initialize to prevent gcc warning */
1510-
}
1511-
else {
1506+
if (timeout_obj != Py_None) {
15121507
/* epoll_wait() has a resolution of 1 millisecond, round towards
15131508
infinity to wait at least timeout seconds. */
15141509
if (_PyTime_FromSecondsObject(&timeout, timeout_obj,
@@ -1525,8 +1520,20 @@ select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj,
15251520
PyErr_SetString(PyExc_OverflowError, "timeout is too large");
15261521
return NULL;
15271522
}
1523+
/* epoll_wait(2) treats all arbitrary negative numbers the same
1524+
for the timeout argument, but -1 is the documented way to block
1525+
indefinitely in the epoll_wait(2) documentation, so we set ms
1526+
to -1 if the value of ms is a negative number.
1527+
1528+
Note that we didn't use INFTIM here since it's non-standard and
1529+
isn't available under Linux. */
1530+
if (ms < 0) {
1531+
ms = -1;
1532+
}
15281533

1529-
deadline = _PyTime_GetMonotonicClock() + timeout;
1534+
if (timeout >= 0) {
1535+
deadline = _PyTime_GetMonotonicClock() + timeout;
1536+
}
15301537
}
15311538

15321539
if (maxevents == -1) {

0 commit comments

Comments
 (0)