GithubHelp home page GithubHelp logo

reactphp / child-process Goto Github PK

View Code? Open in Web Editor NEW
293.0 15.0 44.0 181 KB

Event-driven library for executing child processes with ReactPHP.

Home Page: https://reactphp.org/child-process/

License: MIT License

PHP 100.00%
php reactphp child-process

child-process's Introduction

ChildProcess

CI status installs on Packagist

Event-driven library for executing child processes with ReactPHP.

Development version: This branch contains the code for the upcoming 0.7 release. For the code of the current stable 0.6.x release, check out the 0.6.x branch.

The upcoming 0.7 release will be the way forward for this package. However, we will still actively support 0.6.x for those not yet on the latest version. See also installation instructions for more details.

This library integrates Program Execution with the EventLoop. Child processes launched may be signaled and will emit an exit event upon termination. Additionally, process I/O streams (i.e. STDIN, STDOUT, STDERR) are exposed as Streams.

Table of contents

Quickstart example

$process = new React\ChildProcess\Process('echo foo');
$process->start();

$process->stdout->on('data', function ($chunk) {
    echo $chunk;
});

$process->on('exit', function($exitCode, $termSignal) {
    echo 'Process exited with code ' . $exitCode . PHP_EOL;
});

See also the examples.

Process

Stream Properties

Once a process is started, its I/O streams will be constructed as instances of React\Stream\ReadableStreamInterface and React\Stream\WritableStreamInterface. Before start() is called, these properties are not set. Once a process terminates, the streams will become closed but not unset.

Following common Unix conventions, this library will start each child process with the three pipes matching the standard I/O streams as given below by default. You can use the named references for common use cases or access these as an array with all three pipes.

  • $stdin or $pipes[0] is a WritableStreamInterface
  • $stdout or $pipes[1] is a ReadableStreamInterface
  • $stderr or $pipes[2] is a ReadableStreamInterface

Note that this default configuration may be overridden by explicitly passing custom pipes, in which case they may not be set or be assigned different values. In particular, note that Windows support is limited in that it doesn't support non-blocking STDIO pipes. The $pipes array will always contain references to all pipes as configured and the standard I/O references will always be set to reference the pipes matching the above conventions. See custom pipes for more details.

Because each of these implement the underlying ReadableStreamInterface or WritableStreamInterface, you can use any of their events and methods as usual:

$process = new Process($command);
$process->start();

$process->stdout->on('data', function ($chunk) {
    echo $chunk;
});

$process->stdout->on('end', function () {
    echo 'ended';
});

$process->stdout->on('error', function (Exception $e) {
    echo 'error: ' . $e->getMessage();
});

$process->stdout->on('close', function () {
    echo 'closed';
});

$process->stdin->write($data);
$process->stdin->end($data = null);
// …

For more details, see the ReadableStreamInterface and WritableStreamInterface.

Command

The Process class allows you to pass any kind of command line string:

$process = new Process('echo test');
$process->start();

The command line string usually consists of a whitespace-separated list with your main executable bin and any number of arguments. Special care should be taken to escape or quote any arguments, escpecially if you pass any user input along. Likewise, keep in mind that especially on Windows, it is rather common to have path names containing spaces and other special characters. If you want to run a binary like this, you will have to ensure this is quoted as a single argument using escapeshellarg() like this:

$bin = 'C:\\Program files (x86)\\PHP\\php.exe';
$file = 'C:\\Users\\me\\Desktop\\Application\\main.php';

$process = new Process(escapeshellarg($bin) . ' ' . escapeshellarg($file));
$process->start();

By default, PHP will launch processes by wrapping the given command line string in a sh command on Unix, so that the first example will actually execute sh -c echo test under the hood on Unix. On Windows, it will not launch processes by wrapping them in a shell.

This is a very useful feature because it does not only allow you to pass single commands, but actually allows you to pass any kind of shell command line and launch multiple sub-commands using command chains (with &&, ||, ; and others) and allows you to redirect STDIO streams (with 2>&1 and family). This can be used to pass complete command lines and receive the resulting STDIO streams from the wrapping shell command like this:

$process = new Process('echo run && demo || echo failed');
$process->start();

