|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-10-29 14:59 UTC] sivann at gmail dot com
Description:
------------
connection_status(), connection_aborted () never detect closed remote connection.
After closing connection initiated from client (e.g. wget) connection state becomes CLOSE_WAIT from ESTABLISHED, but those two functions called in a loop never detect that, even when outputing data.
Current solution is to popen netstat, or parse /proc/net/tcp, /proc/net/tcp6 to find if connection is established.
connection_status/connection_aborted should detect the connection loss even without writing anything. Currently those functions do nothing.
Test script:
---------------
<?php
ignore_user_abort(false);
function fecho($str) {
file_put_contents("conlog.txt",$str."",FILE_APPEND);
}
while (1) {
sleep (1);
$d=date(DATE_RFC2822);
$remport=$_SERVER['REMOTE_PORT'];
$remip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
file_put_contents("conlog.txt","date:$d\tip:$remip\tport:$remport\tstatus:$constatus\n",FILE_APPEND);
echo "...\n";
flush();
if (connection_status()!= CONNECTION_NORMAL){ //never works
file_put_contents("conlog.txt","connection closed\n",FILE_APPEND);
break;
}
file_put_contents("conlog.txt","connection_status=".connection_status().
" connection_aborted():".connection_aborted()."\n",FILE_APPEND);
}
?>
Expected result:
----------------
The above writes on a file named conlog.txt. Call the above script with wget. The script should start writing data to conlog.txt at the server. Then press Ctrl+C to stop wget. The script should end.
Actual result:
--------------
The script never stops running.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 03:00:01 2025 UTC |
It seems adding an ob_flush() after flush() fixes it: This works: <?php ignore_user_abort(true); $remport=$_SERVER['REMOTE_PORT']; $remip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']); file_put_contents("conlog.txt","date:$d\tip:$remip\tport:$remport\n",FILE_APPEND); while (1) { sleep (1); $d=date(DATE_RFC2822); echo "...\n"; ob_flush(); flush(); file_put_contents("conlog.txt","connection_status=".connection_status(). " connection_aborted():".connection_aborted()."\n",FILE_APPEND); if (connection_status()!= CONNECTION_NORMAL){ //never works file_put_contents("conlog.txt","connection closed\n",FILE_APPEND); ob_flush(); flush(); break; } } Maybe a doc edit would be needed to clarify this. Moreover it would be easy for connection_status() and connection_aborted() to return connection status without outputting data to the client, by examining /proc/net/tcp (in linux) as we already have remote IP and port. Very useful to know when to abort long-poll requests. But this would be an another improvement ticket I presume.