Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

poll() can't detect event when socket is closed locally?

Tags:

linux

sockets

I'm working on a project that will port a TCP/IP client program onto an embedded ARM-Linux controller board. The client program was originally written in epoll(). However, the target platform is quite old; the only kernel available is 2.4.x, and epoll() is not supported. So I decided to rewrite the I/O loop in poll().

But when I'm testing code, I found that poll() does not act as I expected : it won't return when a TCP/IP client socket is closed locally, by another thread. I've wrote a very simple codes to do some test:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <pthread.h>
#include <poll.h>

struct pollfd   fdList[1];

void *thread_runner(void *arg)
{
    sleep(10);
    close(fdList[0].fd);
    printf("socket closed\n");
    pthread_exit(NULL);
}

int main(void)
{
    struct  sockaddr_in hostAddr;
    int sockFD;
    char buf[32];
    pthread_t handle;

    sockFD = socket(AF_INET, SOCK_STREAM, 0);
    fcntl(sockFD,F_SETFL,O_NONBLOCK|fcntl(sockFD,F_GETFL,0));

    inet_aton("127.0.0.1",&(hostAddr.sin_addr));
    hostAddr.sin_family = AF_INET;
    hostAddr.sin_port  = htons(12345);
    connect(sockFD,(struct sockaddr *)&hostAddr,sizeof(struct sockaddr));

    fdList[0].fd = sockFD;
    fdList[0].events = POLLOUT;

    pthread_create(&handle,NULL,thread_runner,NULL);

    while(1) {
        if(poll(fdList,1,-1) < 1) {
            continue;
        }
        if(fdList[0].revents & POLLNVAL ) {
            printf("POLLNVAL\n");
            exit(-1);
        }
        if(fdList[0].revents & POLLOUT) {
            printf("connected\n");
            fdList[0].events = POLLIN;
        }
        if(fdList[0].revents & POLLHUP ) {
            printf("closed by peer\n");
            close(fdList[0].fd);
            exit(-1);
        }
        if(fdList[0].revents & POLLIN) {
            if( read(fdList[0].fd, buf, sizeof(buf)) < 0) {
                printf("closed by peer\n");
                close(fdList[0].fd);
                exit(-1);
            }
        }
    }
    return 0;
}

In this code I first create a TCP client socket, set to non-blocking mode, add to poll(), and close() the socket in another thread. And the result is: "POLLNVAL" is never printed while the socket is closed.

Is that an expected behavior of poll() ? Will it help if I choose select() instead of poll() ?

like image 479
RichardLiu Avatar asked Jan 20 '23 11:01

RichardLiu


1 Answers

Yes, this is expected behavior. You solve this by using shutdown() on the socket instead of close().

See e.g. http://www.faqs.org/faqs/unix-faq/socket/ section 2.6

EDIT: The reason this is expected is that poll() and select() reacts to events happening on one of their fd's. close() removes the fd, it does not exist at all anymore, and thus it can't have any events associated with it.

like image 113
Erik Avatar answered Jan 28 '23 17:01

Erik