Possible Deadlock in C++ API?

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

Post Reply
dhuang11
Posts: 38
Joined: 02 May 2014, 08:21

Possible Deadlock in C++ API?

Post 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
Attachments
Screen-Shot-2015-08-19-at-4.59.04-PM.jpg
(114.61 KiB) Not downloaded yet
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Possible Deadlock in C++ API?

Post by Lapo »

Thanks for reporting. We'll check and get back to you.
Lapo
--
gotoAndPlay()
...addicted to flash games
MBagnati
Posts: 126
Joined: 12 Feb 2013, 10:57

Re: Possible Deadlock in C++ API?

Post 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.
Post Reply