- if (!epoll_ctl (epoll_fd, mode, fd, &ev))
- fd_kill (EV_A_ fd);
+ if (epoll_ctl (epoll_fd, mode, fd, &ev))
+ if (errno != ENOENT /* on ENOENT the fd went away, so try to do the right thing */
+ || (nev && epoll_ctl (epoll_fd, EPOLL_CTL_ADD, fd, &ev)))
+ fd_kill (EV_A_ fd);