Skip to content

Commit

Permalink
Fix memory leak in blocking action due to event listener that was nev…
Browse files Browse the repository at this point in the history
…er removed
  • Loading branch information
brstgt committed Apr 2, 2018
1 parent 48123ef commit f7df57a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Protocol/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ protected function blockingAction(CommandInterface $command, $eventName, callabl
function (CommandInterface $sentCommand) use ($deferred, $eventName, $handler) {
$successListener = null;
$failListener = function () use ($deferred, $eventName, &$successListener) {
$this->connection->removeListener($eventName, $successListener);
$deferred->reject(new ConnectionLostException());
};
$successListener = function (CommandInterface $recvCommand) use ($sentCommand, $deferred, $handler, &$failListener) {
$this->connection->removeListener('close', $failListener);
$successListener = function (CommandInterface $recvCommand) use ($sentCommand, $deferred, $handler) {
// if the result is not NULL resolve the deferred action with the handler's result
// if the result is NULL we assume the handler communicated the result on the passed in deferred
// itself
Expand All @@ -131,8 +129,12 @@ function (CommandInterface $sentCommand) use ($deferred, $eventName, $handler) {
$deferred->resolve($result);
}
};
$this->connection->once('close', $failListener);
$this->connection->once($eventName, $successListener);
$deferred->promise()->always(function () use ($successListener, $failListener, $eventName) {
$this->connection->removeListener('close', $failListener);
$this->connection->removeListener($eventName, $successListener);
});
$this->connection->on('close', $failListener);
$this->connection->on($eventName, $successListener);
},
function ($e) use ($deferred) {
$deferred->reject($e);
Expand Down

0 comments on commit f7df57a

Please sign in to comment.