Skip to content

Commit dffff8e

Browse files
bug #47857 [HttpKernel] Fix empty request stack when terminating with 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 #47358 the RequestStack is empty when request terminates with exception, which prevents SecurityDataCollector to generate logout URL and generates fatal error. Commits ------- e4d6e7b [HttpKernel] Fix empty request stack when terminating with exception
2 parents 323c713 + e4d6e7b commit dffff8e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ public function terminateWithException(\Throwable $exception, Request $request =
112112
throw $exception;
113113
}
114114

115-
$response = $this->handleThrowable($exception, $request, self::MASTER_REQUEST);
115+
if ($pop = $request !== $this->requestStack->getMasterRequest()) {
116+
$this->requestStack->push($request);
117+
}
118+
119+
try {
120+
$response = $this->handleThrowable($exception, $request, self::MASTER_REQUEST);
121+
} finally {
122+
if ($pop) {
123+
$this->requestStack->pop();
124+
}
125+
}
116126

117127
$response->sendHeaders();
118128
$response->sendContent();

src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\HttpFoundation\Response;
2121
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
2222
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
23+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
2324
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
2425
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2526
use Symfony\Component\HttpKernel\Exception\ControllerDoesNotReturnResponseException;
@@ -340,6 +341,22 @@ public function testTerminate()
340341
$this->assertEquals($response, $capturedResponse);
341342
}
342343

344+
public function testTerminateWithException()
345+
{
346+
$dispatcher = new EventDispatcher();
347+
$requestStack = new RequestStack();
348+
$kernel = $this->getHttpKernel($dispatcher, null, $requestStack);
349+
350+
$dispatcher->addListener(KernelEvents::EXCEPTION, function (ExceptionEvent $event) use (&$capturedRequest, $requestStack) {
351+
$capturedRequest = $requestStack->getCurrentRequest();
352+
$event->setResponse(new Response());
353+
});
354+
355+
$kernel->terminateWithException(new \Exception('boo'), $request = Request::create('/'));
356+
$this->assertSame($request, $capturedRequest);
357+
$this->assertNull($requestStack->getCurrentRequest());
358+
}
359+
343360
public function testVerifyRequestStackPushPopDuringHandle()
344361
{
345362
$request = new Request();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy