title: Asterisk AGI script falls when caller hangup
body: <p>I'm having the simple AGI script, I need to dial 101 extension by calling 6666 number and calculate answered time after call. Everything works fine when callee hangup, but when caller hangup agi script falls with returning 4.
  <br>
  Sorry for my English)
  <br><br>
  <strong>agi.php</strong></p>
  <pre><code>#!/usr/bin/php -q
  &lt;?php  
  require('include/phpagi.php');
  $agi = new AGI();
  $agi-&gt;answer();
  $agi-&gt;exec(&quot;Dial&quot;, &quot;SIP/101&quot;);
  $result = $agi-&gt;get_variable(&quot;ANSWEREDTIME&quot;);
  file_put_contents(&quot;/tmp/test.txt&quot;, json_encode($result));
  $agi-&gt;hangup();
  ?&gt;
  </code></pre>
  <br>
  **sip.conf**
  <br>
  <pre><code>[general]
  externaddr=51.15.53.237:5060
  localnet=10.18.222.53/255.255.255.0 ; локальная сеть
  language=ru
  context=default
  allowoverlap=no
  udpbindaddr=0.0.0.0
  tcpenable=no
  tcpbindaddr=0.0.0.0
  transport=udp
  srvlookup=yes
  allowguest=no
  limitonpeers=yes
  
  [authentication]
  
  [managers-phones](!)
  type=friend
  context=call-out
  secret=qwerty
  host=dynamic
  nat=yes
  qualify=yes
  canreinvite=no
  callgroup=1
  pickupgroup=1
  call-limit=5
  dtmfmode=auto
  disallow=all
  allow=alaw
  allow=ulaw
  allow=g729
  allow=g723
  allow=g722
  
  [100](managers-phones)
  callerid=&quot;Number 100&quot; &lt;100&gt;
  [101](managers-phones)
  callerid=&quot;Number 101&quot; &lt;101&gt;
  </code></pre>
  <br>
  **extensions.conf**
  <br>
  <pre><code>[general]
  static=yes
  writeprotect=no
  [globals]
  [default]
  
  [handup-sip]
  exten =&gt; _X!,1,HangUp()
  
  [call-out]
  exten =&gt; 6666,1,AGI(agi.php)
  exten =&gt; _XXX,1,Dial(SIP/${EXTEN})
  
  
  include =&gt; handup-sip
  </code></pre>
  <br>
  Console output when callee hangup:
  <br>
  <pre><code>== Using SIP RTP CoS mark 5
         &gt; 0x7fabe000deb0 -- Strict RTP learning after remote address set to: 176.213.58.47:7078
      -- Executing [6666@call-out:1] AGI(&quot;SIP/100-00000016&quot;, &quot;agi.php&quot;) in new stack
      -- Launched AGI Script /var/lib/asterisk/agi-bin/agi.php
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_request: agi.php
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_channel: SIP/100-00000016
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_language: ru
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_type: SIP
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_uniqueid: 1594145722.33
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_version: 16.11.1
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_callerid: 100
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_calleridname: Number 100
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_callingpres: 0
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_callingani2: 0
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_callington: 0
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_callingtns: 0
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_dnid: 6666
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_rdnis: unknown
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_context: call-out
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_extension: 6666
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_priority: 1
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_enhanced: 0.0
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_accountcode:
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; agi_threadid: 140374906681088
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt;
  &lt;SIP/100-00000016&gt;AGI Rx &lt;&lt; ANSWER
         &gt; 0x7fabe000deb0 -- Strict RTP switching to RTP target address 176.213.58.47:7078 as source
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; 200 result=0
  &lt;SIP/100-00000016&gt;AGI Rx &lt;&lt; EXEC Dial SIP/101
      -- AGI Script Executing Application: (Dial) Options: (SIP/101)
    == Using SIP RTP CoS mark 5
      -- Called SIP/101
      -- SIP/101-00000017 is ringing
         &gt; 0x7fabd000cb70 -- Strict RTP learning after remote address set to: 176.213.58.47:8000
      -- SIP/101-00000017 answered SIP/100-00000016
      -- Channel SIP/101-00000017 joined 'simple_bridge' basic-bridge &lt;dafb98ba-3e18-4c2e-a51c-46f2f7cc7f5b&gt;
      -- Channel SIP/100-00000016 joined 'simple_bridge' basic-bridge &lt;dafb98ba-3e18-4c2e-a51c-46f2f7cc7f5b&gt;
         &gt; Bridge dafb98ba-3e18-4c2e-a51c-46f2f7cc7f5b: switching from simple_bridge technology to native_rtp
         &gt; Locally RTP bridged 'SIP/100-00000016' and 'SIP/101-00000017' in stack
         &gt; 0x7fabd000cb70 -- Strict RTP switching to RTP target address 176.213.58.47:8000 as source
         &gt; 0x7fabe000deb0 -- Strict RTP learning complete - Locking on source address 176.213.58.47:7078
      -- Channel SIP/101-00000017 left 'native_rtp' basic-bridge &lt;dafb98ba-3e18-4c2e-a51c-46f2f7cc7f5b&gt;
      -- Channel SIP/100-00000016 left 'native_rtp' basic-bridge &lt;dafb98ba-3e18-4c2e-a51c-46f2f7cc7f5b&gt;
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; 200 result=-1
  &lt;SIP/100-00000016&gt;AGI Rx &lt;&lt; GET VARIABLE ANSWEREDTIME
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; 200 result=1 (6)
  &lt;SIP/100-00000016&gt;AGI Rx &lt;&lt; HANGUP
  &lt;SIP/100-00000016&gt;AGI Tx &gt;&gt; 200 result=1
      -- &lt;SIP/100-00000016&gt;AGI Script agi.php completed, returning 4
    == Spawn extension (call-out, 6666, 1) exited non-zero on 'SIP/100-00000016'
  </code></pre>
  <br>
  Console output when caller hangup:
  <br>
  <pre><code>== Using SIP RTP CoS mark 5
         &gt; 0x7fabe000deb0 -- Strict RTP learning after remote address set to: 176.213.58.47:7078
      -- Executing [6666@call-out:1] AGI(&quot;SIP/100-0000001e&quot;, &quot;agi.php&quot;) in new stack
      -- Launched AGI Script /var/lib/asterisk/agi-bin/agi.php
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_request: agi.php
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_channel: SIP/100-0000001e
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_language: ru
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_type: SIP
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_uniqueid: 1594145890.45
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_version: 16.11.1
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_callerid: 100
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_calleridname: Number 100
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_callingpres: 0
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_callingani2: 0
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_callington: 0
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_callingtns: 0
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_dnid: 6666
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_rdnis: unknown
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_context: call-out
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_extension: 6666
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_priority: 1
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_enhanced: 0.0
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_accountcode:
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; agi_threadid: 140374906681088
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt;
  &lt;SIP/100-0000001e&gt;AGI Rx &lt;&lt; ANSWER
         &gt; 0x7fabe000deb0 -- Strict RTP switching to RTP target address 176.213.58.47:7078 as source
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; 200 result=0
  &lt;SIP/100-0000001e&gt;AGI Rx &lt;&lt; EXEC Dial SIP/101
      -- AGI Script Executing Application: (Dial) Options: (SIP/101)
    == Using SIP RTP CoS mark 5
      -- Called SIP/101
      -- SIP/101-0000001f is ringing
         &gt; 0x7fac0000c120 -- Strict RTP learning after remote address set to: 176.213.58.47:8000
      -- SIP/101-0000001f answered SIP/100-0000001e
      -- Channel SIP/101-0000001f joined 'simple_bridge' basic-bridge &lt;e8f305f8-5d84-4ae7-8be5-babcdefdb5ef&gt;
      -- Channel SIP/100-0000001e joined 'simple_bridge' basic-bridge &lt;e8f305f8-5d84-4ae7-8be5-babcdefdb5ef&gt;
         &gt; Bridge e8f305f8-5d84-4ae7-8be5-babcdefdb5ef: switching from simple_bridge technology to native_rtp
         &gt; Locally RTP bridged 'SIP/100-0000001e' and 'SIP/101-0000001f' in stack
         &gt; 0x7fac0000c120 -- Strict RTP switching to RTP target address 176.213.58.47:8000 as source
      -- Channel SIP/101-0000001f left 'native_rtp' basic-bridge &lt;e8f305f8-5d84-4ae7-8be5-babcdefdb5ef&gt;
      -- Channel SIP/100-0000001e left 'native_rtp' basic-bridge &lt;e8f305f8-5d84-4ae7-8be5-babcdefdb5ef&gt;
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; 200 result=-1
  &lt;SIP/100-0000001e&gt;AGI Rx &lt;&lt; GET VARIABLE ANSWEREDTIME
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; 200 result=1 (4)
  &lt;SIP/100-0000001e&gt;AGI Rx &lt;&lt; HANGUP
  &lt;SIP/100-0000001e&gt;AGI Tx &gt;&gt; 200 result=1
      -- &lt;SIP/100-0000001e&gt;AGI Script agi.php completed, returning 4
    == Spawn extension (call-out, 6666, 1) exited non-zero on 'SIP/100-0000001e'
  </code></pre>
  <br>
  As a result when caller hangup there is no file /tmp/test.txt, but when callee hangup there is. Where is problem?
  Asterisk 16 LTS builded from source
  PHP 7.2.24-0ubuntu0.18.04.6