简体   繁体   中英

read socket raise errno 9 randomly

I have a client/server setup communicating over tcp sockets. Functional wise works very nice except i got an errno 9, randomly but quite often, when reading the socket on the server side. According to docs and discussions i could find, errno is raised when read operation is done for a socket which is locally closed. I'm pretty sure i'm not closing the socket in the reading loop on server side. The socket is closed only by client after message is sent. Here is the reading loop on server side

void *client_listener_thread(void *args)
    {
    struct th_params *param = (void  *) args;
    int sockfd, n, client_pos;
    message_t messageR;
    char logbuf[256], buf[256];
    sockfd = param->socket;
    while(1)
        {
        n = read(sockfd, &messageR, sizeof(message_t));
        if (n < 0)
            {
            sprintf(logbuf, "cmd_thread: ERROR reading from socket errno=%d sock=%d thread=%x", errno, sockfd, (uint32_t)(param->client_listener));
            logwts(logbuf);
            break;
            }
        else if(n == 0)
            {
            sprintf(logbuf, "cmd_thread: socket closed by remote peer 2 %x", (uint32_t)(param->client_listener));
            logwts(logbuf);
            break;
            }
        else
            {
            inet_ntop(AF_INET, &messageR.header.sender_ip, buf, 255);
            sprintf(logbuf, "cmd_thread:URC Message: ID = %d sender = %s\n", messageR.header.messageID, buf);
            logwts(logbuf);

// process message
            switch(messageR.header.messageID)
                {
                case IDENT_ACK:
                    ...
                    sprintf(logbuf, "New client registered on socket %d  %d / %s:%d", sockfd, client_pos, buf, messageR.header.sender_port);
                    logwts(logbuf);
                    break;
                ...
                
                default:
                    sprintf(logbuf, "cmd_thread:unprocessed message %d", messageR.header.messageID);
                    logwts(logbuf);
                    break;
                }
            }
        }
    if(sockfd)
        close(sockfd);
    pthread_exit(NULL);
    } 

On client side, it is just open the socket, send the message, close the socket. When running, on server side, i got "New client registered..." message for the first read and on the second read i got either the expected "socket closed by remote peer..." message or unexpected "ERROR reading from socket.." with errno = 9
As i said functional has no problem, no message lost, the message is received and processed. I could ignore the error but i'm tring to understand it first.

Thx to strace could find the culprit. A race condition between threads. One thread was closing the socket while the other thread was still using it. A mutex did the job. Completely true @Sinic's comment "... that the descriptor (in this case 5) is getting closed elsewhere..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM