AGI doesn't work after hang up

Hello there!!

I have a trouble with AGI. My script doesn’t work after hang up but it works very fine before hang up. The script contains 2 part, the first part is getting the key press from the user and validate that code. If the code is correct then some voice message will be played. The second part is about to update the code status from a database after hang up. The dial plan is shown below.

[promotion_code]
exten => _000X.,1,Answer
exten => _000X.,n,AGI(promotion_checking.php)
exten => _000X.,n,Hangup

Here my script

#!/usr/bin/php -q
<?
	declare(ticks = 1);
	set_time_limit(60);
        include("phpagi.php");
	include("sql.php");
	include("promotion.php");
	$AGI = new AGI();
	$db = new AGIDB($AGI);
	pcntl_signal(SIGHUP,"sig_handler");
	$isPromotionIsCorrect = false;

	switch ($AGI->request[promotion_code]) {
		case 'promotion_code':
			$keyPress = $AGI->request[agi_extension];
			list($featuresCode,$promotionCode) = explode("*",$keyPress);
			// validatePromotionCode is a function in promotion.php that return true of false
			$isPromotionIsCorrect = validatePromotionCode($promotionCode);
			if($isPromotionIsCorrect)
			{
				$streamFile = 'promotion-code-correct';
			}
			else
			{
				$streamFile = 'promotion-code-incorrect';
			}
			$AGI->stream_file($streamFile);
			$AGI->hangup();
			exit;
		break;
	}

	function sig_handler($signo)
	{

		global $isPromotionIsCorrect,$promotionCode;
		if($isPromotionIsCorrect)
		{
			// updateCode is a function in promotion.php that will update the code
			updateCode($promotionCode);
		}
		exit;
	}
?>

Ok then I test, everything is very fine the voice message is correct for both cases. But after hang up, my database is no update …

Now I look at CLI log, it had some message look like this…
Autodestruct on dialog '451794437-5060-2@BJC.BGI.B.HC’ with owner in place (Method: BYE)

I google for it and some said “It seems that we had a channel held open that Asterisk couldn’t destroy.”, after recheck with “core show channels”, it is true that channel is still open even the call doesn’t hang up. Therefore SIGHUP is not send to AGI script.

Have anyone ever met this kind of issue before? Please suggest

I’m using Asterisk 1.8.6.0