8000 bug #47358 Fix broken request stack state if throwable is thrown. (Wa… · symfony/symfony@17a8daf · GitHub
[go: up one dir, main page]

Skip to content

Commit 17a8daf

Browse files
committed
bug #47358 Fix broken request stack state if throwable is thrown. (Warxcell)
This PR was merged into the 4.4 branch. Discussion ---------- Fix broken request stack state if throwable is thrown. | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A When using long-running server (like roadrunner) if Throwable is thrown - requestStack is never pop out and it stays in broken state and next requests receive previous request object. Commits ------- e35d6ba Fix RequestStack state if throwable is thrown
2 parents ade30f7 + e35d6ba commit 17a8daf

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,45 @@ public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
3939
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
4040
}
4141

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

0 commit comments

Comments
 (0)
0