From 6e52f3e211f4c27bf7eef5b38d7b104e8ab66a8c Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Tue, 16 May 2017 18:17:12 +0200 Subject: [PATCH] Fix potential issues when operating on destroyed uv handles in shutdown sequence --- lib/Loop/UvDriver.php | 16 +++++++------- phpunit.xml.dist | 3 +++ test/Loop/UvLoopDestructShutdown.phpt | 30 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 test/Loop/UvLoopDestructShutdown.phpt diff --git a/lib/Loop/UvDriver.php b/lib/Loop/UvDriver.php index 74bd9d5..496c06c 100644 --- a/lib/Loop/UvDriver.php +++ b/lib/Loop/UvDriver.php @@ -260,34 +260,34 @@ class UvDriver extends Driver { } $event = $this->events[$id]; - $eventId = (int) $event; + + if (!\uv_is_active($event)) { + return; + } switch ($watcher->type) { case Watcher::READABLE: case Watcher::WRITABLE: $flags = 0; + $eventId = (int) $event; foreach ($this->watchers[$eventId] as $watcher) { $flags |= $watcher->enabled ? $watcher->type : 0; } if ($flags) { \uv_poll_start($event, $flags, $this->ioCallback); - } elseif (\uv_is_active($event)) { + } else { \uv_poll_stop($event); } break; case Watcher::DELAY: case Watcher::REPEAT: - if (\uv_is_active($event)) { - \uv_timer_stop($event); - } + \uv_timer_stop($event); break; case Watcher::SIGNAL: - if (\uv_is_active($event)) { - \uv_signal_stop($event); - } + \uv_signal_stop($event); break; default: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ce62c46..70319e1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,6 +16,9 @@ test + + test + diff --git a/test/Loop/UvLoopDestructShutdown.phpt b/test/Loop/UvLoopDestructShutdown.phpt new file mode 100644 index 0000000..df68d2d --- /dev/null +++ b/test/Loop/UvLoopDestructShutdown.phpt @@ -0,0 +1,30 @@ +--TEST-- +Test order of destruction not interfering with access to UV handles +--SKIPIF-- + +--FILE-- +handle = Loop::repeat(10, function() {}); + } + function __destruct() { + Loop::cancel($this->handle); + print "ok"; + } + }); + Loop::delay(0, [Loop::class, "stop"]); +}); + +?> +--EXPECT-- +ok