From: Wan-Teh Chang <wtc@netscape.com>
Subject: Should poll() report POLLHUP on a half-closed TCP socket?
Date: 1999/06/04 [thread overview]
Message-ID: <37582DB6.CB5D5BE2@netscape.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 3887 bytes --]
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.
[-- Attachment #2: pollserver.c --]
[-- Type: text/plain, Size: 4699 bytes --]
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#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;
}
[-- Attachment #3: selectserver.c --]
[-- Type: text/plain, Size: 4211 bytes --]
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <signal.h>
#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;
}
[-- Attachment #4: badclient.c --]
[-- Type: text/plain, Size: 1564 bytes --]
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
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 <port>\n", argv[0]);
fprintf(stderr, " %s <host> <port>\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;
}
[-- Attachment #5: goodclient.c --]
[-- Type: text/plain, Size: 1627 bytes --]
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
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 <port>\n", argv[0]);
fprintf(stderr, " %s <host> <port>\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;
}
reply other threads:[~1999-06-04 4:00 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox