So here's my newest patched version of cmucl's connect-to-inet-socket:
From the comments:
An attempt to rewrite connect-to-inet-socket in such a way that connection attempts that take a long time will not be stuck in the connect(2) system call preventing cmucl from running other lisp threads.
The strategy here is to put the socket in non-blocking mode, and then call connect, which should immediately return. Then there are three cases to handle:
Cases 1 and 2 are simple enough, we just return the socket or signal an error.
In case 3, we call system:wait-until-fd-is-usable to block just the current thread until the socket is writeable (which signals that the connect has finished, one way or the other).
Once wait-until-fd-is-usable returns, we put the socket back into blocking mode and try to determine whether the connect succeeded or failed (and why it failed). I used an approach described at http://cr.yp.to/docs/connect.html, which is attributed to Douglas C. Schmidt and Ken Keys.
I first call getpeername, and if it returns 0 the connect succeeded, otherwise it failed. If it failed, I try to read a single character from the socket. I know it can't work, but it should cause errno set to the real reason for the failure.
If this works, and is robust, I don't see any reason for it not to replace the connect-to-inet-socket from cmucl.
Posted by jjwiseman at February 15, 2002 01:42 AM