From b2b68c61b869c87e6250f9a24f5bff6367aa924f Mon Sep 17 00:00:00 2001 From: Steve Rhoades Date: Wed, 3 Sep 2014 17:39:07 -0700 Subject: [PATCH] Resolve segfaulting issues with pecl-ev --- .travis.yml | 5 ++++- script.gdb | 3 +++ src/ExtEvLoop.php | 29 +++++++++++++++++++++++++---- tests/AbstractLoopTest.php | 19 ------------------- travis-init.sh | 1 + 5 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 script.gdb diff --git a/.travis.yml b/.travis.yml index 0b94b225..4460f900 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,10 @@ matrix: allow_failures: - php: hhvm +before_script: + - sudo apt-get install gdb + install: ./travis-init.sh script: - - phpunit --coverage-text + - phpunit --coverage-text diff --git a/script.gdb b/script.gdb new file mode 100644 index 00000000..34e270dc --- /dev/null +++ b/script.gdb @@ -0,0 +1,3 @@ +set env MALLOC_CHECK_=3 +run +backtrace full \ No newline at end of file diff --git a/src/ExtEvLoop.php b/src/ExtEvLoop.php index f383c37a..26235e54 100644 --- a/src/ExtEvLoop.php +++ b/src/ExtEvLoop.php @@ -16,6 +16,7 @@ class ExtEvLoop implements LoopInterface private $timers; private $readEvents = array(); private $writeEvents = array(); + private $running = false; public function __construct() @@ -58,7 +59,7 @@ public function removeReadStream($stream) { $key = (int) $stream; if(isset($this->readEvents[$key])) { - $this->readEvents[$key]->stop(); + $this->readEvents[$key]->stop(); unset($this->readEvents[$key]); } } @@ -72,8 +73,8 @@ public function removeWriteStream($stream) { $key = (int) $stream; if(isset($this->writeEvents[$key])) { - $this->writeEvents[(int)$stream]->stop(); - unset($this->writeEvents[(int)$stream]); + $this->writeEvents[$key]->stop(); + unset($this->writeEvents[$key]); } } @@ -147,7 +148,11 @@ public function cancelTimer(TimerInterface $timer) if (isset($this->timers[$timer])) { /* stop EvTimer */ $this->timers[$timer]->stop(); - $this->timers->detach($timer); + + /* defer timer */ + $this->nextTick(function() use ($timer) { + $this->timers->detach($timer); + }); } } @@ -225,4 +230,20 @@ public function stop() { $this->running = false; } + + public function __destruct() + { + // mannually stop all watchers + foreach($this->timers as $timer) { + $this->timers[$timer]->stop(); + } + + foreach($this->readEvents as $event) { + $event->stop(); + } + + foreach($this->writeEvents as $event) { + $event->stop(); + } + } } diff --git a/tests/AbstractLoopTest.php b/tests/AbstractLoopTest.php index ffcb9ea2..b4095379 100644 --- a/tests/AbstractLoopTest.php +++ b/tests/AbstractLoopTest.php @@ -355,25 +355,6 @@ function () { $this->loop->run(); } - public function testPeriodTimerExecutes() - { - $count = 0; - $this->loop->addPeriodicTimer( - 0.001, - function ($timer) use (&$count) { - echo 'tick'; - $count++; - if($count === 3) { - $timer->cancel(); - } - } - ); - - $this->expectOutputString('tickticktick'); - - $this->loop->run(); - } - public function testFutureTick() { $called = false; diff --git a/travis-init.sh b/travis-init.sh index 397e851c..acd6c98c 100755 --- a/travis-init.sh +++ b/travis-init.sh @@ -27,6 +27,7 @@ if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then phpize ./configure make + # make test make install popd echo "extension=ev.so" >> "$(php -r 'echo php_ini_loaded_file();')"