[usocket-devel] :timeout NIL for wait-for-input returns immediately on SBCL

Chun Tian (binghe) binghe.lisp at gmail.com
Wed Dec 24 03:07:52 UTC 2008


Hi, Chaitanya

I found the implementation of SB-UNIX:UNIX-FAST-SELECT in SBCL source  
tree (src/code/unix.lisp)

;;; Perform the UNIX select(2) system call.
(declaim (inline unix-fast-select))
(defun unix-fast-select (num-descriptors
                          read-fds write-fds exception-fds
                          timeout-secs timeout-usecs)
   (declare (type (integer 0 #.fd-setsize) num-descriptors)
            (type (or (alien (* (struct fd-set))) null)
                  read-fds write-fds exception-fds)
            (type (or null (unsigned-byte 31)) timeout-secs timeout- 
usecs))
   (flet ((select (tv-sap)
            (int-syscall ("select" int (* (struct fd-set)) (* (struct  
fd-set))
                                   (* (struct fd-set)) (* (struct  
timeval)))
                         num-descriptors read-fds write-fds exception- 
fds
                         tv-sap)))
     (cond ((or timeout-secs timeout-usecs)
            (with-alien ((tv (struct timeval)))
              (setf (slot tv 'tv-sec) (or timeout-secs 0))
              (setf (slot tv 'tv-usec) (or timeout-usecs 0))
              (select (alien-sap (addr tv)))))
           (t
            (unless *interrupts-enabled*
              (note-dangerous-select))
            (select (int-sap 0))))))

And the Book "UNIX Network Programming Vol 1 (2nd edition)" said about  
select() (p. 150):

   "Wait forever: return only when one of the specified descriptors is  
ready for I/O.
    For this, we specify the /timeout/ argument as a null pointer."

So, to make UNIX-FAST-SELECT wait for ever, both TIMEOUT-SECS and  
TIMEOUT-USECS must be NIL. I think your patch is right, that's a  
usocket bug.

Commited to trunk as r483, thanks very much!

--binghe

On 2008-12-24, at 01:13, Chaitanya Gupta wrote:

> Hi,
>
> On SBCL, providing :timeout NIL to wait-for-input returns  
> immediately even when there is no input on any of the provided  
> sockets (the documentation says it should wait indefinitely).
>
> Attached is a patch (svn diff) which fixes this, but I am not very  
> sure about how sb-unix:unix-fast-select works, so its be best if  
> someone who knows better can review it before committing this  
> patch. :)
>
> Cheers,
> Chaitanya
>
> Index: backend/sbcl.lisp
> ===================================================================
> --- backend/sbcl.lisp	(revision 482)
> +++ backend/sbcl.lisp	(working copy)
> @@ -353,7 +353,7 @@
>                 (1+ (reduce #'max (wait-list-%wait sockets)
>                             :key #'sb-bsd-sockets:socket-file- 
> descriptor))
>                 (sb-alien:addr rfds) nil nil
> -                (when timeout secs) musecs)
> +                (when timeout secs) (when timeout musecs))
> 	     (if (null count)
> 		 (unless (= err sb-unix:EINTR)
> 		   (error (map-errno-error err)))
> _______________________________________________
> usocket-devel mailing list
> usocket-devel at common-lisp.net
> http://common-lisp.net/cgi-bin/mailman/listinfo/usocket-devel

--
Chun Tian (binghe)
NetEase.com, Inc.
P. R. China





More information about the usocket-devel mailing list