Skip to content

Commit

Permalink
Get rid of all memory leaks due to removeAllListeners() are not calle…
Browse files Browse the repository at this point in the history
…d. Executor and QueryCommand mostly
  • Loading branch information
Dejan Markic committed Sep 25, 2024
1 parent 914ff50 commit f0db83f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Io/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
22 changes: 22 additions & 0 deletions src/Io/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit f0db83f

Please sign in to comment.