diff --git a/Lib/test/test_epoll.py b/Lib/test/test_epoll.py index 6459fbafc8c..22e98962498 100644 --- a/Lib/test/test_epoll.py +++ b/Lib/test/test_epoll.py @@ -47,6 +47,17 @@ class TestEPoll(unittest.TestCase): self.serverSocket.listen(1) self.connections = [self.serverSocket] + def test_timeout_rounding(self): + # epoll_wait() has a resolution of 1 millisecond, check if the timeout + # is correctly rounded to the upper bound + epoll = select.epoll() + self.addCleanup(epoll.close) + for timeout in (1e-2, 1e-3, 1e-4): + t0 = time.perf_counter() + epoll.poll(timeout) + dt = time.perf_counter() - t0 + self.assertGreaterEqual(dt, timeout) + def tearDown(self): for skt in self.connections: diff --git a/Misc/NEWS b/Misc/NEWS index 66d65f084f7..88921d4aa78 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -25,6 +25,10 @@ Core and Builtins Library ------- +- Issue #20311: select.epoll.poll() now rounds the timeout away from zero, + instead of rounding towards zero. For example, a timeout of one microsecond + is now rounded to one millisecond, instead of being rounded to zero. + - Issue #20262: Warnings are raised now when duplicate names are added in the ZIP file or too long ZIP file comment is truncated. diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index d44e8de929c..0c9b9d9f8c9 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -1458,7 +1458,9 @@ pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds) return NULL; } else { - timeout = (int)(dtimeout * 1000.0); + /* epoll_wait() has a resolution of 1 millisecond, round away from zero + to wait *at least* dtimeout seconds. */ + timeout = (int)ceil(dtimeout * 1000.0); } if (maxevents == -1) {