<p>Richard Mudgett has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/9330">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip/pjsip_transport_management.c: Fix deadlock with transport keep alive.<br><br>Using the keep_alive_interval option can result in a deadlock between the<br>pjproject transport manager group lock and the monitored transports ao2<br>container lock. The pjproject transport manager group lock has to be<br>superior in the locking order to the monitored transports ao2 container<br>lock because of pjproject callbacks called when already holding the group<br>lock. The lock inversion happens when Asterisk attempts to send a keep<br>alive packet over the reliable transports.<br><br>* Made keepalive_transport_thread() iterate over the monitored transports<br>container rather than use the ao2_callback() method. This avoids holding<br>the container lock when sending the keep alive packet.<br><br>ASTERISK-26686<br><br>Change-Id: I5d5392a52e698bbe41a93f7d8e92bf0e61fe3951<br>---<br>M res/res_pjsip/pjsip_transport_management.c<br>1 file changed, 25 insertions(+), 10 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/30/9330/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_pjsip/pjsip_transport_management.c b/res/res_pjsip/pjsip_transport_management.c</span><br><span>index 1404b6e..d9866f1 100644</span><br><span>--- a/res/res_pjsip/pjsip_transport_management.c</span><br><span>+++ b/res/res_pjsip/pjsip_transport_management.c</span><br><span>@@ -56,21 +56,22 @@</span><br><span> int sip_received;</span><br><span> };</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Callback function to send keepalive */</span><br><span style="color: hsl(0, 100%, 40%);">-static int keepalive_transport_cb(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+static void keepalive_transport_send_keepalive(struct monitored_transport *monitored)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- struct monitored_transport *monitored = obj;</span><br><span> pjsip_tpselector selector = {</span><br><span> .type = PJSIP_TPSELECTOR_TRANSPORT,</span><br><span> .u.transport = monitored->transport,</span><br><span> };</span><br><span> </span><br><span> pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()),</span><br><span style="color: hsl(0, 100%, 40%);">- monitored->transport->key.type, &selector, NULL, keepalive_packet.ptr, keepalive_packet.slen,</span><br><span style="color: hsl(0, 100%, 40%);">- &monitored->transport->key.rem_addr, pj_sockaddr_get_len(&monitored->transport->key.rem_addr),</span><br><span style="color: hsl(120, 100%, 40%);">+ monitored->transport->key.type,</span><br><span style="color: hsl(120, 100%, 40%);">+ &selector,</span><br><span style="color: hsl(120, 100%, 40%);">+ NULL,</span><br><span style="color: hsl(120, 100%, 40%);">+ keepalive_packet.ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+ keepalive_packet.slen,</span><br><span style="color: hsl(120, 100%, 40%);">+ &monitored->transport->key.rem_addr,</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_sockaddr_get_len(&monitored->transport->key.rem_addr),</span><br><span> NULL, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span> }</span><br><span> </span><br><span> /*! \brief Thread which sends keepalives to all active connection-oriented transports */</span><br><span>@@ -90,12 +91,26 @@</span><br><span> return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Once loaded this module just keeps on going as it is unsafe to stop and change the underlying</span><br><span style="color: hsl(0, 100%, 40%);">- * callback for the transport manager.</span><br><span style="color: hsl(120, 100%, 40%);">+ /*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Once loaded this module just keeps on going as it is unsafe to stop</span><br><span style="color: hsl(120, 100%, 40%);">+ * and change the underlying callback for the transport manager.</span><br><span> */</span><br><span> while (keepalive_interval) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_iterator iter;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct monitored_transport *monitored;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> sleep(keepalive_interval);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(transports, OBJ_NODATA, keepalive_transport_cb, NULL);</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 must use the iterator to avoid deadlock between the container lock</span><br><span style="color: hsl(120, 100%, 40%);">+ * and the pjproject transport manager group lock when sending</span><br><span style="color: hsl(120, 100%, 40%);">+ * the keepalive packet.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ iter = ao2_iterator_init(transports, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ for (; (monitored = ao2_iterator_next(&iter)); ao2_ref(monitored, -1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ keepalive_transport_send_keepalive(monitored);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_iterator_destroy(&iter);</span><br><span> }</span><br><span> </span><br><span> ao2_ref(transports, -1);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/9330">change 9330</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/9330"/><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: I5d5392a52e698bbe41a93f7d8e92bf0e61fe3951 </div>
<div style="display:none"> Gerrit-Change-Number: 9330 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Richard Mudgett <rmudgett@digium.com> </div>