From 1d264a53005990a15b380bc31d6191c3af26b2bf Mon Sep 17 00:00:00 2001 From: radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:58:00 +0000 Subject: [PATCH 1/2] fix(event): report failed read watcher registration --- Makefile | 3 + event/hevent.c | 13 ++-- event/hevent.h | 1 + event/hloop.c | 49 +++++++++++----- event/hloop.h | 2 + event/nio.c | 10 +--- scripts/unittest.sh | 3 + unittest/CMakeLists.txt | 8 +++ unittest/read_registration_test.c | 98 +++++++++++++++++++++++++++++++ 9 files changed, 160 insertions(+), 27 deletions(-) create mode 100644 unittest/read_registration_test.c diff --git a/Makefile b/Makefile index 34817430b..16d673029 100644 --- a/Makefile +++ b/Makefile @@ -390,6 +390,9 @@ unittest: prepare libhv $(CC) -g -Wall -O0 -std=c99 -I. -Ibase -o bin/hmutex_test unittest/hmutex_test.c base/htime.c -pthread $(CC) -g -Wall -O0 -std=c99 -I. -Ibase -o bin/connect_test unittest/connect_test.c base/hsocket.c base/htime.c $(CC) -g -Wall -O0 -std=c99 -I. -Ibase -o bin/socketpair_test unittest/socketpair_test.c base/hsocket.c base/htime.c +ifneq ($(findstring linux,$(shell $(CC) -dumpmachine)),) + $(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Issl -Ievent -o bin/read_registration_test unittest/read_registration_test.c -Llib -lhv -pthread +endif $(CC) -g -Wall -O0 -std=c99 -I. -Iutil -o bin/base64 unittest/base64_test.c util/base64.c $(CC) -g -Wall -O0 -std=c99 -I. -Iutil -o bin/md5 unittest/md5_test.c util/md5.c $(CC) -g -Wall -O0 -std=c99 -I. -Iutil -o bin/sha1 unittest/sha1_test.c util/sha1.c diff --git a/event/hevent.c b/event/hevent.c index 49cb525c0..bcea0ff88 100644 --- a/event/hevent.c +++ b/event/hevent.c @@ -767,8 +767,9 @@ size_t hio_write_bufsize(hio_t* io) { } int hio_read_once (hio_t* io) { + if (hio_watch_read(io) != 0) return -1; io->read_flags |= HIO_READ_ONCE; - return hio_read_start(io); + return 0; } int hio_read_until_length(hio_t* io, unsigned int len) { @@ -782,7 +783,8 @@ int hio_read_until_length(hio_t* io, unsigned int len) { hio_read_cb(io, buf, len); return len; } - io->read_flags = HIO_READ_UNTIL_LENGTH; + if (hio_watch_read(io) != 0) return -1; + io->read_flags = HIO_READ_ONCE | HIO_READ_UNTIL_LENGTH; io->read_until_length = len; if (io->readbuf.head > 1024 || io->readbuf.tail - io->readbuf.head < 1024) { hio_memmove_readbuf(io); @@ -793,7 +795,7 @@ int hio_read_until_length(hio_t* io, unsigned int len) { io->readbuf.len < need_len) { hio_alloc_readbuf(io, need_len); } - return hio_read_once(io); + return 0; } int hio_read_until_delim(hio_t* io, unsigned char delim) { @@ -814,14 +816,15 @@ int hio_read_until_delim(hio_t* io, unsigned char delim) { ++p; } } - io->read_flags = HIO_READ_UNTIL_DELIM; + if (hio_watch_read(io) != 0) return -1; + io->read_flags = HIO_READ_ONCE | HIO_READ_UNTIL_DELIM; io->read_until_length = delim; // NOTE: prepare readbuf if (hio_is_loop_readbuf(io) || io->readbuf.len < HLOOP_READ_BUFSIZE) { hio_alloc_readbuf(io, HLOOP_READ_BUFSIZE); } - return hio_read_once(io); + return 0; } int hio_read_remain(hio_t* io) { diff --git a/event/hevent.h b/event/hevent.h index 92c69070e..4b13a1642 100644 --- a/event/hevent.h +++ b/event/hevent.h @@ -231,6 +231,7 @@ struct hio_s { * * hloop_stop => hloop_free => hio_free => HV_FREE(io) */ +int hio_watch_read(hio_t* io); void hio_init(hio_t* io); void hio_ready(hio_t* io); void hio_done(hio_t* io); diff --git a/event/hloop.c b/event/hloop.c index b12802435..8558b2614 100644 --- a/event/hloop.c +++ b/event/hloop.c @@ -233,6 +233,8 @@ static void eventfd_read_cb(hio_t* io, void* buf, int readbytes) { hmutex_unlock(&loop->custom_events_mutex); } +static void hloop_destroy_eventfds(hloop_t* loop); + static int hloop_create_eventfds(hloop_t* loop) { #if defined(OS_UNIX) && HAVE_EVENTFD int efd = eventfd(0, 0); @@ -252,7 +254,14 @@ static int hloop_create_eventfds(hloop_t* loop) { return -1; } #endif - hio_t* io = hread(loop, loop->eventfds[EVENTFDS_READ_INDEX], NULL, 0, eventfd_read_cb); + hio_t* io = hio_get(loop, loop->eventfds[EVENTFDS_READ_INDEX]); + if (hio_watch_read(io) != 0) { + hio_detach(io); + hio_free(io); + hloop_destroy_eventfds(loop); + return -1; + } + io->read_cb = eventfd_read_cb; io->priority = HEVENT_HIGH_PRIORITY; ++loop->intern_nevents; return 0; @@ -266,8 +275,7 @@ static void hloop_destroy_eventfds(hloop_t* loop) { SAFE_CLOSE(loop->eventfds[0]); SAFE_CLOSE(loop->eventfds[1]); #else - // NOTE: Avoid duplication closesocket in hio_cleanup - // SAFE_CLOSESOCKET(loop->eventfds[EVENTFDS_READ_INDEX]); + // The read hio closes the socketpair reader. SAFE_CLOSESOCKET(loop->eventfds[EVENTFDS_WRITE_INDEX]); #endif loop->eventfds[0] = loop->eventfds[1] = -1; @@ -476,12 +484,14 @@ int hloop_run(hloop_t* loop) { loop->tid = hv_gettid(); hlogd("hloop_run tid=%ld", loop->tid); + int ret = 0; if (loop->intern_nevents == 0) { hmutex_lock(&loop->custom_events_mutex); if (loop->eventfds[EVENTFDS_WRITE_INDEX] == -1) { - hloop_create_eventfds(loop); + ret = hloop_create_eventfds(loop); } hmutex_unlock(&loop->custom_events_mutex); + if (ret != 0) goto done; #ifdef DEBUG htimer_add(loop, hloop_stat_timer_cb, HLOOP_STAT_TIMEOUT, INFINITE); @@ -506,13 +516,14 @@ int hloop_run(hloop_t* loop) { } } +done: loop->status = HLOOP_STATUS_STOP; loop->end_hrtime = gethrtime_us(); if (loop->flags & HLOOP_FLAG_AUTO_FREE) { hloop_free(&loop); } - return 0; + return ret; } int hloop_wakeup(hloop_t* loop) { @@ -886,10 +897,6 @@ int hio_add(hio_t* io, hio_cb cb, int events) { hio_ready(io); } - if (cb) { - io->cb = (hevent_cb)cb; - } - int add_events = events & ~io->events; if (add_events && iowatcher_add_event(loop, io->fd, add_events) != 0) { hlogd("iowatcher_add_event failed fd=%d add_events=%d io->events=%d err=%d", @@ -897,6 +904,9 @@ int hio_add(hio_t* io, hio_cb cb, int events) { return -1; } io->events |= add_events; + if (cb) { + io->cb = (hevent_cb)cb; + } if (!io->active) { // A NULL callback only changes the watched mask. Keep the transport @@ -953,9 +963,8 @@ int hio_close_async(hio_t* io) { } //------------------high-level apis------------------------------------------- -hio_t* hread(hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb) { - hio_t* io = hio_get(loop, fd); - assert(io != NULL); +static int hio_start_read(hio_t* io, void* buf, size_t len, hread_cb read_cb) { + if (hio_watch_read(io) != 0) return -1; if (buf && len) { io->readbuf.base = (char*)buf; io->readbuf.len = len; @@ -963,8 +972,20 @@ hio_t* hread(hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb) { if (read_cb) { io->read_cb = read_cb; } - hio_read(io); - return io; + if (io->readbuf.tail > io->readbuf.head && io->unpack_setting == NULL && io->read_flags == 0) { + hio_read_remain(io); + } + return 0; +} + +int hio_read(hio_t* io) { + return hio_start_read(io, NULL, 0, NULL); +} + +hio_t* hread(hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb) { + hio_t* io = hio_get(loop, fd); + assert(io != NULL); + return hio_start_read(io, buf, len, read_cb) == 0 ? io : NULL; } hio_t* hwrite(hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb) { diff --git a/event/hloop.h b/event/hloop.h index b074d8ab5..f0c302366 100644 --- a/event/hloop.h +++ b/event/hloop.h @@ -433,6 +433,7 @@ HV_EXPORT int hio_accept (hio_t* io); // and TLS handshakes have completed. It is not merely TCP connect completion. HV_EXPORT int hio_connect(hio_t* io); +// Returns -1 if the read watch could not be installed. Buffered input is preserved on failure. // hio_add(io, HV_READ) => read => hread_cb HV_EXPORT int hio_read (hio_t* io); #define hio_read_start(io) hio_read(io) @@ -467,6 +468,7 @@ HV_EXPORT int hio_close_async(hio_t* io); //------------------high-level apis------------------------------------------- // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read +// Returns NULL on registration failure. The buffer and callback are installed only on success. HV_EXPORT hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb); // hio_get -> hio_setcb_write -> hio_write HV_EXPORT hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL)); diff --git a/event/nio.c b/event/nio.c index 274834f74..b37f3eec3 100644 --- a/event/nio.c +++ b/event/nio.c @@ -469,18 +469,12 @@ int hio_connect(hio_t* io) { return hio_add(io, hio_handle_events, HV_WRITE); } -int hio_read (hio_t* io) { +int hio_watch_read(hio_t* io) { if (io->closed) { hloge("hio_read called but fd[%d] already closed!", io->fd); return -1; } - hio_add(io, hio_handle_events, HV_READ); - if (io->readbuf.tail > io->readbuf.head && - io->unpack_setting == NULL && - io->read_flags == 0) { - hio_read_remain(io); - } - return 0; + return hio_add(io, hio_handle_events, HV_READ); } static int hio_write4 (hio_t* io, const void* buf, size_t len, struct sockaddr* addr) { diff --git a/scripts/unittest.sh b/scripts/unittest.sh index ca1046e8d..eb93f9e64 100755 --- a/scripts/unittest.sh +++ b/scripts/unittest.sh @@ -8,6 +8,9 @@ cd ${ROOT_DIR} bin/rbtree_test bin/hbase_test +if [ -x bin/read_registration_test ]; then + bin/read_registration_test || exit $? +fi bin/date bin/ifconfig bin/mkdir_p 123/456 diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 6e17408b3..0fad1fe83 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -31,6 +31,13 @@ target_include_directories(connect_test PRIVATE .. ../base) add_executable(socketpair_test socketpair_test.c ../base/hsocket.c ../base/htime.c) target_include_directories(socketpair_test PRIVATE .. ../base) +if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT WITH_IO_URING) +add_executable(read_registration_test read_registration_test.c) +target_include_directories(read_registration_test PRIVATE .. ../base ../ssl ../event) +target_link_libraries(read_registration_test ${HV_LIBRARIES}) +set(READ_REGISTRATION_UNITTEST_TARGET read_registration_test) +endif() + # ------util------ add_executable(base64 base64_test.c ../util/base64.c) target_include_directories(base64 PRIVATE .. ../util) @@ -235,6 +242,7 @@ add_custom_target(unittest DEPENDS ping ftp sendmail + ${READ_REGISTRATION_UNITTEST_TARGET} http_router_test ${HTTP_UNITTEST_TARGETS} ${HTTP_LUA_UNITTEST_TARGETS} diff --git a/unittest/read_registration_test.c b/unittest/read_registration_test.c new file mode 100644 index 000000000..613f51fc1 --- /dev/null +++ b/unittest/read_registration_test.c @@ -0,0 +1,98 @@ +/* Linux epoll rejects regular files. For an already active socket, inject + * a single failed MOD and let the kernel deliver its original read event. */ +#define _GNU_SOURCE +#include "hloop.h" +#include "hbuf.h" +#include +#include +#include +#include + +static int reject_mod_fd = -1; +static int reject_next_add; +static int ready_calls; +static int buffered_reads; + +int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { + if ((op == EPOLL_CTL_MOD && fd == reject_mod_fd) || (op == EPOLL_CTL_ADD && reject_next_add)) { + reject_mod_fd = -1; + reject_next_add = 0; + errno = ENOSPC; + return -1; + } + return (int)syscall(SYS_epoll_ctl, epfd, op, fd, event); +} + +static void require(int ok, const char *message) { + if (!ok) { + fprintf(stderr, "read_registration_test: %s\n", message); + exit(EXIT_FAILURE); + } +} + +static void unexpected_read(hio_t *io, void *buf, int len) { + (void)io; + (void)buf; + (void)len; + require(0, "rejected read dispatched a callback"); +} + +static void buffered_read(hio_t *io, void *buf, int len) { + (void)io; + require(len == 1 && *(char *)buf == 'b', "buffered read delivered the wrong bytes"); + ++buffered_reads; +} + +static void original_ready(hio_t *io) { + char byte; + require(read(hio_fd(io), &byte, 1) == 1 && byte == 'r', "could not receive the original read event"); + ++ready_calls; +} + +static void unexpected_ready(hio_t *io) { + (void)io; + require(0, "rejected upgrade replaced the active callback"); +} + +int main(void) { + if (strcmp(hio_engine(), "epoll") != 0) return 0; + hloop_t *loop = hloop_new(HLOOP_FLAG_RUN_ONCE); + FILE *file = tmpfile(); + require(file != NULL, "could not create a regular file"); + hio_t *file_io = hio_get(loop, fileno(file)); + require(hio_read(file_io) < 0 && hio_events(file_io) == 0, "regular file read reported success"); + char readbuf[32] = "b"; + hio_set_readbuf(file_io, readbuf, sizeof(readbuf)); + hio_setcb_read(file_io, buffered_read); + hio_get_readbuf(file_io)->tail = 1; + require(hio_read(file_io) < 0 && buffered_reads == 0, "failed read consumed buffered input"); + require(hio_read_once(file_io) < 0 && hio_read_until_length(file_io, 2) < 0 && hio_read_until_delim(file_io, '\n') < 0, + "failed framed read reported success"); + char replacement[32]; + require(hread(loop, fileno(file), replacement, sizeof(replacement), unexpected_read) == NULL, "hread reported success"); + require(hio_get_readbuf(file_io)->base == readbuf, "failed hread retained the supplied buffer"); + + require(hio_read_until_length(file_io, 1) == 1 && buffered_reads == 1, "failed hread lost buffered input or its callback"); + + int pair[2]; + require(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0, "could not open socket pair"); + hio_t *io = hio_get(loop, pair[0]); + hio_set_readbuf(io, readbuf, sizeof(readbuf)); + hio_get_readbuf(io)->tail = 1; + require(hread(loop, pair[0], NULL, 0, buffered_read) == io && buffered_reads == 2, "hread did not immediately deliver buffered input"); + require(hio_add(io, original_ready, HV_READ) == 0, "could not register original reader"); + reject_mod_fd = pair[0]; + require(hio_add(io, unexpected_ready, HV_RDWR) < 0 && hio_events(io) == HV_READ, "failed upgrade changed interests"); + require(write(pair[1], "r", 1) == 1 && hloop_run(loop) == 0 && ready_calls == 1, "read interest was lost"); + + hloop_free(&loop); + fclose(file); + close(pair[1]); + + loop = hloop_new(HLOOP_FLAG_RUN_ONCE | HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS); + reject_next_add = 1; + require(hloop_run(loop) < 0, "loop started without a working wake reader"); + require(hloop_run(loop) == 0, "could not retry loop startup"); + hloop_free(&loop); + return 0; +} From 8e42280c7307c1779041493955d5047e3256dd61 Mon Sep 17 00:00:00 2001 From: radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:07:22 +0000 Subject: [PATCH 2/2] Clarify when hio_read reports a registration failure --- event/hloop.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/event/hloop.h b/event/hloop.h index f0c302366..ef998bbfe 100644 --- a/event/hloop.h +++ b/event/hloop.h @@ -433,7 +433,7 @@ HV_EXPORT int hio_accept (hio_t* io); // and TLS handshakes have completed. It is not merely TCP connect completion. HV_EXPORT int hio_connect(hio_t* io); -// Returns -1 if the read watch could not be installed. Buffered input is preserved on failure. +// Returns -1 if the backend reports a read-watch registration failure. Buffered input is preserved on failure. // hio_add(io, HV_READ) => read => hread_cb HV_EXPORT int hio_read (hio_t* io); #define hio_read_start(io) hio_read(io)