title: Epoll_wait returning events on closed file descriptor
body: <p>I'm working with a multithreaded embedded application in which epoll is used for IO in one of the threads. I'm relying on a particular feature of epoll that specifies that closing a file descriptor automatically removes it from the epoll set (Question/Answer 6 in man 7 epoll). In this case, the file descriptor close is done in the same thread that <code>epoll_wait</code> is invoked. What ends up happening is that <code>epoll_wait</code> returns an event on a file descriptor after it has been closed and the program ends up crashing because it tries to access resources that were deallocated when the file descriptor was closed. As far as I know, the file descriptor is not duped anywhere, though I do not know how to validate this. I know for a fact that there are no calls to <code>fork()</code>, <code>dup()</code>, <code>dup2()</code>, or <code>fcntl()</code> with the particular dup option. This particular file descriptor is registered with <code>EPOLLOUT</code>, <code>EPOLLIN</code>, <code>EPOLLERR</code>, and <code>EPOLLHUP</code>. It is level-triggered. Are there any caveats to this feature that anybody knows about? Is the man page incorrect? Any useful information that can help me further debug the issue? I know I could just remove the file descriptor from the set, but I would like to know why this is happening.</p>