From 9ea40b479309fdca9518f30ace51a9329ca96665 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Fri, 5 Jun 2026 11:15:00 +0200 Subject: [PATCH] async: Remove old fds in epoll instance --- src/async.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/async.c b/src/async.c index 615c9c5..ad0c717 100644 --- a/src/async.c +++ b/src/async.c @@ -258,7 +258,7 @@ void async_free(void) { void async_thread(void) { struct epoll_event ev, events[ASYNC_MAX_EVENTS]; - int num_fds; + int num_fds, idx; long ts, min_ts, cur_ts; volatile listen_queue_t *l; evt_listen_t **local; @@ -303,14 +303,21 @@ void async_thread(void) { // fd already exists, delete old one warning("Unable to add file descriptor to epoll instance"); errno = 0; - if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, evt->fd, NULL) != -1) + if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, evt->fd, NULL) != -1) { + local = list_delete(local, evt); + if (local == NULL) { + critical("Unable to resize async local list"); + return; + } + notice("Removed old file descriptor from epoll instance"); continue; + } } else if (errno == EBADF || errno == EPERM) { // fd probably already closed or does not support epoll somehow // FIXME should not happen warning("Unable to add file descriptor to epoll instance"); errno = 0; - local = list_delete(local, &evt); + local = list_remove(local, list_size(local) - 1); if (local == NULL) { critical("Unable to resize async local list"); return; @@ -328,7 +335,7 @@ void async_thread(void) { // calculate wait timeout min_ts = -1000, cur_ts = clock_micros(); for (int i = 0; i < list_size(local); i++) { - evt_listen_t *evt = local[i]; + const evt_listen_t *evt = local[i]; if (!evt->socket || evt->socket->timeout_us < 0) continue; ts = evt->socket->ts_last + evt->socket->timeout_us - cur_ts; @@ -349,7 +356,10 @@ void async_thread(void) { for (int i = 0; i < num_fds; i++) { evt_listen_t *evt = events[i].data.ptr; - if (!list_contains(local, &evt)) continue; + if ((idx = list_find(local, &evt)) == -1) { + error("epoll instance reported event not contained in local list"); + continue; + } void (*cb)(void *) = NULL; if (async_exec_cb(evt, async_e2a(events[i].events), &cb) == 0) { @@ -364,7 +374,7 @@ void async_thread(void) { } } - local = list_delete(local, &evt); + local = list_remove(local, idx); if (local == NULL) { critical("Unable to resize async local list"); return; @@ -381,7 +391,7 @@ void async_thread(void) { // check, if some socket ran into a timeout cur_ts = clock_micros(); for (int i = 0; i < list_size(local); i++) { - evt_listen_t *evt = local[i]; + const evt_listen_t *evt = local[i]; if (!evt->socket) continue; if (evt->socket->timeout_us >= 0 && (cur_ts - evt->socket->ts_last) >= evt->socket->timeout_us) {