[Cocos2d-x] a bug in CCHttpClient
The company's new game "I am a big officer" is about to have a large PR, and everything is ready. At this time, the Test Department reported a small problem. When we started the game, occasionally stuck on the startup interface, prompting: connecting to the server... then I did not respond. The probability of this problem is very low, about 3%, and it would be good to quit and re-open the game. "It should be caused by poor network performance ", we didn't pay too much attention to this bug, but the boss was not at ease. "Even if it is a network problem, it should not be stuck. If new players encounter this situation, they will be lost directly, check the problem." It seems that this is not a small problem, so this bug is assigned to me.
It took some time to reproduce, I found the problem out of CCHttpClient, familiar with the cocos2d-x of the students know that this is an asynchronous network library, it works like this: HttpClient has a working thread inside, the main thread of the game calls the send () function to push the http request into a queue, and then wakes up the working thread and returns the result. After the worker thread is awakened, it extracts the http request from the queue, calls libcurl for processing, and then returns the result to the main thread through callback. When the bug is reproduced, the callback registered by the main thread is not triggered, and the game gets stuck. I guess libcurl gets stuck in the working thread, so I added some prints to the work thread for verification, but the result was unexpected: the working thread was not triggered at all!
Another problem of multi-thread synchronization! This is the code snippet of the worker thread:
// Worker threadstatic void* networkThread(void *data){ CCHttpRequest *request = NULL; while (true) { if (need_quit) { break; } // step 1: send http request if the requestQueue isn't empty request = NULL; pthread_mutex_lock(&s_requestQueueMutex); //Get request task from queue if (0 != s_requestQueue->count()) { request = dynamic_cast
(s_requestQueue->objectAtIndex(0)); s_requestQueue->removeObjectAtIndex(0); // request's refcount = 1 here } pthread_mutex_unlock(&s_requestQueueMutex); if (NULL == request) { // Wait for http request tasks from main thread pthread_cond_wait(&s_SleepCondition, &s_SleepMutex); continue; } // step 2: libcurl sync access
When the worker thread is idle, it is suspended through pthread_cond_wait ().
Code of the send () function:
//Add a get task to queuevoid CCHttpClient::send(CCHttpRequest* request){ if (false == lazyInitThreadSemphore()) { return; } if (!request) { return; } ++s_asyncRequestCount; request->retain(); pthread_mutex_lock(&s_requestQueueMutex); s_requestQueue->addObject(request); pthread_mutex_unlock(&s_requestQueueMutex); // Notify thread start to work pthread_cond_signal(&s_SleepCondition);}
I read it carefully and found this sentence very strange:
pthread_cond_wait(&s_SleepCondition, &s_SleepMutex);
Here, a mutex s_SleepMutex is used, and this lock is not used by other code. This is probably an error: If a mutex lock is used by only one code, there is only one possibility that the Code will be executed in multiple threads. The httpClient has only one thread. After google, the mutex lock was used incorrectly. You can refer to an article on zhihu: http://www.zhihu.com/question/24116967
It explains in detail why the missing semaphores are caused by incorrect locking.
Here is the modified Code. Separate s_SleepMutex is no longer used and replaced by requestQueueMutex.
// Worker threadstatic void* networkThread(void *data){ CCHttpRequest *request = NULL; while (true) { if (need_quit) { break; } // step 1: send http request if the requestQueue isn't empty request = NULL; pthread_mutex_lock(&s_requestQueueMutex); //Get request task from queue while (0 == s_requestQueue->count()) { pthread_cond_wait(&s_SleepCondition, &s_requestQueueMutex); } request = dynamic_cast
(s_requestQueue->objectAtIndex(0)); s_requestQueue->removeObjectAtIndex(0); // request's refcount = 1 here CCLog(s_SleepCondition notified); pthread_mutex_unlock(&s_requestQueueMutex);