This is a long message, but it is relevant to people implementing a server application using poll() and non-blocking file descriptors on Digital UNIX and HP-UX. OS: all revisions of Digital UNIX and HP-UX Summary: poll() reports POLLHUP on a TCP socket that has received a FIN packet from the peer, i.e., a half-closed or fully-closed TCP connection. Description: It is a common programming tip among server implementors that poll() is preferrable to select() when an application has a large number of open file descriptors. (The fd_set datatype used by select() can hold at most FD_SETSIZE file descriptors.) However, the poll() implementations on all revisions of Digital UNIX and HP-UX have a behavior that may be exploited by a client program to put a server's main polling loop into a tight busy spin, consuming a lot of CPU. Many server applications contain a main loop that calls select() or poll() on a set of file descriptors and then invoke I/O functions on file descriptors that are ready for I/O or have error conditions. It is important that select() or poll() not return until some file descriptor is ready for I/O or has an error condition. The poll() implementations on all revisions of Digital UNIX and HP-UX report the POLLHUP event on a TCP socket that has received a FIN packet. Moreover, the POLLHUP event is sticky, meaning that all subsequent poll() calls on that socket will return *immediately* with POLLHUP. A FIN packet may be received when the peer (the client) makes a shutdown(..., SHUT_WR) call to half-close the TCP connection, while leaving the other direction (reading from the server) open. At this moment, the server may still be in the middle of writing the reply to the client. Because of the sticky POLLHUP event on that socket, the poll() calls in the main polling loop don't block. A malicious client would stop reading from the server. The send buffer on the server side would fill up and block further writes. The server would keep polling on that socket for writability (POLLOUT), but poll() would return immediately with POLLHUP, resulting in a tight busy spin. See the diagram below. Half-closed TCP connection POLLHUP client <-------------------------------- server This direction still open The attached programs demonstrate this busy spin. There are two versions of a simple server, pollserver.c and selectserver.c, using poll() and select() in its main loop, respectively. There are two client programs, goodclient.c and badclient.c. They connect to the server, write a request, and half-close the connection. Then the good client reads the server's reply, whereas the bad client just sits idle. On Digital UNIX and HP-UX, pollserver will start to spin and consume CPU but selectserver will block nicely in select(). To run the tests, first run a server. It will print the port number it is listening on. Then run a client with that port number as an argument. You can also run a client on another machine, passing the server host name and port number as arguments. I think it is wrong to report POLLHUP on a socket that has received a FIN packet. The socket is still usable in the case of a half-closed TCP connection. The *unsolicited*, *immediate*, and *persistent* reporting of POLLHUP by poll() can put the main polling loop into a tight busy spin. The fact that a FIN packet has been received can be communicated by having poll() report POLLIN (socket readable) and having the next read() call on that socket return 0, indicating an end of stream. With this behavior, poll() can't be used safely on TCP sockets in general. The only workaround is to go back to using select(). No other Unix flavors that I have looked at have this poll() behavior. Your comments are welcome. Wan-Teh Chang wtc@netscape.com -- My opinions do not necessarily reflect the opinions of my employer.