<p>Friendly Automation <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/13031">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Benjamin Keith Ford: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved
  Friendly Automation: Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">pbx: deadlock when outgoing dialed channel hangs up too quickly<br><br>Here's the basic scenario that occurred when executing an AMI fast originate<br>while at the same time something else locks the channels container, and also<br>wants a lock on the dialed channel:<br><br>1. pbx_outgoing_attempt obtains a lock on a dialed channel<br>2. concurrently another thread obtains a lock on the channels container, and<br>   subsequently requests a lock on the dialed channel. It waits on #1. For<br>   instance, "core show channel <dialed channel"<br>3. the outgoing call does not fail, but ends before the pbx_outgoing_attempt<br>   function exits<br>4. pbx_outgoing_attempt function exits, the outgoing structure destructs, and<br>   attempts to hang up the dialed channel<br>5. hang up tries to obtain the channels container lock, but can't due to #2.<br>6. Asterisk is deadlocked.<br><br>The solution was to allow the pbx_outgoing_exec function to "steal" ownership<br>of the dialed channel, and handle hanging it up. The channel now is either hung<br>up prior to it being potentially locked by the initiating thread, or if locked<br>the hang up takes place in a different thread, thus alleviating the deadlock.<br><br>ASTERISK-28561<br>patches:<br>  iliketrains.diff submitted by Joshua Colp (license 5000)<br><br>Change-Id: I51b42b92dde8f2215b69bb509e28667ee3a3853a<br>---<br>M main/pbx.c<br>1 file changed, 14 insertions(+), 12 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/pbx.c b/main/pbx.c</span><br><span>index 8b869f1..872d674 100644</span><br><span>--- a/main/pbx.c</span><br><span>+++ b/main/pbx.c</span><br><span>@@ -7604,6 +7604,7 @@</span><br><span> {</span><br><span>   RAII_VAR(struct pbx_outgoing *, outgoing, data, ao2_cleanup);</span><br><span>        enum ast_dial_result res;</span><br><span style="color: hsl(120, 100%, 40%);">+     struct ast_channel *chan;</span><br><span> </span><br><span>        res = ast_dial_run(outgoing->dial, NULL, 0);</span><br><span> </span><br><span>@@ -7624,36 +7625,37 @@</span><br><span>                return NULL;</span><br><span>         }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* We steal the channel so we get ownership of when it is hung up */</span><br><span style="color: hsl(120, 100%, 40%);">+  chan = ast_dial_answered_steal(outgoing->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>         if (!ast_strlen_zero(outgoing->app)) {</span><br><span>            struct ast_app *app = pbx_findapp(outgoing->app);</span><br><span> </span><br><span>             if (app) {</span><br><span>                   ast_verb(4, "Launching %s(%s) on %s\n", outgoing->app, S_OR(outgoing->appdata, ""),</span><br><span style="color: hsl(0, 100%, 40%);">-                           ast_channel_name(ast_dial_answered(outgoing->dial)));</span><br><span style="color: hsl(0, 100%, 40%);">-                        pbx_exec(ast_dial_answered(outgoing->dial), app, outgoing->appdata);</span><br><span style="color: hsl(120, 100%, 40%);">+                            ast_channel_name(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+                      pbx_exec(chan, app, outgoing->appdata);</span><br><span>           } else {</span><br><span>                     ast_log(LOG_WARNING, "No such application '%s'\n", outgoing->app);</span><br><span>              }</span><br><span style="color: hsl(0, 100%, 40%);">-       } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                struct ast_channel *answered = ast_dial_answered(outgoing->dial);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+              ast_hangup(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+     } else {</span><br><span>             if (!ast_strlen_zero(outgoing->context)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                   ast_channel_context_set(answered, outgoing->context);</span><br><span style="color: hsl(120, 100%, 40%);">+                      ast_channel_context_set(chan, outgoing->context);</span><br><span>                 }</span><br><span> </span><br><span>                if (!ast_strlen_zero(outgoing->exten)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                     ast_channel_exten_set(answered, outgoing->exten);</span><br><span style="color: hsl(120, 100%, 40%);">+                  ast_channel_exten_set(chan, outgoing->exten);</span><br><span>             }</span><br><span> </span><br><span>                if (outgoing->priority > 0) {</span><br><span style="color: hsl(0, 100%, 40%);">-                     ast_channel_priority_set(answered, outgoing->priority);</span><br><span style="color: hsl(120, 100%, 40%);">+                    ast_channel_priority_set(chan, outgoing->priority);</span><br><span>               }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-           if (ast_pbx_run(answered)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                    ast_log(LOG_ERROR, "Failed to start PBX on %s\n", ast_channel_name(answered));</span><br><span style="color: hsl(0, 100%, 40%);">-                } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                        /* PBX will have taken care of hanging up, so we steal the answered channel so dial doesn't do it */</span><br><span style="color: hsl(0, 100%, 40%);">-                        ast_dial_answered_steal(outgoing->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+           if (ast_pbx_run(chan)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      ast_log(LOG_ERROR, "Failed to start PBX on %s\n", ast_channel_name(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+                  ast_hangup(chan);</span><br><span>            }</span><br><span>    }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/13031">change 13031</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/+/13031"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: I51b42b92dde8f2215b69bb509e28667ee3a3853a </div>
<div style="display:none"> Gerrit-Change-Number: 13031 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: 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@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>