Skip to content

Commit 4294607

Browse files
committed
Fix high CPU usage when only listening for signals with default loop
1 parent 7016d65 commit 4294607

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;
@@ -258,12 +257,12 @@ private function waitForStreamActivity($timeout)
258257
* Emulate a stream_select() implementation that does not break when passed
259258
* empty stream arrays.
260259
*
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 integer|null $timeout Activity timeout in microseconds, or null to wait forever.
260+
* @param array $read An array of read streams to select upon.
261+
* @param array $write An array of write streams to select upon.
262+
* @param int|null $timeout Activity timeout in microseconds, or null to wait forever.
264263
*
265-
* @return integer|false The total number of streams that are ready for read/write.
266-
* Can return false if stream_select() is interrupted by a signal.
264+
* @return int|false The total number of streams that are ready for read/write.
265+
* Can return false if stream_select() is interrupted by a signal.
267266
*/
268267
private function streamSelect(array &$read, array &$write, $timeout)
269268
{
@@ -274,7 +273,13 @@ private function streamSelect(array &$read, array &$write, $timeout)
274273
return @\stream_select($read, $write, $except, $timeout === null ? null : 0, $timeout);
275274
}
276275

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

279284
return 0;
280285
}

0 commit comments

Comments
 (0)