From a47ef8681074c8e8dbe1ddcfa76b88bd0e4585f5 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 27 Jul 2026 19:42:35 +0300 Subject: [PATCH] =?UTF-8?q?uasync:=20fix=20stall=20detection=20=E2=80=94?= =?UTF-8?q?=20record=20timestamp=20after=20poll=20wait,=20not=20before?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/u_async.c | 14 +++++++++----- lib/u_async.h | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/u_async.c b/lib/u_async.c index 88661d5d..ca03ca90 100644 --- a/lib/u_async.c +++ b/lib/u_async.c @@ -985,11 +985,10 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) { if (!ua->sockets || !ua->timeout_heap) return; uint64_t now_us_poll_entry = get_time_us(); - if (ua->last_poll_entry_us && now_us_poll_entry - ua->last_poll_entry_us > 10000) { - DEBUG_WARN(DEBUG_CATEGORY_UASYNC, "Event loop stall: %lluus since last poll entry", - (unsigned long long)(now_us_poll_entry - ua->last_poll_entry_us)); + if (ua->last_poll_exit_us && now_us_poll_entry - ua->last_poll_exit_us > 10000) { + DEBUG_WARN(DEBUG_CATEGORY_UASYNC, "Event loop stall: %lluus since end of last poll processing", + (unsigned long long)(now_us_poll_entry - ua->last_poll_exit_us)); } - ua->last_poll_entry_us = now_us_poll_entry; DEBUG_DEBUG(DEBUG_CATEGORY_UASYNC, "poll(%d sockets, %zu timers, timeout=%d.%dms)", ua->sockets->count, ua->timeout_heap->size, timeout_tb >= 0 ? timeout_tb / 10000 : -1, @@ -1040,6 +1039,7 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) { timeout_ms = (poll_timeout.tv_sec * 1000) + (poll_timeout.tv_usec / 1000); } else { // Nothing to do - return immediately + ua->last_poll_exit_us = get_time_us(); return; } } @@ -1051,6 +1051,7 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) { int max_events = 64; int ret = epoll_wait(ua->epoll_fd, events, max_events, timeout_ms); + ua->last_poll_exit_us = get_time_us(); if (ret < 0) { if (errno == EINTR) { return; @@ -1086,6 +1087,7 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) { nanosleep(&ts, NULL); #endif } + ua->last_poll_exit_us = get_time_us(); process_timeouts(ua); return; } @@ -1129,6 +1131,7 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) { tv.tv_usec = (timeout_ms % 1000) * 1000; int ret = select((int)max_fd + 1, &read_fds, &write_fds, &except_fds, &tv); + ua->last_poll_exit_us = get_time_us(); if (ret < 0) { int err = WSAGetLastError(); @@ -1203,6 +1206,7 @@ void uasync_poll(struct UASYNC* ua, int timeout_tb) { } int ret = poll(ua->poll_fds, ua->poll_fds_count, timeout_ms); + ua->last_poll_exit_us = get_time_us(); if (ret < 0) { if (errno == EINTR) { return; @@ -1327,7 +1331,7 @@ struct UASYNC* uasync_create(void) { ua->posted_tasks_head = NULL; ua->immediate_queue_head = NULL; ua->immediate_queue_tail = NULL; - ua->last_poll_entry_us = 0; + ua->last_poll_exit_us = 0; DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "Creating SA..."); ua->sockets = socket_array_create(16); diff --git a/lib/u_async.h b/lib/u_async.h index 8077bf4e..55155884 100644 --- a/lib/u_async.h +++ b/lib/u_async.h @@ -75,7 +75,7 @@ struct UASYNC { pthread_mutex_t posted_lock; #endif volatile sig_atomic_t stop; - uint64_t last_poll_entry_us; + uint64_t last_poll_exit_us; }; // Type definitions