From de824d16fc2a269a5cda81a2cfddf95d1a872a69 Mon Sep 17 00:00:00 2001 From: lisag Date: Wed, 27 Jan 2021 22:08:45 +0800 Subject: [PATCH 1/2] fix Coroutine\Barrier mem leak --- src/core/Coroutine/Barrier.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/core/Coroutine/Barrier.php b/src/core/Coroutine/Barrier.php index f7645956..9bf5806d 100644 --- a/src/core/Coroutine/Barrier.php +++ b/src/core/Coroutine/Barrier.php @@ -25,17 +25,17 @@ class Barrier public function __destruct() { - if ($this->timer != -1) { + if ($this->timer !== -1) { Timer::clear($this->timer); - if (isset(static::$cancel_list[$this->cid])) { - unset(static::$cancel_list[$this->cid]); + if (isset(self::$cancel_list[$this->cid])) { + unset(self::$cancel_list[$this->cid]); return; } } - if ($this->cid != -1 && $this->cid != Coroutine::getCid()) { + if ($this->cid !== -1 && $this->cid !== Coroutine::getCid()) { Coroutine::resume($this->cid); } else { - static::$cancel_list[$this->cid] = true; + self::$cancel_list[$this->cid] = true; } } @@ -45,16 +45,19 @@ public static function make() } /** + * @param Barrier $barrier + * @param float|int $timeout + * * @throws Exception */ public static function wait(Barrier &$barrier, float $timeout = -1) { - if ($barrier->cid != -1) { + if ($barrier->cid !== -1) { throw new Exception('The barrier is waiting, cannot wait again.'); } $cid = Coroutine::getCid(); $barrier->cid = $cid; - if ($timeout > 0 && ($timeout_ms = intval($timeout * 1000)) > 0) { + if ($timeout > 0 && ($timeout_ms = (int)($timeout * 1000)) > 0) { $barrier->timer = Timer::after($timeout_ms, function () use ($cid) { self::$cancel_list[$cid] = true; Coroutine::resume($cid); @@ -63,6 +66,8 @@ public static function wait(Barrier &$barrier, float $timeout = -1) $barrier = null; if (!isset(self::$cancel_list[$cid])) { Coroutine::yield(); + } else { + unset(self::$cancel_list[$cid]); } } } From ef36f2b8bccea2f30db5cce1aa20874d867a0eb1 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Thu, 28 Jan 2021 11:33:06 +0800 Subject: [PATCH 2/2] Format code --- src/core/Coroutine/Barrier.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/core/Coroutine/Barrier.php b/src/core/Coroutine/Barrier.php index 9bf5806d..442a96d3 100644 --- a/src/core/Coroutine/Barrier.php +++ b/src/core/Coroutine/Barrier.php @@ -45,9 +45,6 @@ public static function make() } /** - * @param Barrier $barrier - * @param float|int $timeout - * * @throws Exception */ public static function wait(Barrier &$barrier, float $timeout = -1) @@ -57,7 +54,7 @@ public static function wait(Barrier &$barrier, float $timeout = -1) } $cid = Coroutine::getCid(); $barrier->cid = $cid; - if ($timeout > 0 && ($timeout_ms = (int)($timeout * 1000)) > 0) { + if ($timeout > 0 && ($timeout_ms = (int) ($timeout * 1000)) > 0) { $barrier->timer = Timer::after($timeout_ms, function () use ($cid) { self::$cancel_list[$cid] = true; Coroutine::resume($cid);