From 3f1f9f41ca50bfe903a5e30bbc07c98a7e141205 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Thu, 11 May 2017 10:39:31 -0500 Subject: [PATCH] Do not call uv_close() if other watchers on the same stream exist --- lib/Loop/UvDriver.php | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/Loop/UvDriver.php b/lib/Loop/UvDriver.php index 3de1aa9..6436d44 100644 --- a/lib/Loop/UvDriver.php +++ b/lib/Loop/UvDriver.php @@ -60,7 +60,7 @@ class UvDriver extends Driver { } foreach ($watchers as $watcher) { - if (!($watcher->type & $events)) { + if (!($watcher->enabled && $watcher->type & $events)) { continue; } @@ -146,11 +146,16 @@ class UvDriver extends Driver { $event = $this->events[$watcherId]; $eventId = (int) $event; - if (empty($this->watchers[$eventId])) { - if (isset($this->io[$eventId])) { - unset($this->streams[$this->io[$eventId]], $this->io[$eventId]); - } + if ($this->watchers[$eventId] instanceof Watcher) { // All except IO watchers. + unset($this->watchers[$eventId]); \uv_close($event); + } else { + unset($this->watchers[$eventId][$watcherId]); + + if (empty($this->watchers[$eventId])) { + unset($this->watchers[$eventId], $this->streams[$this->io[$eventId]], $this->io[$eventId]); + \uv_close($event); + } } unset($this->events[$watcherId]); @@ -201,7 +206,7 @@ class UvDriver extends Driver { $flags = 0; foreach ($this->watchers[$eventId] as $watcher) { - $flags |= $watcher->type; + $flags |= $watcher->enabled ? $watcher->type : 0; } \uv_poll_start($event, $flags, $this->ioCallback); break; @@ -260,33 +265,26 @@ class UvDriver extends Driver { switch ($watcher->type) { case Watcher::READABLE: case Watcher::WRITABLE: - unset($this->watchers[$eventId][$id]); + $flags = 0; + foreach ($this->watchers[$eventId] as $watcher) { + $flags |= $watcher->enabled ? $watcher->type : 0; + } - if (empty($this->watchers[$eventId])) { - unset($this->watchers[$eventId]); - - if (\uv_is_active($event)) { - \uv_poll_stop($event); - } - } else { - $flags = 0; - foreach ($this->watchers[$eventId] as $watcher) { - $flags |= $watcher->type; - } + if ($flags) { \uv_poll_start($event, $flags, $this->ioCallback); + } elseif (\uv_is_active($event)) { + \uv_poll_stop($event); } break; case Watcher::DELAY: case Watcher::REPEAT: - unset($this->watchers[$eventId]); if (\uv_is_active($event)) { \uv_timer_stop($event); } break; case Watcher::SIGNAL: - unset($this->watchers[$eventId]); if (\uv_is_active($event)) { \uv_signal_stop($event); }