Search code examples
csocketsposix-select

Problem with select when closing socket


I'm doing a multiclient server that accepts a connection, forks, and gives the connection to the child so it can handle it. It's a multiclient server, so it has multiple children.

The main process is in an infinite while which makes a select to find out if there is a new incoming connection or if a child is trying to communicate.

The problem arises when I close a client (which is connected to a son of the main server): it randomly happens that the client connection is closed, and the select gets unblocked because supposedly the internal socket (which handles the incoming connection between the children and the main server) was modified, but as far as I am concerned that is not true. What actually happened was that the client closed the connection and the child just died.

Can anyone give me a clue of what it's going on here? I'm quite lost.

This is the code of the infinite loop in the main server:

while (1) {
    /*inicializo variables para el select*/
    sflag = 0;
    FD_ZERO(&readfds);
    FD_SET(sockfd, &readfds);
    FD_SET(sockifd,&readfds);
    max = (sockfd > sockifd) ? sockfd : sockifd;
    for(aux = isockets; aux != NULL; aux = aux -> next){
        FD_SET(aux -> sd, &readfds);
        max = (max > aux -> sd) ? max : aux -> sd;
    }

    printf("pre-select\n");
    select(max + 1, &readfds, NULL, NULL, NULL);
    /*checkeo si salio por actividad en un socket interno*/
    for (aux = isockets; aux != NULL; aux = aux -> next){
        if (FD_ISSET(aux -> sd, &readfds)){
            printf("comunicacion con el socket: %d\n", aux -> sd);
            sflag = 1;
            actsocket = aux -> sd;
            break;
        }
    }
    if (sflag == 1){//mensaje de un hijo
        n = recv(actsocket, buffer, sizeof(buffer), 0);
        if (n == 0) {
            printf("conexion cerrada con el socket interno: %d\n", actsocket);
            close(actsocket);
            isockets = free_sock(isockets, actsocket);
            printf("isockets: %p\n", isockets);
        }
        else if(n < 0) error ("ERROR en comunicacion interna");
        else printf("mensaje del boludon: %s\n", buffer);
    }   
    else if (FD_ISSET(sockifd, &readfds)){// un hijo inicia conexion interna
        printf("antes de accpet interno\n");
        newisockfd = accept(sockifd, (struct sockaddr *) &ucli_addr, &uclilen);
        printf("nueva conexion interna, socketfd: %d\n", newisockfd);
        isockets = add_socket(isockets,newisockfd, 0);
        recorre(isockets);
        if (newisockfd < 0) error ("ERROR en accept unix, padre");
    }
    else if (FD_ISSET (sockfd, &readfds)){/*conexion entrante*/
        printf("conexion entrante\n");
        newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
            if (newsockfd < 0) error("ERROR on accept");
            pid = fork();

        if (pid < 0) error("ERROR on fork");
            if (pid == 0){//hijo
                    close(sockfd);
            dostuff(newsockfd, path, tm,fd[0]);
                    exit(0);

            }
            else {  //padre
            printf("conexion aceptada, pid hijo %d\n", pid);
            close(newsockfd);
        }
    }
    }

So, randomly, when I close a connection, the select unblock as if "sockifd" was modified, but it wasn't. Don't know why is doing that.


Solution

  • One thing that is wrong in your code is that you're not checking the return value of select.

    If select is interrupted by a signal (returns -1 with errno = EINTR, for example SIGCHLD if one of the children died), then the contents of &readfds is undefined, and thus must not be read. (See for example the Linux man page for select.)

    So check the return value if select, and loop right back to it without going through the &readfds processing if there's a temporary error like EINTR.