Edit report at http://bugs.php.net/bug.php?id=54062&edit=1

 ID:                 54062
 Comment by:         james dot mk dot green at gmail dot com
 Reported by:        james dot mk dot green at gmail dot com
 Summary:            PHP does not notice user abort
 Status:             Open
 Type:               Bug
 Package:            Network related
 Operating System:   Linux/Windows
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Feb 21 16:28:57 blofeld apache2: The connection remains

Feb 21 16:29:41 blofeld apache2: Connection opened

Feb 21 16:29:43 blofeld apache2: Completed usleep()

Feb 21 16:29:43 blofeld apache2: ob_end_flush() called

Feb 21 16:29:43 blofeld apache2: Have flushed()

Feb 21 16:29:43 blofeld apache2: Echo completed

Feb 21 16:29:43 blofeld apache2: Have slept

Feb 21 16:29:43 blofeld apache2: Shutdown detected.

Feb 21 16:29:43 blofeld apache2: The result of connection_status() is:
0

Feb 21 16:29:43 blofeld apache2: The connection remains



That's what I get using Firefox with PHP 5.3.3-1ubuntu9.3 on apache
2.2.16-1ubuntu3.1 using ubuntu Ubuntu 10.10.



All I do is type in the url of my disconnect.php, hit enter then
immediately hit the stop button.


Previous Comments:
------------------------------------------------------------------------
[2011-02-21 17:11:06] cataphr...@php.net

Works fine here:



Normal circumstances:

Feb 21 16:06:33 damnation apache2: Connection opened

Feb 21 16:06:35 damnation apache2: Completed usleep()

Feb 21 16:06:35 damnation apache2: ob_end_flush() called

Feb 21 16:06:35 damnation apache2: Have flushed()

Feb 21 16:06:35 damnation apache2: Echo completed

Feb 21 16:06:35 damnation apache2: Have slept

Feb 21 16:06:35 damnation apache2: Shutdown detected.

Feb 21 16:06:35 damnation apache2: The result of connection_status() is:
0

Feb 21 16:06:35 damnation apache2: The connection remains



Stop button:

Feb 21 16:06:44 damnation apache2: Connection opened

Feb 21 16:06:46 damnation apache2: Completed usleep()

Feb 21 16:06:46 damnation apache2: ob_end_flush() called

Feb 21 16:06:46 damnation apache2: Have flushed()



ignore_user_abort(true) + Stop button:

Feb 21 16:07:59 damnation apache2: Connection opened

Feb 21 16:08:01 damnation apache2: Completed usleep()

Feb 21 16:08:01 damnation apache2: ob_end_flush() called

Feb 21 16:08:01 damnation apache2: Have flushed()

Feb 21 16:08:01 damnation apache2: Echo completed

Feb 21 16:08:01 damnation apache2: Have slept

Feb 21 16:08:01 damnation apache2: Shutdown detected.

Feb 21 16:08:01 damnation apache2: The result of connection_status() is:
1

Feb 21 16:08:01 damnation apache2: The connection has aborted



(Ubuntu 10.10, PHP 5.3.3-1ubuntu9.3, Apache/2.2.16)

------------------------------------------------------------------------
[2011-02-21 16:50:09] james dot mk dot green at gmail dot com

The script attached is a test case. The "real script" that triggered
this report does not use sleep or similar. And yes, on two-three
attempts out of several dozen I have seen a client has aborted message.
However, statistically this is beyond unreliable.



Further to the original report I have spoken in ##php on Freenode and
tried their suggestions without any luck.



The "real script" does quite a lot of backend processing while a
software client waits. If the client then times out waiting, the server
script continues to send back the data processed and says that the
client remains connected at the very end (within the shutdown callback).
This led me to my testing of connection handling and this test case.

------------------------------------------------------------------------
[2011-02-21 16:19:04] ceo at l-i-e dot com

"I have never known this to be the case."

...

$usleep = 2000000;

...

usleep($usleep);

...

    if (connection_aborted()) {

        // This almost never happens!

        syslog(LOG_DEBUG, 'The connection has aborted');

    } else {



"almost never" or "never"?



Not really the same...

Not sure PHP even processes and abort in the middle of usleep.

2 seconds is a long time in a normal request.

Try shorter usleep.

And when you *do* get an abort, call exit to really really really END
the script, bypassing any other shutdown functions.

------------------------------------------------------------------------
[2011-02-21 15:44:03] james dot mk dot green at gmail dot com

Description:
------------
Referencing http://php.net/manual/en/features.connection-handling.php



I understand that providing PHP is writing to or reading from the web
server connection, a registered shutdown function should be called
should the client abort.



I have never known this to be the case.



I attach a simple script. It attempts to sleep then write back to the
client. It has a registered shutdown function that checks
connection_status() and connection_aborted(). According to my syslog
however, the full script executes followed by the shutdown function
which says the user is still connected, despite my pressing the browser
stop button immediately after placing the request.



I have tested this on Windows using Lighttpd with PHP 5.2, and on Ubuntu
with Apache preforking PHP 5.3. Both exhibit the same behaviour.



These tests were conducted with ignore_user_abort(true) and (false) - no
difference observed so I removed it.



Test script:
---------------
<?php

syslog(LOG_DEBUG, 'Connection opened');

register_shutdown_function('shutdown');

$usleep = 2000000;

for ($i=0; $i < 100; $i++) {

    $str[$i] = '';

    for ($x=0; $x < 1000; $x++) {

        $str[$i] .= 'flubber';

    }

}

if (isset($_REQUEST['usleep'])) {

    $usleep = (int) $_REQUEST['usleep'];

}

usleep($usleep);

syslog(LOG_DEBUG, 'Completed usleep()');

echo "Thanks for waiting\n";

print_r($str);

ob_end_flush();

syslog(LOG_DEBUG, 'ob_end_flush() called');

flush();

syslog(LOG_DEBUG, 'Have flushed()');

// We get to this part regardless of having already pressed STOP

function shutdown()

{

    echo "Thanks for waiting\n";

    flush();

    syslog(LOG_DEBUG, 'Echo completed');

//    sleep(1);

    syslog(LOG_DEBUG, 'Have slept');

    syslog(LOG_DEBUG, 'Shutdown detected.');

    syslog(LOG_DEBUG, "The result of connection_status() is: " .
connection_status());

    // The above is almost always 0

    if (connection_aborted()) {

        // This almost never happens!

        syslog(LOG_DEBUG, 'The connection has aborted');

    } else {

        // This almost always happens!

        syslog(LOG_DEBUG, 'The connection remains');

    }

}



Expected result:
----------------
syslog entries should stop when PHP writes to the client socket and
notes that the client has sent an abort signal. Then, the shutdown
function should show that the connection_status() is no longer 0, and
that the connection_aborted() method returns true.

Actual result:
--------------
syslog entries continue to Have flushed(), then show that "The result of
connection_status() is: 0." and "The connection remains."



This should not be the case. Perhaps Apache is not sending the
appropriate signal, yet neither is Lighttpd..?


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=54062&edit=1

Reply via email to