[Python-checkins] bpo-29255: Wait in KqueueSelector.select when no fds are registered (GH-19508)

Russell Davis webhook-mailer at python.org
Wed Apr 15 14:57:14 EDT 2020


https://github.com/python/cpython/commit/ba1bcffe5cafc1bb0ac6fdf9ecef51e75e342707
commit: ba1bcffe5cafc1bb0ac6fdf9ecef51e75e342707
branch: master
author: Russell Davis <551404+russelldavis at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-04-15T11:57:06-07:00
summary:

bpo-29255: Wait in KqueueSelector.select when no fds are registered (GH-19508)

Also partially fixes bpo-25680 (there's still a discrepancy in behavior
on Windows that needs to be fixed).

files:
A Misc/NEWS.d/next/Library/2020-04-14-11-31-07.bpo-29255.4EcyIN.rst
M Lib/selectors.py
M Lib/test/test_selectors.py

diff --git a/Lib/selectors.py b/Lib/selectors.py
index a9a0801ef0713..90251dc34a0b3 100644
--- a/Lib/selectors.py
+++ b/Lib/selectors.py
@@ -552,7 +552,10 @@ def unregister(self, fileobj):
 
         def select(self, timeout=None):
             timeout = None if timeout is None else max(timeout, 0)
-            max_ev = len(self._fd_to_key)
+            # If max_ev is 0, kqueue will ignore the timeout. For consistent
+            # behavior with the other selector classes, we prevent that here
+            # (using max). See https://bugs.python.org/issue29255
+            max_ev = max(len(self._fd_to_key), 1)
             ready = []
             try:
                 kev_list = self._selector.control(None, max_ev, timeout)
diff --git a/Lib/test/test_selectors.py b/Lib/test/test_selectors.py
index 31611224dc71e..c449155c4b49f 100644
--- a/Lib/test/test_selectors.py
+++ b/Lib/test/test_selectors.py
@@ -543,6 +543,19 @@ def test_register_bad_fd(self):
         with self.assertRaises(KeyError):
             s.get_key(bad_f)
 
+    def test_empty_select_timeout(self):
+        # Issues #23009, #29255: Make sure timeout is applied when no fds
+        # are registered.
+        s = self.SELECTOR()
+        self.addCleanup(s.close)
+
+        t0 = time()
+        self.assertEqual(s.select(1), [])
+        t1 = time()
+        dt = t1 - t0
+        # Tolerate 2.0 seconds for very slow buildbots
+        self.assertTrue(0.8 <= dt <= 2.0, dt)
+
 
 @unittest.skipUnless(hasattr(selectors, 'DevpollSelector'),
                      "Test needs selectors.DevpollSelector")
diff --git a/Misc/NEWS.d/next/Library/2020-04-14-11-31-07.bpo-29255.4EcyIN.rst b/Misc/NEWS.d/next/Library/2020-04-14-11-31-07.bpo-29255.4EcyIN.rst
new file mode 100644
index 0000000000000..18fbddf2cee73
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-14-11-31-07.bpo-29255.4EcyIN.rst
@@ -0,0 +1 @@
+Wait in `KqueueSelector.select` when no fds are registered



More information about the Python-checkins mailing list