ZF-3396: Zend_Cache - Zend_Log - When using the php://output stream

Description

My coworkers and I found this out the VERY hard way .. but here it comes.

When using logging with Zend_Cache but not setting the logger option. Zend_Cache uses Zend_Log_Writer_Stream('php://output') as default logger. This can cause some trouble. Because on destruction the Zend_Log_Writer_Stream does a fclose on its stream. So if the Zend_Cache is destroyed, no more output can be sent. In our opinion the _php://output_ should never be closed. This can therefor be thought of as a bug in PHP (hmm :D). But as a fix for the ZendFramework we suggest this patch ..


Index: Log/Writer/Stream.php
===================================================================
--- Log/Writer/Stream.php   (revision 9612)
+++ Log/Writer/Stream.php   (working copy)
@@ -43,6 +43,13 @@
     protected $_stream = null;
 
     /**
+     * Is the stream the php://output or not
+     *
+     * @var bool
+     */
+    protected $_isPhpOutput = false;
+
+    /**
      * Class Constructor
      *
      * @param  streamOrUrl     Stream or URL to open as a stream
@@ -61,6 +68,7 @@
 
             $this->_stream = $streamOrUrl;
         } else {
+            if($streamOrUrl == "php://output") $this->_isPhpOutput = true;
             if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) {
                 $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\"";
                 throw new Zend_Log_Exception($msg);
@@ -77,7 +85,7 @@
      */
     public function shutdown()
     {
-        if (is_resource($this->_stream)) {
+        if (is_resource($this->_stream) && !$this->_isPhpOutput) {
             fclose($this->_stream);
         }
     }

http://us.php.net/wrappers.php http://bugs.php.net/bug.php?id=40429 http://bugs.php.net/bug.php?id=42473

Comments

doesn't seem to be a Zend_Cache problem, so I remove the "Zend_Cache component" tag

please, correct if I didn't understand

{quote} /** * @group ZF-3396 */ public function testShutdownDoesNotClosePhpOutputStreamResource() { $writer = new Zend_Log_Writer_Stream('php://output', 'w+');

    ob_start();
    $writer->write(array('message' => 'this write should succeed'));
    ob_end_clean();

    $writer->shutdown();

    ob_start();
    echo "TEST";
    $output = ob_get_contents();
    ob_end_clean();
    $this->assertEquals("TEST", $output);

    $fp = fopen('<a rel="nofollow" href="php://output">php://output</a>', 'w+');
    ob_start();
    fwrite($fp, "TEST");
    $output = ob_get_contents();
    ob_end_clean();

    $this->assertEquals("TEST", $output);
}{quote}

This works for me even WITHOUT your patch. Can you give a test-case were your bug applies and it fails?

Hm wiki markup sucks. I have attached a file with the testcase. Please Review and give a better failure case.

Hi again ..

We will come with a better description on how we got this error.

Thank you for your time, so far :D