Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happen, when epoll file descriptor is closed?

Tags:

epoll

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?

like image 338
k.o. Avatar asked Oct 24 '14 11:10

k.o.


1 Answers

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.

like image 154
cnicutar Avatar answered Sep 23 '22 15:09

cnicutar