3
votes

I am implementing a UNIX domain socket Inter Process communication code and I hit this error randomly - "errno 11: Resource temporarily unavailable" while trying to read from the socket. I use MSG_PEEK to read the number of bytes from the socket, and allocate bytes for receive buffer, and read the actual data.

The socket is a blocking socket and I do not have any code to have non-blocking (in all, accept/read/write). Any pointers on what could cause this on a blocking socket read? From the man page of MSG_PEEK, it seems like EAGAIN could be returned when socket is marked as non-blocking, using O_NONBLOCK.

The failure happens on the recv call below.

        char temp_buffer[BUFFER_MAX];
        num_bytes = recv(_connection_fd, &temp_buffer, BUFFER_SIZE_MAX, MSG_PEEK | MSG_TRUNC);
        if (num_bytes < 0) {
            LogError("Error reading from socket. %s", strerror(errno));
            close(_connection_fd);
            return -1;
        }
        .....
        <Allocate memory>
        .....
        // Read actual data
        num_bytes = read(_connection_fd, buffer, num_bytes);
        ...
        <Send response back to client>
        <Close socket descriptor>
1
if (num_bytes == -1 && errno == EAGAIN) { ... just retry the operation, maybe after some sleep ...} Summay: EAGAIN is not an error, it is a condition why the system call failed, but you could retry it.wildplasser

1 Answers

2
votes

It may also happen to blocking socket and blocking recv, if socket has timeout SO_RCVTIMEO option set (default is 0). BTW, did you consider select() before recv()?