php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30301 connection_status and connection_aborted not working.
Submitted: 2004-10-01 14:25 UTC Modified: 2005-03-15 01:00 UTC
Votes:56
Avg. Score:4.6 ± 0.6
Reproduced:53 of 53 (100.0%)
Same Version:11 (20.8%)
Same OS:7 (13.2%)
From: phpbug at zone-mr dot ath dot cx Assigned:
Status: No Feedback Package: IIS related
PHP Version: 5.0.2 OS: Windows 2003 Server
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-10-01 14:25 UTC] phpbug at zone-mr dot ath dot cx
Description:
------------
I have seen similar bug reports for Apache2 (where the issue is still unresolved, but none for IIS 6.0).

I have tried two different servers, and both PHP 5.02 and PHP4.39. Each server was running Windows 2003 with IIS 6 and exhibits the same symptons.

Connection_status reports 0, even when the stop button is pressed, and there is no browser listening to the script output. Connection_abort also returns false.

Reproduce code:
---------------
ignore_user_abort(???); //Tried both true and false, no difference

while (connection_status()==0) {
 sleep(1);
 echo "test\n";
 flush();
 ob_flush();

 ... //Log an "I'm still here" timestamp, for debugging
 
 if(connection_aborted()) {
  //Log a disconnection into a DB
  exit;
  }
 }
//Log a disconnection into a DB

Expected result:
----------------
When the browser window or connection is closed, I expect the script to stop logging the "I'm still there" message every second, and terminate execution.

