[Python-Dev] Socket timeout and completion based sockets

Kristján Valur Jónsson kristjan at ccpgames.com
Tue Nov 27 10:35:15 CET 2012

This is getting off-topic, but:
CancelIoEx() is a new api that I wasn't aware of (my IOCP solution dates back to 2005).  It appears that this can be used, although the documentation is sketchy.
This worries me:
"If the file handle is associated with a completion port, an I/O completion packet is not queued to the port if a synchronous operation is successfully canceled. For asynchronous operations still pending, the cancel operation will queue an I/O completion packet."
This _might_ mean that synchronizing the cancel request with a callback that expects to be called, but then, may not be called, can be difficult.  It is possible that MS didn't think their API completely through (nothing new here.) 

Anyway, that is somewhat beside the point

Even if we can cancel an ongoing operation there needs to be synchronization, so that any data that is received(or sent) is correctly communicated ot the app.  If there isn't a cancel option in the API (not talking about windows here) then this would mean queueing up data for recv(), and no simple solution for send().

So, basically, what I'm saying is, that enabling re-startable socket timeout semantics for sockets implemented with "completion" semantics, rather than "ready" semantics _can_ be difficult, hence my question.

> -----Original Message-----
> From: Python-Dev [mailto:python-dev-
> bounces+kristjan=ccpgames.com at python.org] On Behalf Of Richard
> Oudkerk
> Sent: 26. nóvember 2012 16:05
> To: python-dev at python.org
> Subject: Re: [Python-Dev] Socket timeout and completion based sockets
> On 26/11/2012 11:49am, Kristján Valur Jónsson wrote:
> > However, other implementations of python sockets, e.g. ones that rely
> > on IO completion, may not have the luxury of using select.  For
> > example, on Windows, there is no way to abort an IOCP socket call, so
> > a timeout must be implemented by aborting the wait.  Dealing with the
> > resulting race can be an interesting challenge.
> I am not quite sure what you mean by "aborting the wait".  But you can abort
> an overlapped operation using CancelIo()/CancelIoEx().
> I have just done some experimenting.
> Using CancelIo()/CancelIoEx() to abort an operation started with
> WSARecv() does not seem to cause a problem -- you just call
> GetOverlappedResult() afterwards to check whether the operation
> completed before it could be aborted.
> But aborting an operation started with WSASend() sometimes seems to
> "break" the connection: a subsequent WSARecv()/WSASend() will fail with
> WSAECONNABORTED or WSAECONNRESET depending on which end of the
> connection you are on.
> So, as you say, if you abort a send then you cannot expect to successfully
> resend the data later.
> --
> Richard
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-
> dev/kristjan%40ccpgames.com

More information about the Python-Dev mailing list