[CONPRO-1555] oneconnect:Core / Application crashed 78/225278/1
authorsamanway <samanway@linux-samanway.sa.corp.samsungelectronics.net>
Tue, 28 Jan 2020 16:38:04 +0000 (22:08 +0530)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Wed, 19 Feb 2020 00:10:08 +0000 (09:10 +0900)
- 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 <samanway.dey@samsung.com>
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
service/notification/src/consumer/NSConsumerScheduler.c

index 9dd920f..14adef5 100644 (file)
@@ -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;
 }