Python script in AGI failing

Hi,

I am using AGI and have a python test script to test as below:

#!/usr/bin/python3

from asterisk.agi import AGI
import os
import traceback

log = open("/tmp/agi_try.log", "w")
os.dup2(log.fileno(), 1) 
os.dup2(log.fileno(), 2)  

try:
    print("Starting AGI script", flush=True)
    agi = AGI()
    agi.verbose("Python AGI is working!", 1)
    agi.stream_file("beep")
    agi.verbose("Beep playback attempted.", 1)
    print("Script completed", flush=True)

except Exception:
    print("Exception occurred:", flush=True)
    traceback.print_exc()

but when i try to run this (by dialing 999 from my phone), i get the logs in asterisk CLI:

-- Executing [999@default:1] Answer("SIP/1*8710-00000007", "") in new stack
       > 0x7fea3804cd40 -- Strict RTP switching to RTP target address 10.10.6.6:12470 as source
    -- Executing [999@default:2] AGI("SIP/1*8710-00000007", "/var/lib/asterisk/agi-bin/test_agi.py") in new stack
    -- Launched AGI Script /var/lib/asterisk/agi-bin/test_agi.py
    -- <SIP/1*8710-00000007>AGI Script /var/lib/asterisk/agi-bin/test_agi.py completed, returning 0
    -- Executing [999@default:3] Hangup("SIP/1*8710-00000007", "") in new stack
  == Spawn extension (default, 999, 3) exited non-zero on 'SIP/1*8710-00000007'

And the agi_try.log file is empty in /tmp folder.

my dialplan looks like:

exten => 999,1,Answer()
 same => n,AGI(/var/lib/asterisk/agi-bin/test_agi.py)
 same => n,Hangup()

Everything was working well but suddenly it stopped. I have even tried redirecting the logs as well (as seen in my script above) but still the AGI fails immediately now.

Can someone point me in the right direction of what the issue could be?

This will break AGI.

HI @david551

can you please guide what should I do? I have removed those lines as you suggested.

AGI is still breaking..

with agi set debug on:

  -- Executing [999@default:1] Answer("SIP/1*8710-0000000e", "") in new stack
       > 0x7fea38053e70 -- Strict RTP switching to RTP target address 10.10.6.6:12512 as source
    -- Executing [999@default:2] AGI("SIP/1*8710-0000000e", "/var/lib/asterisk/agi-bin/test_agi.py") in new stack
    -- Launched AGI Script /var/lib/asterisk/agi-bin/test_agi.py
<SIP/1*8710-0000000e>AGI Tx >> agi_request: /var/lib/asterisk/agi-bin/test_agi.py
<SIP/1*8710-0000000e>AGI Tx >> agi_channel: SIP/1*8710-0000000e
<SIP/1*8710-0000000e>AGI Tx >> agi_language: en
<SIP/1*8710-0000000e>AGI Tx >> agi_type: SIP
<SIP/1*8710-0000000e>AGI Tx >> agi_uniqueid: mztestvm-1743690352.42
<SIP/1*8710-0000000e>AGI Tx >> agi_version: 18.22.0
<SIP/1*8710-0000000e>AGI Tx >> agi_callerid: 8710
<SIP/1*8710-0000000e>AGI Tx >> agi_calleridname: test ai
<SIP/1*8710-0000000e>AGI Tx >> agi_callingpres: 0
<SIP/1*8710-0000000e>AGI Tx >> agi_callingani2: 0
<SIP/1*8710-0000000e>AGI Tx >> agi_callington: 0
<SIP/1*8710-0000000e>AGI Tx >> agi_callingtns: 0
<SIP/1*8710-0000000e>AGI Tx >> agi_dnid: 999
<SIP/1*8710-0000000e>AGI Tx >> agi_rdnis: unknown
<SIP/1*8710-0000000e>AGI Tx >> agi_context: default
<SIP/1*8710-0000000e>AGI Tx >> agi_extension: 999
<SIP/1*8710-0000000e>AGI Tx >> agi_priority: 2
<SIP/1*8710-0000000e>AGI Tx >> agi_enhanced: 0.0
<SIP/1*8710-0000000e>AGI Tx >> agi_accountcode: 1880076156
<SIP/1*8710-0000000e>AGI Tx >> agi_threadid: 140644190815936
<SIP/1*8710-0000000e>AGI Tx >>
<SIP/1*8710-0000000e>AGI Rx << Starting AGI script
<SIP/1*8710-0000000e>AGI Tx >> 510 Invalid or unknown command
<SIP/1*8710-0000000e>AGI Rx << VERBOSE "Python AGI is working!" 1
 /var/lib/asterisk/agi-bin/test_agi.py: Python AGI is working!
<SIP/1*8710-0000000e>AGI Tx >> 200 result=1
<SIP/1*8710-0000000e>AGI Rx << Exception occurred:
<SIP/1*8710-0000000e>AGI Tx >> 510 Invalid or unknown command
    -- <SIP/1*8710-0000000e>AGI Script /var/lib/asterisk/agi-bin/test_agi.py completed, returning 0
    -- Executing [999@default:3] Hangup("SIP/1*8710-0000000e", "") in new stack

This will also break it.

You cannot use standard out for anything other than AGI requests.

Is it possible to determine whether Asterisk is rerouting stderr for the AGI processes it spawns to a logfile, or another location?

the agi issue is intermittent…

As mentioned by others, don’t do this. (The redirection to FD 2, stderr, is fine.)

Or this sort of thing. Write your debug messages to stderr, because stdout is used for AGI communication with Asterisk. E.g.

sys.stderr.write("Starting AGI script\n")

And don’t do this either. Just let Python write the exception report itself to stderr and terminate your script. It can do it more reliably than you can.