New submission from Charles-François Natali: Currently, in case of invalid file descriptor (which can happen easily e.g. if the user closes the FD before unregistering it), selector.select() will react differently depending on the backend:
SelectSelector fails with EBADF PollSelector returns EVENT_READ/EVENT_WRITE for the FD (translated from poll()'s POLLNVAL) EpollSelector returns nothing (epoll silently removes the FD from the set of monitored FD, there's no EPOLLNVAL) KqueueSelector behaves like EpollSelector One possibility would be for PollSelector.select() to raise EBADF in case of POLLNVAL. That would be consistent with select, but not with epoll and kqueue. Another possibility would be to silently fix those, i.e. in case of EBADF/POLLNVAL in select/poll, remove the offending FD from the list of monitored FDs. That's what libev does (see e.g. http://cvs.schmorp.de/libev/ev_poll.c?revision=1.39&view=markup and http://cvs.schmorp.de/libev/ev.c?revision=1.457&view=markup), and that's also what twisted does, at least for select (see http://twistedmatrix.com/trac/browser/trunk/twisted/internet/selectreactor.py#L77). One advantage is that once we've detected a bad FD, we remove it so we're sure we won't loop endlessly: e.g. for poll, if the user doesn't try to use the FD no exception will be raised and selector.select() keep reporting the FD ready. (Note that performing this cleanup is easy for poll since it's flagged with POLLNVAL, but for select this requires looping over the registered FDs, but since that's only called in case of error, it's not performance critical, see Twisted's code) I personally favor the later solution. Second point: if we opt for the later solution, we should probably update {EpollSelector,KqueueSelector}.unregister() to silently catch an error if the FD wasn't registered in the underlying epoll/kqueue. That's what libev does, and amusingly that's what the select module already does: http://hg.python.org/cpython/file/c9644c1f9c94/Modules/selectmodule.c#l1329 Third point: should the current select behavior (ignoring epoll.unregister() error) be kept? Thoughts? ---------- messages: 197701 nosy: giampaolo.rodola, gvanrossum, haypo, neologix, pitrou, sbt priority: normal severity: normal stage: needs patch status: open title: selectors: towards uniform EBADF handling type: enhancement _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue19017> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com