Skip to content

Commit 2c15b29

Browse files
pablogsalserhiy-storchaka
authored andcommitted
bpo-31786: Make functions in the select module blocking when timeout is a small negative value. (#4003)
1 parent 552be9d commit 2c15b29

File tree

7 files changed

+65
-12
lines changed

7 files changed

+65
-12
lines changed

‎Include/pytime.h‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,20 @@ typedef enum {
2929
_PyTime_ROUND_CEILING=1,
3030
/* Round to nearest with ties going to nearest even integer.
3131
For example, used to round from a Python float. */
32-
_PyTime_ROUND_HALF_EVEN
32+
_PyTime_ROUND_HALF_EVEN=2,
33+
/* Round away from zero
34+
For example, used for timeout. _PyTime_ROUND_CEILING rounds
35+
-1e-9 to 0 milliseconds which causes bpo-31786 issue.
36+
_PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps
37+
the timeout sign as expected. select.poll(timeout) must block
38+
for negative values." */
39+
_PyTime_ROUND_UP=3,
40+
/* _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be
41+
used for timeouts. */
42+
_PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP
3343
} _PyTime_round_t;
3444

45+
3546
/* Convert a time_t to a PyLong. */
3647
PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
3748
time_t sec);

‎Lib/test/test_poll.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ def test_threaded_poll(self):
204204
os.write(w, b'spam')
205205
t.join()
206206

207+
@unittest.skipUnless(threading, 'Threading required for this test.')
208+
@reap_threads
209+
def test_poll_blocks_with_negative_ms(self):
210+
for timeout_ms in [None, -1, -1.0, -0.1, -1e-100]:
211+
# Create two file descriptors. This will be used to unlock
212+
# the blocking call to poll.poll inside the thread
213+
r, w = os.pipe()
214+
pollster = select.poll()
215+
pollster.register(r, select.POLLIN)
216+
217+
poll_thread = threading.Thread(target=pollster.poll, args=(timeout_ms,))
218+
poll_thread.start()
219+
poll_thread.join(timeout=0.1)
220+
self.assertTrue(poll_thread.is_alive())
221+
222+
# Write to the pipe so pollster.poll unblocks and the thread ends.
223+
os.write(w, b'spam')
224+
poll_thread.join()
225+
self.assertFalse(poll_thread.is_alive())
226+
os.close(r)
227+
os.close(w)
228+
207229

208230
def test_main():
209231
run_unittest(PollTests)

‎Lib/test/test_time.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ class _PyTime(enum.IntEnum):
3333
ROUND_CEILING = 1
3434
# Round to nearest with ties going to nearest even integer
3535
ROUND_HALF_EVEN = 2
36+
# Round away from zero
37+
ROUND_UP = 3
3638

3739
# Rounding modes supported by PyTime
3840
ROUNDING_MODES = (
3941
# (PyTime rounding method, decimal rounding method)
4042
(_PyTime.ROUND_FLOOR, decimal.ROUND_FLOOR),
4143
(_PyTime.ROUND_CEILING, decimal.ROUND_CEILING),
4244
(_PyTime.ROUND_HALF_EVEN, decimal.ROUND_HALF_EVEN),
45+
(_PyTime.ROUND_UP, decimal.ROUND_UP),
4346
)
4447

4548

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix timeout rounding in the select module to round correctly negative timeouts between -1.0 and 0.0.
2+
The functions now block waiting for events as expected. Previously, the call was incorrectly non-blocking.
3+
Patch by Pablo Galindo.

‎Modules/_testcapimodule.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3012,7 +3012,8 @@ check_time_rounding(int round)
30123012
{
30133013
if (round != _PyTime_ROUND_FLOOR
30143014
&& round != _PyTime_ROUND_CEILING
3015-
&& round != _PyTime_ROUND_HALF_EVEN) {
3015+
&& round != _PyTime_ROUND_HALF_EVEN
3016+
&& round != _PyTime_ROUND_UP) {
30163017
PyErr_SetString(PyExc_ValueError, "invalid rounding");
30173018
return -1;
30183019
}

‎Modules/selectmodule.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,15 @@ select_select(PyObject *self, PyObject *args)
213213
tvp = (struct timeval *)NULL;
214214
else {
215215
if (_PyTime_FromSecondsObject(&timeout, timeout_obj,
216-
_PyTime_ROUND_CEILING) < 0) {
216+
_PyTime_ROUND_TIMEOUT) < 0) {
217217
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
218218
PyErr_SetString(PyExc_TypeError,
219219
"timeout must be a float or None");
220220
}
221221
return NULL;
222222
}
223223

