Skip to content

KqueueSelector miscounts max events #110038

Closed
@sorcio

Description

@sorcio

Bug report

Bug description:

KqueueSelector.select() may not get all events if a fd is registered for both read and write.

kqueue requires to register two filters, one for read and one for write. But KqueueSelector.select() calls kqueue.control() counting the number of registered fds, not the filters. As a result a single call to select() won't return all the available events.

>>> import socket, selectors
>>> sel = selectors.KqueueSelector()
>>> s1, s2 = socket.socketpair()
>>> sel.register(s1, selectors.EVENT_READ | selectors.EVENT_WRITE)
SelectorKey(fileobj=<socket.socket fd=4, family=1, type=1, proto=0>, fd=4, events=3, data=None)
>>> s2.send(b"foo")
3
>>> sel.select()
[(SelectorKey(fileobj=<socket.socket fd=4, family=1, type=1, proto=0>, fd=4, events=3, data=None), 2)]
>>> sel.select()
[(SelectorKey(fileobj=<socket.socket fd=4, family=1, type=1, proto=0>, fd=4, events=3, data=None), 1)]

(Note that the two events are only visible on two subsequent selects)

For users like asyncio, this might mean missing a loop cycle or more (depending on number of registered readers/writers and ready events) before an event is seen. I previously said that this might cause deadlocks but I could not produce an example where a read/write event is never seen.

CPython versions tested on:

3.11, 3.12, CPython main branch

Operating systems tested on:

macOS, Other

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-freebsdtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions