AGI Problem

Home » Asterisk Users » AGI Problem
Asterisk Users 6 Comments

On Sep 23, 2011, at 8:01 PM, Mehmet Avcioglu wrote:
> I have an AGI script that occasionally disappears without completing its action and asterisk logs the following.
> AGI Script script.php completed, returning 4
> Spawn extension (context, 0123456, 2) exited non-zero on ‘Local/0123456@context-f46e;1’

I also changed the dialplan and added a line to print AGISTATUS, but when this “returning 4” happens, asterisk stops there and doesn’t execute any further dialplan actions, so I don’t even see AGISTATUS value.

exten => h,1,AGI(script.php,${ANSWEREDTIME},,,)
exten => h,n,NoOp(${AGISTATUS})

Executing [h@context:1] AGI(“Local/0123456@context-4b79;1”, “script.php,13,,,”) in new stack
AGI Script script.php completed, returning 4
Spawn extension (context, h, 1) exited non-zero on ‘Local/0123456@context-4b79;1’

6 thoughts on - AGI Problem

  • Thats wicked !! hmmm stop your asterisk (if u can afford) and run it like
    “asterisk -cvvvvvg” and then make a call.. see whats your AGI doing in there

  • Thanks for the response. Although I had already gone thorough a lot of these types of QAs during my own problem solving, your suggestions did bring a couple that I had not.

    The program is written in a very top to bottom way (himm stateless? non-oo? not sure what to call it) and would be really hard for me to catch those and figure out where it happened and clean things up. So I have been reluctant to do this. But it is one of those tests that I’ll eventually hit if I cannot solve the problem.

    I was doing extensive logging in my scripts and had turned on full logging for asterisk but I had not turned agi debuging on. I did and carefully inspected the output. And there is nothing out of ordinary between successful executions and unsuccessful ones.

    I am using my own. Yes agreed, but this doesn’t even interact with asterisk at all. Just receives a couple values and updates jobs, database accordingly. Does not return anything back to asterisk. Yes executes fine outside of asterisk and even executes fine within asterisk 99.9% of the time.

    I have not been able to find any difference.

    So “returning 4” doesn’t mean anything special. It is just the return code received from the AGI application that was forked. What about the fact that asterisk does not continue with the dialplan after receiving 4, is that expected behavior?
    I have not been able to find what would make a php cli application exit with 4 other than exit(4). php does not log anything when this happens, doesn’t write anything to stderr (I specifically remained on the console I started * to see stderr). So this can only be a crash of some sort of php cli binary. What type of a crash would cause it to return 4?

    I will keep digging.


  • All you need to do is set a signal handler and call syslog() to log the
    occurrence in a ‘safe’ place and then exit().. If the signal is already
    causing your script to abort, you’re not doing any cleanup or recovery
    anyway. All we want to establish is if you are actually receiving a

    Are you reading the AGI environment in the script?

    How are you passing variables from Asterisk? As channel variables or as
    command line parameters?

    What is the dialplan statement that executes your script?

    Can you post the script source?

    If you captured the AGI dialog from a call when your script failed and you
    feed the same values (including the AGI environment) into your script from
    a shell does it fail?

    On a Unix system level, a 4 means ‘Interrupted system call’ — assuming
    this is what your ‘4’ signifies. BTW, I’m a 1.2 Luddite and the AGI return
    value was meaningless back then.

  • Danny’s suggestion of using System instead of AGI was correct, it was wrong of me to use AGI here as there is no communication. The same script does communicate with asterisk when executed with different options, so I had just kept the same line using different options at different places.

    Today I changed the AGI calls to System at places where there is no response back to asterisk. Kept everything the same, instead of AGI(script.php,var1,var2) made it System(/path/script.php “var1” “var2”) and it worked without a problem. This way I have been able to fix the problem where this caused a deadlock requiring human intervention.

    The lines in the dialplan that continue to use the AGI call still experience the same problem as before. Today it happened 9 times out of 10486 executions, but these are not as fatal as before, so they can almost be ignored.


    Command line parameters, explained above.

    Yes but I really cannot see how the script could have a problem, it is around 2000 lines and since my problem has come to a more manageable level, I would rather not bug you with debugging the whole thing:)

    Yes it runs just fine. It even runs fine at different times from within the asterisk using AGI when the same number calls or is called. So the only variable that is different between executions is time.

    Running these on 1.8.5

    I’ll migrate this setup to a different environment, perhaps try at different asterisk and try the signal catching mentioned before and if I get any more data will update the list.

    Thanks a lot.

  • This is not a ‘fix.’

    All this strongly points at your implementation of the AGI protocol.

    So now the question becomes ‘Is it easier to debug my own private AGI
    library or should I switch to a more mature implementation?’

  • Hello All,

    I need to use agi to handle some issue , after finishing agi i want to hang up the channel , if i call from an extension there is no problem but i want to be the same for PSTN (outside) caller , if someone call asterisk show the hang up channel but the caller is not disconnected and if meanwhile someone inside try to call from an extension the outide caller can listen to DTMF and everything . . . . I would be really grateful if you share your close experience .

    Regards, Mehdi