Skip to content

Commit

Permalink
bug #47857 [HttpKernel] Fix empty request stack when terminating with…
Browse files Browse the repository at this point in the history
… exception (krzyc)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[HttpKernel] Fix empty request stack when terminating with exception

| Q             | A
| ------------- | ---
| Branch?       | 4.4, 5.4, 6.0, 6.1, 6.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #47577
| License       | MIT
| Doc PR        |

After symfony/symfony#47358 the RequestStack is empty when request terminates with exception, which prevents SecurityDataCollector to generate logout URL and generates fatal error.

Commits
-------

e4d6e7b4ba [HttpKernel] Fix empty request stack when terminating with exception
  • Loading branch information
nicolas-grekas committed Oct 18, 2022
2 parents 9a34f1a + 3f61170 commit 26989b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
12 changes: 11 additions & 1 deletion HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,17 @@ public function terminateWithException(\Throwable $exception, Request $request =
throw $exception;
}

$response = $this->handleThrowable($exception, $request, self::MASTER_REQUEST);
if ($pop = $request !== $this->requestStack->getMasterRequest()) {
$this->requestStack->push($request);
}

try {
$response = $this->handleThrowable($exception, $request, self::MASTER_REQUEST);
} finally {
if ($pop) {
$this->requestStack->pop();
}
}

$response->sendHeaders();
$response->sendContent();
Expand Down
17 changes: 17 additions & 0 deletions Tests/HttpKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ControllerDoesNotReturnResponseException;
Expand Down Expand Up @@ -340,6 +341,22 @@ public function testTerminate()
$this->assertEquals($response, $capturedResponse);
}

public function testTerminateWithException()
{
$dispatcher = new EventDispatcher();
$requestStack = new RequestStack();
$kernel = $this->getHttpKernel($dispatcher, null, $requestStack);

$dispatcher->addListener(KernelEvents::EXCEPTION, function (ExceptionEvent $event) use (&$capturedRequest, $requestStack) {
$capturedRequest = $requestStack->getCurrentRequest();
$event->setResponse(new Response());
});

$kernel->terminateWithException(new \Exception('boo'), $request = Request::create('/'));
$this->assertSame($request, $capturedRequest);
$this->assertNull($requestStack->getCurrentRequest());
}

public function testVerifyRequestStackPushPopDuringHandle()
{
$request = new Request();
Expand Down

0 comments on commit 26989b2

Please sign in to comment.