php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68911 ob_flush() works only when producing notice
Submitted: 2015-01-25 17:39 UTC Modified: 2015-01-29 14:06 UTC
From: david at davidsteinsland dot net Assigned:
Status: Not a bug Package: Output Control
PHP Version: 5.5.21 OS: OSX Yosemite
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: david at davidsteinsland dot net
New email:
PHP Version: OS:

 

 [2015-01-25 17:39 UTC] david at davidsteinsland dot net
Description:
------------
This is a weird issue, but I'm trying to setup a basic Server Sent Event example.

The goal is to flush the messages to the browser on each event.

When viewing the script in my browser, or testing it using the JavaScript API, the messages seem to be flushed correctly. However, I notice that the script produces an error:
Notice (8): ob_flush() [ref.outcontrol]: failed to flush buffer. No buffer to flush

If I try to "fix" that notice by putting the shutup operator in front of ob_flush(), or putting it inside an "if (ob_get_level())" statement, the messages are now longer flushed properly. The browser and JavaScript API will wait perhaps 10 seconds before producing output.

But if I let the notice be displayed, each message is flushed correctly.

What is the reason behind this?

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

for ($i = 0, $j = ob_get_level(); $i < $j; $i++) {
	ob_end_flush();
}
ob_implicit_flush(1);

$stop = false;
for ($i = 0; $i < 1000; ++$i) {
	$duration = microtime(true) - env('REQUEST_TIME_FLOAT');

	echo "event: userconnect\n";
	echo 'data: {"username": "bobby", "time": "02:33:48"}';
	echo "\n\n";

	echo "data: Here's a system message of some kind that will get used\n";
	echo "data: to accomplish some task.";
	echo "\n\n";

	echo "event: usermessage\n";
	echo 'data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."}';
	echo "\n\n";

	// Notice here
	ob_flush();

	usleep($sleepTime);
}

Expected result:
----------------
Each message be flushed when written.

Actual result:
--------------
Chunk of output gets flushed at the same time, after a waiting period.
If the notice gets displayed, it works.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-29 14:06 UTC] mike@php.net
-Status: Open +Status: Not a bug
 [2015-01-29 14:06 UTC] mike@php.net
You stop any output buffers with the first loop.
So ob_flush() in the second produces the error.
Use flush() to flush the SAPI's output stream instead.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Nov 11 05:01:28 2024 UTC