<p>George Joseph has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15510">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_refer: Refactor progress locking and serialization<br><br>Under certain conditions, the progress notify serializer can be<br>attempting to access the subscription while sending a notify<br>while another thread is cleaning the subscription up.<br><br>The serilizer usage and dialog locking have been refactored<br>to be both simpler and more consistent.<br><br>* refer_progress_notify(), which runs in the serializer,<br> now holds the dialog lock for it's duration.<br><br>* refer_progress_on_evsub_state(), which doesn't run in<br> the serializer but did already held the dialog lock<br> for its duration, now directly sets progress->sub to NULL<br> instead of queueing a task to the serializer.<br><br>ASTERISK-29313<br><br>Change-Id: I97a8bb01771a3c85345649b8124507f7622a8480<br>---<br>M res/res_pjsip_refer.c<br>1 file changed, 10 insertions(+), 28 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/10/15510/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_pjsip_refer.c b/res/res_pjsip_refer.c</span><br><span>index 030e2be..faffa92 100644</span><br><span>--- a/res/res_pjsip_refer.c</span><br><span>+++ b/res/res_pjsip_refer.c</span><br><span>@@ -115,10 +115,13 @@</span><br><span> pjsip_evsub *sub;</span><br><span> pjsip_tx_data *tdata;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_dlg_inc_lock(notification->progress->dlg);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* If the subscription has already been terminated we can't send a notification */</span><br><span> if (!(sub = notification->progress->sub)) {</span><br><span> ast_debug(3, "Not sending NOTIFY of response '%d' and state '%u' on progress monitor '%p' as subscription has been terminated\n",</span><br><span> notification->response, notification->state, notification->progress);</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_dlg_dec_lock(notification->progress->dlg);</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span>@@ -128,12 +131,9 @@</span><br><span> if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {</span><br><span> ast_debug(3, "Subscription '%p' is being terminated as a result of a NOTIFY, removing REFER progress structure early on progress monitor '%p'\n",</span><br><span> notification->progress->sub, notification->progress);</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_dlg_inc_lock(notification->progress->dlg);</span><br><span> pjsip_evsub_set_mod_data(notification->progress->sub, refer_progress_module.id, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_dlg_dec_lock(notification->progress->dlg);</span><br><span> </span><br><span> /* This is for dropping the reference on the subscription */</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(notification->progress);</span><br><span> </span><br><span> notification->progress->sub = NULL;</span><br><span> }</span><br><span>@@ -158,6 +158,12 @@</span><br><span> pjsip_xfer_send_request(sub, tdata);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (notification->state == PJSIP_EVSUB_STATE_TERMINATED) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(notification->progress);</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%);">+ pjsip_dlg_dec_lock(notification->progress->dlg);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span>@@ -293,17 +299,6 @@</span><br><span> ao2_cleanup(progress);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Serialized callback for subscription termination */</span><br><span style="color: hsl(0, 100%, 40%);">-static int refer_progress_terminate(void *data)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct refer_progress *progress = data;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* The subscription is no longer valid */</span><br><span style="color: hsl(0, 100%, 40%);">- progress->sub = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</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> /*! \brief Callback for REFER subscription state changes */</span><br><span> static void refer_progress_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)</span><br><span> {</span><br><span>@@ -315,20 +310,7 @@</span><br><span> ast_debug(3, "Subscription '%p' has been remotely terminated, waiting for other tasks to complete on progress monitor '%p'\n",</span><br><span> sub, progress);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* It's possible that a task is waiting to remove us already, so bump the refcount of progress so it doesn't get destroyed */</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(progress, +1);</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_dlg_dec_lock(progress->dlg);</span><br><span style="color: hsl(0, 100%, 40%);">- /*</span><br><span style="color: hsl(0, 100%, 40%);">- * XXX We are always going to execute this inline rather than</span><br><span style="color: hsl(0, 100%, 40%);">- * in the serializer because this function is a PJPROJECT</span><br><span style="color: hsl(0, 100%, 40%);">- * callback and thus has to be a SIP servant thread.</span><br><span style="color: hsl(0, 100%, 40%);">- *</span><br><span style="color: hsl(0, 100%, 40%);">- * The likely remedy is to push most of this function into</span><br><span style="color: hsl(0, 100%, 40%);">- * refer_progress_terminate() with ast_sip_push_task().</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sip_push_task_wait_servant(progress->serializer, refer_progress_terminate, progress);</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_dlg_inc_lock(progress->dlg);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(progress, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ progress->sub = NULL;</span><br><span> </span><br><span> ast_debug(3, "Subscription '%p' removed from progress monitor '%p'\n", sub, progress);</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/15510">change 15510</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/c/asterisk/+/15510"/><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-Change-Id: I97a8bb01771a3c85345649b8124507f7622a8480 </div>
<div style="display:none"> Gerrit-Change-Number: 15510 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>