Skip to content

PHPLIB-501: Prevent memory leaks from generators #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions src/Model/CachingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
namespace MongoDB\Model;

use Countable;
use Generator;
use Iterator;
use IteratorIterator;
use Traversable;
use function count;
use function current;
Expand All @@ -41,7 +41,7 @@ class CachingIterator implements Countable, Iterator
/** @var array */
private $items = [];

/** @var Generator */
/** @var IteratorIterator */
private $iterator;

/** @var boolean */
Expand All @@ -61,7 +61,9 @@ class CachingIterator implements Countable, Iterator
*/
public function __construct(Traversable $traversable)
{
$this->iterator = $this->wrapTraversable($traversable);
$this->iterator = new IteratorIterator($traversable);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to just prepare the iterator here and store the current item? That might allow you to remove the prepareIterator() calls from other methods.

As-is, there might be a subtle behavioral change, since the original implementation did rewind the traversable, create the generator, and iterate once to store the current item?


$this->iterator->rewind();
$this->storeCurrentItem();
}

Expand Down Expand Up @@ -101,8 +103,12 @@ public function key()
public function next()
{
if (! $this->iteratorExhausted) {
$this->iteratorAdvanced = true;
$this->iterator->next();

$this->storeCurrentItem();

$this->iteratorExhausted = ! $this->iterator->valid();
}

next($this->items);
Expand Down Expand Up @@ -156,20 +162,4 @@ private function storeCurrentItem()

$this->items[$key] = $this->iterator->current();
}

/**
* Wraps the Traversable with a Generator.
*
* @param Traversable $traversable
* @return Generator
*/
private function wrapTraversable(Traversable $traversable)
{
foreach ($traversable as $key => $value) {
yield $key => $value;
$this->iteratorAdvanced = true;
}

$this->iteratorExhausted = true;
}
}
9 changes: 1 addition & 8 deletions tests/Model/CachingIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testIterationWithEmptySet()

public function testPartialIterationDoesNotExhaust()
{
$traversable = $this->getTraversableThatThrows([1, 2, new Exception()]);
$traversable = $this->getTraversable([1, 2, new Exception()]);
$iterator = new CachingIterator($traversable);

$expectedKey = 0;
Expand Down Expand Up @@ -110,13 +110,6 @@ public function testCountWithEmptySet()
}

private function getTraversable($items)
{
foreach ($items as $item) {
yield $item;
}
}

private function getTraversableThatThrows($items)
{
foreach ($items as $item) {
if ($item instanceof Exception) {
Expand Down