[Home]

Summary:ASTERISK-29217: LOCK() can grant the same lock to multiple channels spuriously
Reporter:Jaco Kroon (jkroon)Labels:
Date Opened:2020-12-18 12:08:23.000-0600Date Closed:2021-01-06 18:20:22.000-0600
Priority:MajorRegression?
Status:Closed/CompleteComponents:Functions/func_lock
Versions:13.38.0 16.15.0 17.9.0 18.1.0 Frequency of
Occurrence
Frequent
Related
Issues:
Environment:Attachments:
Description:Due to a misunderstanding of the wait conditions work in the func/func_lock.c code it's possible that multiple channels will be informed of a successful LOCK(), resulting in bad stuff happening.

Under low lock pressure scenarios this is unlikely to happen (ie, no more than 1 channel should be blocking on LOCK()), but the moment multiple channels are waiting it becomes (but is not guaranteed) possible for multiple channels to be informed they've successfully locked.

Technical details:

From PTHREAD_COND_BROADCAST(3P):

      On a multi-processor, it may be impossible for an implementation of  pthread_cond_signal()
      to avoid the unblocking of more than one thread blocked on a condition variable. For exam‐
      ple,  consider  the  following   partial   implementation   of   pthread_cond_wait()   and
      pthread_cond_signal(), executed by two threads in the order given. One thread is trying to
      wait on the condition variable, another is concurrently  executing  pthread_cond_signal(),
      while a third thread is already waiting.

I won't bore you with the sample code, but what this says is that multiple threads may be woken.  func_lock assumes that it's the only one and assumes it owns the lock on successful return from ast_cond_timedwait (which wraps the pthread_cond_timedwait).

So let's say two threads gets released here.

At this point the threads will sequentially be released (since the mutex gets grabbed again).

The first thread will set ->owner to itself, and increment ->count to 1.

The second thread will set ->owner to itself, and increment ->count to 2.

When the first thread calls UNLOCK() it will be informed it doens't own the lock.

When the second thread calls UNLOCK() it will decrement ->count to 1, and not release the lock until the channel is destroyed.


The lock_broker thread probably further aggravates the problems here, so my patch will elimate it's use as well.

Comments:By: Asterisk Team (asteriskteam) 2020-12-18 12:08:24.651-0600

Thanks for creating a report! The issue has entered the triage process. That means the issue will wait in this status until a Bug Marshal has an opportunity to review the issue. Once the issue has been reviewed you will receive comments regarding the next steps towards resolution. Please note that log messages and other files should not be sent to the Sangoma Asterisk Team unless explicitly asked for. All files should be placed on this issue in a sanitized fashion as needed.

A good first step is for you to review the [Asterisk Issue Guidelines|https://wiki.asterisk.org/wiki/display/AST/Asterisk+Issue+Guidelines] if you haven't already. The guidelines detail what is expected from an Asterisk issue report.

Then, if you are submitting a patch, please review the [Patch Contribution Process|https://wiki.asterisk.org/wiki/display/AST/Patch+Contribution+Process].

Please note that once your issue enters an open state it has been accepted. As Asterisk is an open source project there is no guarantee or timeframe on when your issue will be looked into. If you need expedient resolution you will need to find and pay a suitable developer. Asking for an update on your issue will not yield any progress on it and will not result in a response. All updates are posted to the issue when they occur.

Please note that by submitting data, code, or documentation to Sangoma through JIRA, you accept the Terms of Use present at [https://www.asterisk.org/terms-of-use/|https://www.asterisk.org/terms-of-use/].