From fbe2c15377b22e720a92b4da6d9f9fe72f11c4eb Mon Sep 17 00:00:00 2001 From: omrzk <193513049+omrzk@users.noreply.github.com> Date: Sat, 14 Nov 2026 09:21:26 -0600 Subject: [PATCH] Honor new descriptor and events in uev_io_set() uev_io_set() on an active oneshot watcher keeps watching the old descriptor, for the old events: uev_io_set(w, newfd, UEV_READ | UEV_ONESHOT); It only rearms the watcher, with what is already stored in it. Rearm only when neither has changed, otherwise stop the watcher and set it up again. Closes #35 --- src/io.c | 3 ++- test/.gitignore | 1 + test/Makefile.am | 1 + test/io.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/io.c diff --git a/src/io.c b/src/io.c index 575b12a..59be74d 100644 --- a/src/io.c +++ b/src/io.c @@ -64,7 +64,8 @@ int uev_io_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int e */ int uev_io_set(uev_t *w, int fd, int events) { - if ((events & UEV_ONESHOT) && _uev_watcher_active(w)) + if ((events & UEV_ONESHOT) && _uev_watcher_active(w) && + fd == w->fd && events == w->events) return _uev_watcher_rearm(w); /* Ignore any errors, only to clean up anything lingering ... */ diff --git a/test/.gitignore b/test/.gitignore index 86dfc70..a3e8823 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -4,6 +4,7 @@ active api complete cronrun +io signal timer event diff --git a/test/Makefile.am b/test/Makefile.am index 902d797..8db6210 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -9,6 +9,7 @@ TESTS += active TESTS += api TESTS += complete TESTS += cronrun +TESTS += io TESTS += signal TESTS += timer TESTS += event diff --git a/test/io.c b/test/io.c new file mode 100644 index 0000000..83b6651 --- /dev/null +++ b/test/io.c @@ -0,0 +1,60 @@ +/* Verifies uev_io_set() on an active oneshot watcher */ +#include "check.h" +#include + +static int fd = -1; +static int ev; + +static void cb(uev_t *w, void *arg, int events) +{ + fd = w->fd; + ev = events; +} + +static void run(uev_ctx_t *ctx) +{ + fd = -1; + ev = 0; + fail_unless(uev_run(ctx, UEV_ONCE | UEV_NONBLOCK) == 0); +} + +int main(void) +{ + int a[2], b[2]; + uev_ctx_t ctx; + uev_t w; + + fail_unless(socketpair(AF_UNIX, SOCK_STREAM, 0, a) == 0); + fail_unless(socketpair(AF_UNIX, SOCK_STREAM, 0, b) == 0); + fail_unless(write(b[1], "b", 1) == 1); + + fail_unless(uev_init(&ctx) == 0); + fail_unless(uev_io_init(&ctx, &w, cb, NULL, a[0], UEV_READ | UEV_ONESHOT) == 0); + + /* New descriptor */ + fail_unless(uev_io_set(&w, b[0], UEV_READ | UEV_ONESHOT) == 0); + run(&ctx); + fail_unless(fd == b[0]); + + /* New events, same descriptor */ + fail_unless(uev_io_set(&w, a[0], UEV_WRITE | UEV_ONESHOT) == 0); + run(&ctx); + fail_unless(fd == a[0]); + fail_unless(ev & UEV_WRITE); + + /* Nothing new, only rearm */ + run(&ctx); + fail_unless(fd == -1); + fail_unless(uev_io_set(&w, a[0], UEV_WRITE | UEV_ONESHOT) == 0); + run(&ctx); + fail_unless(fd == a[0]); + + return uev_exit(&ctx); +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */