Skip to content

[HttpKernel] Avoid memory leaks cache attribute, profiler listener #60934

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
param('profiler_listener.only_main_requests'),
])
->tag('kernel.event_subscriber')
->tag('kernel.reset', ['method' => 'reset'])

->set('console_profiler_listener', ConsoleProfilerListener::class)
->args([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@

->set('controller.cache_attribute_listener', CacheAttributeListener::class)
->tag('kernel.event_subscriber')
->tag('kernel.reset', ['method' => 'reset'])

->set('controller.helper', ControllerHelper::class)
->tag('container.service_subscriber')
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"symfony/error-handler": "^7.3|^8.0",
"symfony/event-dispatcher": "^6.4|^7.0|^8.0",
"symfony/http-foundation": "^7.3|^8.0",
"symfony/http-kernel": "^7.2|^8.0",
"symfony/http-kernel": "^7.4|^8.0",
"symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "^7.1|^8.0",
"symfony/finder": "^6.4|^7.0|^8.0",
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

7.4
---

* `Symfony\Component\HttpKernel\EventListener\CacheAttributeListener` is now resettable to prevent memory leaks
* `Symfony\Component\HttpKernel\EventListener\ProfilerListener` is now resettable to prevent memory leaks

7.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\Service\ResetInterface;

/**
* Handles HTTP cache headers configured via the Cache attribute.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class CacheAttributeListener implements EventSubscriberInterface
class CacheAttributeListener implements EventSubscriberInterface, ResetInterface
{
/**
* @var \SplObjectStorage<Request, \DateTimeInterface>
Expand Down Expand Up @@ -182,6 +183,12 @@ public static function getSubscribedEvents(): array
];
}

public function reset(): void
{
$this->lastModified = new \SplObjectStorage();
$this->etags = new \SplObjectStorage();
}

private function getExpressionLanguage(): ExpressionLanguage
{
return $this->expressionLanguage ??= class_exists(ExpressionLanguage::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Contracts\Service\ResetInterface;

/**
* ProfilerListener collects data for the current request by listening to the kernel events.
Expand All @@ -30,7 +31,7 @@
*
* @final
*/
class ProfilerListener implements EventSubscriberInterface
class ProfilerListener implements EventSubscriberInterface, ResetInterface
{
private ?\Throwable $exception = null;
/** @var \SplObjectStorage<Request, Profile> */
Expand Down Expand Up @@ -129,8 +130,7 @@ public function onKernelTerminate(TerminateEvent $event): void
$this->profiler->saveProfile($this->profiles[$request]);
}

$this->profiles = new \SplObjectStorage();
$this->parents = new \SplObjectStorage();
$this->reset();
Copy link
Member

Choose a reason for hiding this comment

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

we don't need this call then, since reset is going to be called anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just trying to preserve the old logic.
I'll drop it if it's not necessary then

}

public static function getSubscribedEvents(): array
Expand All @@ -141,4 +141,11 @@ public static function getSubscribedEvents(): array
KernelEvents::TERMINATE => ['onKernelTerminate', -1024],
];
}

public function reset(): void
{
$this->profiles = new \SplObjectStorage();
$this->parents = new \SplObjectStorage();
$this->exception = null;
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"symfony/event-dispatcher": "^7.3|^8.0",
"symfony/http-foundation": "^7.3|^8.0",
"symfony/polyfill-ctype": "^1.8",
"symfony/service-contracts": "^2.5|^3",
"psr/log": "^1|^2|^3"
},
"require-dev": {
Expand Down
Loading