Skip to content

Commit

Permalink
[Process] Disable exception on stream_select timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Jun 13, 2013
1 parent de78e86 commit 8a14b59
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions Process.php
Expand Up @@ -306,15 +306,17 @@ public function start($callback = null)
$w = $writePipes;
$e = null;

$n = @stream_select($r, $w, $e, 0, ceil(static::TIMEOUT_PRECISION * 1E6));

if (false === $n) {
if (false === $n = @stream_select($r, $w, $e, 0, ceil(static::TIMEOUT_PRECISION * 1E6))) {
// if a system call has been interrupted, forget about it, let's try again
if ($this->hasSystemCallBeenInterrupted()) {
continue;
}
break;
}
if ($n === 0) {
proc_terminate($this->process);

throw new RuntimeException('The process timed out.');
// nothing has changed, let's wait until the process is ready
if (0 === $n) {
continue;
}

if ($w) {
Expand Down Expand Up @@ -404,10 +406,9 @@ public function wait($callback = null)

// let's have a look if something changed in streams
if (false === $n = @stream_select($r, $w, $e, 0, ceil(static::TIMEOUT_PRECISION * 1E6))) {
$lastError = error_get_last();

// stream_select returns false when the `select` system call is interrupted by an incoming signal
if (isset($lastError['message']) && false === stripos($lastError['message'], 'interrupted system call')) {
// if a system call has been interrupted, forget about it, let's try again
// otherwise, an error occured, let's reset pipes
if (!$this->hasSystemCallBeenInterrupted()) {
$this->pipes = array();
}

Expand Down Expand Up @@ -1140,4 +1141,17 @@ private function processFileHandles($callback, $closeEmptyHandles = false)
}
}
}

/**
* Returns true if a system call has been interrupted.
*
* @return Boolean
*/
private function hasSystemCallBeenInterrupted()
{
$lastError = error_get_last();

// stream_select returns false when the `select` system call is interrupted by an incoming signal
return isset($lastError['message']) && false !== stripos($lastError['message'], 'interrupted system call');
}
}

0 comments on commit 8a14b59

Please sign in to comment.