-
Notifications
You must be signed in to change notification settings - Fork 362
Description
Please confirm you have already done the following
- I have searched the repository for related/existing bug reports
- I have all the details the issue requires
Please answer the following prompt
- This issue is replicable using the unmodified sample application
Describe the bug
In the following code snippet from the KVS WebRTC C SDK, a condition variable is broadcast before acquiring the associated mutex, which is incorrect and can lead to missed wakeups or race conditions:
if (IS_VALID_CVAR_VALUE(pSampleConfiguration->cvar) && IS_VALID_MUTEX_VALUE(pSampleConfiguration->sampleConfigurationObjLock)) { CVAR_BROADCAST(pSampleConfiguration->cvar); MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock); MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock); }
Issue:
Calling CVAR_BROADCAST before locking the associated mutex (sampleConfigurationObjLock) violates standard POSIX threading best practices. The correct sequence should be:
Lock the mutex.
Modify shared state if needed.
Call CVAR_BROADCAST or CVAR_SIGNAL.
Unlock the mutex.
Failing to follow this sequence may result in the broadcast being missed by threads waiting on the condition variable, leading to unexpected hangs or logic bugs.
Expected Behavior
The sequence should be revised as follows:
if (IS_VALID_CVAR_VALUE(pSampleConfiguration->cvar) && IS_VALID_MUTEX_VALUE(pSampleConfiguration->sampleConfigurationObjLock)) { MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock); CVAR_BROADCAST(pSampleConfiguration->cvar); MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock); }
Current Behavior
you can find this above code in [amazon-kinesis-video-streams-webrtc-sdk-c/samples
/Common.c - ](https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-c/blob/cf817bc5d18f3e4bd499c6b0f9a68c6f4d7e01de/samples/Common.c#L1240C1-L1245C1)
Reproduction Steps
Mention above
WebRTC C SDK version being used
1.12.1
If it was working in a previous version, which one?
No response
Compiler and Version used
10
Operating System and version
Ubuntu 20.04
Platform being used
Embedded Linux