8000 Improve error reporting when spawning child process fails by clue · Pull Request #73 · reactphp/child-process · GitHub
[go: up one dir, main page]

Skip to content

Improve error reporting when spawning child process fails #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ public function start(LoopInterface $loop, $interval = 0.1)
$options['suppress_errors'] = true;
}

$this->process = \proc_open($cmd, $fdSpec, $pipes, $this->cwd, $this->env, $options);
$this->process = @\proc_open($cmd, $fdSpec, $pipes, $this->cwd, $this->env, $options);

if (!\is_resource($this->process)) {
throw new \RuntimeException('Unable to launch a new process.');
$error = \error_get_last();
throw new \RuntimeException('Unable to launch a new process: ' . $error['message']);
}

// count open process pipes and await close event for each to drain buffers before detecting exit
Expand Down
50 changes: 50 additions & 0 deletions tests/AbstractProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,56 @@ public function testStartWithCustomPipesWillAssignPipes()
$this->assertInstanceOf('React\Stream\WritableStreamInterface', $process->pipes[3]);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage No such file or directory
*/
public function testStartWithInvalidFileDescriptorPathWillThrow()
{
$fds = array(
4 => array('file', '/dev/does-not-exist', 'r')
);

$process = new Process('exit 0', null, null, $fds);
$process->start($this->createLoop());
}

public function testStartWithExcessiveNumberOfFileDescriptorsWillThrow()
{
if (PHP_VERSION_ID < 70000) {
$this->markTestSkipped('PHP 7+ only, causes memory overflow on legacy PHP 5');
}

$ulimit = exec('ulimit -n 2>&1');
if ($ulimit < 1) {
$this->markTestSkipped('Unable to determine limit of open files (ulimit not available?)');
}

$loop = $this->createLoop();

// create 70% (usually ~700) dummy file handles in this parent dummy
$limit = (int)($ulimit * 0.7);
$fds = array();
for ($i = 0; $i < $limit; ++$i) {
$fds[$i] = fopen('/dev/null', 'r');
}

// try to create child process with another ~700 dummy file handles
$new = array_fill(0, $limit, array('file', '/dev/null', 'r'));
$process = new Process('ping example.com', null, null, $new);

try {
$process->start($loop);

$this->fail('Did not expect to reach this point');
} catch (\RuntimeException $e) {
// clear dummy files handles to make some room again (avoid fatal errors for autoloader)
$fds = array();

$this->assertContains('Too many open files', $e->getMessage());
}
}

public function testIsRunning()
{
if (DIRECTORY_SEPARATOR === '\\') {
Expand Down
0