Hello,
This is a rather technical issue that I thought might be most appropriate for the development forum.Please let me know if it’s best logged elsewhere or as an Asterisk bug.
We have an issue with the answer of a call when using Asterisk AGI and forking child processes. Inbound calls route to an AGI, which then sends the call to a telephone using Dial. The Dial also says to run a macro when the call is answered, and the macro runs an AGI. If the AGI completes quickly then there’s no problem - when the destination telephone answers they can talk with the caller, and all is well.
However if the AGI run on call answer forks a child process, and the child process is delayed in exiting for some reason, then even when the destination telephone has answered the call, the fact that the call is answered is not relayed back to the caller until the child process exits. Below is a link to a program which reproduces the problem as simply as possible. In production our AGI makes an HTTP POST request, but in the sample program it just does a sleep for 10 seconds.
You’ll see that the child process does a double fork, which is the standard Linux method of orphaning a process so that it’s adopted by init and no longer belongs to the original parent. We even run setpgrp() and setsid() to try and ensure the child is in a separate process group and session, but this doesn’t seem to help.
Somehow despite the double fork Asterisk knows that the child process exists, and waits for it to exit before passing on the call answer event to the caller. The question is, how does Asterisk know this, and how can we prevent it? Thank you in advance!
Here is a link to the AGI program: fork.pl - Google Drive
The Asterisk contexts used are:
[call-start]
exten => _., 1, AGI(/usr/local/bin/fork.pl)
[call-answered]
exten => _., 1, AGI(/usr/local/bin/fork.pl)