224-
if (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_CEILING) == -1)
224+
if (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_TIMEOUT) == -1)
225225
return NULL;
226226
if (tv.tv_sec < 0) {
227227
PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
@@ -540,15 +540,15 @@ poll_poll(pollObject *self, PyObject *args)
540540
}
541541
else {
542542
if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj,
543-
_PyTime_ROUND_CEILING) < 0) {
543+
_PyTime_ROUND_TIMEOUT) < 0) {
544544
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
545545
PyErr_SetString(PyExc_TypeError,
546546
"timeout must be an integer or None");
547547
}
548548
return NULL;
549549
}
550550

551-
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
551+
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_TIMEOUT);
552552
if (ms < INT_MIN || ms > INT_MAX) {
553553
PyErr_SetString(PyExc_OverflowError, "timeout is too large");
554554
return NULL;
@@ -896,15 +896,15 @@ devpoll_poll(devpollObject *self, PyObject *args)
896896
}
897897
else {
898898
if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj,
899-
_PyTime_ROUND_CEILING) < 0) {
899+
_PyTime_ROUND_TIMEOUT) < 0) {
900900
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
901901
PyErr_SetString(PyExc_TypeError,
902902
"timeout must be an integer or None");
903903
}
904904
return NULL;
905905
}
906906

907-
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
907+
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_TIMEOUT);
908908
if (ms < -1 || ms > INT_MAX) {
909909
PyErr_SetString(PyExc_OverflowError, "timeout is too large");
910910
return NULL;
@@ -1513,7 +1513,7 @@ pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
15131513
/* epoll_wait() has a resolution of 1 millisecond, round towards
15141514
infinity to wait at least timeout seconds. */
15151515
if (_PyTime_FromSecondsObject(&timeout, timeout_obj,
1516-
_PyTime_ROUND_CEILING) < 0) {
1516+
_PyTime_ROUND_TIMEOUT) < 0) {
15171517
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
15181518
PyErr_SetString(PyExc_TypeError,
15191519
"timeout must be an integer or None");
@@ -2128,7 +2128,7 @@ kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
21282128
}
21292129
else {
21302130
if (_PyTime_FromSecondsObject(&timeout,
2131-
otimeout, _PyTime_ROUND_CEILING) < 0) {
2131+
otimeout, _PyTime_ROUND_TIMEOUT) < 0) {
21322132
PyErr_Format(PyExc_TypeError,
21332133
"timeout argument must be a number "
21342134
"or None, got %.200s",

‎Python/pytime.c‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ _PyTime_Round(double x, _PyTime_round_t round)
120120
else if (round == _PyTime_ROUND_CEILING) {
121121
d = ceil(d);
122122
}
123-
else {
123+
else if (round == _PyTime_ROUND_FLOOR) {
124124
d = floor(d);
125125
}
126+
else {
127+
assert(round == _PyTime_ROUND_UP);
128+
d = (d >= 0.0) ? ceil(d) : floor(d);
129+
}
126130
return d;
127131
}
128132

@@ -427,14 +431,23 @@ _PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
427431
return t / k;
428432
}
429433
}
430-
else {
434+
else if (round == _PyTime_ROUND_FLOOR){
431435
if (t >= 0) {
432436
return t / k;
433437
}
434438
else {
435439
return (t - (k - 1)) / k;
436440
}
437441
}
442+
else {
443+
assert(round == _PyTime_ROUND_UP);
444+
if (t >= 0) {
445+
return (t + k - 1) / k;
446+
}
447+
else {
448+
return (t - (k - 1)) / k;
449+
}
450+
}
438451
}
439452

440453
_PyTime_t

0 commit comments

Comments
 (0)