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');