This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author zwol
Recipients zwol
Date 2016-03-29.15:28:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1459265321.27.0.359178089762.issue26666@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-03-29 15:28:41zwolsetrecipients: + zwol
2016-03-29 15:28:41zwolsetmessageid: <1459265321.27.0.359178089762.issue26666@psf.upfronthosting.co.za>
2016-03-29 15:28:41zwollinkissue26666 messages
2016-03-29 15:28:40zwolcreate