<p>Walter Doekes has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/10991">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">sched: Don't allow ast_sched_del to deadlock ast_sched_runq from same thread<br><br>When fixing ASTERISK-24212, a change was done so a scheduled callback could not<br>be removed while it was running. The caller of ast_sched_del would have to wait.<br><br>However, when the caller of ast_sched_del is the callback itself (however wrong<br>this might be), this new check would cause a deadlock: it would wait forever<br>for itself.<br><br>This changeset introduces an additional check: if ast_sched_del is called<br>by the callback itself, it is immediately rejected (along with an ERROR log and<br>a backtrace). Additionally, the AST_SCHED_DEL_UNREF macro is adjusted so the<br>after-ast_sched_del-refcall function is only run if ast_sched_del returned<br>success.<br><br>This should fix the following spurious race condition found in chan_sip:<br>- thread 1: schedule sip_poke_peer_now (using AST_SCHED_REPLACE)<br>- thread 2: run sip_poke_peer_now<br>- thread 2: blank out sched-ID (too soon!)<br>- thread 1: set sched-ID (too late!)<br>- thread 2: try to delete the currently running sched-ID<br><br>After this fix, an ERROR would be logged, but no deadlocks (in do_monitor) nor<br>excess calls to sip_unref_peer(peer) (causing double frees of rtp_instances and<br>other madness) should occur.<br><br>ASTERISK-28282<br><br>Change-Id: Ic26777fa0732725e6ca7010df17af77a012aa856<br>---<br>M include/asterisk/sched.h<br>M main/sched.c<br>2 files changed, 25 insertions(+), 6 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/91/10991/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/sched.h b/include/asterisk/sched.h</span><br><span>index 804b05c..8e2a990 100644</span><br><span>--- a/include/asterisk/sched.h</span><br><span>+++ b/include/asterisk/sched.h</span><br><span>@@ -71,20 +71,24 @@</span><br><span> </span><br><span> /*!</span><br><span> * \brief schedule task to get deleted and call unref function</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Only calls unref function if the delete succeeded.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span> * \sa AST_SCHED_DEL</span><br><span> * \since 1.6.1</span><br><span> */</span><br><span> #define AST_SCHED_DEL_UNREF(sched, id, refcall) \</span><br><span> do { \</span><br><span style="color: hsl(0, 100%, 40%);">- int _count = 0; \</span><br><span style="color: hsl(0, 100%, 40%);">- while (id > -1 && ast_sched_del(sched, id) && ++_count < 10) { \</span><br><span style="color: hsl(120, 100%, 40%);">+ int _count = 0, _id = id; \</span><br><span style="color: hsl(120, 100%, 40%);">+ while (_id > -1 && ast_sched_del(sched, _id) && ++_count < 10) { \</span><br><span> usleep(1); \</span><br><span> } \</span><br><span style="color: hsl(0, 100%, 40%);">- if (_count == 10) \</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Unable to cancel schedule ID %d. This is probably a bug (%s: %s, line %d).\n", id, __FILE__, __PRETTY_FUNCTION__, __LINE__); \</span><br><span style="color: hsl(0, 100%, 40%);">- if (id > -1) \</span><br><span style="color: hsl(120, 100%, 40%);">+ if (_count == 10) { \</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Unable to cancel schedule ID %d. This is probably a bug (%s: %s, line %d).\n", _id, __FILE__, __PRETTY_FUNCTION__, __LINE__); \</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (_id > -1) { \</span><br><span> refcall; \</span><br><span style="color: hsl(0, 100%, 40%);">- id = -1; \</span><br><span style="color: hsl(120, 100%, 40%);">+ id = -1; \</span><br><span style="color: hsl(120, 100%, 40%);">+ } \</span><br><span> } while (0);</span><br><span> </span><br><span> /*!</span><br><span>diff --git a/main/sched.c b/main/sched.c</span><br><span>index e5a6e52..a092bf4 100644</span><br><span>--- a/main/sched.c</span><br><span>+++ b/main/sched.c</span><br><span>@@ -118,6 +118,7 @@</span><br><span> struct sched_thread *sched_thread;</span><br><span> /*! The scheduled task that is currently executing */</span><br><span> struct sched *currently_executing;</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_t currently_executing_on_thread_id;</span><br><span> </span><br><span> #ifdef SCHED_MAX_CACHE</span><br><span> AST_LIST_HEAD_NOLOCK(, sched) schedc; /*!< Cache of unused schedule structures and how many */</span><br><span>@@ -626,6 +627,18 @@</span><br><span> ast_log(LOG_WARNING,"sched entry %d not in the sched heap?\n", s->sched_id->id);</span><br><span> }</span><br><span> sched_release(con, s);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (con->currently_executing_on_thread_id == pthread_self()) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* We might trample on deleted memory at this point. Not good,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but it's better than a deadlock.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Thou shalt not reschedule things from a scheduled callback!</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR,</span><br><span style="color: hsl(120, 100%, 40%);">+ "BUG! Trying to delete sched %d from the same callback %p (sched %d). "</span><br><span style="color: hsl(120, 100%, 40%);">+ "Ignoring so we don't deadlock\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ id, con->currently_executing->callback, con->currently_executing->sched_id->id);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log_backtrace();</span><br><span style="color: hsl(120, 100%, 40%);">+ /* We'll return -1 below, because s is NULL. The caller</span><br><span style="color: hsl(120, 100%, 40%);">+ * will rightly assume that the unscheduling failed. */</span><br><span> } else if (con->currently_executing && (id == con->currently_executing->sched_id->id)) {</span><br><span> s = con->currently_executing;</span><br><span> s->deleted = 1;</span><br><span>@@ -775,10 +788,12 @@</span><br><span> */</span><br><span> </span><br><span> con->currently_executing = current;</span><br><span style="color: hsl(120, 100%, 40%);">+ con->currently_executing_on_thread_id = pthread_self();</span><br><span> ast_mutex_unlock(&con->lock);</span><br><span> res = current->callback(current->data);</span><br><span> ast_mutex_lock(&con->lock);</span><br><span> con->currently_executing = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ con->currently_executing_on_thread_id = 0;</span><br><span> ast_cond_signal(¤t->cond);</span><br><span> </span><br><span> if (res && !current->deleted) {</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10991">change 10991</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/10991"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Ic26777fa0732725e6ca7010df17af77a012aa856 </div>
<div style="display:none"> Gerrit-Change-Number: 10991 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Walter Doekes <walter+asterisk@wjd.nu> </div>