Skip to content
This repository was archived by the owner on May 25, 2020. It is now read-only.
This repository was archived by the owner on May 25, 2020. It is now read-only.

Error with application state resetting (when http kernel generates an exception) #42

Closed
@tarampampam

Description

@tarampampam

Describe the bug

In exceptional situations (eg.: some important external services, that used by application - is unavailable) application state between requests are not reseted.

When some error occured - first request generates error, second shows "random" application page.

Also I think - behaviour can be reproduced then exception will be thrown on HTTP middleware termination.

Expected behaviour

Each incoming request must be processed with "fresh" application state.

System information

Please, complete the following information:

  • PHP version(s): 7.2.14 (in docker)
  • Package version(s): 3.1.0

Additional context

Error screenshot:

image

"Fast fix" idea:

diff --git a/src/Worker.php b/src/Worker.php
index 8f102d1..75c3759 100644
--- a/src/Worker.php
+++ b/src/Worker.php
@@ -71,18 +71,27 @@ class Worker implements WorkerInterface
             $http_kernel = $sandbox->make(HttpKernelContract::class);
 
             try {
-                $this->fireEvent($sandbox, new Events\BeforeLoopIterationEvent($sandbox, $req));
-                $request = Request::createFromBase($http_factory->createRequest($req));
-
-                $this->fireEvent($sandbox, new Events\BeforeRequestHandlingEvent($sandbox, $request));
-                $response = $http_kernel->handle($request);
-                $this->fireEvent($sandbox, new Events\AfterRequestHandlingEvent($sandbox, $request, $response));
-
-                $psr7_response = $psr7_factory->createResponse($response);
-                $psr7_client->respond($psr7_response);
-                $http_kernel->terminate($request, $response);
-
-                $this->fireEvent($sandbox, new Events\AfterLoopIterationEvent($sandbox, $request, $response));
+                try {
+                    $this->fireEvent($sandbox, new Events\BeforeLoopIterationEvent($sandbox, $req));
+                    $request = Request::createFromBase($http_factory->createRequest($req));
+
+                    $this->fireEvent($sandbox, new Events\BeforeRequestHandlingEvent($sandbox, $request));
+                    $response = $http_kernel->handle($request);
+                    $this->fireEvent($sandbox, new Events\AfterRequestHandlingEvent($sandbox, $request, $response));
+
+                    $psr7_response = $psr7_factory->createResponse($response);
+                    $psr7_client->respond($psr7_response);
+                    $http_kernel->terminate($request, $response);
+                } catch (\Throwable $e) {
+                    if (
+                        isset($request, $response) && $request instanceof \Symfony\Component\HttpFoundation\Request
+                        && $response instanceof \Symfony\Component\HttpFoundation\Response
+                    ) {
+                        $this->fireEvent($sandbox, new Events\AfterLoopIterationEvent($sandbox, $request, $response));
+                    }
+
+                    throw $e;
+                }
             } catch (\Throwable $e) {
                 $psr7_client->getWorker()->error((string) $e);
             } finally {

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions