29

Assume the following code where "sock" is a handle to TCP socket that was previously registered with an epoll file descriptor designated by epfd.

epoll_ctl(epfd, EPOLL_CTL_DEL, sock, &ev);
close(sock);

Is it still necessary to call epoll_ctl if the socket is going to get subsequently closed anyway? Or does the socket get implicitly deregistered as a result of closing it?

selbie
  • 100,020
  • 15
  • 103
  • 173

1 Answers1

39

From the man page:

Q6 Will closing a file descriptor cause it to be removed from all epoll sets automatically?

A6 Yes, but be aware of the following point. A file descriptor is a reference to an open file description (see open(2)). Whenever a descriptor is duplicated via dup(2), dup2(2), fcntl(2) F_DUPFD, or fork(2), a new file descriptor referring to the same open file description is created. An open file description continues to exist until all file descriptors referring to it have been closed. A file descriptor is removed from an epoll set only after all the file descriptors referring to the underlying open file description have been closed (or before if the descriptor is explicitly removed using epoll_ctl(2) EPOLL_CTL_DEL). This means that even after a file descriptor that is part of an epoll set has been closed, events may be reported for that file descriptor if other file descriptors referring to the same underlying file description remain open.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 2
    Thanks! I kept doing "man epoll_ctl", "man epoll_create" looking for an answer, but forgot about the summary man page for "man epoll". – selbie Jan 03 '12 at 07:15
  • So provided nobody has duplicated it with dup() and the process hasn't forked, you're ok? – MarkR Jan 04 '12 at 16:14
  • Yes, I think that sums it nicely. – Frédéric Hamidi Jan 04 '12 at 16:16
  • 2
    That does not seem to be the case, however. I just wrote a simple snippet that created a listening socket, added it to an epoll, closed the socket and went on to epoll_wait. It was there, it fired if a con had been made in between and del succeeded. – Tommi Kyntola Dec 05 '13 at 18:57
  • I think the desired behaviour is that it should be removed. But I'm seeing that if you do a half shutdown it won't be removed. Beware of that. It makes sense that a half shutdown won't remove it. – hookenz Oct 03 '16 at 23:58
  • 3
    @Matt Yeah, obviously file descriptors don't close if you don't `close()` them... – Will Mar 13 '19 at 01:45