fix(core): Fix a busy-loop when timeouts are less than 1ms (fixes #7102)

This commit is contained in:
Julius Pfrommer 2025-03-23 20:59:25 +01:00 committed by Julius Pfrommer
parent c5f59df0a1
commit 041d6a2173

View File

@ -68,12 +68,20 @@ UA_StatusCode
UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) {
UA_assert(listenTimeout >= 0);
/* If there is a positive timeout, wait at least one millisecond, the
* minimum for blocking epoll_wait. This prevents a busy-loop, as the
* open62541 library allows even smaller timeouts, which can result in a
* zero timeout due to rounding to an integer here. */
int timeout = (int)(listenTimeout / UA_DATETIME_MSEC);
if(timeout == 0 && listenTimeout > 0)
timeout = 1;
/* Poll the registered sockets */
struct epoll_event epoll_events[64];
int epollfd = el->epollfd;
UA_UNLOCK(&el->elMutex);
int events = epoll_wait(epollfd, epoll_events, 64,
(int)(listenTimeout / UA_DATETIME_MSEC));
int events = epoll_wait(el->epollfd, epoll_events, 64, timeout);
UA_LOCK(&el->elMutex);
/* TODO: Replace with pwait2 for higher-precision timeouts once this is
* available in the standard library.
*
@ -83,7 +91,6 @@ UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) {
* };
* int events = epoll_pwait2(epollfd, epoll_events, 64,
* precisionTimeout, NULL); */
UA_LOCK(&el->elMutex);
/* Handle error conditions */
if(events == -1) {