Actual result:
--------------
The script continues to run, updating the timestamp every second, until the time limit is reached. Connection_aborted, connection_status seem to indicate the connection is still active (but it isn't - the browser is closed, netstat confirms that there are no connections).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-07 22:06 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-03-15 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2005-10-11 10:51 UTC] klimov at mmk dot ru
I have tried PHP 4.4.0 and 5.0.5 and latest cvs. My server is running on Windows 2003 with IIS 6. The functions connection_aborted and connection_status doesn't work properly too.
 [2005-10-18 22:16 UTC] ecarpenter at itex dot co dot za
I am having the same problem on SuSE Linux 9.3 running Linux 2.6.11.4-21.9-default, Apache 2.0.53 and PHP 4.3.10 (cli) (built: Aug 31 2005 16:14:46)

The script below can be used to recreate the problem. After I have press the Stop button (Firefox 1.0.7) the script continues to execute until it is killed 15 minutes later without the shutdown function being executed.

<?
function foo() {
	exec("echo '".date("Y/m/d H:i:s")." Terminating Status:".connection_status()."' >> /tmp/test");
}
register_shutdown_function('foo');

exec("echo '".date("Y/m/d H:i:s")." Starting' > /tmp/test");
$count=0;
while (true) {
	$count++;
	exec("echo '".date("Y/m/d H:i:s")." Running ".$count." Status:".connection_status()."' >> /tmp/test");
}
?>
 [2006-02-08 14:30 UTC] phpbug at paragontech dot com
Try to do a ob_end_flush(); at the top of your script.
 [2007-02-13 21:02 UTC] akravtsov at rogers dot com
I am having the same problem! I have tried everything. This has not been fixed since Oct 2004??

This is my code:


<?php
//ob_end_flush();
	//This should make it work:
	set_time_limit(0);
	ignore_user_abort(false);
	error_reporting(E_ALL);
	ini_set("max_execution_time", "0");
	
	$x = 0;
	$error = fopen("file.txt", "a");
	echo "Beginning neverending loop";
	
	//Alternate method
	register_shutdown_function("help");
	

	while($x < 40)
	{
	
	//ob_end_clean();
	
	//ob_start();
	
		$xx =  $x."__Connstat: ".connection_status()."Aborted? ".connection_aborted()."\r\n\r\n";
		echo $xx;
		fwrite($error, $xx, strlen($xx)); //This is written fine.
		$x++;
		flush();  //If I include this flush, the program stops when I hit the stop button. If I don't, it doesn't stop even if I hit the stop button.
		sleep(1);
	
		if(connection_status() != 0 || connection_aborted())
		{
			$msg = "QUIT";
			fwrite($error, $msg, strlen($msg));
			exit();

		} 
	}
	
	function help()
	{
		$f = fopen("test.txt", "a");
		$m = "QUIT";
		fwrite($f, $m, strlen($m));
		fclose($f);
		exit();
	}
	

?>
 [2007-04-19 05:14 UTC] benb at dpac dot tas dot gov dot au
I too am experiencing this problem with Apache 2.2.4 and PHP 5.2.1 on Ubuntu. Kernel 2.6. 

Using a similar script to the one listed above. Connection_status() returns 0, as does connection_aborted.

However I am streaming video. When streaming this amount of data, the connection does in fact hang as the image data can't make it to the client.

I added a shutdown function with register_shutdown_function() to verify this though. Several minutes after my on-disk log file stops recording data being sent, the shutdown function writes a message to the same log file to say that the script has finally been terminated.

So i'm assuming that apache is still trying to send data to the client even though they're no longer listening and it's the apache write_timeout that's finally killing the thread.

Because the script is effectively dead in the water while waiting for data to be received by the client, i can't put in any timeout code to compensate. So i'm about to try and put a set_time_limit(10) call before each data transmission so that an output hang is (hopefully) handled after 10 seconds rather than several minutes.

It would be great though if this was fixed. It would make life so much easier!
 [2007-06-01 16:50 UTC] mike at dodgeit dot com
How about fixing this bug thats been open since 2004!!!
 [2008-09-22 06:41 UTC] samedi at online dot de
I am having exactly the same problem! (Using PHP 5.2.6)

Four years and this bug does really still exist?
 [2008-10-27 17:06 UTC] ger_stingray427 at yahoo dot com
before year 2004 this function worked ?
maybe we request new feature and this function works, but not as expected for us.
 [2009-11-13 19:34 UTC] ricardo at haha dot com dot br
I had this problem years ago, now the problem came back after upgrading php... i tried everything i found and in the end the function ob_end_flush();  on top of script as post here solved the problem. Im using windows 7 x64 / php 5.2.4 / apache 2.2.14 (win32)

Thanks
 [2011-08-19 12:41 UTC] david dot schueler at wapkamera dot de
This problem still exists!
I use Apache2 and PHP 5.3.5-pl0-gentoo with Suhosin-Patch as apache module.

The script is use:
<?php
error_reporting(0);
ini_set('max_execution_time',300);

header('Content-Type: application/json');

echo json_encode(array('timestamp'=>time(),'status'=>'start'))."\n";

$ping = time();
while (true) {
	echo json_encode(array('timestamp'=>time(),'status'=>'trigger'))."\n";
	flush();
	if (connection_aborted()) exit;
	sleep(10);
}
die(json_encode(array('timestamp'=>time(),'status'=>'errorstop')));
?>

Look what Wireshark says about this:
TCP      34878 > 80 [SYN]
TCP      80 > 34878 [SYN, ACK]
TCP      34878 > 80 [ACK]
HTTP     GET /testl.php HTTP/1.1
TCP      80 > 34878 [ACK]
HTTP     HTTP/1.1 200 OK  (application/json)
TCP      34878 > 80 [ACK]
TCP      34878 > 80 [FIN, ACK] <== hitting of browsers STOP button
TCP      80 > 34878 [ACK]
HTTP     Continuation or non-HTTP traffic
TCP      34878 > 80 [RST]

As you can see, the script is stopped AFTER one more output line is sent to the browser. It responds with a RST packet, because the port is already closed.

So connection_aborted() is totally useless.

But this behavior is essential for use in server push cases, where one (AJAX) request establishes a connection and waits for long time to get data from the server.
But if the browser ends the connection, the script doesn't get informed about that and will loop forever in the worst case.

I suppose the Apache webserver does not inform the mod_php about the connection termination, or it's a PHP code issue.

Someone of the developers should really have a look at this!

David
 [2013-07-17 16:04 UTC] gustas dot domain at terra dot com dot br
This problem still exists! PHP Version 5.3.8
Firefox and android native browser OR when sleep functions is added on code (ALL browsers), I found this issue.

...
while (!feof($fp))) {
if ((connection_aborted()) || (connection_status() != 0)) {
break;
}
// usleep(100000); 
}
...
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Dec 03 08:03:33 2021 UTC