php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67381 Output buffers stop working after client disconnect
Submitted: 2014-06-04 13:27 UTC Modified: 2015-04-13 15:06 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: phpbugs at musiclogistics dot net Assigned: mike (profile)
Status: Closed Package: Output Control
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
 [2014-06-04 13:27 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-06-27 16:16 UTC] phpbugs at musiclogistics dot net
Sorry, the delivery of the extended test script was blocked by our server config. This is now fixed.
 [2014-06-30 13:09 UTC] mike@php.net
-Assigned To: +Assigned To: mike
 [2014-11-19 08:42 UTC] mike@php.net
This could be a regression introduced in PHP-5.4 with the new output API.
Hard to say, though, because the previous code is such a mess.
 [2014-11-19 09:06 UTC] mike@php.net
Related to Bug #64152
 [2015-04-13 15:06 UTC] mike@php.net
-Status: Assigned +Status: Closed
 [2015-04-13 15:06 UTC] mike@php.net
Behavior fixed in PHP-7. See https://wiki.php.net/rfc/continue_ob
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 15:01:25 2019 UTC