[Home]

Summary:ASTERISK-30394: [patch] DUNDi process_clearcache() can result in deadlock
Reporter:Peter Racz (pracz)Labels:patch
Date Opened:2023-01-12 08:09:30.000-0600Date Closed:
Priority:MajorRegression?
Status:Open/NewComponents:PBX/pbx_dundi
Versions:20.1.0 Frequency of
Occurrence
Related
Issues:
Environment:Attachments:( 0) dundi-clearcache.patch
Description:The method process_clearcache() in pbx_dundi.c does not handle thread cancelling correctly. The code is trying to limit the cancellation to within the 60s sleep call. However, it is not correct.

From the man page for pthread_cancel():
{quote}
A  thread's  cancelability  state,  determined  by   pthread_setcancel‐
state(3), can be enabled (the default for new threads) or disabled.  If
a thread has disabled cancellation, then a cancellation request remains
queued  until the thread enables cancellation.  If a thread has enabled
cancellation, then its cancelability type determines when  cancellation
occurs.

A  thread's  cancellation type, determined by pthread_setcanceltype(3),
may be either asynchronous or deferred (the default for  new  threads).
Asynchronous cancelability means that the thread can be canceled at any
time (usually immediately, but the system  does  not  guarantee  this).
Deferred  cancelability  means  that cancellation will be delayed until
the thread next calls a function that is a cancellation point.  A  list
of  functions  that  are  or  may be cancellation points is provided in
pthreads(7).
{quote}

Additionally, if a thread holds a mutex when it is cancelled, the mutex will remain locked, and any future attempts to lock the mutex will block forever.

In the code, the cancellation is deferred to a cancellation point. Most blocking IO operations are cancellation points. ast_db_gettree() and ast_db_del() take the dblock mutex and then performs IO operations on the sqlite3 database. So the cancellation can happen when the dblock is held, and then subsequent calls to ast_db operations blocks forever.

The patch sets pthread_setcancelstate() to disabled except while performing the sleep. The patch is against the latest version in git.

Remark: The deadlock probably does not happen in practice with the latest Asterisk version but the code is buggy. We use a somewhat modified reload() method and then we see this issue.
Comments:By: Asterisk Team (asteriskteam) 2023-01-12 08:09:31.079-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/].

By: Joshua C. Colp (jcolp) 2023-01-12 08:13:05.969-0600

Attaching patches requires signing of the license agreement which can be done by going to "Sign a License Agreement" at the top of the page and filling it out. It is not instant and is reviewed by legal. After acceptance then patches can be attached.

By: Peter Racz (pracz) 2023-01-12 08:22:22.587-0600

Thank you for the hint. When going to "Sign a License Agreement", the page shows me "You already have a license that is approved or pending review." So, I should already have a license agreement and I did submit patches in the past.

By: Joshua C. Colp (jcolp) 2023-01-12 08:25:36.987-0600

Your patch is showing up, I think you uploaded it at the same time I replied.