<p>Joshua C. Colp <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/10968">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  George Joseph: 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: lock transport monitor when setting 'removing' flag<br><br>A previous patch attempt to mitigate blocked threads on transport shutdown for<br>a given contact. It was thought that a second lock could be avoided by checking<br>the 'removing' flag on the transport monitor twice (once before and once after<br>the normal named aor locking). However as with usual threading issues if the<br>timing was right the original problem still occured.<br><br>This patch adds locking around the first 'removing' flag check and set, thus<br>nullifying the secondary check, so it was removed.<br><br>ASTERISK-28213<br><br>Change-Id: Iaa8e36e5311789549b76d8de42dfcea96013b2ed<br>---<br>M res/res_pjsip_registrar.c<br>1 file changed, 12 insertions(+), 18 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 9be9c41..ced5dca 100644</span><br><span>--- a/res/res_pjsip_registrar.c</span><br><span>+++ b/res/res_pjsip_registrar.c</span><br><span>@@ -341,25 +341,15 @@</span><br><span> </span><br><span>     lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "aor", monitor->aor_name);</span><br><span>         if (!lock) {</span><br><span style="color: hsl(120, 100%, 40%);">+          ao2_lock(monitor);</span><br><span style="color: hsl(120, 100%, 40%);">+            monitor->removing = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+             ao2_unlock(monitor);</span><br><span>                 ao2_ref(monitor, -1);</span><br><span>                return 0;</span><br><span>    }</span><br><span> </span><br><span>        ao2_lock(lock);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-     /*</span><br><span style="color: hsl(0, 100%, 40%);">-       * We're now locked so check again to make sure some other thread is not</span><br><span style="color: hsl(0, 100%, 40%);">-     * currently removing the contact, or already has.</span><br><span style="color: hsl(0, 100%, 40%);">-       */</span><br><span style="color: hsl(0, 100%, 40%);">-     if (monitor->removing) {</span><br><span style="color: hsl(0, 100%, 40%);">-             ao2_unlock(lock);</span><br><span style="color: hsl(0, 100%, 40%);">-               ast_named_lock_put(lock);</span><br><span style="color: hsl(0, 100%, 40%);">-               ao2_ref(monitor, -1);</span><br><span style="color: hsl(0, 100%, 40%);">-           return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-       }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-       monitor->removing = 1;</span><br><span style="color: hsl(0, 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>@@ -401,14 +391,15 @@</span><br><span>     * same monitor from different threads. Only one of the calls needs to do the</span><br><span>         * actual removing of the contact, so if one is currently removing then any</span><br><span>   * subsequent calls can skip.</span><br><span style="color: hsl(0, 100%, 40%);">-    *</span><br><span style="color: hsl(0, 100%, 40%);">-       * We'll call it non locked here, but check again once locked just in case the</span><br><span style="color: hsl(0, 100%, 40%);">-       * flag was updated (see register_contact_transport_remove_cb).</span><br><span>       */</span><br><span style="color: hsl(120, 100%, 40%);">+   ao2_lock(monitor);</span><br><span>   if (monitor->removing) {</span><br><span style="color: hsl(120, 100%, 40%);">+           ao2_unlock(monitor);</span><br><span>                 return;</span><br><span>      }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ monitor->removing = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  /*</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>@@ -417,8 +408,11 @@</span><br><span>         */</span><br><span>  ao2_ref(monitor, +1);</span><br><span>        if (ast_sip_push_task(NULL, register_contact_transport_remove_cb, monitor)) {</span><br><span style="color: hsl(120, 100%, 40%);">+         monitor->removing = 0;</span><br><span>            ao2_ref(monitor, -1);</span><br><span>        }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   ao2_unlock(monitor);</span><br><span> }</span><br><span> </span><br><span> AST_VECTOR(excess_contact_vector, struct ast_sip_contact *);</span><br><span>@@ -729,8 +723,8 @@</span><br><span>                           * the contact won't be valid anymore if that happens.</span><br><span>                            */</span><br><span>                          contact_name = ast_sorcery_object_get_id(contact);</span><br><span style="color: hsl(0, 100%, 40%);">-                              monitor = ao2_alloc_options(sizeof(*monitor) + 2 + strlen(aor_name)</span><br><span style="color: hsl(0, 100%, 40%);">-                                     + strlen(contact_name), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+                             monitor = ao2_alloc(sizeof(*monitor) + 2 + strlen(aor_name)</span><br><span style="color: hsl(120, 100%, 40%);">+                                   + strlen(contact_name), NULL);</span><br><span>                               if (monitor) {</span><br><span>                                       strcpy(monitor->aor_name, aor_name);/* Safe */</span><br><span>                                    monitor->contact_name = monitor->aor_name + strlen(aor_name) + 1;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10968">change 10968</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/10968"/><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: merged </div>
<div style="display:none"> Gerrit-Change-Id: Iaa8e36e5311789549b76d8de42dfcea96013b2ed </div>
<div style="display:none"> Gerrit-Change-Number: 10968 </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: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua C. Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Richard Mudgett <rmudgett@digium.com> </div>