Agi completed with return code 4 but not execute competely

hello all,
why sometimes agi files failed to execute without any certain error.
i use bellow agi in many parts of my dialplan :
exten => _xxxx.,n,agi(dial_connect_ack-logger.php,{ivrkey},{calllogid},{callid},{uniqi},{actionid},{meta},{exten},{clientnumber},${app},-6)

in each call , this agi executed about 6 times, in some call there is no problem and every things works well
but sometimes, in a call 4 times that agi executes, every thing is ok but for the 5th time that agi wants to execute , it failed to run completely and returned back with code 4 :

pbx.c: Executing [s@queueack:1] AGI(“Local/1280@queue-agent-00006d5d;2”, “dial_qconnect_ack-logger.php,0,10228742,10228751,1562161619.152338,5728445729098546782,1280,8082,queue,-7,1650,9100,start”) in new stack

VERBOSE[32422][C-0000393b] res_agi.c: Launched AGI Script /var/lib/asterisk/agi-bin/dial_qconnect_ack-logger.php

VERBOSE[32422][C-0000393b] res_agi.c: dial_qconnect_ack-logger.php,0,10228742,10228751,1562161619.152338,5728445729098546782,1280,8082,queue,-7,1650,9100,start: uniq==>1562161619.152338

VERBOSE[32422][C-0000393b] res_agi.c: <Local/1280@queue-agent-00006d5d;2>AGI Script dial_qconnect_ack-logger.php completed, returning 4

as you can see it execute just first app in agi, verbose uniq : uniq==>1562161619.152338
but didn’t complete it and just return and continue dialplan , then after some lines of diaplan it will execute this agi again for the 6th time , this time it works completely.

ofcourse this numbers (problem in 5th time) are not static, sometime this agi didn’t work for the fisrt time:

#!/usr/bin/php -q
<?php
require('phpagi.php');
require('/var/lib/asterisk/agi-bin/function-AGI.php');
error_reporting(E_ALL);

$agi = new AGI();

$url_ack = 'http://10.1.10.44:11254/api/v1/call/calls/call-action-ack';
$url_token = 'http://10.1.10.44:11257/api/ca/v1/common/oauth2/token';

$ivrkey = $agi->request['agi_arg_1'];
$calllogid = $agi->request['agi_arg_2'];
$callid = $agi->request['agi_arg_3'];
$uniq = $agi->request['agi_arg_4'];
$actionid = $agi->request['agi_arg_5'];
$meta = $agi->request['agi_arg_6'];
$ext = $agi->request['agi_arg_7'];
$queue = $agi->request['agi_arg_8'];
$app = $agi->request['agi_arg_9'];
$actionid = $agi->request['agi_arg_10'];
$code = $agi->request['agi_arg_11'];
$type = $agi->request['agi_arg_12'];
$mode = $agi->request['agi_arg_13'];

$uniq = $agi->get_variable("uniqi");
$uniq = $uniq['data'];
$agi->verbose("uniq==>".$uniq."");

//LOG
$log_msg = $uniq.'--'.$callid.'--- start of dial_qconnect_ack.php';
$log = logger($log_msg);

$token = $agi->get_variable("DB(CA/token)");
$token = $token['data'];
$ackdata = array(
                'Extension' => $ext,
                'QueueName' => $queue,
                'QueueAgentCallStatus' => $code
                );

                $ackdata = json_encode($ackdata);

                $app = 'queue-7';

/////////////*Start action-ack api call in dial Connect
$ackcall = ackcall($ivrkey,$callid,$calllogid,$uniq,$actionid,$meta,$url_ack,$url_token,$token,$app,$mode,$type,$cid,$did,NULL,$ackdata);

if ($ackcall['status'] == 200) {
                $agi->verbose("*****".$callid."******* Call Action Ack Sent To CA SUCCESSFULLY *****when CONNECT in ".$app."******");

}else{
                $agi->verbose("*****".$callid."*******call action ack NOT respoding*****when CONNECT in ".$app."******");

}

if ($type == 9100) {
$agi->exec("Set __queueack=yes");
}else{
$agi->exec("Set __queueack=no");
}

?>

i tested agi , it has no problem.

how can i solve it :anguished:

The returned value is read from the AGI, so the AGI script must have actively sent it. Your problem would seem to be in the script, or the class library, not in Asterisk.

but why this happens just sometimes and not all the times

Looks like you do a remote request using the ackcall() function, I dont know what that function does, as it is included on /var/lib/asterisk/agi-bin/function-AGI.php file, but mybe there is a time out response from the remote server and that’s why the issue