Skip to content

Commit

Permalink
fix(el): Unlock EventLoop mutex before wait condition
Browse files Browse the repository at this point in the history
If UA_Server_addPubSubConnection was called in a (real-time) linux
system from a task with lower priority than the task that called
UA_Server_run, a deadlock occured.
  • Loading branch information
keba-estr authored and jpfr committed Oct 7, 2023
1 parent 9ea4629 commit 25af928
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions arch/eventloop_posix_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) {

/* Poll the registered sockets */
struct epoll_event epoll_events[64];
int events = epoll_wait(el->epollfd, epoll_events, 64,
int epollfd = el->epollfd;
UA_UNLOCK(&el->elMutex);
int events = epoll_wait(epollfd, epoll_events, 64,
(int)(listenTimeout / UA_DATETIME_MSEC));
/* TODO: Replace with pwait2 for higher-precision timeouts once this is
* available in the standard library.
Expand All @@ -79,8 +81,9 @@ UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) {
* (long)(listenTimeout / UA_DATETIME_SEC),
* (long)((listenTimeout % UA_DATETIME_SEC) * 100)
* };
* int events = epoll_pwait2(el->epollfd, epoll_events, 64,
* int events = epoll_pwait2(epollfd, epoll_events, 64,
* precisionTimeout, NULL); */
UA_LOCK(&el->elMutex);

/* Handle error conditions */
if(events == -1) {
Expand Down
2 changes: 2 additions & 0 deletions arch/eventloop_posix_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ UA_EventLoopPOSIX_pollFDs(UA_EventLoopPOSIX *el, UA_DateTime listenTimeout) {
#endif
};

UA_UNLOCK(&el->elMutex);
int selectStatus = UA_select(highestfd+1, &readset, &writeset, &errset, &tmptv);
UA_LOCK(&el->elMutex);
if(selectStatus < 0) {
/* We will retry, only log the error */
UA_LOG_SOCKET_ERRNO_WRAP(
Expand Down

0 comments on commit 25af928

Please sign in to comment.