From 8465f8181eda45e3d6cc5d6c3d08ca36db04763b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 5 Jul 2023 15:30:57 +0200 Subject: tcp_server: keep track of client threads This is a major change, obviously; brought to me by Valgrind, which noticed that we don't actually clean up after cimple-client threads. For a more thorough explanation, please see the added comment in tcp_server.c. --- src/event_loop.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/event_loop.c') diff --git a/src/event_loop.c b/src/event_loop.c index 93b67f2..75a323c 100644 --- a/src/event_loop.c +++ b/src/event_loop.c @@ -7,7 +7,6 @@ #include "event_loop.h" #include "log.h" -#include "net.h" #include "string.h" #include @@ -126,7 +125,6 @@ static void event_loop_remove(struct event_loop *loop, struct event_fd *entry) log_debug("Removing descriptor %d from event loop\n", entry->fd); SIMPLEQ_REMOVE(&loop->entries, entry, event_fd, entries); - net_close(entry->fd); event_fd_destroy(entry); --loop->nfds; } @@ -191,11 +189,15 @@ static struct pollfd *make_pollfds(const struct event_loop *loop) int event_loop_run(struct event_loop *loop) { + /* Cache the number of event descriptors so that event handlers can + * append new ones. */ + const nfds_t nfds = loop->nfds; + struct pollfd *fds = make_pollfds(loop); if (!fds) return -1; - int ret = poll(fds, loop->nfds, -1); + int ret = poll(fds, nfds, -1); if (ret < 0) { log_errno("poll"); return ret; @@ -203,7 +205,7 @@ int event_loop_run(struct event_loop *loop) ret = 0; struct event_fd *entry = SIMPLEQ_FIRST(&loop->entries); - for (nfds_t i = 0; i < loop->nfds; ++i) { + for (nfds_t i = 0; i < nfds; ++i) { struct event_fd *next = SIMPLEQ_NEXT(entry, entries); if (!fds[i].revents) -- cgit v1.2.3