Note that Windows support is limited in that it doesn't support STDIO streams at all and also that processes will not be run in a wrapping shell by default. If you want to run a shell built-in function such as echo hello or sleep 10, you may have to prefix your command line with an explicit shell like cmd /c echo hello.

In other words, the underlying shell is responsible for managing this command line and launching the individual sub-commands and connecting their STDIO streams as appropriate. This implies that the Process class will only receive the resulting STDIO streams from the wrapping shell, which will thus contain the complete input/output with no way to discern the input/output of single sub-commands.

If you want to discern the output of single sub-commands, you may want to implement some higher-level protocol logic, such as printing an explicit boundary between each sub-command like this:

$process = new Process('cat first && echo --- && cat second');
$process->start();

As an alternative, considering launching one process at a time and listening on its exit event to conditionally start the next process in the chain. This will give you an opportunity to configure the subsequent process I/O streams:

$first = new Process('cat first');
$first->start();

$first->on('exit', function () {
    $second = new Process('cat second');
    $second->start();
});

Keep in mind that PHP uses the shell wrapper for ALL command lines on Unix. While this may seem reasonable for more complex command lines, this actually also applies to running the most simple single command:

$process = new Process('yes');
$process->start();

This will actually spawn a command hierarchy similar to this on Unix:

5480 … \_ php example.php
5481 …    \_ sh -c yes
5482 …        \_ yes

This means that trying to get the underlying process PID or sending signals will actually target the wrapping shell, which may not be the desired result in many cases.

If you do not want this wrapping shell process to show up, you can simply prepend the command string with exec on Unix platforms, which will cause the wrapping shell process to be replaced by our process:

$process = new Process('exec yes');
$process->start();

This will show a resulting command hierarchy similar to this:

5480 … \_ php example.php
5481 …    \_ yes

This means that trying to get the underlying process PID and sending signals will now target the actual command as expected.

Note that in this case, the command line will not be run in a wrapping shell. This implies that when using exec, there's no way to pass command lines such as those containing command chains or redirected STDIO streams.

As a rule of thumb, most commands will likely run just fine with the wrapping shell. If you pass a complete command line (or are unsure), you SHOULD most likely keep the wrapping shell. If you're running on Unix and you want to pass an invidual command only, you MAY want to consider prepending the command string with exec to avoid the wrapping shell.

Termination

The exit event will be emitted whenever the process is no longer running. Event listeners will receive the exit code and termination signal as two arguments:

$process = new Process('sleep 10');
$process->start();

$process->on('exit', function ($code, $term) {
    if ($term === null) {
        echo 'exit with code ' . $code . PHP_EOL;
    } else {
        echo 'terminated with signal ' . $term . PHP_EOL;
    }
});

Note that $code is null if the process has terminated, but the exit code could not be determined. Similarly, $term is null unless the process has terminated in response to an uncaught signal sent to it. This is not a limitation of this project, but actual how exit codes and signals are exposed on POSIX systems, for more details see also here.

It's also worth noting that process termination depends on all file descriptors being closed beforehand. This means that all process pipes will emit a close event before the exit event and that no more data events will arrive after the exit event. Accordingly, if either of these pipes is in a paused state (pause() method or internally due to a pipe() call), this detection may not trigger.

The terminate(?int $signal = null): bool method can be used to send the process a signal (SIGTERM by default). Depending on which signal you send to the process and whether it has a signal handler registered, this can be used to either merely signal a process or even forcefully terminate it.

$process->terminate(SIGUSR1);

Keep the above section in mind if you want to forcefully terminate a process. If your process spawn sub-processes or implicitly uses the wrapping shell mentioned above, its file descriptors may be inherited to child processes and terminating the main process may not necessarily terminate the whole process tree. It is highly suggested that you explicitly close() all process pipes accordingly when terminating a process:

$process = new Process('sleep 10');
$process->start();

Loop::addTimer(2.0, function () use ($process) {
    foreach ($process->pipes as $pipe) {
        $pipe->close();
    }
    $process->terminate();
});

For many simple programs these seamingly complicated steps can also be avoided by prefixing the command line with exec to avoid the wrapping shell and its inherited process pipes as mentioned above.

$process = new Process('exec sleep 10');
$process->start();

Loop::addTimer(2.0, function () use ($process) {
    $process->terminate();
});

