Page 1 of 1

Possible Deadlock in C++ API?

Posted: 20 Aug 2015, 10:44
by dhuang11
We are seeing some situations (infrequent - but does happen), where SmartFox appears to be deadlocked between locks in ThreadManager, mtxDisconnect, and mutexes for reading from TCPClient. Everything stops responding but the app doesn't crash. Please see the attached 2 screenshots showing where this lock contention is taking place in SFS2X C++ code. We think we've fixed this by changing ThreadManager in this way:

Code: Select all

void ThreadManager::InThread()
{
   boost::posix_time::milliseconds workTime(5);

   while (running)
   {
        boost::this_thread::sleep(workTime);
       
        if (running == false) break;
       
        boost::shared_ptr<map<string, boost::shared_ptr<void> > > item;
       
        while (true)
        {
            inQueueLocker.lock();
            if(inThreadQueue->size() <= 0) {
                inQueueLocker.unlock();
                break;
            }
            item = inThreadQueue->front();
            inThreadQueue->pop_front();
            inQueueLocker.unlock();
           
            ProcessItem(item);
            item->clear();
        }
   }
}


Code: Select all

void ThreadManager::OutThread()
{
   boost::posix_time::milliseconds workTime(5);

   while (running)
   {
      boost::this_thread::sleep(workTime); 
            
      if (running == false) break;
            
      boost::shared_ptr<map<string, boost::shared_ptr<void> > > item;

      while (true)
      {
            outQueueLocker.lock();
            if (outThreadQueue->size() <= 0) {
                outQueueLocker.unlock();
                break;
            }
         item = outThreadQueue->front();
         outThreadQueue->pop_front();
            outQueueLocker.unlock();
           
         ProcessOutItem(item);
         item->clear();
      }
   }
}


Notice how the lock is shortened to only lock popping from the queue and not to also lock the actual processing of the input or output item.

Please take a look at this for us in more detail and provide us a more official patch for this issue. Thanks!

Screen-Shot-2015-08-19-at-4.58.13-PM.jpg
(218.7 KiB) Not downloaded yet


Screen-Shot-2015-08-19-at-4.58.13-PM.jpg
(218.7 KiB) Not downloaded yet

Re: Possible Deadlock in C++ API?

Posted: 20 Aug 2015, 13:52
by Lapo
Thanks for reporting. We'll check and get back to you.

Re: Possible Deadlock in C++ API?

Posted: 21 Aug 2015, 10:31
by MBagnati
Thanks for reporting.
You are right, API goes in deadlock state when described scenario occurs.
The fix that you suggest is good, it will be included into next API release.
In the meanwhile, apply your fix to your API version.

Thanks for your help.