6

Suppose I create epoll file descriptor (epfd) by call

epfd = epoll_create( 10 );

Next I adding some count of file descriptors into this set by calling epoll_ctl(epfd,EPOLL_CTL_ADD,...) and wait for events in event loop by calling epoll_wait in separate thread.

What happened if I close epfd (by call close(epfd) in thread, other then epoll_wait thread) when epoll set is not empty and epoll_wait(epfd,...) in progress? Is epoll_wait terminated? With which results?

k.o.
  • 91
  • 6

1 Answers1

6

Predictably, Linux does the same thing as it does for select(2). From the manual page:

For a discussion of what may happen if a file descriptor in an epoll instance being monitored by epoll_wait() is closed in another thread, see select(2).

And from the select(2) page:

If a file descriptor being monitored by select() is closed in another thread, the result is unspecified. [...] On Linux (and some other systems), closing the file descriptor in another thread has no effect on select()

The tl;dr; is "don't do it":

In summary, any application that relies on a particular behavior in this scenario must be considered buggy.

cnicutar
  • 178,505
  • 25
  • 365
  • 392