Skip to content
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

Fix single scalar hydrator memory leak on exception #8483

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Expand Up @@ -205,12 +205,13 @@ public function hydrateAll($stmt, $resultSetMapping, array $hints = [])
$this->_hints = $hints;

$this->_em->getEventManager()->addEventListener([Events::onClear], $this);

$this->prepare();

$result = $this->hydrateAllData();

$this->cleanup();
try {
$result = $this->hydrateAllData();
} finally {
$this->cleanup();
}

return $result;
}
Expand Down
71 changes: 63 additions & 8 deletions tests/Doctrine/Tests/ORM/Hydration/AbstractHydratorTest.php
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit_Framework_MockObject_MockObject;
Expand Down Expand Up @@ -72,17 +73,26 @@ protected function setUp(): void
*/
public function testOnClearEventListenerIsDetachedOnCleanup(): void
{
$eventListenerHasBeenRegistered = false;

$this
->mockEventManager
->expects(self::at(0))
->expects(self::once())
->method('addEventListener')
->with([Events::onClear], $this->hydrator);
->with([Events::onClear], $this->hydrator)
->willReturnCallback(function () use (&$eventListenerHasBeenRegistered): void {
$this->assertFalse($eventListenerHasBeenRegistered);
$eventListenerHasBeenRegistered = true;
});

$this
->mockEventManager
->expects(self::at(1))
->expects(self::once())
->method('removeEventListener')
->with([Events::onClear], $this->hydrator);
->with([Events::onClear], $this->hydrator)
->willReturnCallback(function () use (&$eventListenerHasBeenRegistered): void {
$this->assertTrue($eventListenerHasBeenRegistered);
});

iterator_to_array($this->hydrator->iterate($this->mockStatement, $this->mockResultMapping));
}
Expand All @@ -92,18 +102,63 @@ public function testOnClearEventListenerIsDetachedOnCleanup(): void
*/
public function testHydrateAllRegistersAndClearsAllAttachedListeners(): void
{
$eventListenerHasBeenRegistered = false;

$this
->mockEventManager
->expects(self::at(0))
->expects(self::once())
->method('addEventListener')
->with([Events::onClear], $this->hydrator);
->with([Events::onClear], $this->hydrator)
->willReturnCallback(function () use (&$eventListenerHasBeenRegistered): void {
$this->assertFalse($eventListenerHasBeenRegistered);
$eventListenerHasBeenRegistered = true;
});

$this
->mockEventManager
->expects(self::at(1))
->expects(self::once())
->method('removeEventListener')
->with([Events::onClear], $this->hydrator);
->with([Events::onClear], $this->hydrator)
->willReturnCallback(function () use (&$eventListenerHasBeenRegistered): void {
$this->assertTrue($eventListenerHasBeenRegistered);
});

$this->hydrator->hydrateAll($this->mockStatement, $this->mockResultMapping);
}

/**
* @group #8482
*/
public function testHydrateAllClearsAllAttachedListenersEvenOnError(): void
{
$eventListenerHasBeenRegistered = false;

$this
->mockEventManager
->expects(self::once())
->method('addEventListener')
->with([Events::onClear], $this->hydrator)
->willReturnCallback(function () use (&$eventListenerHasBeenRegistered): void {
$this->assertFalse($eventListenerHasBeenRegistered);
$eventListenerHasBeenRegistered = true;
});

$this
->mockEventManager
->expects(self::once())
->method('removeEventListener')
->with([Events::onClear], $this->hydrator)
->willReturnCallback(function () use (&$eventListenerHasBeenRegistered): void {
$this->assertTrue($eventListenerHasBeenRegistered);
});

$this
->hydrator
->expects(self::once())
->method('hydrateAllData')
->willThrowException(new ORMException());

$this->expectException(ORMException::class);
$this->hydrator->hydrateAll($this->mockStatement, $this->mockResultMapping);
}
}