Commit b8766610 authored by Simon Morlat's avatar Simon Morlat

Fix crash in MediaRelay (rare race conditions with mSessions.size() iterating...

Fix crash in MediaRelay (rare race conditions with mSessions.size() iterating over the list while a session is being added or removed).
parent d29bb052
......@@ -423,6 +423,7 @@ void RelaySession::transfer(time_t curtime, const shared_ptr<RelayChannel> &chan
MediaRelayServer::MediaRelayServer(MediaRelay *module) : mModule(module) {
mRunning = false;
mSessionsCount = 0;
if (pipe(mCtlPipe) == -1) {
LOGF("Could not create MediaRelayServer control pipe.");
}
......@@ -466,6 +467,7 @@ MediaRelayServer::~MediaRelayServer() {
pthread_join(mThread, NULL);
}
mSessions.clear();
mSessionsCount = 0;
close(mCtlPipe[0]);
close(mCtlPipe[1]);
}
......@@ -475,11 +477,12 @@ shared_ptr<RelaySession> MediaRelayServer::createSession(const std::string &fron
shared_ptr<RelaySession> s = make_shared<RelaySession>(this, frontId, frontRelayIps);
mMutex.lock();
mSessions.push_back(s);
mSessionsCount++;
mMutex.unlock();
if (!mRunning)
start();
LOGD("There are now %zu relay sessions running on MediaRelayServer [%p]", mSessions.size(), this);
LOGD("There are now %zu relay sessions running on MediaRelayServer [%p]", mSessionsCount, this);
/*write to the control pipe to wakeup the server thread */
update();
return s;
......@@ -554,7 +557,8 @@ void MediaRelayServer::run() {
for (auto it = mSessions.begin(); it != mSessions.end();) {
if (!(*it)->isUsed()) {
it = mSessions.erase(it);
LOGD("There are now %i relay sessions running.", (int)mSessions.size());
mSessionsCount--;
LOGD("There are now %i relay sessions running.", (int)mSessionsCount);
} else {
(*it)->checkPollFd(&pfd, curtime);
++it;
......
......@@ -118,6 +118,7 @@ class MediaRelayServer {
static void *threadFunc(void *arg);
Mutex mMutex;
std::list<std::shared_ptr<RelaySession>> mSessions;
size_t mSessionsCount; /* since std::list::size() is O(n), we use our own counter*/
MediaRelay *mModule;
pthread_t mThread;
int mCtlPipe[2];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment