3

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?

5
  • It's been a while since I used poll and friends, but I don't remember ever adding an fd to a poll set before it was properly set up (i.e., after calling accept, connect or listen). Are you sure that adding a non-initialized socket to an epoll set is valid?
    – vanza
    Commented Feb 8, 2013 at 3:45
  • Absolutely valid. You can add any valid socket even before connect/bind/accept.
    – hookenz
    Commented Feb 8, 2013 at 3:49
  • This code prints: "Events: err=0, hup=16". So are you really really sure it's valid? (Well, I guess getting POLLHUP is valid, but you get what I meant.)
    – vanza
    Commented Feb 8, 2013 at 4:10
  • That link takes me to "unknown pastbin id". Anyway, changing the order of things and filtering the event properly solved it.
    – hookenz
    Commented Feb 11, 2013 at 1:02
  • I had set the expiration to 1 day; I re-uploaded it here with 1 month expiration now.
    – vanza
    Commented Feb 11, 2013 at 2:54

1 Answer 1

4

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.

2
  • No that is incorrect. Your socket is alive as soon as a call to socket() has completed. i.e. it is a properly initialized socket as far as TCP/IP is concerned. I have other code that creates it non-blocking, adds it to epoll and calls connect. When epoll_wait returns a writeable event I know it's connected. In this code, I don't see the HUP event.
    – hookenz
    Commented Feb 10, 2013 at 22:11
  • See my other comment with the pastebin code. An uninitialized (i.e. before connect / listen) socket always seems to cause EPOLLHUP (at least when in blocking mode).
    – vanza
    Commented Feb 11, 2013 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.