Skip to content

Commit e5c4190

Browse files
committed
Fix RequestStack state if throwable is thrown
1 parent ade30f7 commit e5c4190

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
7676
{
7777
$request->headers->set('X-Php-Ob-Level', (string) ob_get_level());
7878

79+
$this->requestStack->push($request);
7980
try {
8081
return $this->handleRaw($request, $type);
8182
} catch (\Exception $e) {
@@ -89,6 +90,8 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
8990
}
9091

9192
return $this->handleThrowable($e, $request, $type);
93+
} finally {
94+
$this->requestStack->pop();
9295
}
9396
}
9497

@@ -127,8 +130,6 @@ public function terminateWithException(\Throwable $exception, Request $request =
127130
*/
128131
private function handleRaw(Request $request, int $type = self::MASTER_REQUEST): Response
129132
{
130-
$this->requestStack->push($request);
131-
132133
// request
133134
$event = new RequestEvent($this, $request, $type);
134135
$this->dispatcher->dispatch($event, KernelEvents::REQUEST);
@@ -205,7 +206,6 @@ private function filterResponse(Response $response, Request $request, int $type)
205206
private function finishRequest(Request $request, int $type)
206207
{
207208
$this->dispatcher->dispatch(new FinishRequestEvent($this, $request, $type), KernelEvents::FINISH_REQUEST);
208-
$this->requestStack->pop();
209209
}
210210

211211
/**

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests;
1313

14+
use Error;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Component\EventDispatcher\EventDispatcher;
1617
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -39,6 +40,45 @@ public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
3940
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
4041
}
4142

43+
public function testRequestStackIsNotBrokenWhenControllerThrowsAnExceptionAndCatchIsTrue()
44+
{
45+
$requestStack = new RequestStack();
46+
$kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); }, $requestStack);
47+
48+
try {
49+
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
50+
} catch (\Throwable $exception) {
51+
}
52+
53+
self::assertNull($requestStack->getCurrentRequest());
54+
}
55+
56+
public function testRequestStackIsNotBrokenWhenControllerThrowsAnExceptionAndCatchIsFalse()
57+
{
58+
$requestStack = new RequestStack();
59+
$kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); }, $requestStack);
60+
61+
try {
62+
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
63+
} catch (\Throwable $exception) {
64+
}
65+
66+
self::assertNull($requestStack->getCurrentRequest());
67+
}
68+
69+
public function testRequestStackIsNotBrokenWhenControllerThrowsAnThrowable()
70+
{
71+
$requestStack = new RequestStack();
72+
$kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new Error(); }, $requestStack);
73+
74+
try {
75+
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
76+
} catch (\Throwable $exception) {
77+
}
78+
79+
self::assertNull($requestStack->getCurrentRequest());
80+
}
81+
4282
public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered()
4383
{
4484
$this->expectException(\RuntimeException::class);

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