Many command line programs also wait for data on STDIN and terminate cleanly when this pipe is closed. For example, the following can be used to "soft-close" a cat process:

$process = new Process('cat');
$process->start();

Loop::addTimer(2.0, function () use ($process) {
    $process->stdin->end();
});

While process pipes and termination may seem confusing to newcomers, the above properties actually allow some fine grained control over process termination, such as first trying a soft-close and then applying a force-close after a timeout.

Custom pipes

Following common Unix conventions, this library will start each child process with the three pipes matching the standard I/O streams by default. For more advanced use cases it may be useful to pass in custom pipes, such as explicitly passing additional file descriptors (FDs) or overriding default process pipes.

Note that passing custom pipes is considered advanced usage and requires a more in-depth understanding of Unix file descriptors and how they are inherited to child processes and shared in multi-processing applications.

If you do not want to use the default standard I/O pipes, you can explicitly pass an array containing the file descriptor specification to the constructor like this:

$fds = array(
    // standard I/O pipes for stdin/stdout/stderr
    0 => array('pipe', 'r'),
    1 => array('pipe', 'w'),
    2 => array('pipe', 'w'),

    // example FDs for files or open resources
    4 => array('file', '/dev/null', 'r'),
    6 => fopen('log.txt','a'),
    8 => STDERR,

    // example FDs for sockets
    10 => fsockopen('localhost', 8080),
    12 => stream_socket_server('tcp://0.0.0.0:4711')
);

$process = new Process($cmd, null, null, $fds);
$process->start();

Unless your use case has special requirements that demand otherwise, you're highly recommended to (at least) pass in the standard I/O pipes as given above. The file descriptor specification accepts arguments in the exact same format as the underlying proc_open() function.

Once the process is started, the $pipes array will always contain references to all pipes as configured and the standard I/O references will always be set to reference the pipes matching common Unix conventions. This library supports any number of pipes and additional file descriptors, but many common applications being run as a child process will expect that the parent process properly assigns these file descriptors.

Windows Compatibility

Due to platform constraints, this library provides only limited support for spawning child processes on Windows. In particular, PHP does not allow accessing standard I/O pipes on Windows without blocking. As such, this project will not allow constructing a child process with the default process pipes and will instead throw a LogicException on Windows by default:

// throws LogicException on Windows
$process = new Process('ping example.com');
$process->start();

