Skip to content

Commit

Permalink
Merge pull request #126 from clue-labs/legacy-unbuffered
Browse files Browse the repository at this point in the history
Work around reading from unbuffered pipe stream in legacy PHP < 5.4.28 and PHP < 5.5.12
  • Loading branch information
WyriHaximus authored Dec 21, 2017
2 parents 5223048 + 8a394f0 commit 6f1e608
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/DuplexResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct($stream, LoopInterface $loop, $readChunkSize = null,
// This does not affect the default event loop implementation (level
// triggered), so we can ignore platforms not supporting this (HHVM).
// Pipe streams (such as STDIN) do not seem to require this and legacy
// PHP < 5.4 causes SEGFAULTs on unbuffered pipe streams, so skip this.
// PHP versions cause SEGFAULTs on unbuffered pipe streams, so skip this.
if (function_exists('stream_set_read_buffer') && !$this->isLegacyPipe($stream)) {
stream_set_read_buffer($stream, 0);
}
Expand Down Expand Up @@ -201,14 +201,18 @@ public function handleData($stream)
/**
* Returns whether this is a pipe resource in a legacy environment
*
* This works around a legacy PHP bug (#61019) that was fixed in PHP 5.4.28+
* and PHP 5.5.12+ and newer.
*
* @param resource $resource
* @return bool
* @link https://github.com/reactphp/child-process/issues/40
*
* @codeCoverageIgnore
*/
private function isLegacyPipe($resource)
{
if (PHP_VERSION_ID < 50400) {
if (PHP_VERSION_ID < 50428 || (PHP_VERSION_ID >= 50500 && PHP_VERSION_ID < 50512)) {
$meta = stream_get_meta_data($resource);

if (isset($meta['stream_type']) && $meta['stream_type'] === 'STDIO') {
Expand Down
8 changes: 6 additions & 2 deletions src/ReadableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct($stream, LoopInterface $loop, $readChunkSize = null)
// This does not affect the default event loop implementation (level
// triggered), so we can ignore platforms not supporting this (HHVM).
// Pipe streams (such as STDIN) do not seem to require this and legacy
// PHP < 5.4 causes SEGFAULTs on unbuffered pipe streams, so skip this.
// PHP versions cause SEGFAULTs on unbuffered pipe streams, so skip this.
if (function_exists('stream_set_read_buffer') && !$this->isLegacyPipe($stream)) {
stream_set_read_buffer($stream, 0);
}
Expand Down Expand Up @@ -154,14 +154,18 @@ public function handleData()
/**
* Returns whether this is a pipe resource in a legacy environment
*
* This works around a legacy PHP bug (#61019) that was fixed in PHP 5.4.28+
* and PHP 5.5.12+ and newer.
*
* @param resource $resource
* @return bool
* @link https://github.com/reactphp/child-process/issues/40
*
* @codeCoverageIgnore
*/
private function isLegacyPipe($resource)
{
if (PHP_VERSION_ID < 50400) {
if (PHP_VERSION_ID < 50428 || (PHP_VERSION_ID >= 50500 && PHP_VERSION_ID < 50512)) {
$meta = stream_get_meta_data($resource);

if (isset($meta['stream_type']) && $meta['stream_type'] === 'STDIO') {
Expand Down

0 comments on commit 6f1e608

Please sign in to comment.