]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: epoll: Make sure we can add a new event
authorOlivier Houchard <ohouchard@haproxy.com>
Tue, 25 Feb 2025 11:01:40 +0000 (12:01 +0100)
committerOlivier Houchard <cognet@ci0.org>
Wed, 26 Feb 2025 12:00:18 +0000 (13:00 +0100)
Check that the call to epoll_ctl() succeeds, and if it does not, if
we're adding a new event and it fails with EEXIST, then delete and
re-add the event. There are a few cases where we may already have events
for a fd. If epoll_ctl() fails for any reason, use BUG_ON to make sure
we immediately crash, as this should not happen.

src/ev_epoll.c

index 21770aa35eccdf5a2939ab5dbceca2fbe37685e2..e0dd52158e2d510cbf5798f783d5801ccbf46954 100644 (file)
@@ -154,7 +154,14 @@ static void _update_fd(int fd)
  done:
        ev.events &= ~epoll_mask;
        ev.data.u64 = ((u64)fdtab[fd].generation << 32) + fd;
-       epoll_ctl(epoll_fd[tid], opcode, fd, &ev);
+       if (epoll_ctl(epoll_fd[tid], opcode, fd, &ev) != 0) {
+               if (opcode == EPOLL_CTL_ADD && errno == EEXIST) {
+                       BUG_ON(epoll_ctl(epoll_fd[tid], EPOLL_CTL_DEL, fd, &ev) != 0);
+                       BUG_ON(epoll_ctl(epoll_fd[tid], EPOLL_CTL_ADD, fd, &ev) != 0);
+               } else {
+                       BUG_ON(1, "epoll_ctl() failed when it should not");
+               }
+       }
 }
 
 /*