There are a number of alternatives and workarounds as detailed below if you want to run a child process on Windows, each with its own set of pros and cons:

  • As of PHP 8, you can start the child process with socket pair descriptors in place of normal standard I/O pipes like this:

    $process = new Process(
        'ping example.com',
        null,
        null,
        [
            ['socket'],
            ['socket'],
            ['socket']
        ]
    );
    $process->start();
    
    $process->stdout->on('data', function ($chunk) {
        echo $chunk;
    });

    These socket pairs support non-blocking process I/O on any platform, including Windows. However, not all programs accept stdio sockets.

  • This package does work on Windows Subsystem for Linux (or WSL) without issues. When you are in control over how your application is deployed, we recommend installing WSL when you want to run this package on Windows.

  • If you only care about the exit code of a child process to check if its execution was successful, you can use custom pipes to omit any standard I/O pipes like this:

    $process = new Process('ping example.com', null, null, array());
    $process->start();
    
    $process->on('exit', function ($exitcode) {
        echo 'exit with ' . $exitcode . PHP_EOL;
    });

    Similarly, this is also useful if your child process communicates over sockets with remote servers or even your parent process using the Socket component. This is usually considered the best alternative if you have control over how your child process communicates with the parent process.

  • If you only care about command output after the child process has been executed, you can use custom pipes to configure file handles to be passed to the child process instead of pipes like this:

    $process = new Process('ping example.com', null, null, array(
        array('file', 'nul', 'r'),
        $stdout = tmpfile(),
        array('file', 'nul', 'w')
    ));
    $process->start();
    
    $process->on('exit', function ($exitcode) use ($stdout) {
        echo 'exit with ' . $exitcode . PHP_EOL;
    
        // rewind to start and then read full file (demo only, this is blocking).
        // reading from shared file is only safe if you have some synchronization in place
        // or after the child process has terminated.
        rewind($stdout);
        echo stream_get_contents($stdout);
        fclose($stdout);
    });

    Note that this example uses tmpfile()/fopen() for illustration purposes only. This should not be used in a truly async program because the filesystem is inherently blocking and each call could potentially take several seconds. See also the Filesystem component as an alternative.

  • If you want to access command output as it happens in a streaming fashion, you can use redirection to spawn an additional process to forward your standard I/O streams to a socket and use custom pipes to omit any actual standard I/O pipes like this:

    $server = new React\Socket\Server('127.0.0.1:0');
    $server->on('connection', function (React\Socket\ConnectionInterface $connection) {
        $connection->on('data', function ($chunk) {
            echo $chunk;
        });
    });
    
    $command = 'ping example.com | foobar ' . escapeshellarg($server->getAddress());
    $process = new Process($command, null, null, array());
    $process->start();
    
    $process->on('exit', function ($exitcode) use ($server) {
        $server->close();
        echo 'exit with ' . $exitcode . PHP_EOL;
    });

    Note how this will spawn another fictional foobar helper program to consume the standard output from the actual child process. This is in fact similar to the above recommendation of using socket connections in the child process, but in this case does not require modification of the actual child process.

    In this example, the fictional foobar helper program can be implemented by simply consuming all data from standard input and forwarding it to a socket connection like this:

    $socket = stream_socket_client($argv[1]);
    do {
        fwrite($socket, $data = fread(STDIN, 8192));
    } while (isset($data[0]));

    Accordingly, this example can also be run with plain PHP without having to rely on any external helper program like this:

    $code = '$s=stream_socket_client($argv[1]);do{fwrite($s,$d=fread(STDIN, 8192));}while(isset($d[0]));';
    $command = 'ping example.com | php -r ' . escapeshellarg($code) . ' ' . escapeshellarg($server->getAddress());
    $process = new Process($command, null, null, array());
    $process->start();

    See also example #23.

    Note that this is for illustration purposes only and you may want to implement some proper error checks and/or socket verification in actual production use if you do not want to risk other processes connecting to the server socket. In this case, we suggest looking at the excellent createprocess-windows.

Additionally, note that the command given to the Process will be passed to the underlying Windows-API (CreateProcess) as-is and the process will not be launched in a wrapping shell by default. In particular, this means that shell built-in functions such as echo hello or sleep 10 may have to be prefixed with an explicit shell command like this:

$process = new Process('cmd /c echo hello', null, null, $pipes);
$process->start();

Install

The recommended way to install this library is through Composer. New to Composer?

Once released, this project will follow SemVer. At the moment, this will install the latest development version:

composer require react/child-process:^0.7@dev

See also the CHANGELOG for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 8+ and HHVM. It's highly recommended to use the latest supported PHP version for this project.

Note that legacy platforms that use PHP compiled with the legacy --enable-sigchild option may not reliably determine the child process' exit code in some cases. This should not affect most installations as this configure option is not used by default and most distributions (such as Debian and Ubuntu) are known to not use this by default. This option may be used on some installations that use Oracle OCI8, see phpinfo() output to check if your installation might be affected.

See above note for limited Windows Compatibility.

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

composer install

To run the test suite, go to the project root and run:

vendor/bin/phpunit

License

MIT, see LICENSE file.

child-process's People

Contributors

cboden avatar clue avatar e3betht avatar gdejong avatar iamluc avatar igorw avatar jmikola avatar jsor avatar mbonneau avatar mdrost avatar nhedger avatar reedy avatar simonfrings avatar staabm avatar wyrihaximus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

child-process's Issues

Improper detection of sigchld compile flag

On several distributions now phpinfo() no longer returns flag information. There needs to be a better way to detect whether --enable-sigchld was compiled in. Or, alternatively, it should be possible to tell the Process object constructor that it should be treated as enabled (so that the pipe trick can be used).

I'm using PHP from the IUS packages on CentOS 7. But I have read this is also a problem for Debian-based distributions.

Windows compatibility?

Hi guys,

I've been trying to use reactphp/child-process on Windows and I've had hard times having it to work.
I traced the problem back to a bug in PHP:

https://bugs.php.net/bug.php?id=51800

The bug has been solved, at least in recent PHP versions.

To sum it up:

I have an application that uses child-process and that works flawlessly on Linux.
On Windows, reactphp/child-process does not work with PHP 5.5.12. The PHP process stalls and nothing is happening (just like in the PHP bug)

