From a9b8f8e8a9d75fee10a3d036854d283a9d10600e Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 15 Apr 2024 18:12:05 -0500 Subject: [PATCH] Address #1391 - set fd to negative value if not used. See https://groups.google.com/g/comp.unix.programmer/c/bNNadBIEpTo/m/G5gs1mqNhbIJ?pli=1 for a conversation and workaround. --- src/core/ev.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/core/ev.c b/src/core/ev.c index 1473b82ae..2c79306e9 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -1823,10 +1823,17 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) { /* set event flags */ for (size_t i = 0; i < janet_vm.stream_count; i++) { JanetStream *stream = janet_vm.streams[i]; - janet_vm.fds[i + 1].events = 0; - janet_vm.fds[i + 1].revents = 0; - if (stream->read_fiber && stream->read_fiber->ev_callback) janet_vm.fds[i + 1].events |= POLLIN; - if (stream->write_fiber && stream->write_fiber->ev_callback) janet_vm.fds[i + 1].events |= POLLOUT; + struct pollfd *pfd = janet_vm.fds + i + 1; + pfd->events = 0; + pfd->revents = 0; + JanetFiber *rf = stream->read_fiber; + JanetFiber *wf = stream->write_fiber; + if (rf && rf->ev_callback) pfd->events |= POLLIN; + if (wf && wf->ev_callback) pfd->events |= POLLOUT; + /* Hack to ignore a file descriptor - make file descriptor negative if we want to ignore */ + if (!pfd->events) { + pfd->fd = -pfd->fd; + } } /* Poll for events */ @@ -1843,6 +1850,14 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) { JANET_EXIT("failed to poll events"); } + /* Undo negative hack */ + for (size_t i = 0; i < janet_vm.stream_count; i++) { + struct pollfd *pfd = janet_vm.fds + i + 1; + if (!pfd->events) { + pfd->fd = -pfd->fd; + } + } + /* Check selfpipe */ if (janet_vm.fds[0].revents & POLLIN) { janet_vm.fds[0].revents = 0;