This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Error with application state resetting (when http kernel generates an exception) #42
Labels
Comments
Sorry, but the quick fix did not work. We are continuing the investigation |
So, final worker diff is: diff --git a/src/Worker.php b/src/Worker.php
index 8f102d1..ffa6cb3 100644
--- a/src/Worker.php
+++ b/src/Worker.php
@@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace AvtoDev\RoadRunnerLaravel;
+use Throwable;
use RuntimeException;
use Illuminate\Http\Request;
use InvalidArgumentException;
@@ -58,6 +59,8 @@ class Worker implements WorkerInterface
$this->fireEvent($app, new Events\BeforeLoopStartedEvent($app));
while ($req = $psr7_client->acceptRequest()) {
+ $responded = false;
+
if ($refresh_app === true) {
$sandbox = $this->createApplication($this->base_path);
$this->bootstrapApplication($sandbox, $psr7_client);
@@ -69,6 +72,8 @@ class Worker implements WorkerInterface
/** @var HttpKernelContract $http_kernel */
$http_kernel = $sandbox->make(HttpKernelContract::class);
+ /** @var ConfigRepository $config */
+ $config = $sandbox->make(ConfigRepository::class);
try {
$this->fireEvent($sandbox, new Events\BeforeLoopIterationEvent($sandbox, $req));
@@ -80,11 +85,16 @@ class Worker implements WorkerInterface
$psr7_response = $psr7_factory->createResponse($response);
$psr7_client->respond($psr7_response);
+ $responded = true;
$http_kernel->terminate($request, $response);
$this->fireEvent($sandbox, new Events\AfterLoopIterationEvent($sandbox, $request, $response));
- } catch (\Throwable $e) {
- $psr7_client->getWorker()->error((string) $e);
+ } catch (Throwable $e) {
+ if ($responded !== true) {
+ $psr7_client->getWorker()->error($this->exceptionToString($e, $this->isDebugModeEnabled($config)));
+ }
+
+ $this->fireEvent($sandbox, new Events\LoopErrorOccurredEvent($sandbox, $req, $e));
} finally {
unset($http_kernel, $response, $request, $sandbox);
@@ -95,6 +105,29 @@ class Worker implements WorkerInterface
$this->fireEvent($app, new Events\AfterLoopStoppedEvent($app));
}
+ /**
+ * @param Throwable $e
+ * @param bool $is_debug
+ *
+ * @return string
+ */
+ protected function exceptionToString(Throwable $e, bool $is_debug): string
+ {
+ return $is_debug
+ ? (string) $e
+ : 'Internal server error';
+ }
+
+ /**
+ * @param ConfigRepository $config
+ *
+ * @return bool
+ */
+ protected function isDebugModeEnabled(ConfigRepository $config): bool
+ {
+ return $config->get('app.debug', false) === true;
+ }
+
/**
* @param ApplicationContract $app
* already in #43 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
7.2.14
(in docker)3.1.0
Additional context
Error screenshot:
"Fast fix" idea:
The text was updated successfully, but these errors were encountered: