Bug #67381 [Com]: Output buffers stop working after client disconnect
| From: | phpbugs at musiclogistics dot net | Date: | Fri, 27 Jun 2014 16:16:55 +0000 |
| Subject: | Bug #67381 [Com]: Output buffers stop working after client disconnect | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186365@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67381&edit=1
ID: 67381
Comment by: phpbugs at musiclogistics dot net
Reported by: phpbugs at musiclogistics dot net
Summary: Output buffers stop working after client disconnect
Status: Open
Type: Bug
Package: Output Control
Operating System: all
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Sorry, the delivery of the extended test script was blocked by our server config. This is now fixed.
Previous Comments:
------------------------------------------------------------------------
[2014-06-04 13:27:22] phpbugs at musiclogistics dot net
Description:
------------
Capturing output as a string with an output buffer stops working
as soon as PHP has detected a client disconnect. The buffer is always empty, regardless of whether
something was "echoed" into it.
Verified with different PHP 5.4 and 5.5 builds on Linux and Win7 with Apache 2.2, Apache 2.4 and
PHP-FPM SAPIs.
While this behaviour may be intentional (although not documented to my knowledge), it may break
common use cases.
Writing into an output buffer and then grabbing the contents is what e.g. the popular template
engine Twig does when rendering a template.
A common case when this might happen after client disconnect is when Twig is used to render the body
of an e-mail at the end of a long-running request.
A more comprehensive test script which also demonstrates a related GD bug can be found here: http://www.musiclogistics.net/phpbugs/write_to_ob_after_disconnect.phps
Test script:
---------------
// Prevent script from exiting when client disconnects
ignore_user_abort(true);
// Flush & close all output buffers to make the echo/flush calls
// below send data to the client immediately
while (ob_get_level()) { ob_end_flush(); }
// Wait until client has closed connection
while (!connection_aborted()) {
echo "Abort the request in your browser now by pressing <Esc> or clicking the little X
in the adress bar...\n";
flush();
sleep(1);
}
ob_start();
echo 'Hello World';
$captured_output = ob_get_clean();
file_put_contents(__DIR__ . '/ob-bug-test.log', strlen($captured_output));
Expected result:
----------------
11 (in the log file 'ob-bug-test.log')
Actual result:
--------------
0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67381&edit=1