Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cfa927

Browse files
volans-vstinner
authored andcommitted
bpo-31334: Fix timeout in select.poll.poll() (GH-3277)
Always pass -1, or INFTIM where defined, to the poll() system call when a negative timeout is passed to the poll.poll([timeout]) method in the select module. Various OSes throw an error with arbitrary negative values.
1 parent 2c15b29 commit 6cfa927

4 files changed

Lines changed: 21 additions & 10 deletions

File tree

Lib/test/test_poll.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def test_threaded_poll(self):
207207
@unittest.skipUnless(threading, 'Threading required for this test.')
208208
@reap_threads
209209
def test_poll_blocks_with_negative_ms(self):
210-
for timeout_ms in [None, -1, -1.0, -0.1, -1e-100]:
210+
for timeout_ms in [None, -1000, -1, -1.0, -0.1, -1e-100]:
211211
# Create two file descriptors. This will be used to unlock
212212
# the blocking call to poll.poll inside the thread
213213
r, w = os.pipe()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ Brad Clements
292292
Robbie Clemons
293293
Steve Clift
294294
Hervé Coatanhay
295+
Riccardo Coccioli
295296
Nick Coghlan
296297
Josh Cogliati
297298
Dave Cole
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``poll.poll([timeout])`` in the ``select`` module for arbitrary negative
2+
timeouts on all OSes where it can only be a non-negative integer or -1.
3+
Patch by Riccardo Coccioli.

Modules/selectmodule.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -525,20 +525,14 @@ poll_poll(pollObject *self, PyObject *args)
525525
PyObject *result_list = NULL, *timeout_obj = NULL;
526526
int poll_result, i, j;
527527
PyObject *value = NULL, *num = NULL;
528-
_PyTime_t timeout, ms, deadline;
528+
_PyTime_t timeout = -1, ms = -1, deadline = 0;
529529
int async_err = 0;
530530

531531
if (!PyArg_ParseTuple(args, "|O:poll", &timeout_obj)) {
532532
return NULL;
533533
}
534534

535-
/* Check values for timeout */
536-
if (timeout_obj == NULL || timeout_obj == Py_None) {
537-
timeout = -1;
538-
ms = -1;
539-
deadline = 0; /* initialize to prevent gcc warning */
540-
}
541-
else {
535+
if (timeout_obj != NULL && timeout_obj != Py_None) {
542536
if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj,
543537
_PyTime_ROUND_TIMEOUT) < 0) {
544538
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
@@ -554,7 +548,20 @@ poll_poll(pollObject *self, PyObject *args)
554548
return NULL;
555549
}
556550

557-
deadline = _PyTime_GetMonotonicClock() + timeout;
551+
if (timeout >= 0) {
552+
deadline = _PyTime_GetMonotonicClock() + timeout;
553+
}
554+
}
555+
556+
/* On some OSes, typically BSD-based ones, the timeout parameter of the
557+
poll() syscall, when negative, must be exactly INFTIM, where defined,
558+
or -1. See issue 31334. */
559+
if (ms < 0) {
560+
#ifdef INFTIM
561+
ms = INFTIM;
562+
#else
563+
ms = -1;
564+
#endif
558565
}
559566

560567
/* Avoid concurrent poll() invocation, issue 8865 */

0 commit comments

Comments
 (0)