-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
Closed
ivkalita
wants to merge
7
commits into
reactphp:master
from
ivkalita:stream-select-loop-int-overflow
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9021dc8
Add StreamSelectLoop time conversion methods.
ivkalita 8279d9b
Change StreamSelectLoop to keep timeout in seconds, replace usleep wi…
ivkalita 4400ede
Fix StreamSelectLoopTest::testSmallTimerInterval to expect nanoseconds.
ivkalita 08c709b
Add StreamSelectLoop test for PHP_INT_MAX seconds timer interval.
ivkalita 4347fa3
Change StreamSelectLoop::streamSelect null-timeout handling.
ivkalita 3c8a5d6
Add explanation about StreamSelectLoop::getSeconds int overflow worka…
ivkalita bb81383
Move StreamSelectLoop::streamSelect micro- and nanoseconds calculatio…
ivkalita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,118 @@ private function waitForStreamActivity($timeout) | |
} | ||
} | ||
|
||
/** | ||
* Returns integer amount of seconds in $time. | ||
* | ||
* @param float $time – time in seconds | ||
* | ||
* @return int | ||
*/ | ||
private static function getSeconds($time) | ||
{ | ||
/* | ||
* intval(floor($time)) will produce int overflow | ||
* if $time is (float)PHP_INT_MAX: | ||
* (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 | ||
* ----------------------------------------------- | ||
* Loose comparision is intentional, cause $time | ||
* is float and | ||
* (float)PHP_INT_MAX !== PHP_INT_MAX | ||
*/ | ||
if ($time == PHP_INT_MAX) { | ||
return PHP_INT_MAX; | ||
} | ||
|
||
return intval(floor($time)); | ||
} | ||
|
||
/** | ||
* Returns integer amount of microseconds in $time. | ||
* | ||
* @param float $time – time in seconds | ||
* | ||
* @return int | ||
*/ | ||
private static function getMicroseconds($time) | ||
{ | ||
$fractional = fmod($time, 1); | ||
$microseconds = round($fractional * self::MICROSECONDS_PER_SECOND); | ||
|
||
return intval($microseconds); | ||
} | ||
|
||
/** | ||
* Returns integer amount of nanoseconds in $time. | ||
* The precision is 1 microsecond. | ||
* | ||
* @param float $time – time in seconds | ||
* | ||
* @return int | ||
*/ | ||
private static function getNanoseconds($time) | ||
{ | ||
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. | ||
* | ||
* @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 = $timeout === null ? null : self::getSeconds($timeout); | ||
|
||
if ($read || $write) { | ||
$except = null; | ||
$except = []; | ||
$microseconds = $timeout === null ? 0 : self::getMicroseconds($timeout); | ||
|
||
// 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); | ||
if ($timeout !== null) { | ||
$nanoseconds = self::getNanoseconds($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 $microseconds | ||
* | ||
* @return int | ||
*/ | ||
protected function doSelectStream(array &$read, array &$write, array &$except, $seconds, $microseconds) | ||
{ | ||
// 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 $seconds | ||
* @param int $nanoseconds | ||
*/ | ||
protected function sleep($seconds, $nanoseconds = 0) | ||
{ | ||
if ($seconds > 0 || $nanoseconds > 0) { | ||
time_nanosleep($seconds, $nanoseconds); | ||
} | ||
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 |
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a rather subtle BC break as consumers could possibly rely on this
protected
API. Not saying this is an issue with your proposed change, it's more that wescrewed upcould have done better with defining our external API.