hookenz
hookenz

Reputation: 38967

Why am I getting the EPOLLHUP event on a brand new socket

I have some code that when run on a virtual machine is misbehaving for some reason.

The order of initialization is:

s_listen = socket(...)
bind(s_listen, ...)
epoll_ctl(epfd, EPOLL_CTL_ADD, s_listen, ...)
listen(s_listen, SOMAXCONN)

There is an event loop/thread running and processing events on the epoll file descriptor before bind is even called.

That event loop gets an EPOLLHUP before the call to listen() on the newly created s_listen socket.

So my question is, why am I getting the EPOLLHUP event on a brand new socket?

The error goes away when I put the epoll_ctl after call to listen(), however will that cause some potential connection events to be missed should they come in before the socket is added to epoll?

Upvotes: 3

Views: 4480

Answers (1)

vanza
vanza

Reputation: 9903

As my example in the comments shows, it seems you can't poll the socket before it's properly initialized, unless you want to handle EPOLLHUP.

As for the question, no, you won't miss any events. Calling listen() then epoll() is the same you'd have to do otherwise (listen() + blocking accept()); actual incoming connections between those calls are handled by the kernel and stay waiting until your code handles them.

Upvotes: 4

Related Questions