From 093902432fb24f42ade2857c23eca950e0b41a2f Mon Sep 17 00:00:00 2001 From: Hassan Sahibzada Date: Wed, 22 Sep 2021 13:27:39 -0400 Subject: [PATCH] fix a few bugs --- samples/Common.c | 5 ----- samples/kvsWebRTCClientMaster.c | 14 ++++++++++++++ src/source/Ice/ConnectionListener.c | 5 ++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/samples/Common.c b/samples/Common.c index 64007774ce..55f948a2e5 100644 --- a/samples/Common.c +++ b/samples/Common.c @@ -1025,11 +1025,6 @@ STATUS freeSampleConfiguration(PSampleConfiguration* ppSampleConfiguration) MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock); locked = TRUE; } - // Cancel the media thread - if(!(pSampleConfiguration->mediaThreadStarted)) { - DLOGD("Canceling media thread"); - THREAD_CANCEL(pSampleConfiguration->mediaSenderTid); - } for (i = 0; i < pSampleConfiguration->streamingSessionCount; ++i) { retStatus = gatherIceServerStats(pSampleConfiguration->sampleStreamingSessionList[i]); diff --git a/samples/kvsWebRTCClientMaster.c b/samples/kvsWebRTCClientMaster.c index dcaaa7c653..bd80b5cc7e 100644 --- a/samples/kvsWebRTCClientMaster.c +++ b/samples/kvsWebRTCClientMaster.c @@ -122,6 +122,20 @@ INT32 main(INT32 argc, CHAR* argv[]) // Kick of the termination sequence ATOMIC_STORE_BOOL(&pSampleConfiguration->appTerminateFlag, TRUE); + if (IS_VALID_MUTEX_VALUE(pSampleConfiguration->sampleConfigurationObjLock)) { + MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock); + } + + // Cancel the media thread + if (pSampleConfiguration->mediaThreadStarted) { + DLOGD("Canceling media thread"); + THREAD_CANCEL(pSampleConfiguration->mediaSenderTid); + } + + if (IS_VALID_MUTEX_VALUE(pSampleConfiguration->sampleConfigurationObjLock)) { + MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock); + } + if (pSampleConfiguration->mediaSenderTid != INVALID_TID_VALUE) { THREAD_JOIN(pSampleConfiguration->mediaSenderTid, NULL); } diff --git a/src/source/Ice/ConnectionListener.c b/src/source/Ice/ConnectionListener.c index 92adbc5fd9..39ebaab8aa 100644 --- a/src/source/Ice/ConnectionListener.c +++ b/src/source/Ice/ConnectionListener.c @@ -374,7 +374,10 @@ PVOID connectionListenerReceiveDataRoutine(PVOID arg) CleanUp: - if (pConnectionListener != NULL) { + // The check for valid mutex is necessary because when we're in freeConnectionListener + // we may free the mutex in another thread so by the time we get here accessing the lock + // will result in accessing a resource after it has been freed + if (pConnectionListener != NULL && IS_VALID_MUTEX_VALUE(pConnectionListener->lock)) { // As TID is 64 bit we can't atomically update it and need to do it under the lock MUTEX_LOCK(pConnectionListener->lock); pConnectionListener->receiveDataRoutine = INVALID_TID_VALUE;