Skip to content

Commit b81916b

Browse files
authored
Merge pull request #183 from clue-labs/signals-only
Fix high CPU usage when only listening for signals with default loop
2 parents f15ba6f + 4294607 commit b81916b

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/StreamSelectLoop.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace React\EventLoop;
44

5-
use React\EventLoop\Signal\Pcntl;
65
use React\EventLoop\Tick\FutureTickQueue;
76
use React\EventLoop\Timer\Timer;
87
use React\EventLoop\Timer\Timers;
@@ -259,12 +258,12 @@ private function waitForStreamActivity($timeout)
259258
* Emulate a stream_select() implementation that does not break when passed
260259
* empty stream arrays.
261260
*
262-
* @param array &$read An array of read streams to select upon.
263-
* @param array &$write An array of write streams to select upon.
264-
* @param integer|null $timeout Activity timeout in microseconds, or null to wait forever.
261+
* @param array $read An array of read streams to select upon.
262+
* @param array $write An array of write streams to select upon.
263+
* @param int|null $timeout Activity timeout in microseconds, or null to wait forever.
265264
*
266-
* @return integer|false The total number of streams that are ready for read/write.
267-
* Can return false if stream_select() is interrupted by a signal.
265+
* @return int|false The total number of streams that are ready for read/write.
266+
* Can return false if stream_select() is interrupted by a signal.
268267
*/
269268
private function streamSelect(array &$read, array &$write, $timeout)
270269
{
@@ -275,7 +274,13 @@ private function streamSelect(array &$read, array &$write, $timeout)
275274
return @\stream_select($read, $write, $except, $timeout === null ? null : 0, $timeout);
276275
}
277276

278-
$timeout && \usleep($timeout);
277+
if ($timeout > 0) {
278+
\usleep($timeout);
279+
} elseif ($timeout === null) {
280+
// wait forever (we only reach this if we're only awaiting signals)
281+
// this may be interrupted and return earlier when a signal is received
282+
\sleep(PHP_INT_MAX);
283+
}
279284

280285
return 0;
281286
}

0 commit comments

Comments
 (0)