It seems to work with PHP 5.6.6, although I'm having some strange behaviour (I'm facing locks of several seconds, then the application starts again...)

I've tried to run the unit tests of the master branch and many are failing in Windows. I'm not sure if those are supposed to work. Did you have any of those unit tests running on Windows? If yes, what version of PHP did you use?

Do you need some help working on that compatibility or is there something I missed?

Don't replace generated command from \ to \\

Hi,

we are using this component for rector, currently, we currently have issue with spaced root project, that we have:

/Users/samsonasik/www/spaced prj sample/target/rector.php

and we want to change to:

/Users/samsonasik/www/spaced\ prj\ sample/target/rector.php

and this seems generaetd as double \\:

-/Users/samsonasik/www/spaced\ prj\ sample/target/rector.php
+/Users/samsonasik/www/spaced\\ prj\\ sample/target/rector.php

is this possible to be fixed here? Thank you.

How use callbacks and pass arguments to the child process

I need to run a callback once the child process to stop to perform certain actions.

Also I need to pass arguments to the child process so that everything is run, I tried to pass the $conn object variable but it seems he does not accept it. How to resolve this?

What I have tried:

public function onOpen(ConnectionInterface $conn) {
    $callback = function () use ($conn) {
        echo "Hey, ".md5($conn->resourceId)."!\n";
    };

    $this->childProcess->stdin->write($conn, $callback);

He gave to understand what I'm trying to do? After passing the argument to the child process I want that callback is triggered and the user function md5 be used without blocking the process parents..

Clean up API

The roadmap (#31) currently mentions "clean up API" for the v0.7.0 release, so I'm filing this ticket to discuss possible things that could be cleaned up.

Here's some of the things that I would like to address:

1.) The Process constructor and the start() method form a temporal dependence (you have to call start() exactly once after constructing). Additionally, because creating a process requires creating an instance of Process means that this is really hard to test in higher-level components that dynamically create the command for example (you can't inject a mock for instance, see https://github.com/clue/reactphp-ssh-proxy/blob/7ac2c246a1a5fd86246452d04346092cdd8bb1a7/src/SshProcessConnector.php#L89 for example). As an alternative, I would suggest adding a Launcher (name subject to discussion) which acts as a "factory" to create processes. This solves many of these problems, allows easily creating custom implementations for the points discussed below and also allows simply injecting a mock to higher-level implementations. Example usage:

// concept only
$launcher = new Launcher($loop);
$process = $launcher->launch('ping google.com');

$process->on('exit', 'var_dump');

2.) Inherited file descriptors (#51) are likely not what most use cases want. We should provide a "default launcher" which automatically takes care of closing all file descriptors. This can easily be integrated into the "launcher" concept above and we may or may not want to provide additional options to control this behavior.

3.) Windows support (#67) requires special care even for the easiest use cases. We should provide a "windows launcher" which automatically takes care of setting up I/O redirection for most common use cases. This can easily be integrated into the "launcher" concept above.

4.) The API is quite complex for common use cases. We should provide additional helper methods to cover common use cases. This can easily be integrated into the "launcher" concept above. For example, it's very common you only consume readable data from process output or want to buffer all the data:

// concept only
$launcher = new Launcher($loop);
$stream = $launcher->stream('ping example.com');

$stream->on('data', function ($chunk) {
    echo $chunk;
});


// concept only
$launcher = new Launcher($loop);
$promise = $launcher->buffer('ping example.com');

$promise->then(function ($data) {
    echo $data;
});

Any input is welcome! 👍

Next steps for ChildProcess with ReactPHP v3

We're currently moving forward with working on ReactPHP v3 and releasing the roadmap tickets for all our components (see reactphp/event-loop#271 and others). We still have some components that we haven't finalized plans for, especially with the next major version approaching. It's important to address how we can make sure these components are aligned with the upcoming ReactPHP v3.

To give some background information, the reactphp/child-process component is currently in version 0.6.5 and hasn't received a stable release yet. There is an open ticket describing the roadmap to a stable v1.0.0, which was opened back in August of 2017 and a lot has happened since then.

Needless to say, ChildProcess is still an important component for executing child processes with ReactPHP and will most definitely receive compatibility with the other v3 components. There's still the question when it will receive its own v3 and what about the outstanding stable v1.0.0?

Happy about input on this, so let's discuss possible options and decide on what makes the most sense 🚀

Emit end/close event once streams end

The stdout and stderr stream pipes should emit an end/close event once their underlying stream ends. In particular, they should not emit a data event with an empty string.

Callback as child processes

Swoole's process abstraction uses a callback rather than calling exec, and then provides an exec method that can be used within the callback for executing commands if that's desired within the child process. See https://openswoole.com/docs/modules/swoole-process-construct . This gives the option of executing PHP code within the child process.

Every time a child process is created within PHP you fork the entire PHP instance, extensions and all. Yet to run any type of PHP within the reactphp child you need to call the the php/php-cli command which will than create yet another php instance, this one without access to any of the original parents resources. This is both inefficient and restrictive. Keep in mind that if the intent is to purposefully restrict the spawned php environment that php/php-cli can still be called via the exec method, as can other procedures to further reduce privileges of that process (if the parent process has privileges to do so).

Segmentation Fault (core dumped)

I'm getting a segmentation fault error from my ratchet server while attempting to do

$process->stdin->write($json);

server is:

Ubuntu 14.4
PHP memory_limit = 128M
PHP 5.5.9

I'm using the following versions of ratchet and react child process

"cboden/ratchet": "^0.3.6",
"react/child-process": "^0.4.3"

code is working locally on os x and php 5.5.38

Process close does not wait for stdout/stderr to drain

I ran into an edge case with child-process that I suspect may also affect stream. First of all, here is a reproduce case:

<?php

use React\ChildProcess\Process;
use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;

require 'vendor/autoload.php';

function run(LoopInterface $loop, $cmd, callable $callback) {
    $process = new Process($cmd);

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

    $process->on(
        'exit',
        function ($exitCode) use ($callback, $cmd, &$stdOut, &$stdErr) {
            if ($exitCode) {
                throw new \Exception('Error running command ' . $cmd . ': ' . $exitCode . PHP_EOL . $stdOut . PHP_EOL . $stdErr);
            }

            $callback($stdOut);
        }
    );

    $process->start($loop);

    $process->stdout->on(
        'data',
        function ($output) use (&$stdOut) {
            $stdOut .= $output;
        }
    );
    $process->stderr->on(
        'data',
        function ($output) use (&$stdErr) {
            $stdErr .= $output;
        }
    );
}

function ssh($host, $cmd) {
    return 'ssh -o "PasswordAuthentication no" -A ' . escapeshellarg($host) . ' ' . escapeshellarg($cmd);
}

// list of 17 hosts to SSH into
$hosts = [...];

$attempts = 0;
while (true) {
    $attempts++;
    echo '.';
    if ($attempts % 60 === 0) {
        echo "\n";
    }

    // StreamSelectLoop
    $loop = Factory::create();

    foreach ($hosts as $host) {
        $loop->addTimer(
            0.001,
            function () use ($host, $loop, $attempts) {
                run($loop, ssh($host, 'echo foo'), function ($value) use ($attempts) {
                    if ($value !== "foo\n") {
                        throw new \Exception('Found bad value ' . json_encode($value) . ' after ' . $attempts . ' attempts');
                    }
                });
            }
        );
    }

    $loop->run();
}

The bad value returned here is an empty string.

I have been able to reproduce the problem in some cases without the timer and without the ssh command. But this is the most reliable reproduce case I have been able to come up with. On my local machine (running OSX) the problem is reproduced in 1-20 attempts. When running in a server environment it generally takes 20-200 attempts.

I managed to figure out that the process stdin's Stream::handleClose() is being called without the PHP stream's buffer being consumed.

A good way of seeing that is by replacing Stream::handleClose() with:

public function handleClose()
{
    if (is_resource($this->stream)) {
        $rest = stream_get_contents($this->stream);
        if ($rest !== '') {
            throw new \Exception($rest);
        }
        fclose($this->stream);
    }
}

You will see that the string "foo\n" was still in the buffer. And also get a backtrace to where the close() call came from.

Stream already waits for buffered writes to go out before closing. But it does not process buffered reads. I believe that is the source of the bug.

Enjoy!

Consider adding specific libuv adapter

We may want to add a specific adapter taking advantage of libuv's process handling logic:

To be clear, this project works just fine with all loop implementations, including the default StreamSelectLoop and ExtUvLoop. This potential adapter could take advantage of some of the optimizations inside libuv and avoid some of the known limitations of the project.

In particular, this may help with Windows support (#9) in the future because libuv supports pipes with overlapped mode on Windows. Note that a Windows build of ext-uv does not appear to be available at the time of writing this, but it is believed to change again in the future (amphp/ext-uv#63).

Fatal error when phpinfo is disabled

If you put the phpinfo function in disabled_functions and try to use class Process with php8.0, it will throw the next error

PHP Fatal error: Uncaught Error: Call to undefined function phpinfo() in .../vendor/react/child-process/src/Process.php:373

There is the next code where it throw error attempting to call phpinfo function https://github.com/reactphp/child-process/blob/master/src/Process.php#L435

    /**
     * Return whether PHP has been compiled with the '--enable-sigchild' option.
     *
     * @see \Symfony\Component\Process\Process::isSigchildEnabled()
     * @return bool
     */
    public static final function isSigchildEnabled()
    {
        if (null !== self::$sigchild) {
            return self::$sigchild;
        }
        \ob_start();
        \phpinfo(\INFO_GENERAL);
        return self::$sigchild = \false !== \strpos(\ob_get_clean(), '--enable-sigchild');
    }

In the comments to the method there is a reference to a similar method from the Symfony class, where the disabled phpinfo state is provided https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/Process/Process.php#L1377

    protected function isSigchildEnabled()
    {
        if (null !== self::$sigchild) {
            return self::$sigchild;
        }

        if (!\function_exists('phpinfo')) {
            return self::$sigchild = false;
        }

        ob_start();
        phpinfo(\INFO_GENERAL);

        return self::$sigchild = false !== strpos(ob_get_clean(), '--enable-sigchild');
    }

Since phpinfo is disabled for security purposes in my case, is it possible to add this behavior?
Ready to send a Pull Request

Thank you for your time, checking this issue.

process terminate not working as expected

When I try to terminate the parent process with SIGINT (kill -2 parentPId), child processes are still running. I'm using signal handler for SIGINT in the parent class and there I call Process::terminate($singal). Child proccess ends only if I output something, for example if I uncomment echo PHP_EOL; in child.php

Code sample:

parent.php

<?php

use React\EventLoop\Factory;

require_once 'vendor/autoload.php';

$loop = Factory::create();

$startProcess = (function () use ($loop) {
    $process = new \React\ChildProcess\Process('php child.php');
    $process->start($loop);

    $process->stdout->on('data', function ($chunk) {
            echo $chunk;
    });

    return $process;
});

$processList = [
    $startProcess(),
    $startProcess()
];

$loop->addSignal(SIGINT, function ($signal) use ($processList, $loop) {
    foreach ($processList as $process) {
        foreach ($process->pipes as $pipe) {
            $pipe->close();
        }

        $process->terminate(SIGINT);
    }

    $loop->stop();
});


$loop->run();

child.php

<?php

require_once 'vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

$loop->addPeriodicTimer(1, function () {
    //echo PHP_EOL;
});

$loop->addSignal(SIGINT, function () use ($loop) {
    $loop->stop();
});

$loop->run();

is it possible to debug a child process?

Hi,
I'm using a package (seregazhuk/php-watcher) that is based on reactphp/child-process

I'm trying to debug using xdebug the child process but it seems that when the process starts, no new "debugging socket" is being created as I was expecting.

I was trying to pass an explicit PHP-xdebug argument to start the debugger but it doesn't work.

I wanted to know whether I'm missing anything?

Thanks,
Niv

P.S - sorry for posting the issue with no body

Roadmap to v1.0.0

Let's face it, this project is currently beta, but has been used in production for years :shipit:

We're currently following a v0.X.Y release scheme (http://sentimentalversioning.org/).

We should finally make this stable and fully adhere to SemVer and release a stable v1.0.0.

To a large extend, a stable v1.0.0 helps making BC breaks more explicit and thus the whole project more reliable from a consumer perspective. This project is actively maintained and has received some major updates in the last weeks and has some major updates planned in the next weeks. Given our current versioning scheme, we'd like to ensure all anticipated BC breaks will be merged before the planned v1.0.0 release.

As such, I've set up a roadmap that enlists only the major changes for each version among with planned release dates towards a stable v1.0.0 release:

v0.4.0 ✅

  • Released 2014-07-31
  • Initial tagged version

v0.4.1 ✅

  • Released 2016-08-01
  • Standalone component and tests

v0.4.2 ✅

  • Released 2017-03-10
  • Stream v0.5 API

v0.4.3 ✅

  • Released 2017-03-14
  • Support PHP 5.3 - PHP 7.1

v0.5.0 ✅

  • Released 2017-08-15
  • Readable/Writable pipes
  • Drop Windows support entirely

v0.6.0 ✅

  • Releases 2019-01-14
  • Custom pipes
  • Add (limited) Windows support

v0.7.0

  • Planned 2022-Q1??
  • API cleanup

v1.0.0

  • Planned 2022-??
  • No new changes planned, this should merely mark the previous release as "stable"

This ticket aims to serve as a basic overview and does not contain every single change. Please also see the milestone links and the CHANGELOG for more details.

Obviously, this roadmap is subject to change and I'll try to keep it updated as we progress. In order to avoid cluttering this, please keep discussion in this ticket to a minimum and consider reaching out to us through new tickets or Twitter etc.

Spawn child process and creatie server in it

Is it possible something similar to node cluster module

`cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
cluster.on('fork', function(worker) {
newPID = worker.process.pid;
console.log(worker.process.pid);
});
cluster.on('exit', function(worker, code, signal) {
cluster.fork();
});
} 
if (cluster.isWorker) {
//create server here and logic to restart process with process.exit(1); 
}); 
}`

When we have child and master process and we could restart server with simple endpoint /restart in which we are calling process.exit(1) - ( in node.js this works properly )

PHPUnit Code Coverage for runned process

Is it possible somehow to run PHPUnit Code Coverage on the process which is run with Process? In my application, I run a couple of PHP sub-processes of my code, but I want to check Code Coverage on that.

[FYI] Child processes inherit open file handles from parent

Opening this as a FYI only, note that child processes inherit open file handles from the parent process.

https://bugs.php.net/bug.php?id=70932

Depending on how child processes are used this may or may not cause issues. I'm currently not aware of any issues in the ReactPHP ecosystem, so I'm merely opening this as a reminder and to gather some input on this.

One option would be adding a heads up to the README and consider this issue resolved for now. Thanks to @kelunik for making us aware of this issue!

Add support for additional file descriptors

In writing https://github.com/Vektah/phpunit-parallel I wanted to set up an additional pipe between the parent and child processes, but it seems there is currently no good way to do this.

We could add it to the constructor, but the constructor already has a bunch of arguments:

/**
    * Constructor.
    *
    * @param string $cmd     Command line to run
    * @param string $cwd     Current working directory or null to inherit
    * @param array  $env     Environment variables or null to inherit
    * @param array  $options Options for proc_open()
    * @throws RuntimeException When proc_open() is not installed
    */
    public function __construct($cmd, $cwd = null, array $env = null, array $options = array())

It really shouldn't be mutable state though otherwise cleanup could be broken and file descriptors could start leaking.

I'm happy to throw a PR up but I want to get some thoughts first.

Block of code in question:

        $fdSpec = array(
            array('pipe', 'r'), // stdin
            array('pipe', 'w'), // stdout
            array('pipe', 'w'), // stderr
            // I want to add additional pipes here. 
        );

enhanced sigchild support will also need to move down the appropriate number of file descriptors when new descriptors are added.

What is the correct way to obtain data from a subprocess

I'm trying to get in the event on data to verify if a thread has a specific string but it happens that when there are many strings printing with echo in the console or after printing the first string does not check me well.

I do not know if it is the correct way to obtain data from a childprocess but this is the method understood in the examples

$this->childprocess->stdout->on('data', function ($chunk) { if ($chunk == "/exit"){ echo $chunk; } });

also try it with trim ($ chunk)

at the beginning it works and the condition fulfills me but when the childprocess throws me a lot of data (many echo) it is lost and I do not know if it is the correct way to obtain data

Persistent SSH Child Process

Is it possible to create a persistent connection to an IP over ssh and then be able to execute commands at will without having to ssh again?

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.