Edit report at https://bugs.php.net/bug.php?id=64644&edit=1
ID: 64644
Comment by: matteosistisette at gmail dot com
Reported by: matteosistisette at gmail dot com
Summary: Client-side abortion should be detected regardless
of sending output
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Linux
PHP Version: 5.3.24
Block user comment: N
Private report: N
New Comment:
Unbelievable. Then the huge design flaw is in Apache.
Previous Comments:
------------------------------------------------------------------------
[2013-04-13 21:59:18] [email protected]
Apache handles the communication with the client browser and provides no
mechanism for PHP to discover a client abort except on an actual write.
------------------------------------------------------------------------
[2013-04-13 21:56:04] matteosistisette at gmail dot com
Ok, I see. The problem is that I hadn't disabled gzip in Apache, so the output
wasn't being flushed until the end. As documented:
"""PHP will not detect that the user has aborted the connection until an
attempt
is made to send information to the client. Simply using an echo statement does
not guarantee that information is sent, see flush()."""
However, this is a huge design flaw. PHP definitely should be able to detect
the
client's abortion when it happens and not the next time it attempts to actually
send output. Having to change the way output is handled just in order to be
able
to detect client disconnection is ridiculous (and not always feasible).
I don't know if the server API currently provides a way for the server (i.e.
Apache) to inform PHP of the client's disconnection, but if there is not it
must
be provided.
------------------------------------------------------------------------
[2013-04-13 21:52:32] [email protected]
What you are missing is that your single-character output is getting buffered.
There is no way to detect a client abort until you actually try to send
something
to the client either by filling the output buffer and having it send
automatically or by forcing it by calling flush() after your echo. So in your
test case, add a flush() call after your echo "." and you will see it works as
documented.
------------------------------------------------------------------------
[2013-04-13 18:55:46] matteosistisette at gmail dot com
Description:
------------
PHP Version 5.3.3-7+squeeze14 (for some reason it is not in the dropdown)
I have read and deeply understood these:
http://www.php.net/manual/en/features.connection-handling.php
http://www.php.net/manual/en/function.register-shutdown-function.php
However, neither in 5.1.6 nor in 5.3.3 do things work even close to described
there:
- connection_status() always return true, even after the client has closed the
connection.
- execution of the script keeps going on after the client has closed the
connection, even though ignore_user_abort is 0
- a function registered with register_shutdown_function() is not run until the
script reaches ends. The script is NOT interrupted (and hence the function not
called) when the client aborts the connection.
So basically PHP just doesn't detect the client's disconnection AT ALL.
Note that this is NOT as if ignore_user_abort was set to 1: if that was the
case
then connection_status() would return 1 even though the script would keep
running and the shutdown function would not be called until the end. That is
not
the case.
ini_get("ignore_user_abort") returns 0, as expected.
I'm observing this both on PHP 5.1.6 on CentOS 5.9, and on PHP 5.3 on Debian
6.0.3
Steps to reproduce:
- put the script below in the public_html folder
- tail -f /path/to/error_log
- visit the url of the php script from a browser or client
- hit the stop button in the browser or abort connection from the client
- watch the error_log
Test script:
---------------
<?php
function myShutdown() {
error_log("myShutdown ".connection_status()."
".ini_get("ignore_user_abort"));
}
register_shutdown_function(myShutdown);
echo "Hi!";
error_log(" *** test/test *** ");
for ($i=0; $i<10; $i++) {
sleep(1);
error_log(".");
echo ".";
}
?>
Expected result:
----------------
Expected:
at the moment when the client abort connection, you should stop seeing new "."
in
the log, and you should see immediately: myShutdown 1 0
Actual result:
--------------
Observed:
Exactly 10 dots (".") appear one at every second, even if the client aborts
connection before the 10th dot is reached, and only at the end you see
"myShutdown 0 0"
This differs with the documented behavior. Everything consistently behaves as
if
connection from the client was NEVER aborted.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64644&edit=1