Skip to content

Commit

Permalink
[HttpKernel] Fix empty request stack when terminating with exception
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyc authored and nicolas-grekas committed Oct 18, 2022
1 parent d84ce93 commit 3f61170
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 3f61170

Please sign in to comment.