From fff370c2d89acfed702b7c615650706c3c1fadd2 Mon Sep 17 00:00:00 2001 From: Jens Lorenz Date: Thu, 29 Mar 2018 16:11:34 +0200 Subject: AMUtil: Fix startup sequence addFdPoll and removeFdPoll In case plugins add new fdpoll elements and directly removing them afterwards the state inside the structure will be changed to REMOVE. REMOVE will lead all the time to an erase which will not work in case the pointer is at .end() of vector or the element itself is not the correct one. Both cases will be fixed just by checking if the fd is the same. Signed-off-by: Jens Lorenz --- AudioManagerUtilities/src/CAmSocketHandler.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'AudioManagerUtilities/src/CAmSocketHandler.cpp') diff --git a/AudioManagerUtilities/src/CAmSocketHandler.cpp b/AudioManagerUtilities/src/CAmSocketHandler.cpp index 1e7db6a..e5752b4 100644 --- a/AudioManagerUtilities/src/CAmSocketHandler.cpp +++ b/AudioManagerUtilities/src/CAmSocketHandler.cpp @@ -40,9 +40,9 @@ #include #endif +#define END_EVENT (UINT64_MAX >> 1) #define SHPOLL_IS_ACTIVE(state) (!(state == poll_states_e::REMOVE || state == poll_states_e::CLOSE)) - namespace am { @@ -72,7 +72,7 @@ CAmSocketHandler::CAmSocketHandler() : ssize_t bytes = read(pollfd.fd, &events, sizeof(events)); if (bytes == sizeof(events)) { - if (events == UINT64_MAX-1) + if (events >= END_EVENT) { for (auto & elem : mMapShPoll) { @@ -80,6 +80,7 @@ CAmSocketHandler::CAmSocketHandler() : elem.second.state = poll_states_e::UNINIT; } mDispatchDone = true; + } return; } @@ -145,7 +146,14 @@ void CAmSocketHandler::start_listenting() // fallthrough case poll_states_e::REMOVE: - fdPollIt = fdPollingArray.erase(fdPollIt); + /* The first check is needed for the restart behavior were an element is marked + * as removed but not part of the polling array - which is stored on heap. + * The second check is needed in case a map element was inserted newly but was + * directly marked after as REMOVE but in between there was no sync. of the + * polling array processed. + */ + if (fdPollIt != fdPollingArray.end() && fdPollIt->fd == elem.pollfdValue.fd) + fdPollIt = fdPollingArray.erase(fdPollIt); it = mMapShPoll.erase(it); continue; @@ -246,7 +254,7 @@ void CAmSocketHandler::exit_mainloop() stop_listening(); //fire the ending filedescriptor - const uint64_t events = UINT64_MAX-1; + const uint64_t events = END_EVENT; if (write(mEventFd, &events, sizeof(events)) < 0) { //Failed to write to event fd... -- cgit v1.2.1