Non-blocking tcp connects to epoll

My linux application runs non-blocking TCP-syscall and then uses epoll_wait to detect the completion of a three-way handshake. Sometimes epoll_wait returns with the POLLOUT and POLLERR events specified for the same socket descriptor.

I would like to understand what is happening at the TCP level. I can not play it on demand. I assume that between the two calls to epoll_wait inside my event loop, we had the sequence SYN + ACK / ACK / FIN, but again I cannot play it.

+5
linux tcp epoll
source share
2 answers

This will probably happen if the connection failed — for example, with a “connection timeout” (for sockets that perform a non-blocking connection, POLLOUT set when the connection operation is completed for both successful and unsuccessful results).

When the POLLOUT installed on the socket, use getsockopt(sock, SOL_SOCKET, SO_ERROR, ...) to check if the connection is successful or not (in this case, the socket option SO_ERROR is 0, otherwise it indicates why the connection failed) .

+4
source share

Here is some good information on non-blocking tcp connect () .

When a socket error is detected (i.e. the connection is closed / rejected / delayed), epoll will return the registered POLLIN / POLLOUT percentages with POLLERR. So epoll_wait () will return POLLOUT | POLLERR if you registered POLLOUT or POLLIN | POLLOUT | POLLERR if registered POLLIN | POLLOUT.

Just because epoll returns POLLIN does not mean that the data will be readable, because recv () may just return an error from a non-blocking connect () call. I think epoll returns all the logged events using POLLERR to make sure the program calls send () / recv () / etc .. and receives a socket error. Some programs never check POLLERR / POLLHUP and only catch socket errors the next time send () / recv () is called.

+2
source share

All Articles