20071114

Error in glibc's select() function

While doing some programming for work, I discovered that the select() function provided by glibc does not accurately describe when data is ready to read from a socket.

The purpose of select() is to block on a file descriptor until data becomes ready, unless a specified relative timeout expires or an error occurs.

When data is ready, select will return a positive number corresponding to one of the sockets contained in one of the file-descriptor sets that were passed as parameters. If a timeout occurs, zero is returned. If an error occured, -1 is returned and errno is set appropriately.

I found that if select was called, a timeout occurred, then data became ready to read on the socket, if select was called one more time (after data has obviously arrived), then select fails to notice. That implies that glibc select() only checks for socket readiness whenever the kernel signals that the socket is ready, and not when it is initially called.

From the select(2) man page, there exists no such condition stating that the data must become available after select has been called and not before.

That's a major bug if I've ever heard of one. Hmm... it's too bad my project can't wait until the next release of glibc, when someone might have corrected this.

No comments: