<p>Joshua Colp <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/14869">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve; Approved for Submit
Kevin Harwell: Looks good to me, approved
George Joseph: Looks good to me, but someone else must approve
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">Bridging: Use a ref to bridge_channel's channel to prevent crash.<br><br>There's a race condition with bridging where a bridge can be torn down<br>causing the bridge_channel's ast_channel to become NULL when it's still<br>needed. This particular case happened with attended transfers, but the<br>crash occurred when trying to publish a stasis message. Now, the<br>bridge_channel is locked, a ref to the ast_channel is obtained, and that<br>ref is passed down the chain.<br><br>Change-Id: Ic48715c0c041615d17d286790ae3e8c61bb28814<br>---<br>M include/asterisk/bridge_channel.h<br>M main/bridge.c<br>M main/bridge_channel.c<br>3 files changed, 52 insertions(+), 3 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/bridge_channel.h b/include/asterisk/bridge_channel.h</span><br><span>index 4504d6b..fb2fd4f 100644</span><br><span>--- a/include/asterisk/bridge_channel.h</span><br><span>+++ b/include/asterisk/bridge_channel.h</span><br><span>@@ -194,6 +194,20 @@</span><br><span> };</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Get a ref to the bridge_channel's ast_channel</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param bridge_channel The bridge channel</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note The returned channel NEEDS to be unref'd once you are done with it. In general, this</span><br><span style="color: hsl(120, 100%, 40%);">+ * function is best used when accessing the bridge_channel chan from outside of a bridging</span><br><span style="color: hsl(120, 100%, 40%);">+ * thread.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval ref'd ast_channel on success</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval NULL otherwise</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_channel *ast_bridge_channel_get_chan(struct ast_bridge_channel *bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Try locking the bridge_channel.</span><br><span> *</span><br><span> * \param bridge_channel What to try locking</span><br><span>diff --git a/main/bridge.c b/main/bridge.c</span><br><span>index 9304495..d25ffdf 100644</span><br><span>--- a/main/bridge.c</span><br><span>+++ b/main/bridge.c</span><br><span>@@ -1728,7 +1728,10 @@</span><br><span> ast_channel_lock(chan);</span><br><span> ast_channel_internal_bridge_channel_set(chan, NULL);</span><br><span> ast_channel_unlock(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Due to a race condition, we lock the bridge channel here for ast_bridge_channel_get_chan */</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(bridge_channel);</span><br><span> bridge_channel->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(bridge_channel);</span><br><span> /* If bridge_channel->swap is not NULL then the join failed. */</span><br><span> ao2_t_cleanup(bridge_channel->swap, "Bridge complete: join failed");</span><br><span> bridge_channel->swap = NULL;</span><br><span>@@ -1797,7 +1800,10 @@</span><br><span> ast_channel_lock(chan);</span><br><span> ast_channel_internal_bridge_channel_set(chan, NULL);</span><br><span> ast_channel_unlock(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Lock here for ast_bridge_channel_get_chan */</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(bridge_channel);</span><br><span> bridge_channel->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(bridge_channel);</span><br><span> /* If bridge_channel->swap is not NULL then the join failed. */</span><br><span> ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Independent impart join failed");</span><br><span> bridge_channel->swap = NULL;</span><br><span>@@ -1898,7 +1904,10 @@</span><br><span> ast_channel_lock(chan);</span><br><span> ast_channel_internal_bridge_channel_set(chan, NULL);</span><br><span> ast_channel_unlock(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Lock here for ast_bridge_channel_get_chan */</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(bridge_channel);</span><br><span> bridge_channel->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(bridge_channel);</span><br><span> ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Impart failed");</span><br><span> bridge_channel->swap = NULL;</span><br><span> ast_bridge_features_destroy(bridge_channel->features);</span><br><span>@@ -4749,14 +4758,22 @@</span><br><span> </span><br><span> if (to_transferee_bridge_channel) {</span><br><span> /* Take off hold if they are on hold. */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_bridge_channel_write_unhold(to_transferee_bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_bridge_channel_write_unhold(to_transferee_bridge_channel)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Transferee channel disappeared during transfer!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ res = AST_BRIDGE_TRANSFER_FAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+ goto end;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> </span><br><span> if (to_target_bridge_channel) {</span><br><span> const char *target_complete_sound;</span><br><span> </span><br><span> /* Take off hold if they are on hold. */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_bridge_channel_write_unhold(to_target_bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_bridge_channel_write_unhold(to_target_bridge_channel)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Target channel disappeared during transfer!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ res = AST_BRIDGE_TRANSFER_FAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+ goto end;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> /* Is there a courtesy sound to play to the target? */</span><br><span> ast_channel_lock(to_transfer_target);</span><br><span>diff --git a/main/bridge_channel.c b/main/bridge_channel.c</span><br><span>index ade98fd..3c5e87b 100644</span><br><span>--- a/main/bridge_channel.c</span><br><span>+++ b/main/bridge_channel.c</span><br><span>@@ -208,6 +208,17 @@</span><br><span> ast_sem_post(&sync_struct->sem);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_channel *ast_bridge_channel_get_chan(struct ast_bridge_channel *bridge_channel)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_channel *chan;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+ chan = ao2_bump(bridge_channel->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return chan;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)</span><br><span> {</span><br><span> struct ast_bridge *bridge;</span><br><span>@@ -1177,7 +1188,14 @@</span><br><span> </span><br><span> int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_channel *chan = ast_bridge_channel_get_chan(bridge_channel);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</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%);">+ ast_channel_publish_cached_blob(chan, ast_channel_unhold_type(), NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(chan, -1);</span><br><span> </span><br><span> return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/14869">change 14869</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/+/14869"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 17 </div>
<div style="display:none"> Gerrit-Change-Id: Ic48715c0c041615d17d286790ae3e8c61bb28814 </div>
<div style="display:none"> Gerrit-Change-Number: 14869 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>