PHP:读取管道流时线程崩溃

PHP: thread crash when reading pipe stream

我目前正在 PHP 中开发一个部署框架,遇到了一些关于线程和流的问题。

我想启动一个进程,读取它的 stdout 和 stderr(分别!),回显它和 return 进程终止时流的完整内容。

为了获得该功能,我使用了两个线程,每个线程都在读取不同的流 (stdout|stderr)。现在我遇到的问题是 php 在第二次调用 fgets 时崩溃。 (错误代码 0x5,错误偏移量 0x000610e7)。

经过大量的试验和错误后,我发现当我向 run 函数添加一个虚拟数组时,崩溃并不总是发生,但它按预期工作。 有谁知道为什么会这样?

我正在使用 Windows 7,PHP 5.4.22,MSVC9,pthreads 2.0.9

private static $pipeSpecsSilent = array(
   0 => array("pipe", "r"), // stdin
   1 => array("pipe", "w"), // stdout
   2 => array("pipe", "w")); // stderr

public function start()
{
    $this->procID = proc_open($this->executable, $this::$pipeSpecsSilent, $this->pipes);
    if (is_resource($this->procID))
    {
        $stdoutThread = new POutputThread($this->pipes[1]);
        $stderrThread = new POutputThread($this->pipes[2]);
        $stderrThread->start();
        $stdoutThread->start();

        $stdoutThread->join();
        $stderrThread->join();

        $stdout = trim($stdoutThread->getStreamValue());
        $stderr = trim($stderrThread->getStreamValue());

        $this->stop();
        return array('stdout' => $stdout, 'stderr' => $stderr);
    }
    return null;
}

/**
 * Closes all pipes and the process handle
 */
private function stop()
{
    for ($x = 0; $x < count($this->pipes); $x++)
    {
        fclose($this->pipes[$x]);
    }

    $this->resultValue = proc_close($this->procID);
}


class POutputThread extends Thread
{
    private $pipe;
    private $content;

    public function __construct($pipe)
    {
        $this->pipe = $pipe;
    }

    public function run()
    {
        $content = '';

        // this line is requires as we get a crash without it.
        // it seems like there is something odd happening?
        $stackDummy = array('', '');

        while (($line = fgets($this->pipe)))
        {
            PLog::i($line);
            $content .= $line;
        }
        $this->content = $content;
    }

    /**
     * Returns the value of the stream that was read
     *
     * @return string
     */
    public function getStreamValue()
    {
        return $this->content;
    }
}

我发现了问题:

虽然我在所有线程终止后关闭流,但似乎需要关闭正在从中读取的线程内的流。 所以我在 run 函数中用 fclose($this->pipe); 替换了 $this->stop(); 调用,一切正常。