Skip to content

Commit

Permalink
Fixed bug causing NativeReactor to run at 100% CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoebi committed Feb 22, 2016
1 parent 5341a4c commit 4b66747
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 1.0.8

- Fix NativeReactor running a busy loop if no timers are active.
Properly block now in NativeReactor inside stream_select().

### 1.0.7

- Several combinator functions could result in a Promise already
Expand Down
10 changes: 6 additions & 4 deletions lib/NativeReactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,13 @@ private function doTick($noWait) {
// @TODO Instead of iterating all timers hunting for a keep-alive
// we should just use a specific counter to cache the number
// of keep-alive timers in use at any given time
$timeToNextAlarm = 0;
$timeToNextAlarm = null;
foreach ($this->timerOrder as $watcherId => $time) {
if ($this->watchers[$watcherId]->keepAlive) {
// The reset() is important because the foreach modifies
// the internal array pointer.
// the internal array pointer with PHP 5.
$nextTimerAt = \reset($this->timerOrder);
$timeToNextAlarm = \round($nextTimerAt - \microtime(true), 4);
$timeToNextAlarm = ($timeToNextAlarm > 0) ? $timeToNextAlarm : 0;
break;
}
}
Expand All @@ -228,7 +227,10 @@ private function selectActionableStreams($timeout) {
$w = $this->writeStreams;
$e = null;

if ($timeout <= 0) {
if ($timeout === null) {
$sec = null;
$usec = null;
} elseif ($timeout <= 0) {
$sec = 0;
$usec = 0;
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/UvReactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class UvReactorTest extends ReactorTest {
public static function setUpBeforeClass() {
if (!defined('SIGUSR1')) {
if (!defined('SIGUSR1') && extension_loaded("uv")) {
define('SIGUSR1', \Uv::SIGUSR1);
}
}
Expand Down

0 comments on commit 4b66747

Please sign in to comment.