Linux – Losing bytes on an epoll controlled non-blocking socket when the other side writes and closes

epolllinuxnonblockingsockets

I've a non-blocking socket currently subscribed to:

 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP | EPOLLRDHUP| EPOLLET;

It receives a couple of EPOLLINs which I read non-blocking till EAGAIN and then I receive HUP & RDHUP, sometimes with a few bytes more to read.

The other side is just:

send(socket,960_bytes_buffer)
close(socket);

I've tried recv with msg_peek directly in the event loop for both epollin and in close time, and adding received data it doesn't receive 960 always, sometimes only around 480 bytes.

Making the socket non-blocking or putting a sleep(1) in the client between the send and the close works OK.

It looks to me more a problem of non-blocking sockets than epoll related. Something simple as "nc -l -p port" receives the proper amount of bytes.

Best Answer

Have a look at The ultimate SO_LINGER page, or: why is my tcp not reliable which nicely explains what is happening and how to fix it.

Related Topic