From 966d915f3c9db57332b921b5cc7d147cf20a77e6 Mon Sep 17 00:00:00 2001 From: Alireza <134321679+radkesvat@users.noreply.github.com> Date: Tue, 29 Jul 2025 14:26:11 +0330 Subject: [PATCH] Fix race condition after hio_detach Patch authored by @ithewei based on issue #754. Submitted via PR to track and close the issue. --- event/hevent.h | 8 ++++++++ event/hloop.c | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/event/hevent.h b/event/hevent.h index be0389738..1650b22a5 100644 --- a/event/hevent.h +++ b/event/hevent.h @@ -291,5 +291,13 @@ void hio_memmove_readbuf(hio_t* io); EVENT_ACTIVE(ev);\ ev->pending = 0;\ } while(0) + +#define EVENT_UNPENDING(ev) \ + do {\ + if (ev->pending) {\ + ev->pending = 0;\ + ev->loop->npendings--;\ + }\ + } while(0) #endif // HV_EVENT_H_ diff --git a/event/hloop.c b/event/hloop.c index 9f3133172..bd0d9e179 100644 --- a/event/hloop.c +++ b/event/hloop.c @@ -117,7 +117,7 @@ static int hloop_process_pendings(hloop_t* loop) { cur = loop->pendings[i]; while (cur) { next = cur->pending_next; - if (cur->pending) { + if (cur->pending && cur->loop == loop) { if (cur->active && cur->cb) { cur->cb(cur); ++ncbs; @@ -875,6 +875,7 @@ int hio_del(hio_t* io, int events) { io->loop->nios--; // NOTE: not EVENT_DEL, avoid free EVENT_INACTIVE(io); + EVENT_UNPENDING(io); } return 0; }