-
-
Notifications
You must be signed in to change notification settings - Fork 131
StreamSelectLoop: Int overflow for big timer intervals #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
9021dc8
8279d9b
4400ede
08c709b
4347fa3
3c8a5d6
bb81383
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |
| class StreamSelectLoop implements LoopInterface | ||
| { | ||
| const MICROSECONDS_PER_SECOND = 1000000; | ||
| const NANOSECONDS_PER_SECOND = 1000000000; | ||
| const NANOSECONDS_PER_MICROSECOND = 1000; | ||
|
|
||
| private $futureTickQueue; | ||
| private $timers; | ||
|
|
@@ -152,20 +154,9 @@ public function run() | |
| // Future-tick queue has pending callbacks ... | ||
| if (!$this->running || !$this->futureTickQueue->isEmpty()) { | ||
| $timeout = 0; | ||
|
|
||
| // There is a pending timer, only block until it is due ... | ||
| } elseif ($scheduledAt = $this->timers->getFirst()) { | ||
| $timeout = $scheduledAt - $this->timers->getTime(); | ||
| if ($timeout < 0) { | ||
| $timeout = 0; | ||
| } else { | ||
| /* | ||
| * round() needed to correct float error: | ||
| * https://github.com/reactphp/event-loop/issues/48 | ||
| */ | ||
| $timeout = round($timeout * self::MICROSECONDS_PER_SECOND); | ||
| } | ||
|
|
||
| $timeout = max($scheduledAt - $this->timers->getTime(), 0); | ||
| // The only possible event is stream activity, so wait forever ... | ||
| } elseif ($this->readStreams || $this->writeStreams) { | ||
| $timeout = null; | ||
|
|
@@ -189,6 +180,8 @@ public function stop() | |
|
|
||
| /** | ||
| * Wait/check for stream activity, or until the next timer is due. | ||
| * | ||
| * @param float $timeout | ||
| */ | ||
| private function waitForStreamActivity($timeout) | ||
| { | ||
|
|
@@ -219,28 +212,125 @@ private function waitForStreamActivity($timeout) | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns integer amount of seconds in $time or null. | ||
| * | ||
| * @param float|null $time – time in seconds | ||
| * | ||
| * @return int|null | ||
| */ | ||
| private static function getSeconds($time) | ||
| { | ||
| if ($time === null) { | ||
| return null; | ||
| } | ||
|
|
||
| /* | ||
| * Workaround for PHP int overflow: | ||
| * (float)PHP_INT_MAX == PHP_INT_MAX => true | ||
| * (int)(float)PHP_INT_MAX == PHP_INT_MAX => false | ||
| * (int)(float)PHP_INT_MAX == PHP_INT_MIN => true | ||
| */ | ||
| if ($time == PHP_INT_MAX) { | ||
| return PHP_INT_MAX; | ||
| } | ||
|
|
||
| return intval(floor($time)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns integer amount of microseconds in $time or null. | ||
| * | ||
| * @param float|null $time – time in seconds | ||
| * | ||
| * @return int|null | ||
| */ | ||
| private static function getMicroseconds($time) | ||
| { | ||
| if ($time === null) { | ||
| return null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably return |
||
| } | ||
| $fractional = fmod($time, 1); | ||
| $microseconds = round($fractional * self::MICROSECONDS_PER_SECOND); | ||
|
|
||
| return intval($microseconds); | ||
| } | ||
|
|
||
| /** | ||
| * Returns integer amount of nanoseconds in $time or null. | ||
| * The precision is 1 microsecond. | ||
| * | ||
| * @param float|null $time – time in seconds | ||
| * | ||
| * @return int|null | ||
| */ | ||
| private static function getNanoseconds($time) | ||
| { | ||
| if ($time === null) { | ||
| return null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably return |
||
| } | ||
|
|
||
| return intval(self::getMicroseconds($time) * self::NANOSECONDS_PER_MICROSECOND); | ||
| } | ||
|
|
||
| /** | ||
| * Emulate a stream_select() implementation that does not break when passed | ||
| * empty stream arrays. | ||
| * | ||
| * @param array &$read An array of read streams to select upon. | ||
| * @param array &$write An array of write streams to select upon. | ||
| * @param integer|null $timeout Activity timeout in microseconds, or null to wait forever. | ||
| * @param float|null $timeout Activity timeout in seconds, or null to wait forever. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a rather subtle BC break as consumers could possibly rely on this |
||
| * | ||
| * @return integer|false The total number of streams that are ready for read/write. | ||
| * Can return false if stream_select() is interrupted by a signal. | ||
| */ | ||
| protected function streamSelect(array &$read, array &$write, $timeout) | ||
| { | ||
| $seconds = self::getSeconds($timeout); | ||
| $microseconds = self::getMicroseconds($timeout); | ||
| $nanoseconds = self::getNanoseconds($timeout); | ||
|
|
||
| if ($read || $write) { | ||
| $except = null; | ||
| $except = []; | ||
|
|
||
| // suppress warnings that occur, when stream_select is interrupted by a signal | ||
| return @stream_select($read, $write, $except, $timeout === null ? null : 0, $timeout); | ||
| return $this->doSelectStream($read, $write, $except, $seconds, $microseconds); | ||
| } | ||
|
|
||
| $timeout && usleep($timeout); | ||
| $this->sleep($seconds, $nanoseconds); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * Proxy for built-in stream_select method. | ||
| * | ||
| * @param array $read | ||
| * @param array $write | ||
| * @param array $except | ||
| * @param int|null $seconds | ||
| * @param int|null $microseconds | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should only accept |
||
| * | ||
| * @return int | ||
| */ | ||
| protected function doSelectStream(array &$read, array &$write, array &$except, $seconds, $microseconds = null) | ||
| { | ||
| // suppress warnings that occur, when stream_select is interrupted by a signal | ||
| return @stream_select($read, $write, $except, $seconds, $microseconds); | ||
| } | ||
|
|
||
| /** | ||
| * Sleeps for $seconds and $nanoseconds. | ||
| * | ||
| * @param int|null $seconds | ||
| * @param int|null $nanoseconds | ||
| */ | ||
| protected function sleep($seconds, $nanoseconds = 0) | ||
| { | ||
| if ($seconds === null || $nanoseconds === null) { | ||
| return; | ||
| } | ||
| if ($seconds > 0 || $nanoseconds > 0) { | ||
| time_nanosleep($seconds, $nanoseconds); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to see explicit handling of |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strict comparision (
===)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsor,
$timeis float, so strict comparison will always returnfalse.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 (maybe add a comment that loose comparision is intentional)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsor I added more description about this tricky
ifin 3c8a5d6 😄