async: Remove old fds in epoll instance

This commit is contained in:
2026-06-05 11:15:00 +02:00
parent a240bb7e48
commit 9ea40b4793
+17 -7
View File
@@ -258,7 +258,7 @@ void async_free(void) {
void async_thread(void) { void async_thread(void) {
struct epoll_event ev, events[ASYNC_MAX_EVENTS]; struct epoll_event ev, events[ASYNC_MAX_EVENTS];
int num_fds; int num_fds, idx;
long ts, min_ts, cur_ts; long ts, min_ts, cur_ts;
volatile listen_queue_t *l; volatile listen_queue_t *l;
evt_listen_t **local; evt_listen_t **local;
@@ -303,14 +303,21 @@ void async_thread(void) {
// fd already exists, delete old one // fd already exists, delete old one
warning("Unable to add file descriptor to epoll instance"); warning("Unable to add file descriptor to epoll instance");
errno = 0; 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; continue;
}
} else if (errno == EBADF || errno == EPERM) { } else if (errno == EBADF || errno == EPERM) {
// fd probably already closed or does not support epoll somehow // fd probably already closed or does not support epoll somehow
// FIXME should not happen // FIXME should not happen
warning("Unable to add file descriptor to epoll instance"); warning("Unable to add file descriptor to epoll instance");
errno = 0; errno = 0;
local = list_delete(local, &evt); local = list_remove(local, list_size(local) - 1);
if (local == NULL) { if (local == NULL) {
critical("Unable to resize async local list"); critical("Unable to resize async local list");
return; return;
@@ -328,7 +335,7 @@ void async_thread(void) {
// calculate wait timeout // calculate wait timeout
min_ts = -1000, cur_ts = clock_micros(); min_ts = -1000, cur_ts = clock_micros();
for (int i = 0; i < list_size(local); i++) { 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; if (!evt->socket || evt->socket->timeout_us < 0) continue;
ts = evt->socket->ts_last + evt->socket->timeout_us - cur_ts; 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++) { for (int i = 0; i < num_fds; i++) {
evt_listen_t *evt = events[i].data.ptr; 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; void (*cb)(void *) = NULL;
if (async_exec_cb(evt, async_e2a(events[i].events), &cb) == 0) { 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) { if (local == NULL) {
critical("Unable to resize async local list"); critical("Unable to resize async local list");
return; return;
@@ -381,7 +391,7 @@ void async_thread(void) {
// check, if some socket ran into a timeout // check, if some socket ran into a timeout
cur_ts = clock_micros(); cur_ts = clock_micros();
for (int i = 0; i < list_size(local); i++) { 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) continue;
if (evt->socket->timeout_us >= 0 && (cur_ts - evt->socket->ts_last) >= evt->socket->timeout_us) { if (evt->socket->timeout_us >= 0 && (cur_ts - evt->socket->ts_last) >= evt->socket->timeout_us) {