Summary:ASTERISK-20061: PHP Agi Socket Server stopped working - socket_read spamming Resource temporarily unavailable
Reporter:Eike Maier (infy1801)Labels:
Date Opened:2012-06-27 07:07:44Date Closed:2012-08-10 10:36:39
Versions: Frequency of
is related toASTERISK-25635 run_agi() while() loop loops indefinitely because of fgets() returns EAGAIN
Environment:CentOS 5, kernel-2.6.238Attachments:( 0) debug.log
Description:Hi Developers,

I have a php agi socket server and various classes to extend my pbx. This socket server worked well with Ver.,
but after i updated to version it stopped working. The function socket_read threw the error 11(Ressource temporarily unavailable). I searched the mistake in the res_agi.c file and found that the following lines caused an infinite loop:

while (len > 1) {
   res = fgets(buf + buflen, len, readf);
   if (feof(readf))
   if (ferror(readf) && ((errno != EINTR) && (errno != EAGAIN)))
   if (res != NULL && !agi->fast)
   buflen = strlen(buf);
   if (buflen && buf[buflen - 1] == '\n')
   len = sizeof(buf) - buflen;
   if (agidebug)
       ast_verbose( "AGI Rx << temp buffer %s - errno %s\n", buf, strerror(errno));
Comparing the res_agi.c file from Ver. and, i changed the following back to Ver.

while (len > 1) {


while (buflen < (len - 1)) {


len = sizeof(buf) - buflen;


len -= buflen;

After i compiled these changes (fully recompiled asterisk in fact) and updated my pbx again and
my php agi socket server were fully working.

I don't understand correctly why the len is set to the size of the bytes for this datatype and substracting the lenght of buf string.


P.S: Please excuse my bad english...
Comments:By: Matt Jordan (mjordan) 2012-06-27 08:25:39.763-0500

This was changed to account for fgets failing with EAGAIN, in r302548.

In the res_agi source file, do you mind reverting your change, setting agidebug to 1, recompiling, and producing a DEBUG log illustrating the actions Asterisk takes when your socket disconnects with a resource unavailable error?

By: Eike Maier (infy1801) 2012-06-27 09:28:01.516-0500

This is a full debug log from my pbx with the original res_agi.c ;)

Removed some output (one call over about 5 sec wrote me a log with 340.000 lines... 70 MB)

By: Jonathan Rose (jrose) 2012-07-25 15:30:42.542-0500

Hey Eike, I'm not 100% positive on this, but it looks to me like you probably didn't send a newline character at the end of your AGI message to Asterisk.

I say this because:

from: AGI Rx << temp buffer SET VARIABLE VMACTIVE_FIRST ""

SET VARIABLE VMACTIVE_FIRST "" - looks like a complete AGI command, so if the errno becomes EGAIN here (which is what correlates with that Resource temporarily unavailable message), that means we are anticipating more stuff to enter that buffer, which means we haven't received either an EOF or a newline yet.

I think I'll go ahead and add a little note to the end of that to this message indicating that it still anticipates a newline, but if you could get back to me with either your server script or a confirmation that this is what's happening or that you know for sure this isn't what is happening, that would be very helpful.