Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThreadedCommServer race condition causing segfault #66

Open
GingerPrince opened this issue Jun 11, 2020 · 2 comments
Open

ThreadedCommServer race condition causing segfault #66

GingerPrince opened this issue Jun 11, 2020 · 2 comments

Comments

@GingerPrince
Copy link

We’ve found a bug that’s can cause a segfault in MOOSDB, I've attached a patch for a fix as .txt files as that's what's supported for upload. There are possible performance implications with our patch, as we're now mutex locking ProcessClient which may have been concurrent before, and now is more sequential. This can only be avoided with C+17 shared_lock or some complicated back-port of that to C++11 ( our compiler standard).

The bug ultimately caused a segfault in MOOSDB at ThreadedCommServer.cpp#388:


bool ThreadedCommServer::ProcessClient(ClientThreadSharedData &SDFromClient,MOOS::ServerAudit & Auditor)
...
for(q=m_ClientThreads.begin();q!=m_ClientThreads.end();++q)
{
ClientThread* pClient = q->second;
if(m_pfnFetchAllMailCallBack!=NULL && pClient->IsAsynchronous())

because pClient is null/uninitialised. This is caused by a race condition with:


bool ThreadedCommServer::AddAndStartClientThread(XPCTcpSocket & NewClientSocket,const std::string & sName)
...
m_ClientThreads[sName] = pNewClientThread;

Adding to m_ClientThreads needs to be mutually exclusive with any other operation on the map and needs guarding with a mutex (only with the Add though, multiple Reads can happen at once).

ThreadedCommServer.cpp.txt
ThreadedCommServer.h.txt

@GingerPrince
Copy link
Author

I should add, I don’t think the performance impact will be big, if at all. I’m not even sure how often ProcessClient is called simultaneously and even if it is, we’ve been careful not to hold the mutex longer than needed. Just thought I should point out the fact there is now a mutex in there that could have an impact.

@russkel
Copy link
Contributor

russkel commented Apr 16, 2022

If one were to compile MOOS with ThreadSanitizer (https://clang.llvm.org/docs/ThreadSanitizer.html) from clang enabled it might bring out other issues like this.

Also would help if there was a proper test suite that this could be run on.

russkel added a commit to russkel/core-moos that referenced this issue Apr 16, 2022
russkel added a commit to russkel/core-moos that referenced this issue May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants