Skip to content

Commit

Permalink
Avoid garbage reference by hiding resolver from call stack on PHP 7+
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed May 4, 2018
1 parent 24923df commit c00b39f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public function __construct(callable $resolver, callable $canceller = null)
{
$this->canceller = $canceller;

$this->call($resolver);
// Explicitly overwrite arguments with null values before invoking
// resolver function. This ensure that these arguments do not show up
// in the stack trace in PHP 7+ only.
$cb = $resolver;
$resolver = $canceller = null;
$this->call($cb);
}

public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
Expand Down
18 changes: 18 additions & 0 deletions tests/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReference
$this->assertSame(0, gc_collect_cycles());
}

/**
* @test
* @requires PHP 7
* @see self::shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException
*/
public function shouldRejectWithoutCreatingGarbageCyclesIfResolverWithReferenceThrowsException()
{
gc_collect_cycles();

$promise = new Promise(function () use (&$promise) {
throw new \Exception('foo');
});

unset($promise);

$this->assertSame(0, gc_collect_cycles());
}

/** @test */
public function shouldIgnoreNotifyAfterReject()
{
Expand Down

0 comments on commit c00b39f

Please sign in to comment.