<p>Joshua C. Colp <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/10909">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Sean Bright: Looks good to me, but someone else must approve
  Joshua C. Colp: Looks good to me, approved; Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_registrar: mitigate blocked threads on reliable transport shutdown<br><br>When a reliable transport is shutdown it's possible for the pjsip registrar<br>resource shutdown handler to get called multiple times. If this happens and one<br>of the threads is taking "too long" (slow database call for instance) then the<br>others get blocked waiting to delete.<br><br>Since it only takes one to delete the contact then the other threads should be<br>able to continue on if one of the threads is currently "deleting". This patch<br>makes it so now when a thread enters the shutdown handler it checks to see if a<br>thread is currently already "deleting". If so, then the thread does not attempt<br>to get the lock, and instead continues on thus avoiding the blockage.<br><br>ASTERISK-28213 #close<br><br>Change-Id: I7563ca596312b1dff4f3ab41483e89fe2862328a<br>---<br>M res/res_pjsip_registrar.c<br>1 file changed, 29 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_pjsip_registrar.c b/res/res_pjsip_registrar.c</span><br><span>index efd2bd9..83fbdf8 100644</span><br><span>--- a/res/res_pjsip_registrar.c</span><br><span>+++ b/res/res_pjsip_registrar.c</span><br><span>@@ -318,6 +318,8 @@</span><br><span>          * \note Stored after aor_name in space reserved when struct allocated.</span><br><span>       */</span><br><span>  char *contact_name;</span><br><span style="color: hsl(120, 100%, 40%);">+   /*! Indicates that the monitor is in the process of removing a contact */</span><br><span style="color: hsl(120, 100%, 40%);">+     int removing;</span><br><span>        /*! AOR name the contact is associated */</span><br><span>    char aor_name[0];</span><br><span> };</span><br><span>@@ -344,6 +346,20 @@</span><br><span>       }</span><br><span> </span><br><span>        ao2_lock(aor);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * We're now locked so check again to make sure some other thread is not</span><br><span style="color: hsl(120, 100%, 40%);">+   * currently removing the contact, or already has.</span><br><span style="color: hsl(120, 100%, 40%);">+     */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (monitor->removing) {</span><br><span style="color: hsl(120, 100%, 40%);">+           ao2_unlock(aor);</span><br><span style="color: hsl(120, 100%, 40%);">+              ao2_ref(aor, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+             ao2_ref(monitor, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+         return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   monitor->removing = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  contact = ast_sip_location_retrieve_contact(monitor->contact_name);</span><br><span>       if (contact) {</span><br><span>               ast_sip_location_delete_contact(contact);</span><br><span>@@ -380,6 +396,19 @@</span><br><span>     struct contact_transport_monitor *monitor = data;</span><br><span> </span><br><span>        /*</span><br><span style="color: hsl(120, 100%, 40%);">+     * It's possible for this shutdown handler to get called multiple times for the</span><br><span style="color: hsl(120, 100%, 40%);">+    * same monitor from different threads. Only one of the calls needs to do the</span><br><span style="color: hsl(120, 100%, 40%);">+  * actual removing of the contact, so if one is currently removing then any</span><br><span style="color: hsl(120, 100%, 40%);">+    * subsequent calls can skip.</span><br><span style="color: hsl(120, 100%, 40%);">+  *</span><br><span style="color: hsl(120, 100%, 40%);">+     * We'll call it non locked here, but check again once locked just in case the</span><br><span style="color: hsl(120, 100%, 40%);">+     * flag was updated (see register_contact_transport_remove_cb).</span><br><span style="color: hsl(120, 100%, 40%);">+        */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (monitor->removing) {</span><br><span style="color: hsl(120, 100%, 40%);">+           return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /*</span><br><span>    * Push off to a default serializer.  This is in case sorcery</span><br><span>         * does database accesses for contacts.  Database accesses may</span><br><span>        * not be on this machine.  We don't want to tie up the pjsip</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10909">change 10909</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/10909"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: I7563ca596312b1dff4f3ab41483e89fe2862328a </div>
<div style="display:none"> Gerrit-Change-Number: 10909 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation (1000185) </div>
<div style="display:none"> Gerrit-Reviewer: Joshua C. Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Sean Bright <sean.bright@gmail.com> </div>