[issue26666] File object hook to modify select(ors) event mask

Zack Weinberg report at bugs.python.org
Tue Mar 29 11:28:41 EDT 2016


New submission from Zack Weinberg:

This is pretty esoteric, please bear with me.  I'm attempting to enhance a transparent-SOCKS module (https://github.com/Anorov/PySocks) to support non-blocking connect().  This means, you should be able to do this:

    socks.set_default_proxy(socks.SOCKS5, "localhost")
    s = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
    s.setblocking(False)
    err = s.connect_ex(address)
    if err == errno.EINPROGRESS:
       select.select([], [s], []) # returns when connection completes

Note the position of s in the select() arguments: the documented behavior of non-blocking connect(), at the operating system level, is that the socket will become *writable* when the connection resolves (whether successfully or not).  However, in this case, under the hood, the socket is *already connected* -- to the proxy server -- after socksocket() returns.  When connect_ex() returns EINPROGRESS, the thing we're really waiting for is a SOCKS-protocol reply message, i.e. the socket needs to become *readable* before the application can continue.  An application that used the above code (with a hypothetical version of PySocks where this was supported) would get woken up immediately, since the initial SOCKS client->server message doesn't even come close to filling up the TCP send buffer.

There's no practical way to hide this divergence with the current library.  What would be needed, I think, is a pair of new special methods on filelikes, which rewrite the set of events to listen for and the set of events to report.  Hypothetical, for my use case:

    def __preselect__(self, events):
        if not self._connecting: return events
        return selectors.EVENT_READ

    def __postselect__(self, events):
        if not self._connecting: return events
        return selectors.EVENT_WRITE

(I'm using the high-level selectors.EVENT_* constants for illustration.  This needs to happen in the low-level select-module methods, because callers can't be expected to use selectors.)

There are a bunch of awkward corner cases to worry about with this, and I'm not even sure the feature is worth it, but I thought I'd write up the problem and see what other people think.

----------
components: IO
messages: 262612
nosy: zwol
priority: normal
severity: normal
status: open
title: File object hook to modify select(ors) event mask
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26666>
_______________________________________


More information about the Python-bugs-list mailing list