Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] ErrorListener::onKernelException should ignore exception thrown after kernel.terminate #51912

Closed
@VincentLanglet

Description

@VincentLanglet

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 the TwigErrorRenderer
  1. 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)
  2. 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 some Failed 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 custom TwigBundle/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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions