Skip to content

bpo-29255: Wait in KqueueSelector when no fds are registered #19508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wait in `KqueueSelector.select` when no fds are registered