From bcf33db9326e6a35f123df8295c7ce1528dd72f3 Mon Sep 17 00:00:00 2001 From: samanway Date: Tue, 28 Jan 2020 22:08:04 +0530 Subject: [PATCH] [CONPRO-1555] oneconnect:Core / Application crashed - In function NSConsumerMsgPushThreadFunc for locking 'msgHandleThread', there could be a possibility of destruction of the thread running NSConsumerMsgHandleThreadFunc function and destruction of the mutex as well at the time of locking it - Hence handled this critical mutex with a global lock https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/657 (cherry-picked from 5289e48a0aa9ce007d44cd443dd73cc8f509fcee) Change-Id: I1892df4276b6ffcb969a91fd23c9f3d9f3c5a959 Signed-off-by: samanway-dey Signed-off-by: DoHyun Pyun --- .../notification/src/consumer/NSConsumerScheduler.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/service/notification/src/consumer/NSConsumerScheduler.c b/service/notification/src/consumer/NSConsumerScheduler.c index 9dd920f..14adef5 100644 --- a/service/notification/src/consumer/NSConsumerScheduler.c +++ b/service/notification/src/consumer/NSConsumerScheduler.c @@ -45,6 +45,8 @@ #include "NSConsumerMQPlugin.h" #endif +pthread_mutex_t NSConsumerQueueMutex; + void * NSConsumerMsgHandleThreadFunc(void * handle); void * NSConsumerMsgPushThreadFunc(void * data); @@ -92,6 +94,9 @@ NSResult NSConsumerMessageHandlerInit() ret = NSConsumerSystemInit(); NS_VERIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR); + NS_LOG(DEBUG, "mutex init"); + pthread_mutex_init(&NSConsumerQueueMutex, NULL); + NS_LOG(DEBUG, "create queue"); queue = NSCreateQueue(); NS_VERIFY_NOT_NULL(queue, NS_ERROR); @@ -118,10 +123,10 @@ NSResult NSConsumerPushEvent(NSTask * task) void NSConsumerMessageHandlerExit() { - NSConsumerListenerTermiate(); NSCancelAllSubscription(); + pthread_mutex_lock(&NSConsumerQueueMutex); NSConsumerThread * thread = *(NSGetMsgHandleThreadHandle()); NSThreadStop(thread); NSSetMsgHandleThreadHandle(NULL); @@ -129,6 +134,8 @@ void NSConsumerMessageHandlerExit() NSConsumerQueue * queue = *(NSGetMsgHandleQueue()); NSDestroyQueue(queue); NSSetMsgHandleQueue(NULL); + pthread_mutex_unlock(&NSConsumerQueueMutex); + pthread_mutex_destroy(&NSConsumerQueueMutex); NSDestroyInternalCachedList(); } @@ -139,7 +146,9 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle) NSConsumerQueueObject * obj = NULL; NS_LOG(DEBUG, "create thread for consumer message handle"); + pthread_mutex_lock(&NSConsumerQueueMutex); NSConsumerThread * queueHandleThread = (NSConsumerThread *) threadHandle; + pthread_mutex_unlock(&NSConsumerQueueMutex); NS_VERIFY_NOT_NULL(queueHandleThread, NULL); while (true) @@ -151,18 +160,20 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle) continue; } + pthread_mutex_lock(&NSConsumerQueueMutex); if (!queueHandleThread->isStarted && NSIsQueueEmpty(queue)) { + pthread_mutex_unlock(&NSConsumerQueueMutex); NS_LOG(ERROR, "msg handler thread will be terminated"); break; } if (NSIsQueueEmpty(queue)) { + pthread_mutex_unlock(&NSConsumerQueueMutex); usleep(2000); continue; } - NSThreadLock(queueHandleThread); NS_LOG(DEBUG, "msg handler working"); obj = NSPopQueue(queue); @@ -174,7 +185,7 @@ void * NSConsumerMsgHandleThreadFunc(void * threadHandle) } NSThreadUnlock(queueHandleThread); - + pthread_mutex_unlock(&NSConsumerQueueMutex); } return NULL; @@ -186,6 +197,7 @@ void * NSConsumerMsgPushThreadFunc(void * data) NSConsumerQueue * queue = NULL; NS_LOG(DEBUG, "get queueThread handle"); + pthread_mutex_lock(&NSConsumerQueueMutex); NSConsumerThread * msgHandleThread = *(NSGetMsgHandleThreadHandle()); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(msgHandleThread, NULL, NSOICFree(data)); @@ -219,6 +231,7 @@ void * NSConsumerMsgPushThreadFunc(void * data) } NSThreadUnlock(msgHandleThread); + pthread_mutex_unlock(&NSConsumerQueueMutex); return NULL; } -- 2.7.4