What happen, when epoll file descriptor is closed?
Asked Answered
T

1

7

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?

Trabue answered 24/10, 2014 at 11:17 Comment(0)
B
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.

Bauxite answered 24/10, 2014 at 11:55 Comment(2)
Is this still the case? The paragraph pointing to select()'s manual page seems to no longer be part of epoll_wait()'s page (while it is still present in poll()'s, for example).Herbalist
(The documentation was changed here: github.com/mkerrisk/man-pages/commit/…)Herbalist

© 2022 - 2024 — McMap. All rights reserved.