From 1a1bc7d96fe3f5ad3b0fcd3bc3734462999a985c Mon Sep 17 00:00:00 2001 From: ruti <> Date: Sat, 4 May 2024 18:58:23 +0300 Subject: [PATCH] fix: conev add/del race --- conev.c | 15 ++++++++++++--- conev.h | 1 + proxy.c | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/conev.c b/conev.c index 260dbe5..ed90691 100644 --- a/conev.c +++ b/conev.c @@ -14,6 +14,7 @@ struct poolhd *init_pool(int count) } pool->max = count; pool->count = 0; + pool->init_count = 0; pool->iters = 0; #ifndef NOEPOLL @@ -51,12 +52,17 @@ struct eval *add_event(struct poolhd *pool, enum eid type, return 0; } val = pool->links[c]; - } while (c && val->del_iter == pool->iters); + } while (c < pool->init_count && val->del_iter == pool->iters); + if (c != pool->count) { + struct eval *t = pool->links[c]; + pool->links[c] = pool->links[pool->count]; + pool->links[pool->count] = t; + } memset(val, 0, sizeof(*val)); val->fd = fd; - val->index = c; + val->index = pool->count; val->type = type; #ifndef NOEPOLL @@ -68,7 +74,7 @@ struct eval *add_event(struct poolhd *pool, enum eid type, } val->events = ev.events; #else - struct pollfd *pfd = &(pool->pevents[c]); + struct pollfd *pfd = &(pool->pevents[pool->count]); pfd->fd = fd; pfd->events = POLLIN | e; @@ -76,6 +82,9 @@ struct eval *add_event(struct poolhd *pool, enum eid type, #endif pool->count++; + if (pool->count > pool->init_count) { + pool->init_count = pool->count; + } return val; } diff --git a/conev.h b/conev.h index 09245b7..dac1727 100644 --- a/conev.h +++ b/conev.h @@ -85,6 +85,7 @@ struct eval { struct poolhd { int max; + int init_count; int count; int efd; struct eval **links; diff --git a/proxy.c b/proxy.c index 569d5d0..568cc8a 100644 --- a/proxy.c +++ b/proxy.c @@ -807,7 +807,7 @@ int event_loop(int srvfd) uniperror("(e)poll"); break; } - LOG(LOG_L, "new event: fd: %d, evt: %s\n", val->fd, eid_name[val->type]); + LOG(LOG_L, "new event: fd: %d, evt: %s, del_iter: %d\n", val->fd, eid_name[val->type], val->del_iter); if (val->del_iter) { continue;