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