0
0
mirror of https://github.com/hufrea/byedpi.git synced 2025-07-04 21:14:18 +00:00

fix: conev add/del race

This commit is contained in:
ruti 2024-05-04 19:42:19 +03:00
parent 1a1bc7d96f
commit 0f67d56be4
3 changed files with 32 additions and 42 deletions

44
conev.c
View File

@ -14,7 +14,6 @@ struct poolhd *init_pool(int count)
} }
pool->max = count; pool->max = count;
pool->count = 0; pool->count = 0;
pool->init_count = 0;
pool->iters = 0; pool->iters = 0;
#ifndef NOEPOLL #ifndef NOEPOLL
@ -44,23 +43,13 @@ struct poolhd *init_pool(int count)
struct eval *add_event(struct poolhd *pool, enum eid type, struct eval *add_event(struct poolhd *pool, enum eid type,
int fd, int e) int fd, int e)
{ {
int c = pool->count - 1; if (pool->count >= pool->max) {
struct eval *val;
do {
c++;
if (c >= pool->max) {
return 0; return 0;
} }
val = pool->links[c]; struct eval *val = pool->links[pool->count];
} 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)); memset(val, 0, sizeof(*val));
val->mod_iter = pool->iters;
val->fd = fd; val->fd = fd;
val->index = pool->count; val->index = pool->count;
val->type = type; val->type = type;
@ -82,16 +71,13 @@ struct eval *add_event(struct poolhd *pool, enum eid type,
#endif #endif
pool->count++; pool->count++;
if (pool->count > pool->init_count) {
pool->init_count = pool->count;
}
return val; return val;
} }
void del_event(struct poolhd *pool, struct eval *val) void del_event(struct poolhd *pool, struct eval *val)
{ {
if (val->del_iter) { if (!val->fd) {
return; return;
} }
if (val->buff.data) { if (val->buff.data) {
@ -100,7 +86,7 @@ void del_event(struct poolhd *pool, struct eval *val)
} }
close(val->fd); close(val->fd);
val->fd = 0; val->fd = 0;
val->del_iter = pool->iters; val->mod_iter = pool->iters;
pool->count--; pool->count--;
struct eval *ev = pool->links[pool->count]; struct eval *ev = pool->links[pool->count];
@ -158,6 +144,7 @@ void destroy_pool(struct poolhd *pool)
#ifndef NOEPOLL #ifndef NOEPOLL
struct eval *next_event(struct poolhd *pool, int *offs, int *type) struct eval *next_event(struct poolhd *pool, int *offs, int *type)
{ {
while (1) {
int i = *offs; int i = *offs;
if (i < 0) { if (i < 0) {
i = (epoll_wait(pool->efd, pool->pevents, pool->max, -1) - 1); i = (epoll_wait(pool->efd, pool->pevents, pool->max, -1) - 1);
@ -169,10 +156,14 @@ struct eval *next_event(struct poolhd *pool, int *offs, int *type)
} }
pool->iters++; pool->iters++;
} }
struct eval *val = pool->pevents[i].data.ptr;
*offs = i - 1; *offs = i - 1;
if (val->mod_iter == pool->iters) {
continue;
}
*type = pool->pevents[i].events; *type = pool->pevents[i].events;
return val;
return pool->pevents[i].data.ptr; }
} }
@ -204,14 +195,17 @@ struct eval *next_event(struct poolhd *pool, int *offs, int *typel)
pool->iters++; pool->iters++;
} }
short type = pool->pevents[i].revents; short type = pool->pevents[i].revents;
if (!type) if (!type) {
continue; continue;
}
struct eval *val = pool->links[i];
if (val->mod_iter == pool->iters) {
continue;
}
pool->pevents[i].revents = 0; pool->pevents[i].revents = 0;
*offs = i - 1; *offs = i - 1;
*typel = type; *typel = type;
return val;
return pool->links[i];
} }
} }

View File

@ -80,12 +80,11 @@ struct eval {
#ifndef NOEPOLL #ifndef NOEPOLL
uint32_t events; uint32_t events;
#endif #endif
unsigned int del_iter; unsigned int mod_iter;
}; };
struct poolhd { struct poolhd {
int max; int max;
int init_count;
int count; int count;
int efd; int efd;
struct eval **links; struct eval **links;

View File

@ -807,11 +807,8 @@ int event_loop(int srvfd)
uniperror("(e)poll"); uniperror("(e)poll");
break; break;
} }
LOG(LOG_L, "new event: fd: %d, evt: %s, del_iter: %d\n", val->fd, eid_name[val->type], val->del_iter); LOG(LOG_L, "new event: fd: %d, evt: %s, mod_iter: %d\n", val->fd, eid_name[val->type], val->mod_iter);
if (val->del_iter) {
continue;
}
switch (val->type) { switch (val->type) {
case EV_ACCEPT: case EV_ACCEPT:
if ((etype & POLLHUP) || if ((etype & POLLHUP) ||