Code Monkey home page Code Monkey logo

Comments (2)

folkhack avatar folkhack commented on May 19, 2024

Chiming in to say that I've verified this issue (by sinking a metric-boatload of time into it) and upon digging into the internals was very overwhelmed by presenting a potential fix... don't see a really clear path to a bugfix on this one =(

Currently I am using this temporary hack within Process/Stream:

public function close()
{
    if (!$this->writable && !$this->closing) {
        return;
    }

    $this->closing = false;

    $this->readable = false;
    $this->writable = false;

    $this->emit('end', array($this));
    $this->emit('close', array($this));
    $this->loop->removeStream($this->stream);
    $this->buffer->removeAllListeners();

    // Check one last time for anything in the buffer
    if (is_resource($this->stream)) {

        $rest = stream_get_contents($this->stream);

        if ($rest !== '') {

            $this->emit('data', array($rest, $this));
        }
    }

    $this->removeAllListeners();

    $this->handleClose();
}

This is totally not ideal but seems to be working in the short-term by firing a last-minute stream_get_contents and emitting the data event with it's results before tear-down. This is obviously not inline with any existing buffering logic/etc.

Thank you to @igorwwwwwwwwwwwwwwwwwwww and his awesome username for documenting and verifying this issue as it helped me get to a short-term solution =)

from child-process.

mbonneau avatar mbonneau commented on May 19, 2024

This reproduces what I believe is the same issue:

$loop = new \React\EventLoop\StreamSelectLoop();

$testString = 'x';

$process = new \React\ChildProcess\Process("echo $testString");

$stdOut = '';
$stdErr = '';

$process->on(
    'exit',
    function ($exitCode) use (&$stdOut, &$stdErr, $testString) {
        echo "exited\n";

        if ($exitCode) {
            throw new \Exception('Error running command');
        }

        if ($stdOut != $testString . "\n") {
            throw new \Exception('That is strange - output is ' . strlen($stdOut) . ' bytes');
        }
    }
);

$process->start($loop);

$process->stdout->on(
    'data',
    function ($output) use (&$stdOut) {
        echo ".";
        $stdOut .= $output;
    }
);
$process->stderr->on(
    'data',
    function ($output) use (&$stdErr) {
        echo "\nErr: --$output--\n";
        $stdErr .= $output;
    }
);

$loop->tick();
sleep(1); // comment this line out and it works fine

$loop->run();

As @igorwwwwwwwwwwwwwwwwwwww pointed out, this is probably an issue with buffered reads.

The process exits (and the library closes all the streams) prior to any data being read. removing the sleep allows all the "stuff" to happen in that default 0.1 default second interval.

from child-process.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.