From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,e801c1219b948ed,start X-Google-Attributes: gidfee84,public X-Google-Thread: fcda7,e801c1219b948ed,start X-Google-Attributes: gidfcda7,public From: Wan-Teh Chang Subject: Should poll() report POLLHUP on a half-closed TCP socket? Date: 1999/06/04 Message-ID: <37582DB6.CB5D5BE2@netscape.com> X-Deja-AN: 485783936 X-Accept-Language: en Content-Type: multipart/mixed; boundary="------------A1A1A17D058FD784A8D177FF" Organization: Netscape Communications Corporation Mime-Version: 1.0 Reply-To: wtc@netscape.com Newsgroups: comp.sys.hp.hpux,comp.unix.osf.osf1 This is a multi-part message in MIME format. --------------A1A1A17D058FD784A8D177FF Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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. --------------A1A1A17D058FD784A8D177FF Content-Type: text/plain; charset=us-ascii; name="pollserver.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="pollserver.c" #include #include #include #include #include #include #include #include #include #define BUFSIZE (64 * 1024) #define TOTAL_WRITE_SIZE (128 * 1024) void print_poll_revents(int revents) { printf("poll revents is %d:", revents); if (revents & POLLIN) { printf(" POLLIN"); } if (revents & POLLOUT) { printf(" POLLOUT"); } #ifdef POLLWRBAND if (revents & POLLWRBAND) { printf(" POLLWRBAND"); } #endif if (revents & POLLHUP) { printf(" POLLHUP"); } if (revents & POLLERR) { printf(" POLLERR"); } if (revents & POLLNVAL) { printf(" POLLNVAL"); } printf("\n"); } int main(int argc, char **argv) { struct sigaction act; int listen_sock; int accept_sock; struct sockaddr_in addr; int addr_len; int flags; struct pollfd pollfd; char buf[BUFSIZE]; int n; int ntotal; act.sa_handler = SIG_IGN; sigemptyset(&act.sa_mask); act.sa_flags = 0; if (sigaction(SIGPIPE, &act, NULL) == -1) { fprintf(stderr, "sigaction failed: %d\n", errno); exit(1); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(0); listen_sock = socket(PF_INET, SOCK_STREAM, 0); if (listen_sock == -1) { fprintf(stderr, "socket failed: %d\n", errno); exit(1); } if (bind(listen_sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) { fprintf(stderr, "bind failed: %d\n", errno); exit(1); } addr_len = sizeof(addr); if (getsockname(listen_sock, (struct sockaddr *) &addr, &addr_len) == -1) { fprintf(stderr, "getsockname failed: %d\n", errno); exit(1); } printf("bound to port %d\n", ntohs(addr.sin_port)); fflush(stdout); if (listen(listen_sock, 5) == -1) { fprintf(stderr, "listen failed: %d\n", errno); exit(1); } while (1) { addr_len = sizeof(addr); accept_sock = accept(listen_sock, (struct sockaddr *) &addr, &addr_len); if (accept_sock == -1) { fprintf(stderr, "accept failed: %d\n", errno); exit(1); } printf("connection accepted\n"); flags = fcntl(accept_sock, F_GETFL, 0); if (flags == -1) { fprintf(stderr, "fcntl failed: %d\n", errno); exit(1); } flags |= O_NONBLOCK; if (fcntl(accept_sock, F_SETFL, flags) == -1) { fprintf(stderr, "fcntl failed: %d\n", errno); exit(1); } /* Read until end of stream */ pollfd.fd = accept_sock; pollfd.events = POLLIN; do { if (poll(&pollfd, 1, -1) == -1) { fprintf(stderr, "poll failed: %d\n", errno); exit(1); } print_poll_revents(pollfd.revents); n = read(accept_sock, buf, sizeof(buf)); if (n != -1) { printf("read %d bytes\n", n); } else { if (errno == EAGAIN) { printf("read failed with EAGAIN\n"); } else if (errno == EWOULDBLOCK) { printf("read failed with EWOULDBLOCK\n"); } else { fprintf(stderr, "read failed: %d\n", errno); exit(1); } } } while (n != 0); pollfd.events = POLLOUT; ntotal = 0; do { if (poll(&pollfd, 1, -1) == -1) { fprintf(stderr, "poll failed: %d\n", errno); exit(1); } print_poll_revents(pollfd.revents); n = write(accept_sock, buf, sizeof(buf)); if (n != -1) { printf("wrote %d bytes\n", n); ntotal += n; } else { if (errno == EAGAIN) { printf("write failed with EAGAIN\n"); } else if (errno == EWOULDBLOCK) { printf("write failed with EWOULDBLOCK\n"); } else if (errno == EPIPE) { printf("write failed with EPIPE\n"); break; } else { fprintf(stderr, "write failed: %d\n", errno); exit(1); } } } while (ntotal < TOTAL_WRITE_SIZE); if (close(accept_sock) == -1) { fprintf(stderr, "close failed: %d\n", errno); exit(1); } } return 0; } --------------A1A1A17D058FD784A8D177FF Content-Type: text/plain; charset=us-ascii; name="selectserver.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="selectserver.c" #include #include #include #include #include #include #include #include #include #define BUFSIZE (64 * 1024) #define TOTAL_WRITE_SIZE (128 * 1024) int main(int argc, char **argv) { struct sigaction act; int listen_sock; int accept_sock; struct sockaddr_in addr; int addr_len; int flags; fd_set readset, writeset; char buf[BUFSIZE]; int n; int ntotal; act.sa_handler = SIG_IGN; sigemptyset(&act.sa_mask); act.sa_flags = 0; if (sigaction(SIGPIPE, &act, NULL) == -1) { fprintf(stderr, "sigaction failed: %d\n", errno); exit(1); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(0); listen_sock = socket(PF_INET, SOCK_STREAM, 0); if (listen_sock == -1) { fprintf(stderr, "socket failed: %d\n", errno); exit(1); } if (bind(listen_sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) { fprintf(stderr, "bind failed: %d\n", errno); exit(1); } addr_len = sizeof(addr); if (getsockname(listen_sock, (struct sockaddr *) &addr, &addr_len) == -1) { fprintf(stderr, "getsockname failed: %d\n", errno); exit(1); } printf("bound to port %d\n", ntohs(addr.sin_port)); fflush(stdout); if (listen(listen_sock, 5) == -1) { fprintf(stderr, "listen failed: %d\n", errno); exit(1); } while (1) { addr_len = sizeof(addr); accept_sock = accept(listen_sock, (struct sockaddr *) &addr, &addr_len); if (accept_sock == -1) { fprintf(stderr, "accept failed: %d\n", errno); exit(1); } printf("connection accepted\n"); flags = fcntl(accept_sock, F_GETFL, 0); if (flags == -1) { fprintf(stderr, "fcntl failed: %d\n", errno); exit(1); } flags |= O_NONBLOCK; if (fcntl(accept_sock, F_SETFL, flags) == -1) { fprintf(stderr, "fcntl failed: %d\n", errno); exit(1); } /* Read until end of stream */ do { FD_ZERO(&readset); FD_SET(accept_sock, &readset); if (select(accept_sock+1, &readset, NULL, NULL, NULL) == -1) { fprintf(stderr, "select failed: %d\n", errno); exit(1); } n = read(accept_sock, buf, sizeof(buf)); if (n != -1) { printf("read %d bytes\n", n); } else { if (errno == EAGAIN) { printf("read failed with EAGAIN\n"); } else if (errno == EWOULDBLOCK) { printf("read failed with EWOULDBLOCK\n"); } else { fprintf(stderr, "read failed: %d\n", errno); exit(1); } } } while (n != 0); ntotal = 0; do { FD_ZERO(&writeset); FD_SET(accept_sock, &writeset); if (select(accept_sock+1, NULL, &writeset, NULL, NULL) == -1) { fprintf(stderr, "select failed: %d\n", errno); exit(1); } n = write(accept_sock, buf, sizeof(buf)); if (n != -1) { printf("wrote %d bytes\n", n); ntotal += n; } else { if (errno == EAGAIN) { printf("write failed with EAGAIN\n"); } else if (errno == EWOULDBLOCK) { printf("write failed with EWOULDBLOCK\n"); } else if (errno == EPIPE) { printf("write failed with EPIPE\n"); break; } else { fprintf(stderr, "write failed: %d\n", errno); exit(1); } } } while (ntotal < TOTAL_WRITE_SIZE); if (close(accept_sock) == -1) { fprintf(stderr, "close failed: %d\n", errno); exit(1); } } return 0; } --------------A1A1A17D058FD784A8D177FF Content-Type: text/plain; charset=us-ascii; name="badclient.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="badclient.c" #include #include #include #include #include #include #include int main(int argc, char **argv) { int sock; int flags; struct hostent *he; struct sockaddr_in addr; if (argc != 2 && argc != 3) { fprintf(stderr, "Usage: %s \n", argv[0]); fprintf(stderr, " %s \n", argv[0]); exit(1); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; if (argc == 2) { addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons((short) atoi(argv[1])); } else { he = gethostbyname(argv[1]); if (he == NULL) { fprintf(stderr, "gethostbyname failed\n"); exit(1); } memcpy(&addr.sin_addr, he->h_addr_list[0], he->h_length); addr.sin_port = htons((short) atoi(argv[2])); } sock = socket(PF_INET, SOCK_STREAM, 0); if (sock == -1) { fprintf(stderr, "socket failed: %d\n", errno); exit(1); } if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) { fprintf(stderr, "connect failed: %d\n", errno); exit(1); } printf("connect succeeded\n"); if (write(sock, "gimme bytes", 12) == -1) { fprintf(stderr, "write failed: %d\n", errno); exit(1); } if (shutdown(sock, SHUT_WR) == -1) { fprintf(stderr, "shutdown failed: %d\n", errno); exit(1); } while (1) { sleep(1); } return 0; } --------------A1A1A17D058FD784A8D177FF Content-Type: text/plain; charset=us-ascii; name="goodclient.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="goodclient.c" #include #include #include #include #include #include #include int main(int argc, char **argv) { int sock; int flags; struct hostent *he; struct sockaddr_in addr; int n; char buf[1024]; if (argc != 2 && argc != 3) { fprintf(stderr, "Usage: %s \n", argv[0]); fprintf(stderr, " %s \n", argv[0]); exit(1); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; if (argc == 2) { addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons((short) atoi(argv[1])); } else { he = gethostbyname(argv[1]); if (he == NULL) { fprintf(stderr, "gethostbyname failed\n"); exit(1); } memcpy(&addr.sin_addr, he->h_addr_list[0], he->h_length); addr.sin_port = htons((short) atoi(argv[2])); } sock = socket(PF_INET, SOCK_STREAM, 0); if (sock == -1) { fprintf(stderr, "socket failed: %d\n", errno); exit(1); } if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) { fprintf(stderr, "connect failed: %d\n", errno); exit(1); } printf("connect succeeded\n"); if (write(sock, "gimme bytes", 12) == -1) { fprintf(stderr, "write failed: %d\n", errno); exit(1); } if (shutdown(sock, SHUT_WR) == -1) { fprintf(stderr, "shutdown failed: %d\n", errno); exit(1); } do { n = read(sock, buf, sizeof(buf)); } while (n > 0); return 0; } --------------A1A1A17D058FD784A8D177FF--