From f0db83f49e732622f8716fcbd8c3135ebc26d244 Mon Sep 17 00:00:00 2001 From: Dejan Markic Date: Wed, 25 Sep 2024 17:05:48 +0200 Subject: [PATCH] Get rid of all memory leaks due to removeAllListeners() are not called. Executor and QueryCommand mostly --- src/Io/Connection.php | 6 ++++++ src/Io/Executor.php | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Io/Connection.php b/src/Io/Connection.php index 73313ba..1a3d295 100644 --- a/src/Io/Connection.php +++ b/src/Io/Connection.php @@ -179,6 +179,12 @@ public function close() } $this->emit('close'); + /** + * MemoryLeak: Remove all listeners from executor, so it + * will be removed from memory when connection + * is closed. + */ + $this->executor->removeAllListeners(); $this->removeAllListeners(); } diff --git a/src/Io/Executor.php b/src/Io/Executor.php index 4452907..eb1f727 100644 --- a/src/Io/Executor.php +++ b/src/Io/Executor.php @@ -23,6 +23,28 @@ public function isIdle() public function enqueue($command) { + /** + * MemoryLeak: Make sure removeAllListeners is called on commands, + * otherwise they might stay in memory for ever. + */ + $command->on( + 'error', + function () use ($command) { + $command->removeAllListeners(); + } + ); + $command->on( + 'success', + function () use ($command) { + $command->removeAllListeners(); + } + ); + $command->on( + 'end', + function () use ($command) { + $command->removeAllListeners(); + } + ); $this->queue->enqueue($command); $this->emit('new');