Bug 31255

Summary: select with zero timeout returns 0 even when data pending
Product: Base System Reporter: root <dozen>
Component: kernAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.3-RELEASE   
Hardware: Any   
OS: Any   

Description root 2001-10-13 20:00:06 UTC
        struct timeval tv = { 0,0 };
        rc = select(...,&tv);

        This code will always return zero even if data is pending
        one any descriptor or any descriptor is ready to write.
        This is because check for timed out performed before 
        asking kernel about descriptor state.

	Single Unix Spec do not states exactly that zero timeout shall
        lead to immediate return with correct descriptor number as result,
        but it looks like it is supposed to be as such.

Fix: 

The problem code is in /usr/src/sys/kern/sys_generic.c
Comment 1 Bruce Evans 2001-10-14 03:09:41 UTC
On 13 Oct 2001 root@do-labs.spb.ru wrote:

> >Description:
>         struct timeval tv = { 0,0 };
>         rc = select(...,&tv);
>
>         This code will always return zero even if data is pending
>         one any descriptor or any descriptor is ready to write.
>         This is because check for timed out performed before
>         asking kernel about descriptor state.

I can't see this.  The descriptors are checked before the timeout.  The
timeout is just copyin()'ed and (bogusly) validated early, so select()
may return an error for certain problems with the timeout even when there
is a ready descriptor so there is no need to use the timeout.

> 	Single Unix Spec do not states exactly that zero timeout shall
>         lead to immediate return with correct descriptor number as result,
>         but it looks like it is supposed to be as such.

POSIX.1-200x-draft7 seems to be exact enough.  It says that if none of
the descriptors are ready, then select() shall block until one becomes
ready or the timeout expires, etc.  It doesn't say anything about using
the timeout if a descriptor is ready.

Bruce
Comment 2 iedowse freebsd_committer freebsd_triage 2002-12-01 19:13:01 UTC
State Changed
From-To: open->closed


Using select with a zero timeout to poll for ready descriptors works 
fine for me, and many things depend on it working. Without example 
code that demonstrates it not working, I can only assume it was a 
bug in your code that calls select.