From:             romain at neutron dot io
Operating system: Irrelevant
PHP version:      5.4.19
Package:          Program Execution
Bug Type:         Bug
Bug description:Using proc_open with file handles fails on large buffers

Description:
------------
When using proc_open with file handles instead of pipes (required on
Windows, 
because of bug https://bugs.php.net/bug.php?id=51800), when reading large
outputs,  
the result might not retrieve what's expected.

The problem has been mentioned when compiling large sass files to stdout on

windows. The output may sometimes be different than the actual output you
can get 
running the command in console.

The provided script fails 3 times on 5 on windows environment. Nearly
everytime on 
OSX.

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

error_reporting(E_ALL);

$cmd = sprintf('php -r %s', escapeshellarg('fwrite(STDOUT, $in =
file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);'));    
$stdout = tmpfile();
$stderr = tmpfile();
$descriptors = array(
    array('pipe', 'r'),
    $stdout,
    $stderr,
);
$handles = array(
    $stdout, $stderr
);

$datastdin = str_repeat('*!', 8192*128);

$options = array_merge(array('suppress_errors' => true, 'binary_pipes' =>
true, 'bypass_shell' => false));
$process = proc_open($cmd, $descriptors, $pipes, getcwd(), array(),
$options);

fwrite($pipes[0], $datastdin);
fclose($pipes[0]);
unset($pipes[0]);
$read = array();
while ($handles) {
    $status = proc_get_status($process);
    if (!$status['running']) { 
        proc_close($process);
    }
    $h = $handles;
    foreach ($h as $offset => $handle) {
        fseek($handle, isset($read[$offset]) ? strlen($read[$offset]) :
0);
        
        if (!isset($read[$offset])) {
            $read[$offset] = '';
        }
        $read[$offset] .= fread($handle, 8192);
        if (false === $status['running'] && feof($handle)) {
            fclose($handle);
            unset($handles[$offset]);
        }
    }
}

foreach ($read as $r) {
    assert($r === $datastdin);
}

Expected result:
----------------
No errors, assertion is good.

Actual result:
--------------
PHP Warning:  assert(): Assertion failed in
C:\symfony\src\Symfony\Component\Pro
cess\bug.php on line 47
PHP Warning:  assert(): Assertion failed in
C:\symfony\src\Symfony\Component\Pro
cess\bug.php on line 47

-- 
Edit bug report at https://bugs.php.net/bug.php?id=65650&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=65650&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=65650&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=65650&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=65650&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=65650&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=65650&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=65650&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=65650&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=65650&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=65650&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=65650&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=65650&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=65650&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65650&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=65650&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=65650&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=65650&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65650&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=65650&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=65650&r=mysqlcfg

Reply via email to