Description
Symfony version(s) affected
all
Description
Related to #45205 issue, which was ignored...
When an exception is thrown in a kernel.terminate
event,
kernel.exception
is thrown- the Symfony ErrorListener::onKernelException handler the exception
$response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
is called- this trigger the
ErrorController
and then theTwigErrorRenderer
- The response rendered seems useless since the response was already sent before the terminate event. (cf https://symfony.com/doc/current/components/http_kernel.html#8-the-kernel-terminate-event)
- It can produce extra-useless exception, in my case I have a custom view
TwigBundle/Exception/error.html.twig
which use the session and/or flashbag and I'm getting someFailed to start the session because headers have already been sent
.
Introducing a kernel.terminate_exception
and using it instead of kernel.exception
could even appear as a BC break for kernel.exception
listeners or a bugfix because according to the schema in the doc https://symfony.com/doc/current/components/http_kernel.html#handling-exceptions-the-kernel-exception-event, we can see the order Exception -> Response -> Terminate
.
How to reproduce
- Throw an exception in a
kernel.terminate
listener -> You'll get extra code executed for no reason - Add
app.session.flashbag.get('success')
call in a customTwigBundle/Exception/error.html.twig
template. -> You'll get failed to start the session error in your logs.
Possible Solution
Either throw a different event for exception thrown after the terminate exception
Either, at least provide a ExceptionEvent::isTerminated
or TerminableInterface::isTerminated
, which would allow to write
if ($thing->isTerminated()) {
return;
}
at the beginning of some ExceptionListener like the ErrorListener
Additional Context
No response